12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784 |
- (function universalModuleDefinition(root, factory) {
- var amdDependencies = [];
- var BABYLON = root.BABYLON || this.BABYLON;
- if(typeof exports === 'object' && typeof module === 'object') {
- BABYLON = BABYLON || require("babylonjs");
- module.exports = factory(BABYLON);
- } else if(typeof define === 'function' && define.amd) {
- amdDependencies.push("babylonjs");
- define("babylonjs-serializers", amdDependencies, factory);
- } else if(typeof exports === 'object') {
- BABYLON = BABYLON || require("babylonjs");
- exports["babylonjs-serializers"] = factory(BABYLON);
- } else {
- root["BABYLON"] = factory(BABYLON);
- }
- })(this, function(BABYLON) {
- BABYLON = BABYLON || this.BABYLON;
- var __decorate=this&&this.__decorate||function(e,t,r,c){var o,f=arguments.length,n=f<3?t:null===c?c=Object.getOwnPropertyDescriptor(t,r):c;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,r,c);else for(var l=e.length-1;l>=0;l--)(o=e[l])&&(n=(f<3?o(n):f>3?o(t,r,n):o(t,r))||n);return f>3&&n&&Object.defineProperty(t,r,n),n};
- var __extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,o){t.__proto__=o}||function(t,o){for(var n in o)o.hasOwnProperty(n)&&(t[n]=o[n])};return function(o,n){function r(){this.constructor=o}t(o,n),o.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}();
- var BABYLON;
- (function (BABYLON) {
- var OBJExport = /** @class */ (function () {
- function OBJExport() {
- }
- //Exports the geometrys of a Mesh array in .OBJ file format (text)
- OBJExport.OBJ = function (mesh, materials, matlibname, globalposition) {
- var output = [];
- var v = 1;
- if (materials) {
- if (!matlibname) {
- matlibname = 'mat';
- }
- output.push("mtllib " + matlibname + ".mtl");
- }
- for (var j = 0; j < mesh.length; j++) {
- output.push("g object" + j);
- output.push("o object_" + j);
- //Uses the position of the item in the scene, to the file (this back to normal in the end)
- var lastMatrix = null;
- if (globalposition) {
- var newMatrix = BABYLON.Matrix.Translation(mesh[j].position.x, mesh[j].position.y, mesh[j].position.z);
- lastMatrix = BABYLON.Matrix.Translation(-(mesh[j].position.x), -(mesh[j].position.y), -(mesh[j].position.z));
- mesh[j].bakeTransformIntoVertices(newMatrix);
- }
- //TODO: submeshes (groups)
- //TODO: smoothing groups (s 1, s off);
- if (materials) {
- var mat = mesh[j].material;
- if (mat) {
- output.push("usemtl " + mat.id);
- }
- }
- var g = mesh[j].geometry;
- if (!g) {
- continue;
- }
- var trunkVerts = g.getVerticesData('position');
- var trunkNormals = g.getVerticesData('normal');
- var trunkUV = g.getVerticesData('uv');
- var trunkFaces = g.getIndices();
- var curV = 0;
- if (!trunkVerts || !trunkNormals || !trunkUV || !trunkFaces) {
- continue;
- }
- for (var i = 0; i < trunkVerts.length; i += 3) {
- output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
- curV++;
- }
- for (i = 0; i < trunkNormals.length; i += 3) {
- output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
- }
- for (i = 0; i < trunkUV.length; i += 2) {
- output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
- }
- for (i = 0; i < trunkFaces.length; i += 3) {
- output.push("f " + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) +
- " " + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) +
- " " + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v));
- }
- //back de previous matrix, to not change the original mesh in the scene
- if (globalposition && lastMatrix) {
- mesh[j].bakeTransformIntoVertices(lastMatrix);
- }
- v += curV;
- }
- var text = output.join("\n");
- return (text);
- };
- //Exports the material(s) of a mesh in .MTL file format (text)
- //TODO: Export the materials of mesh array
- OBJExport.MTL = function (mesh) {
- var output = [];
- var m = mesh.material;
- output.push("newmtl mat1");
- output.push(" Ns " + m.specularPower.toFixed(4));
- output.push(" Ni 1.5000");
- output.push(" d " + m.alpha.toFixed(4));
- output.push(" Tr 0.0000");
- output.push(" Tf 1.0000 1.0000 1.0000");
- output.push(" illum 2");
- output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
- output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
- output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
- output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
- //TODO: uv scale, offset, wrap
- //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
- var uvscale = "";
- if (m.ambientTexture) {
- output.push(" map_Ka " + uvscale + m.ambientTexture.name);
- }
- if (m.diffuseTexture) {
- output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
- //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
- }
- if (m.specularTexture) {
- output.push(" map_Ks " + uvscale + m.specularTexture.name);
- /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
- if (m.useGlossinessFromSpecularMapAlpha) {
- output.push(" map_Ns "+uvscale + m.specularTexture.name);
- }
- */
- }
- /* TODO: emissive texture not in .MAT format (???)
- if (m.emissiveTexture) {
- output.push(" map_d "+uvscale+m.emissiveTexture.name);
- }
- */
- if (m.bumpTexture) {
- output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
- }
- if (m.opacityTexture) {
- output.push(" map_d " + uvscale + m.opacityTexture.name);
- }
- var text = output.join("\n");
- return (text);
- };
- return OBJExport;
- }());
- BABYLON.OBJExport = OBJExport;
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.objSerializer.js.map
- var BABYLON;
- (function (BABYLON) {
- ;
- /**
- * Class for generating glTF data from a Babylon scene.
- */
- var GLTF2Export = /** @class */ (function () {
- function GLTF2Export() {
- }
- /**
- * Exports the geometry of the scene to .gltf file format.
- * @param scene - Babylon scene with scene hierarchy information.
- * @param filePrefix - File prefix to use when generating the glTF file.
- * @param options - Exporter options.
- * @returns - Returns an object with a .gltf file and associates texture names
- * as keys and their data and paths as values.
- */
- GLTF2Export.GLTF = function (scene, filePrefix, options) {
- var glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
- var gltfGenerator = new BABYLON.GLTF2._Exporter(scene, options);
- if (scene.isReady) {
- return gltfGenerator._generateGLTF(glTFPrefix);
- }
- else {
- throw new Error("glTF Serializer: Scene is not ready!");
- }
- };
- /**
- * Exports the geometry of the scene to .glb file format.
- * @param scene - Babylon scene with scene hierarchy information.
- * @param filePrefix - File prefix to use when generating glb file.
- * @param options - Exporter options.
- * @returns - Returns an object with a .glb filename as key and data as value
- */
- GLTF2Export.GLB = function (scene, filePrefix, options) {
- var glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
- var gltfGenerator = new BABYLON.GLTF2._Exporter(scene, options);
- if (scene.isReady) {
- return gltfGenerator._generateGLB(glTFPrefix);
- }
- else {
- throw new Error("glTF Serializer: Scene is not ready!");
- }
- };
- return GLTF2Export;
- }());
- BABYLON.GLTF2Export = GLTF2Export;
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.glTFSerializer.js.map
- /**
- * Module for the Babylon glTF 2.0 exporter. Should ONLY be used internally.
- * @hidden - capitalization of GLTF2 module.
- */
- var BABYLON;
- (function (BABYLON) {
- var GLTF2;
- (function (GLTF2) {
- /**
- * Converts Babylon Scene into glTF 2.0.
- * @hidden
- */
- var _Exporter = /** @class */ (function () {
- /**
- * Creates a glTF Exporter instance, which can accept optional exporter options.
- * @param babylonScene - Babylon scene object
- * @param options - Options to modify the behavior of the exporter.
- */
- function _Exporter(babylonScene, options) {
- this.asset = { generator: "BabylonJS", version: "2.0" };
- this.babylonScene = babylonScene;
- this.bufferViews = new Array();
- this.accessors = new Array();
- this.meshes = new Array();
- this.scenes = new Array();
- this.nodes = new Array();
- this.images = new Array();
- this.materials = new Array();
- this.textures = new Array();
- this.imageData = {};
- this.convertToRightHandedSystem = this.babylonScene.useRightHandedSystem ? false : true;
- if (options) {
- this.options = options;
- }
- }
- /**
- * Creates a buffer view based on teh supplied arguments
- * @param bufferIndex - index value of the specified buffer
- * @param byteOffset - byte offset value
- * @param byteLength - byte length of the bufferView
- * @param byteStride - byte distance between conequential elements.
- * @param name - name of the buffer view
- * @returns - bufferView for glTF
- */
- _Exporter.prototype.createBufferView = function (bufferIndex, byteOffset, byteLength, byteStride, name) {
- var bufferview = { buffer: bufferIndex, byteLength: byteLength };
- if (byteOffset) {
- bufferview.byteOffset = byteOffset;
- }
- if (name) {
- bufferview.name = name;
- }
- if (byteStride) {
- bufferview.byteStride = byteStride;
- }
- return bufferview;
- };
- /**
- * Creates an accessor based on the supplied arguments
- * @param bufferviewIndex - The index of the bufferview referenced by this accessor.
- * @param name - The name of the accessor.
- * @param type - The type of the accessor.
- * @param componentType - The datatype of components in the attribute.
- * @param count - The number of attributes referenced by this accessor.
- * @param byteOffset - The offset relative to the start of the bufferView in bytes.
- * @param min - Minimum value of each component in this attribute.
- * @param max - Maximum value of each component in this attribute.
- * @returns - accessor for glTF
- */
- _Exporter.prototype.createAccessor = function (bufferviewIndex, name, type, componentType, count, byteOffset, min, max) {
- var accessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
- if (min) {
- accessor.min = min;
- }
- if (max) {
- accessor.max = max;
- }
- if (byteOffset) {
- accessor.byteOffset = byteOffset;
- }
- return accessor;
- };
- /**
- * Calculates the minimum and maximum values of an array of position floats.
- * @param positions - Positions array of a mesh.
- * @param vertexStart - Starting vertex offset to calculate min and max values.
- * @param vertexCount - Number of vertices to check for min and max values.
- * @returns - min number array and max number array.
- */
- _Exporter.prototype.calculateMinMaxPositions = function (positions, vertexStart, vertexCount) {
- var min = [Infinity, Infinity, Infinity];
- var max = [-Infinity, -Infinity, -Infinity];
- var positionStrideSize = 3;
- var end = vertexStart + vertexCount;
- if (vertexCount) {
- for (var i = vertexStart; i < end; ++i) {
- var indexOffset = positionStrideSize * i;
- var position = BABYLON.Vector3.FromArray(positions, indexOffset);
- var vector = this.convertToRightHandedSystem ? _Exporter.GetRightHandedVector3(position).asArray() : position.asArray();
- for (var j = 0; j < positionStrideSize; ++j) {
- var num = vector[j];
- if (num < min[j]) {
- min[j] = num;
- }
- if (num > max[j]) {
- max[j] = num;
- }
- ++indexOffset;
- }
- }
- }
- return { min: min, max: max };
- };
- /**
- * Converts a vector3 array to right-handed.
- * @param vector - vector3 Array to convert to right-handed.
- * @returns - right-handed Vector3 array.
- */
- _Exporter.GetRightHandedVector3 = function (vector) {
- return new BABYLON.Vector3(vector.x, vector.y, -vector.z);
- };
- /**
- * Converts a vector4 array to right-handed.
- * @param vector - vector4 Array to convert to right-handed.
- * @returns - right-handed vector4 array.
- */
- _Exporter.GetRightHandedVector4 = function (vector) {
- return new BABYLON.Vector4(vector.x, vector.y, -vector.z, -vector.w);
- };
- /**
- * Converts a quaternion to right-handed.
- * @param quaternion - Source quaternion to convert to right-handed.
- */
- _Exporter.GetRightHandedQuaternion = function (quaternion) {
- return new BABYLON.Quaternion(-quaternion.x, -quaternion.y, quaternion.z, quaternion.w);
- };
- /**
- * Writes mesh attribute data to a data buffer.
- * Returns the bytelength of the data.
- * @param vertexBufferKind - Indicates what kind of vertex data is being passed in.
- * @param meshAttributeArray - Array containing the attribute data.
- * @param byteOffset - The offset to start counting bytes from.
- * @param dataBuffer - The buffer to write the binary data to.
- * @returns - Byte length of the attribute data.
- */
- _Exporter.prototype.writeAttributeData = function (vertexBufferKind, meshAttributeArray, byteOffset, dataBuffer) {
- var byteOff = byteOffset;
- var stride = BABYLON.VertexBuffer.DeduceStride(vertexBufferKind);
- var end = meshAttributeArray.length / stride;
- var byteLength = 0;
- for (var k = 0; k < end; ++k) {
- var index = k * stride;
- var vector = [];
- if (vertexBufferKind === BABYLON.VertexBuffer.PositionKind || vertexBufferKind === BABYLON.VertexBuffer.NormalKind) {
- var vertexData = BABYLON.Vector3.FromArray(meshAttributeArray, index);
- vector = this.convertToRightHandedSystem ? _Exporter.GetRightHandedVector3(vertexData).asArray() : vertexData.asArray();
- }
- else if (vertexBufferKind === BABYLON.VertexBuffer.TangentKind || vertexBufferKind === BABYLON.VertexBuffer.ColorKind) {
- var vertexData = BABYLON.Vector4.FromArray(meshAttributeArray, index);
- vector = (this.convertToRightHandedSystem && !(vertexBufferKind === BABYLON.VertexBuffer.ColorKind)) ? _Exporter.GetRightHandedVector4(vertexData).asArray() : vertexData.asArray();
- }
- else if (vertexBufferKind === BABYLON.VertexBuffer.UVKind || vertexBufferKind === BABYLON.VertexBuffer.UV2Kind) {
- vector = [meshAttributeArray[index], meshAttributeArray[index + 1]];
- }
- else {
- BABYLON.Tools.Warn("Unsupported Vertex Buffer Type: " + vertexBufferKind);
- }
- for (var i = 0; i < vector.length; ++i) {
- dataBuffer.setFloat32(byteOff, vector[i], true);
- byteOff += 4;
- }
- }
- byteLength = meshAttributeArray.length * 4;
- return byteLength;
- };
- /**
- * Generates glTF json data
- * @param shouldUseGlb - Indicates whether the json should be written for a glb file.
- * @param glTFPrefix - Text to use when prefixing a glTF file.
- * @param prettyPrint - Indicates whether the json file should be pretty printed (true) or not (false).
- * @returns - json data as string
- */
- _Exporter.prototype.generateJSON = function (shouldUseGlb, glTFPrefix, prettyPrint) {
- var buffer = { byteLength: this.totalByteLength };
- var glTF = {
- asset: this.asset
- };
- if (buffer.byteLength) {
- glTF.buffers = [buffer];
- }
- if (this.nodes && this.nodes.length) {
- glTF.nodes = this.nodes;
- }
- if (this.meshes && this.meshes.length) {
- glTF.meshes = this.meshes;
- }
- if (this.scenes && this.scenes.length) {
- glTF.scenes = this.scenes;
- glTF.scene = 0;
- }
- if (this.bufferViews && this.bufferViews.length) {
- glTF.bufferViews = this.bufferViews;
- }
- if (this.accessors && this.accessors.length) {
- glTF.accessors = this.accessors;
- }
- if (this.materials && this.materials.length) {
- glTF.materials = this.materials;
- }
- if (this.textures && this.textures.length) {
- glTF.textures = this.textures;
- }
- if (this.images && this.images.length) {
- if (!shouldUseGlb) {
- glTF.images = this.images;
- }
- else {
- glTF.images = [];
- // Replace uri with bufferview and mime type for glb
- var imageLength = this.images.length;
- var byteOffset = this.totalByteLength;
- for (var i = 0; i < imageLength; ++i) {
- var image = this.images[i];
- if (image.uri) {
- var imageData = this.imageData[image.uri];
- var imageName = image.uri.split('.')[0] + " image";
- var bufferView = this.createBufferView(0, byteOffset, imageData.data.length, undefined, imageName);
- byteOffset += imageData.data.buffer.byteLength;
- this.bufferViews.push(bufferView);
- image.bufferView = this.bufferViews.length - 1;
- image.name = imageName;
- image.mimeType = imageData.mimeType;
- image.uri = undefined;
- glTF.images.push(image);
- }
- }
- buffer.byteLength = byteOffset;
- }
- }
- if (!shouldUseGlb) {
- buffer.uri = glTFPrefix + ".bin";
- }
- var jsonText = prettyPrint ? JSON.stringify(glTF, null, 2) : JSON.stringify(glTF);
- return jsonText;
- };
- /**
- * Generates data for .gltf and .bin files based on the glTF prefix string
- * @param glTFPrefix - Text to use when prefixing a glTF file.
- * @returns - GLTFData with glTF file data.
- */
- _Exporter.prototype._generateGLTF = function (glTFPrefix) {
- var binaryBuffer = this.generateBinary();
- var jsonText = this.generateJSON(false, glTFPrefix, true);
- var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
- var glTFFileName = glTFPrefix + '.gltf';
- var glTFBinFile = glTFPrefix + '.bin';
- var container = new BABYLON._GLTFData();
- container.glTFFiles[glTFFileName] = jsonText;
- container.glTFFiles[glTFBinFile] = bin;
- if (this.imageData) {
- for (var image in this.imageData) {
- container.glTFFiles[image] = new Blob([this.imageData[image].data], { type: this.imageData[image].mimeType });
- }
- }
- return container;
- };
- /**
- * Creates a binary buffer for glTF
- * @returns - array buffer for binary data
- */
- _Exporter.prototype.generateBinary = function () {
- var byteOffset = 0;
- byteOffset = this.createScene(this.babylonScene, byteOffset);
- return this.binaryBuffer;
- };
- /**
- * Pads the number to a multiple of 4
- * @param num - number to pad
- * @returns - padded number
- */
- _Exporter.prototype._getPadding = function (num) {
- var remainder = num % 4;
- var padding = remainder === 0 ? remainder : 4 - remainder;
- return padding;
- };
- /**
- * Generates a glb file from the json and binary data.
- * Returns an object with the glb file name as the key and data as the value.
- * @param glTFPrefix
- * @returns - object with glb filename as key and data as value
- */
- _Exporter.prototype._generateGLB = function (glTFPrefix) {
- var binaryBuffer = this.generateBinary();
- var jsonText = this.generateJSON(true);
- var glbFileName = glTFPrefix + '.glb';
- var headerLength = 12;
- var chunkLengthPrefix = 8;
- var jsonLength = jsonText.length;
- var imageByteLength = 0;
- for (var key in this.imageData) {
- imageByteLength += this.imageData[key].data.byteLength;
- }
- var jsonPadding = this._getPadding(jsonLength);
- var binPadding = this._getPadding(binaryBuffer.byteLength);
- var imagePadding = this._getPadding(imageByteLength);
- var byteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding + imageByteLength + imagePadding;
- //header
- var headerBuffer = new ArrayBuffer(headerLength);
- var headerBufferView = new DataView(headerBuffer);
- headerBufferView.setUint32(0, 0x46546C67, true); //glTF
- headerBufferView.setUint32(4, 2, true); // version
- headerBufferView.setUint32(8, byteLength, true); // total bytes in file
- //json chunk
- var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
- var jsonChunkBufferView = new DataView(jsonChunkBuffer);
- jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
- jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
- //json chunk bytes
- var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
- for (var i = 0; i < jsonLength; ++i) {
- jsonData[i] = jsonText.charCodeAt(i);
- }
- //json padding
- var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
- for (var i = 0; i < jsonPadding; ++i) {
- jsonPaddingView[i] = 0x20;
- }
- //binary chunk
- var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
- var binaryChunkBufferView = new DataView(binaryChunkBuffer);
- binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength + imageByteLength + imagePadding, true);
- binaryChunkBufferView.setUint32(4, 0x004E4942, true);
- // binary padding
- var binPaddingBuffer = new ArrayBuffer(binPadding);
- var binPaddingView = new Uint8Array(binPaddingBuffer);
- for (var i = 0; i < binPadding; ++i) {
- binPaddingView[i] = 0;
- }
- var imagePaddingBuffer = new ArrayBuffer(imagePadding);
- var imagePaddingView = new Uint8Array(imagePaddingBuffer);
- for (var i = 0; i < imagePadding; ++i) {
- imagePaddingView[i] = 0;
- }
- var glbData = [headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer];
- // binary data
- for (var key in this.imageData) {
- glbData.push(this.imageData[key].data.buffer);
- }
- glbData.push(binPaddingBuffer);
- glbData.push(imagePaddingBuffer);
- var glbFile = new Blob(glbData, { type: 'application/octet-stream' });
- var container = new BABYLON._GLTFData();
- container.glTFFiles[glbFileName] = glbFile;
- return container;
- };
- /**
- * Sets the TRS for each node
- * @param node - glTF Node for storing the transformation data.
- * @param babylonMesh - Babylon mesh used as the source for the transformation data.
- */
- _Exporter.prototype.setNodeTransformation = function (node, babylonMesh) {
- if (!babylonMesh.position.equalsToFloats(0, 0, 0)) {
- node.translation = this.convertToRightHandedSystem ? _Exporter.GetRightHandedVector3(babylonMesh.position).asArray() : babylonMesh.position.asArray();
- }
- if (!babylonMesh.scaling.equalsToFloats(1, 1, 1)) {
- node.scale = babylonMesh.scaling.asArray();
- }
- var rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(babylonMesh.rotation.y, babylonMesh.rotation.x, babylonMesh.rotation.z);
- if (babylonMesh.rotationQuaternion) {
- rotationQuaternion = rotationQuaternion.multiply(babylonMesh.rotationQuaternion);
- }
- if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
- node.rotation = this.convertToRightHandedSystem ? _Exporter.GetRightHandedQuaternion(rotationQuaternion).asArray() : rotationQuaternion.asArray();
- }
- };
- /**
- * Creates a bufferview based on the vertices type for the Babylon mesh
- * @param kind - Indicates the type of vertices data.
- * @param babylonMesh - The Babylon mesh to get the vertices data from.
- * @param byteOffset - The offset from the buffer to start indexing from.
- * @param dataBuffer - The buffer to write the bufferview data to.
- * @returns bytelength of the bufferview data.
- */
- _Exporter.prototype.createBufferViewKind = function (kind, babylonMesh, byteOffset, dataBuffer) {
- var bufferMesh = null;
- var byteLength = 0;
- if (babylonMesh instanceof BABYLON.Mesh) {
- bufferMesh = babylonMesh;
- }
- else if (babylonMesh instanceof BABYLON.InstancedMesh) {
- bufferMesh = babylonMesh.sourceMesh;
- }
- if (bufferMesh) {
- var vertexData = bufferMesh.getVerticesData(kind);
- if (vertexData) {
- if (dataBuffer && vertexData) { // write data to buffer
- byteLength = this.writeAttributeData(kind, vertexData, byteOffset, dataBuffer);
- byteOffset += byteLength;
- }
- else {
- byteLength = vertexData.length * 4;
- var bufferView = this.createBufferView(0, byteOffset, byteLength, undefined, kind + " - " + bufferMesh.name);
- byteOffset += byteLength;
- this.bufferViews.push(bufferView);
- }
- }
- }
- return byteLength;
- };
- /**
- * Sets data for the primitive attributes of each submesh
- * @param mesh - glTF Mesh object to store the primitive attribute information.
- * @param babylonMesh - Babylon mesh to get the primitive attribute data from.
- * @param byteOffset - The offset in bytes of the buffer data.
- * @param dataBuffer - Buffer to write the attribute data to.
- * @returns - bytelength of the primitive attributes plus the passed in byteOffset.
- */
- _Exporter.prototype.setPrimitiveAttributes = function (mesh, babylonMesh, byteOffset, dataBuffer) {
- var bufferMesh = null;
- if (babylonMesh instanceof BABYLON.Mesh) {
- bufferMesh = babylonMesh;
- }
- else if (babylonMesh instanceof BABYLON.InstancedMesh) {
- bufferMesh = babylonMesh.sourceMesh;
- }
- var attributeData = [
- { kind: BABYLON.VertexBuffer.PositionKind, accessorType: "VEC3" /* VEC3 */ },
- { kind: BABYLON.VertexBuffer.NormalKind, accessorType: "VEC3" /* VEC3 */ },
- { kind: BABYLON.VertexBuffer.ColorKind, accessorType: "VEC4" /* VEC4 */ },
- { kind: BABYLON.VertexBuffer.TangentKind, accessorType: "VEC4" /* VEC4 */ },
- { kind: BABYLON.VertexBuffer.UVKind, accessorType: "VEC2" /* VEC2 */ },
- { kind: BABYLON.VertexBuffer.UV2Kind, accessorType: "VEC2" /* VEC2 */ },
- ];
- var indexBufferViewIndex = null;
- if (bufferMesh) {
- // For each BabylonMesh, create bufferviews for each 'kind'
- for (var _i = 0, attributeData_1 = attributeData; _i < attributeData_1.length; _i++) {
- var attribute = attributeData_1[_i];
- var attributeKind = attribute.kind;
- if (bufferMesh.isVerticesDataPresent(attributeKind)) {
- byteOffset += this.createBufferViewKind(attributeKind, babylonMesh, byteOffset, dataBuffer);
- attribute.bufferViewIndex = this.bufferViews.length - 1;
- }
- }
- if (bufferMesh.getTotalIndices()) {
- var indices = bufferMesh.getIndices();
- if (indices) {
- if (dataBuffer) {
- var end = indices.length;
- var byteOff = byteOffset;
- for (var k = 0; k < end; ++k) {
- dataBuffer.setUint32(byteOff, indices[k], true);
- byteOff += 4;
- }
- byteOffset = byteOff;
- }
- else {
- var byteLength = indices.length * 4;
- var bufferView = this.createBufferView(0, byteOffset, byteLength, undefined, "Indices - " + bufferMesh.name);
- byteOffset += byteLength;
- this.bufferViews.push(bufferView);
- indexBufferViewIndex = this.bufferViews.length - 1;
- }
- }
- }
- if (babylonMesh.subMeshes) {
- var uvCoordsPresent = false;
- // go through all mesh primitives (submeshes)
- for (var _a = 0, _b = babylonMesh.subMeshes; _a < _b.length; _a++) {
- var submesh = _b[_a];
- var meshPrimitive = { attributes: {} };
- // Create a bufferview storing all the positions
- if (!dataBuffer) {
- for (var _c = 0, attributeData_2 = attributeData; _c < attributeData_2.length; _c++) {
- var attribute = attributeData_2[_c];
- var attributeKind = attribute.kind;
- var vertexData = bufferMesh.getVerticesData(attributeKind);
- if (vertexData) {
- var stride = BABYLON.VertexBuffer.DeduceStride(attributeKind);
- var minMax = void 0;
- var min = null;
- var max = null;
- var bufferViewIndex = attribute.bufferViewIndex;
- if (bufferViewIndex != undefined) { // check to see if bufferviewindex has a numeric value assigned.
- if (attributeKind == BABYLON.VertexBuffer.PositionKind) {
- minMax = this.calculateMinMaxPositions(vertexData, 0, vertexData.length / stride);
- min = minMax.min;
- max = minMax.max;
- }
- var accessor = this.createAccessor(bufferViewIndex, attributeKind + " - " + babylonMesh.name, attribute.accessorType, 5126 /* FLOAT */, vertexData.length / stride, 0, min, max);
- this.accessors.push(accessor);
- switch (attributeKind) {
- case BABYLON.VertexBuffer.PositionKind: {
- meshPrimitive.attributes.POSITION = this.accessors.length - 1;
- break;
- }
- case BABYLON.VertexBuffer.NormalKind: {
- meshPrimitive.attributes.NORMAL = this.accessors.length - 1;
- break;
- }
- case BABYLON.VertexBuffer.ColorKind: {
- meshPrimitive.attributes.COLOR_0 = this.accessors.length - 1;
- break;
- }
- case BABYLON.VertexBuffer.TangentKind: {
- meshPrimitive.attributes.TANGENT = this.accessors.length - 1;
- break;
- }
- case BABYLON.VertexBuffer.UVKind: {
- meshPrimitive.attributes.TEXCOORD_0 = this.accessors.length - 1;
- uvCoordsPresent = true;
- break;
- }
- case BABYLON.VertexBuffer.UV2Kind: {
- meshPrimitive.attributes.TEXCOORD_1 = this.accessors.length - 1;
- uvCoordsPresent = true;
- break;
- }
- default: {
- BABYLON.Tools.Warn("Unsupported Vertex Buffer Type: " + attributeKind);
- }
- }
- }
- }
- }
- if (indexBufferViewIndex) {
- // Create accessor
- var accessor = this.createAccessor(indexBufferViewIndex, "indices - " + babylonMesh.name, "SCALAR" /* SCALAR */, 5125 /* UNSIGNED_INT */, submesh.indexCount, submesh.indexStart * 4, null, null);
- this.accessors.push(accessor);
- meshPrimitive.indices = this.accessors.length - 1;
- }
- }
- if (bufferMesh.material) {
- var materialIndex = null;
- if (bufferMesh.material instanceof BABYLON.StandardMaterial || bufferMesh.material instanceof BABYLON.PBRMetallicRoughnessMaterial || bufferMesh.material instanceof BABYLON.PBRMaterial) {
- materialIndex = babylonMesh.getScene().materials.indexOf(bufferMesh.material);
- }
- else if (bufferMesh.material instanceof BABYLON.MultiMaterial) {
- var babylonMultiMaterial = bufferMesh.material;
- var material = babylonMultiMaterial.subMaterials[submesh.materialIndex];
- if (material) {
- materialIndex = babylonMesh.getScene().materials.indexOf(material);
- }
- }
- else {
- BABYLON.Tools.Warn("Material type " + bufferMesh.material.getClassName() + " for material " + bufferMesh.material.name + " is not yet implemented in glTF serializer.");
- }
- if (materialIndex != null && Object.keys(meshPrimitive.attributes).length > 0) {
- if (uvCoordsPresent || !GLTF2._GLTFMaterial._HasTexturesPresent(this.materials[materialIndex])) {
- meshPrimitive.material = materialIndex;
- }
- else {
- // If no texture coordinate information is present, make a copy of the material without the textures to be glTF compliant.
- var newMat = GLTF2._GLTFMaterial._StripTexturesFromMaterial(this.materials[materialIndex]);
- this.materials.push(newMat);
- meshPrimitive.material = this.materials.length - 1;
- }
- }
- }
- mesh.primitives.push(meshPrimitive);
- }
- }
- }
- return byteOffset;
- };
- /**
- * Creates a glTF scene based on the array of meshes.
- * Returns the the total byte offset.
- * @param babylonScene - Babylon scene to get the mesh data from.
- * @param byteOffset - Offset to start from in bytes.
- * @returns bytelength + byteoffset
- */
- _Exporter.prototype.createScene = function (babylonScene, byteOffset) {
- if (babylonScene.meshes.length) {
- var babylonMeshes = babylonScene.meshes;
- var scene = { nodes: new Array() };
- GLTF2._GLTFMaterial._ConvertMaterialsToGLTF(babylonScene.materials, "image/png" /* PNG */, this.images, this.textures, this.materials, this.imageData, true);
- var result = this.createNodeMap(babylonScene, byteOffset);
- this.nodeMap = result.nodeMap;
- this.totalByteLength = result.byteOffset;
- this.binaryBuffer = new ArrayBuffer(this.totalByteLength);
- var dataBuffer = new DataView(this.binaryBuffer);
- for (var i = 0; i < babylonMeshes.length; ++i) {
- var babylonMesh = babylonMeshes[i];
- // Build Hierarchy with the node map.
- var glTFNodeIndex = this.nodeMap[babylonMesh.uniqueId];
- var glTFNode = this.nodes[glTFNodeIndex];
- if (!babylonMesh.parent) {
- if (this.options &&
- this.options.shouldExportMesh != undefined &&
- !this.options.shouldExportMesh(babylonMesh)) {
- BABYLON.Tools.Log("Omitting " + babylonMesh.name + " from scene.");
- }
- else {
- scene.nodes.push(glTFNodeIndex);
- }
- }
- var directDescendents = babylonMesh.getDescendants(true);
- if (!glTFNode.children && directDescendents && directDescendents.length) {
- glTFNode.children = [];
- for (var _i = 0, directDescendents_1 = directDescendents; _i < directDescendents_1.length; _i++) {
- var descendent = directDescendents_1[_i];
- glTFNode.children.push(this.nodeMap[descendent.uniqueId]);
- }
- }
- var mesh = { primitives: new Array() };
- byteOffset = this.setPrimitiveAttributes(mesh, babylonMesh, byteOffset, dataBuffer);
- }
- this.scenes.push(scene);
- }
- return byteOffset;
- };
- /**
- * Creates a mapping of Node unique id to node index
- * @param scene - Babylon Scene.
- * @param byteOffset - The initial byte offset.
- * @returns - Node mapping of unique id to index.
- */
- _Exporter.prototype.createNodeMap = function (scene, byteOffset) {
- var nodeMap = {};
- for (var _i = 0, _a = scene.meshes; _i < _a.length; _i++) {
- var babylonMesh = _a[_i];
- var result = this.createNode(babylonMesh, byteOffset, null);
- this.nodes.push(result.node);
- nodeMap[babylonMesh.uniqueId] = this.nodes.length - 1;
- byteOffset = result.byteOffset;
- }
- return { nodeMap: nodeMap, byteOffset: byteOffset };
- };
- /**
- * Creates a glTF node from a Babylon mesh.
- * @param babylonMesh - Source Babylon mesh.
- * @param byteOffset - The initial byte offset.
- * @param dataBuffer - Buffer for storing geometry data.
- * @returns - Object containing an INode and byteoffset.
- */
- _Exporter.prototype.createNode = function (babylonMesh, byteOffset, dataBuffer) {
- // create node to hold translation/rotation/scale and the mesh
- var node = {};
- if (babylonMesh.name) {
- node.name = babylonMesh.name;
- }
- // Set transformation
- this.setNodeTransformation(node, babylonMesh);
- // create mesh
- var mesh = { primitives: new Array() };
- mesh.primitives = [];
- byteOffset = this.setPrimitiveAttributes(mesh, babylonMesh, byteOffset, dataBuffer);
- if (mesh.primitives.length) {
- this.meshes.push(mesh);
- node.mesh = this.meshes.length - 1;
- }
- return { node: node, byteOffset: byteOffset };
- };
- return _Exporter;
- }());
- GLTF2._Exporter = _Exporter;
- })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.glTFExporter.js.map
- var BABYLON;
- (function (BABYLON) {
- /**
- * Class for holding and downloading glTF file data
- */
- var _GLTFData = /** @class */ (function () {
- /**
- * Initializes the glTF file object.
- */
- function _GLTFData() {
- this.glTFFiles = {};
- }
- /**
- * Downloads the glTF data as files based on their names and data.
- */
- _GLTFData.prototype.downloadFiles = function () {
- /**
- * Checks for a matching suffix at the end of a string (for ES5 and lower).
- * @param str - Source string.
- * @param suffix - Suffix to search for in the source string.
- * @returns - Boolean indicating whether the suffix was found (true) or not (false).
- */
- function endsWith(str, suffix) {
- return str.indexOf(suffix, str.length - suffix.length) !== -1;
- }
- for (var key in this.glTFFiles) {
- var link = document.createElement('a');
- document.body.appendChild(link);
- link.setAttribute("type", "hidden");
- link.download = key;
- var blob = this.glTFFiles[key];
- var mimeType = void 0;
- if (endsWith(key, ".glb")) {
- mimeType = { type: "model/gltf-binary" };
- }
- else if (endsWith(key, ".bin")) {
- mimeType = { type: "application/octet-stream" };
- }
- else if (endsWith(key, ".gltf")) {
- mimeType = { type: "model/gltf+json" };
- }
- else if (endsWith(key, ".jpeg" || ".jpg")) {
- mimeType = { type: "image/jpeg" /* JPEG */ };
- }
- else if (endsWith(key, ".png")) {
- mimeType = { type: "image/png" /* PNG */ };
- }
- link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
- link.click();
- }
- };
- return _GLTFData;
- }());
- BABYLON._GLTFData = _GLTFData;
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.glTFData.js.map
- var BABYLON;
- (function (BABYLON) {
- var GLTF2;
- (function (GLTF2) {
- /**
- * Utility methods for working with glTF material conversion properties. This class should only be used internally.
- * @hidden
- */
- var _GLTFMaterial = /** @class */ (function () {
- function _GLTFMaterial() {
- }
- /**
- * Specifies if two colors are approximately equal in value.
- * @param color1 - first color to compare to.
- * @param color2 - second color to compare to.
- * @param epsilon - threshold value
- */
- _GLTFMaterial.FuzzyEquals = function (color1, color2, epsilon) {
- return BABYLON.Scalar.WithinEpsilon(color1.r, color2.r, epsilon) &&
- BABYLON.Scalar.WithinEpsilon(color1.g, color2.g, epsilon) &&
- BABYLON.Scalar.WithinEpsilon(color1.b, color2.b, epsilon);
- };
- /**
- * Gets the materials from a Babylon scene and converts them to glTF materials.
- * @param scene - babylonjs scene.
- * @param mimeType - texture mime type.
- * @param images - array of images.
- * @param textures - array of textures.
- * @param materials - array of materials.
- * @param imageData - mapping of texture names to base64 textures
- * @param hasTextureCoords - specifies if texture coordinates are present on the material.
- */
- _GLTFMaterial._ConvertMaterialsToGLTF = function (babylonMaterials, mimeType, images, textures, materials, imageData, hasTextureCoords) {
- for (var i = 0; i < babylonMaterials.length; ++i) {
- var babylonMaterial = babylonMaterials[i];
- if (babylonMaterial instanceof BABYLON.StandardMaterial) {
- _GLTFMaterial._ConvertStandardMaterial(babylonMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords);
- }
- else if (babylonMaterial instanceof BABYLON.PBRMetallicRoughnessMaterial) {
- _GLTFMaterial._ConvertPBRMetallicRoughnessMaterial(babylonMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords);
- }
- else if (babylonMaterial instanceof BABYLON.PBRMaterial) {
- _GLTFMaterial._ConvertPBRMaterial(babylonMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords);
- }
- else {
- throw new Error("Unsupported material type: " + babylonMaterial.name);
- }
- }
- };
- /**
- * Makes a copy of the glTF material without the texture parameters.
- * @param originalMaterial - original glTF material.
- * @returns glTF material without texture parameters
- */
- _GLTFMaterial._StripTexturesFromMaterial = function (originalMaterial) {
- var newMaterial = {};
- if (originalMaterial) {
- newMaterial.name = originalMaterial.name;
- newMaterial.doubleSided = originalMaterial.doubleSided;
- newMaterial.alphaMode = originalMaterial.alphaMode;
- newMaterial.alphaCutoff = originalMaterial.alphaCutoff;
- newMaterial.emissiveFactor = originalMaterial.emissiveFactor;
- var originalPBRMetallicRoughness = originalMaterial.pbrMetallicRoughness;
- if (originalPBRMetallicRoughness) {
- newMaterial.pbrMetallicRoughness = {};
- newMaterial.pbrMetallicRoughness.baseColorFactor = originalPBRMetallicRoughness.baseColorFactor;
- newMaterial.pbrMetallicRoughness.metallicFactor = originalPBRMetallicRoughness.metallicFactor;
- newMaterial.pbrMetallicRoughness.roughnessFactor = originalPBRMetallicRoughness.roughnessFactor;
- }
- }
- return newMaterial;
- };
- /**
- * Specifies if the material has any texture parameters present.
- * @param material - glTF Material.
- * @returns boolean specifying if texture parameters are present
- */
- _GLTFMaterial._HasTexturesPresent = function (material) {
- if (material.emissiveTexture || material.normalTexture || material.occlusionTexture) {
- return true;
- }
- var pbrMat = material.pbrMetallicRoughness;
- if (pbrMat) {
- if (pbrMat.baseColorTexture || pbrMat.metallicRoughnessTexture) {
- return true;
- }
- }
- return false;
- };
- /**
- * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material.
- * @param babylonStandardMaterial
- * @returns - glTF Metallic Roughness Material representation
- */
- _GLTFMaterial._ConvertToGLTFPBRMetallicRoughness = function (babylonStandardMaterial) {
- var P0 = new BABYLON.Vector2(0, 1);
- var P1 = new BABYLON.Vector2(0, 0.1);
- var P2 = new BABYLON.Vector2(0, 0.1);
- var P3 = new BABYLON.Vector2(1300, 0.1);
- /**
- * Given the control points, solve for x based on a given t for a cubic bezier curve.
- * @param t - a value between 0 and 1.
- * @param p0 - first control point.
- * @param p1 - second control point.
- * @param p2 - third control point.
- * @param p3 - fourth control point.
- * @returns - number result of cubic bezier curve at the specified t.
- */
- function _cubicBezierCurve(t, p0, p1, p2, p3) {
- return ((1 - t) * (1 - t) * (1 - t) * p0 +
- 3 * (1 - t) * (1 - t) * t * p1 +
- 3 * (1 - t) * t * t * p2 +
- t * t * t * p3);
- }
- /**
- * Evaluates a specified specular power value to determine the appropriate roughness value,
- * based on a pre-defined cubic bezier curve with specular on the abscissa axis (x-axis)
- * and roughness on the ordinant axis (y-axis).
- * @param specularPower - specular power of standard material.
- * @returns - Number representing the roughness value.
- */
- function _solveForRoughness(specularPower) {
- var t = Math.pow(specularPower / P3.x, 0.333333);
- return _cubicBezierCurve(t, P0.y, P1.y, P2.y, P3.y);
- }
- var diffuse = babylonStandardMaterial.diffuseColor.toLinearSpace().scale(0.5);
- var opacity = babylonStandardMaterial.alpha;
- var specularPower = BABYLON.Scalar.Clamp(babylonStandardMaterial.specularPower, 0, this._maxSpecularPower);
- var roughness = _solveForRoughness(specularPower);
- var glTFPbrMetallicRoughness = {
- baseColorFactor: [
- diffuse.r,
- diffuse.g,
- diffuse.b,
- opacity
- ],
- metallicFactor: 0,
- roughnessFactor: roughness,
- };
- return glTFPbrMetallicRoughness;
- };
- /**
- * Computes the metallic factor
- * @param diffuse - diffused value
- * @param specular - specular value
- * @param oneMinusSpecularStrength - one minus the specular strength
- * @returns - metallic value
- */
- _GLTFMaterial._SolveMetallic = function (diffuse, specular, oneMinusSpecularStrength) {
- if (specular < _GLTFMaterial._dielectricSpecular.r) {
- _GLTFMaterial._dielectricSpecular;
- return 0;
- }
- var a = _GLTFMaterial._dielectricSpecular.r;
- var b = diffuse * oneMinusSpecularStrength / (1.0 - _GLTFMaterial._dielectricSpecular.r) + specular - 2.0 * _GLTFMaterial._dielectricSpecular.r;
- var c = _GLTFMaterial._dielectricSpecular.r - specular;
- var D = b * b - 4.0 * a * c;
- return BABYLON.Scalar.Clamp((-b + Math.sqrt(D)) / (2.0 * a), 0, 1);
- };
- /**
- * Gets the glTF alpha mode from the Babylon Material
- * @param babylonMaterial - Babylon Material
- * @returns - The Babylon alpha mode value
- */
- _GLTFMaterial._GetAlphaMode = function (babylonMaterial) {
- if (babylonMaterial instanceof BABYLON.StandardMaterial) {
- var babylonStandardMaterial = babylonMaterial;
- if ((babylonStandardMaterial.alpha != 1.0) ||
- (babylonStandardMaterial.diffuseTexture != null && babylonStandardMaterial.diffuseTexture.hasAlpha) ||
- (babylonStandardMaterial.opacityTexture != null)) {
- return "BLEND" /* BLEND */;
- }
- else {
- return "OPAQUE" /* OPAQUE */;
- }
- }
- else if (babylonMaterial instanceof BABYLON.PBRMetallicRoughnessMaterial) {
- var babylonPBRMetallicRoughness = babylonMaterial;
- switch (babylonPBRMetallicRoughness.transparencyMode) {
- case BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE: {
- return "OPAQUE" /* OPAQUE */;
- }
- case BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND: {
- return "BLEND" /* BLEND */;
- }
- case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST: {
- return "MASK" /* MASK */;
- }
- case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND: {
- BABYLON.Tools.Warn(babylonMaterial.name + ": GLTF Exporter | Alpha test and blend mode not supported in glTF. Alpha blend used instead.");
- return "BLEND" /* BLEND */;
- }
- default: {
- throw new Error("Unsupported alpha mode " + babylonPBRMetallicRoughness.transparencyMode);
- }
- }
- }
- else if (babylonMaterial instanceof BABYLON.PBRMaterial) {
- var babylonPBRMaterial = babylonMaterial;
- switch (babylonPBRMaterial.transparencyMode) {
- case BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE: {
- return "OPAQUE" /* OPAQUE */;
- }
- case BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND: {
- return "BLEND" /* BLEND */;
- }
- case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST: {
- return "MASK" /* MASK */;
- }
- case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND: {
- BABYLON.Tools.Warn(babylonMaterial.name + ": GLTF Exporter | Alpha test and blend mode not supported in glTF. Alpha blend used instead.");
- return "BLEND" /* BLEND */;
- }
- default: {
- throw new Error("Unsupported alpha mode " + babylonPBRMaterial.transparencyMode);
- }
- }
- }
- else {
- throw new Error("Unsupported Babylon material type");
- }
- };
- /**
- * Converts a Babylon Standard Material to a glTF Material.
- * @param babylonStandardMaterial - BJS Standard Material.
- * @param mimeType - mime type to use for the textures.
- * @param images - array of glTF image interfaces.
- * @param textures - array of glTF texture interfaces.
- * @param materials - array of glTF material interfaces.
- * @param imageData - map of image file name to data.
- * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
- */
- _GLTFMaterial._ConvertStandardMaterial = function (babylonStandardMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords) {
- BABYLON.Tools.Warn(babylonStandardMaterial.name + ": Standard Material is currently not fully supported/implemented in glTF serializer");
- var glTFPbrMetallicRoughness = _GLTFMaterial._ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial);
- var glTFMaterial = { name: babylonStandardMaterial.name };
- if (babylonStandardMaterial.backFaceCulling != null && !babylonStandardMaterial.backFaceCulling) {
- if (!babylonStandardMaterial.twoSidedLighting) {
- BABYLON.Tools.Warn(babylonStandardMaterial.name + ": Back-face culling enabled and two-sided lighting disabled is not supported in glTF.");
- }
- glTFMaterial.doubleSided = true;
- }
- if (hasTextureCoords) {
- if (babylonStandardMaterial.diffuseTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonStandardMaterial.diffuseTexture, mimeType, images, textures, imageData);
- if (glTFTexture != null) {
- glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
- }
- }
- if (babylonStandardMaterial.bumpTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonStandardMaterial.bumpTexture, mimeType, images, textures, imageData);
- if (glTFTexture) {
- glTFMaterial.normalTexture = glTFTexture;
- }
- }
- if (babylonStandardMaterial.emissiveTexture) {
- var glTFEmissiveTexture = _GLTFMaterial._ExportTexture(babylonStandardMaterial.emissiveTexture, mimeType, images, textures, imageData);
- if (glTFEmissiveTexture) {
- glTFMaterial.emissiveTexture = glTFEmissiveTexture;
- }
- glTFMaterial.emissiveFactor = [1.0, 1.0, 1.0];
- }
- if (babylonStandardMaterial.ambientTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonStandardMaterial.ambientTexture, mimeType, images, textures, imageData);
- if (glTFTexture) {
- var occlusionTexture = {
- index: glTFTexture.index
- };
- glTFMaterial.occlusionTexture = occlusionTexture;
- occlusionTexture.strength = 1.0;
- }
- }
- }
- if (babylonStandardMaterial.alpha < 1.0 || babylonStandardMaterial.opacityTexture) {
- if (babylonStandardMaterial.alphaMode === BABYLON.Engine.ALPHA_COMBINE) {
- glTFMaterial.alphaMode = "BLEND" /* BLEND */;
- }
- else {
- BABYLON.Tools.Warn(babylonStandardMaterial.name + ": glTF 2.0 does not support alpha mode: " + babylonStandardMaterial.alphaMode.toString());
- }
- }
- if (babylonStandardMaterial.emissiveColor && !this.FuzzyEquals(babylonStandardMaterial.emissiveColor, BABYLON.Color3.Black(), this._epsilon)) {
- glTFMaterial.emissiveFactor = babylonStandardMaterial.emissiveColor.asArray();
- }
- glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
- materials.push(glTFMaterial);
- };
- /**
- * Converts a Babylon PBR Metallic Roughness Material to a glTF Material.
- * @param babylonPBRMetalRoughMaterial - BJS PBR Metallic Roughness Material.
- * @param mimeType - mime type to use for the textures.
- * @param images - array of glTF image interfaces.
- * @param textures - array of glTF texture interfaces.
- * @param materials - array of glTF material interfaces.
- * @param imageData - map of image file name to data.
- * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
- */
- _GLTFMaterial._ConvertPBRMetallicRoughnessMaterial = function (babylonPBRMetalRoughMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords) {
- var glTFPbrMetallicRoughness = {};
- if (babylonPBRMetalRoughMaterial.baseColor) {
- glTFPbrMetallicRoughness.baseColorFactor = [
- babylonPBRMetalRoughMaterial.baseColor.r,
- babylonPBRMetalRoughMaterial.baseColor.g,
- babylonPBRMetalRoughMaterial.baseColor.b,
- babylonPBRMetalRoughMaterial.alpha
- ];
- }
- if (babylonPBRMetalRoughMaterial.metallic != null && babylonPBRMetalRoughMaterial.metallic !== 1) {
- glTFPbrMetallicRoughness.metallicFactor = babylonPBRMetalRoughMaterial.metallic;
- }
- if (babylonPBRMetalRoughMaterial.roughness != null && babylonPBRMetalRoughMaterial.roughness !== 1) {
- glTFPbrMetallicRoughness.roughnessFactor = babylonPBRMetalRoughMaterial.roughness;
- }
- var glTFMaterial = {
- name: babylonPBRMetalRoughMaterial.name
- };
- if (babylonPBRMetalRoughMaterial.doubleSided) {
- glTFMaterial.doubleSided = babylonPBRMetalRoughMaterial.doubleSided;
- }
- if (hasTextureCoords) {
- if (babylonPBRMetalRoughMaterial.baseTexture != null) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMetalRoughMaterial.baseTexture, mimeType, images, textures, imageData);
- if (glTFTexture != null) {
- glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
- }
- }
- if (babylonPBRMetalRoughMaterial.normalTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMetalRoughMaterial.normalTexture, mimeType, images, textures, imageData);
- if (glTFTexture) {
- glTFMaterial.normalTexture = glTFTexture;
- }
- }
- if (babylonPBRMetalRoughMaterial.occlusionTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMetalRoughMaterial.occlusionTexture, mimeType, images, textures, imageData);
- if (glTFTexture) {
- glTFMaterial.occlusionTexture = glTFTexture;
- if (babylonPBRMetalRoughMaterial.occlusionStrength != null) {
- glTFMaterial.occlusionTexture.strength = babylonPBRMetalRoughMaterial.occlusionStrength;
- }
- }
- }
- if (babylonPBRMetalRoughMaterial.emissiveTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMetalRoughMaterial.emissiveTexture, mimeType, images, textures, imageData);
- if (glTFTexture != null) {
- glTFMaterial.emissiveTexture = glTFTexture;
- }
- }
- }
- if (this.FuzzyEquals(babylonPBRMetalRoughMaterial.emissiveColor, BABYLON.Color3.Black(), this._epsilon)) {
- glTFMaterial.emissiveFactor = babylonPBRMetalRoughMaterial.emissiveColor.asArray();
- }
- if (babylonPBRMetalRoughMaterial.transparencyMode != null) {
- var alphaMode = _GLTFMaterial._GetAlphaMode(babylonPBRMetalRoughMaterial);
- if (alphaMode !== "OPAQUE" /* OPAQUE */) { //glTF defaults to opaque
- glTFMaterial.alphaMode = alphaMode;
- if (alphaMode === "BLEND" /* BLEND */) {
- glTFMaterial.alphaCutoff = babylonPBRMetalRoughMaterial.alphaCutOff;
- }
- }
- }
- glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
- materials.push(glTFMaterial);
- };
- /**
- * Converts an image typed array buffer to a base64 image.
- * @param buffer - typed array buffer.
- * @param width - width of the image.
- * @param height - height of the image.
- * @param mimeType - mimetype of the image.
- * @returns - base64 image string.
- */
- _GLTFMaterial._CreateBase64FromCanvas = function (buffer, width, height, mimeType) {
- var imageCanvas = document.createElement('canvas');
- imageCanvas.id = "WriteCanvas";
- var ctx = imageCanvas.getContext('2d');
- imageCanvas.width = width;
- imageCanvas.height = height;
- var imgData = ctx.createImageData(width, height);
- imgData.data.set(buffer);
- ctx.putImageData(imgData, 0, 0);
- return imageCanvas.toDataURL(mimeType);
- };
- /**
- * Generates a white texture based on the specified width and height.
- * @param width - width of the texture in pixels.
- * @param height - height of the texture in pixels.
- * @param scene - babylonjs scene.
- * @returns - white texture.
- */
- _GLTFMaterial._CreateWhiteTexture = function (width, height, scene) {
- var data = new Uint8Array(width * height * 4);
- for (var i = 0; i < data.length; ++i) {
- data[i] = 255;
- }
- var rawTexture = BABYLON.RawTexture.CreateRGBATexture(data, width, height, scene);
- return rawTexture;
- };
- /**
- * Resizes the two source textures to the same dimensions. If a texture is null, a default white texture is generated. If both textures are null, returns null.
- * @param texture1 - first texture to resize.
- * @param texture2 - second texture to resize.
- * @param scene - babylonjs scene.
- * @returns resized textures or null.
- */
- _GLTFMaterial._ResizeTexturesToSameDimensions = function (texture1, texture2, scene) {
- var texture1Size = texture1 ? texture1.getSize() : { width: 0, height: 0 };
- var texture2Size = texture2 ? texture2.getSize() : { width: 0, height: 0 };
- var resizedTexture1;
- var resizedTexture2;
- if (texture1Size.width < texture2Size.width) {
- if (texture1) {
- resizedTexture1 = BABYLON.TextureTools.CreateResizedCopy(texture1, texture2Size.width, texture2Size.height, true);
- }
- else {
- resizedTexture1 = this._CreateWhiteTexture(texture2Size.width, texture2Size.height, scene);
- }
- resizedTexture2 = texture2;
- }
- else if (texture1Size.width > texture2Size.width) {
- if (texture2) {
- resizedTexture2 = BABYLON.TextureTools.CreateResizedCopy(texture2, texture1Size.width, texture1Size.height, true);
- }
- else {
- resizedTexture2 = this._CreateWhiteTexture(texture1Size.width, texture1Size.height, scene);
- }
- resizedTexture1 = texture1;
- }
- else {
- resizedTexture1 = texture1;
- resizedTexture2 = texture2;
- }
- return {
- "texture1": resizedTexture1,
- "texture2": resizedTexture2
- };
- };
- /**
- * Convert Specular Glossiness Textures to Metallic Roughness.
- * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
- * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
- * @param diffuseTexture - texture used to store diffuse information.
- * @param specularGlossinessTexture - texture used to store specular and glossiness information.
- * @param factors - specular glossiness material factors.
- * @param mimeType - the mime type to use for the texture.
- * @returns pbr metallic roughness interface or null.
- */
- _GLTFMaterial._ConvertSpecularGlossinessTexturesToMetallicRoughness = function (diffuseTexture, specularGlossinessTexture, factors, mimeType) {
- if (!(diffuseTexture || specularGlossinessTexture)) {
- return null;
- }
- var scene = diffuseTexture ? diffuseTexture.getScene() : specularGlossinessTexture.getScene();
- if (!scene) {
- throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!");
- }
- var resizedTextures = this._ResizeTexturesToSameDimensions(diffuseTexture, specularGlossinessTexture, scene);
- var diffuseSize = resizedTextures.texture1.getSize();
- var diffuseBuffer;
- var specularGlossinessBuffer;
- var width = diffuseSize.width;
- var height = diffuseSize.height;
- var pixels = (resizedTextures.texture1.readPixels());
- if (pixels instanceof Uint8Array) {
- diffuseBuffer = (resizedTextures.texture1.readPixels());
- }
- else {
- throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: " + resizedTextures.texture1.name);
- }
- pixels = resizedTextures.texture2.readPixels();
- if (pixels instanceof Uint8Array) {
- specularGlossinessBuffer = (resizedTextures.texture2.readPixels());
- }
- else {
- throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: " + resizedTextures.texture2.name);
- }
- var byteLength = specularGlossinessBuffer.byteLength;
- var metallicRoughnessBuffer = new Uint8Array(byteLength);
- var baseColorBuffer = new Uint8Array(byteLength);
- var strideSize = 4;
- var maxBaseColor = BABYLON.Color3.Black();
- var maxMetallic = 0;
- var maxRoughness = 0;
- for (var h = 0; h < height; ++h) {
- for (var w = 0; w < width; ++w) {
- var offset = (width * h + w) * strideSize;
- var diffuseColor = BABYLON.Color3.FromInts(diffuseBuffer[offset], diffuseBuffer[offset + 1], diffuseBuffer[offset + 2]).toLinearSpace().multiply(factors.diffuseColor);
- var specularColor = BABYLON.Color3.FromInts(specularGlossinessBuffer[offset], specularGlossinessBuffer[offset + 1], specularGlossinessBuffer[offset + 2]).toLinearSpace().multiply(factors.specularColor);
- var glossiness = (specularGlossinessBuffer[offset + 3] / 255) * factors.glossiness;
- var specularGlossiness = {
- diffuseColor: diffuseColor,
- specularColor: specularColor,
- glossiness: glossiness
- };
- var metallicRoughness = this._ConvertSpecularGlossinessToMetallicRoughness(specularGlossiness);
- maxBaseColor.r = Math.max(maxBaseColor.r, metallicRoughness.baseColor.r);
- maxBaseColor.g = Math.max(maxBaseColor.g, metallicRoughness.baseColor.g);
- maxBaseColor.b = Math.max(maxBaseColor.b, metallicRoughness.baseColor.b);
- maxMetallic = Math.max(maxMetallic, metallicRoughness.metallic);
- maxRoughness = Math.max(maxRoughness, metallicRoughness.roughness);
- baseColorBuffer[offset] = metallicRoughness.baseColor.r * 255;
- baseColorBuffer[offset + 1] = metallicRoughness.baseColor.g * 255;
- baseColorBuffer[offset + 2] = metallicRoughness.baseColor.b * 255;
- baseColorBuffer[offset + 3] = resizedTextures.texture1.hasAlpha ? diffuseBuffer[offset + 3] : 255;
- metallicRoughnessBuffer[offset] = 0;
- metallicRoughnessBuffer[offset + 1] = metallicRoughness.roughness * 255;
- metallicRoughnessBuffer[offset + 2] = metallicRoughness.metallic * 255;
- metallicRoughnessBuffer[offset + 3] = 255;
- }
- }
- // Retrieves the metallic roughness factors from the maximum texture values.
- var metallicRoughnessFactors = {
- baseColor: maxBaseColor,
- metallic: maxMetallic,
- roughness: maxRoughness
- };
- var writeOutMetallicRoughnessTexture = false;
- var writeOutBaseColorTexture = false;
- for (var h = 0; h < height; ++h) {
- for (var w = 0; w < width; ++w) {
- var destinationOffset = (width * h + w) * strideSize;
- baseColorBuffer[destinationOffset] /= metallicRoughnessFactors.baseColor.r > this._epsilon ? metallicRoughnessFactors.baseColor.r : 1;
- baseColorBuffer[destinationOffset + 1] /= metallicRoughnessFactors.baseColor.g > this._epsilon ? metallicRoughnessFactors.baseColor.g : 1;
- baseColorBuffer[destinationOffset + 2] /= metallicRoughnessFactors.baseColor.b > this._epsilon ? metallicRoughnessFactors.baseColor.b : 1;
- var linearBaseColorPixel = BABYLON.Color3.FromInts(baseColorBuffer[destinationOffset], baseColorBuffer[destinationOffset + 1], baseColorBuffer[destinationOffset + 2]);
- var sRGBBaseColorPixel = linearBaseColorPixel.toGammaSpace();
- baseColorBuffer[destinationOffset] = sRGBBaseColorPixel.r * 255;
- baseColorBuffer[destinationOffset + 1] = sRGBBaseColorPixel.g * 255;
- baseColorBuffer[destinationOffset + 2] = sRGBBaseColorPixel.b * 255;
- if (!this.FuzzyEquals(sRGBBaseColorPixel, BABYLON.Color3.White(), this._epsilon)) {
- writeOutBaseColorTexture = true;
- }
- metallicRoughnessBuffer[destinationOffset + 1] /= metallicRoughnessFactors.roughness > this._epsilon ? metallicRoughnessFactors.roughness : 1;
- metallicRoughnessBuffer[destinationOffset + 2] /= metallicRoughnessFactors.metallic > this._epsilon ? metallicRoughnessFactors.metallic : 1;
- var metallicRoughnessPixel = BABYLON.Color3.FromInts(255, metallicRoughnessBuffer[destinationOffset + 1], metallicRoughnessBuffer[destinationOffset + 2]);
- if (!this.FuzzyEquals(metallicRoughnessPixel, BABYLON.Color3.White(), this._epsilon)) {
- writeOutMetallicRoughnessTexture = true;
- }
- }
- }
- if (writeOutMetallicRoughnessTexture) {
- var metallicRoughnessBase64 = this._CreateBase64FromCanvas(metallicRoughnessBuffer, width, height, mimeType);
- metallicRoughnessFactors.metallicRoughnessTextureBase64 = metallicRoughnessBase64;
- }
- if (writeOutBaseColorTexture) {
- var baseColorBase64 = this._CreateBase64FromCanvas(baseColorBuffer, width, height, mimeType);
- metallicRoughnessFactors.baseColorTextureBase64 = baseColorBase64;
- }
- return metallicRoughnessFactors;
- };
- /**
- * Converts specular glossiness material properties to metallic roughness.
- * @param specularGlossiness - interface with specular glossiness material properties.
- * @returns - interface with metallic roughness material properties.
- */
- _GLTFMaterial._ConvertSpecularGlossinessToMetallicRoughness = function (specularGlossiness) {
- var diffusePerceivedBrightness = _GLTFMaterial._GetPerceivedBrightness(specularGlossiness.diffuseColor);
- var specularPerceivedBrightness = _GLTFMaterial._GetPerceivedBrightness(specularGlossiness.specularColor);
- var oneMinusSpecularStrength = 1 - _GLTFMaterial._GetMaxComponent(specularGlossiness.specularColor);
- var metallic = _GLTFMaterial._SolveMetallic(diffusePerceivedBrightness, specularPerceivedBrightness, oneMinusSpecularStrength);
- var baseColorFromDiffuse = specularGlossiness.diffuseColor.scale(oneMinusSpecularStrength / (1.0 - this._dielectricSpecular.r) / Math.max(1 - metallic, this._epsilon));
- var baseColorFromSpecular = specularGlossiness.specularColor.subtract(this._dielectricSpecular.scale(1 - metallic)).scale(1 / Math.max(metallic, this._epsilon));
- var baseColor = BABYLON.Color3.Lerp(baseColorFromDiffuse, baseColorFromSpecular, metallic * metallic);
- baseColor = baseColor.clampToRef(0, 1, baseColor);
- var metallicRoughness = {
- baseColor: baseColor,
- metallic: metallic,
- roughness: 1 - specularGlossiness.glossiness
- };
- return metallicRoughness;
- };
- /**
- * Calculates the surface reflectance, independent of lighting conditions.
- * @param color - Color source to calculate brightness from.
- * @returns number representing the perceived brightness, or zero if color is undefined.
- */
- _GLTFMaterial._GetPerceivedBrightness = function (color) {
- if (color) {
- return Math.sqrt(0.299 * color.r * color.r + 0.587 * color.g * color.g + 0.114 * color.b * color.b);
- }
- return 0;
- };
- /**
- * Returns the maximum color component value.
- * @param color
- * @returns maximum color component value, or zero if color is null or undefined.
- */
- _GLTFMaterial._GetMaxComponent = function (color) {
- if (color) {
- return Math.max(color.r, Math.max(color.g, color.b));
- }
- return 0;
- };
- /**
- * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors.
- * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
- * @param mimeType - mime type to use for the textures.
- * @param images - array of glTF image interfaces.
- * @param textures - array of glTF texture interfaces.
- * @param glTFPbrMetallicRoughness - glTF PBR Metallic Roughness interface.
- * @param imageData - map of image file name to data.
- * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
- * @returns - glTF PBR Metallic Roughness factors.
- */
- _GLTFMaterial._ConvertMetalRoughFactorsToMetallicRoughness = function (babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords) {
- var metallicRoughness = {
- baseColor: babylonPBRMaterial.albedoColor,
- metallic: babylonPBRMaterial.metallic,
- roughness: babylonPBRMaterial.roughness
- };
- if (hasTextureCoords) {
- if (babylonPBRMaterial.albedoTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.albedoTexture, mimeType, images, textures, imageData);
- if (glTFTexture) {
- glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
- }
- }
- if (babylonPBRMaterial.metallicTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.metallicTexture, mimeType, images, textures, imageData);
- if (glTFTexture != null) {
- glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFTexture;
- }
- }
- }
- return metallicRoughness;
- };
- /**
- * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors.
- * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
- * @param mimeType - mime type to use for the textures.
- * @param images - array of glTF image interfaces.
- * @param textures - array of glTF texture interfaces.
- * @param glTFPbrMetallicRoughness - glTF PBR Metallic Roughness interface.
- * @param imageData - map of image file name to data.
- * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
- * @returns - glTF PBR Metallic Roughness factors.
- */
- _GLTFMaterial._ConvertSpecGlossFactorsToMetallicRoughness = function (babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords) {
- var specGloss = {
- diffuseColor: babylonPBRMaterial.albedoColor || BABYLON.Color3.White(),
- specularColor: babylonPBRMaterial.reflectivityColor || BABYLON.Color3.White(),
- glossiness: babylonPBRMaterial.microSurface || 1,
- };
- if (babylonPBRMaterial.reflectivityTexture && !babylonPBRMaterial.useMicroSurfaceFromReflectivityMapAlpha) {
- throw new Error("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture currently not supported");
- }
- var metallicRoughnessFactors = this._ConvertSpecularGlossinessTexturesToMetallicRoughness(babylonPBRMaterial.albedoTexture, babylonPBRMaterial.reflectivityTexture, specGloss, mimeType);
- if (!metallicRoughnessFactors) {
- metallicRoughnessFactors = this._ConvertSpecularGlossinessToMetallicRoughness(specGloss);
- }
- else {
- if (hasTextureCoords) {
- if (metallicRoughnessFactors.baseColorTextureBase64) {
- var glTFBaseColorTexture = _GLTFMaterial._GetTextureInfoFromBase64(metallicRoughnessFactors.baseColorTextureBase64, "bjsBaseColorTexture_" + (textures.length) + ".png", mimeType, images, textures, imageData);
- if (glTFBaseColorTexture != null) {
- glTFPbrMetallicRoughness.baseColorTexture = glTFBaseColorTexture;
- }
- }
- if (metallicRoughnessFactors.metallicRoughnessTextureBase64) {
- var glTFMRColorTexture = _GLTFMaterial._GetTextureInfoFromBase64(metallicRoughnessFactors.metallicRoughnessTextureBase64, "bjsMetallicRoughnessTexture_" + (textures.length) + ".png", mimeType, images, textures, imageData);
- if (glTFMRColorTexture != null) {
- glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFMRColorTexture;
- }
- }
- }
- }
- return metallicRoughnessFactors;
- };
- /**
- * Converts a Babylon PBR Metallic Roughness Material to a glTF Material.
- * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
- * @param mimeType - mime type to use for the textures.
- * @param images - array of glTF image interfaces.
- * @param textures - array of glTF texture interfaces.
- * @param materials - array of glTF material interfaces.
- * @param imageData - map of image file name to data.
- * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
- */
- _GLTFMaterial._ConvertPBRMaterial = function (babylonPBRMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords) {
- var glTFPbrMetallicRoughness = {};
- var metallicRoughness;
- var glTFMaterial = {
- name: babylonPBRMaterial.name
- };
- var useMetallicRoughness = babylonPBRMaterial.isMetallicWorkflow();
- if (useMetallicRoughness) {
- metallicRoughness = this._ConvertMetalRoughFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords);
- }
- else {
- metallicRoughness = this._ConvertSpecGlossFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords);
- }
- if (!(this.FuzzyEquals(metallicRoughness.baseColor, BABYLON.Color3.White(), this._epsilon) && babylonPBRMaterial.alpha >= this._epsilon)) {
- glTFPbrMetallicRoughness.baseColorFactor = [
- metallicRoughness.baseColor.r,
- metallicRoughness.baseColor.g,
- metallicRoughness.baseColor.b,
- babylonPBRMaterial.alpha
- ];
- }
- if (metallicRoughness.metallic != null && metallicRoughness.metallic !== 1) {
- glTFPbrMetallicRoughness.metallicFactor = metallicRoughness.metallic;
- }
- if (metallicRoughness.roughness != null && metallicRoughness.roughness !== 1) {
- glTFPbrMetallicRoughness.roughnessFactor = metallicRoughness.roughness;
- }
- if (babylonPBRMaterial.backFaceCulling != null && !babylonPBRMaterial.backFaceCulling) {
- if (!babylonPBRMaterial.twoSidedLighting) {
- BABYLON.Tools.Warn(babylonPBRMaterial.name + ": Back-face culling enabled and two-sided lighting disabled is not supported in glTF.");
- }
- glTFMaterial.doubleSided = true;
- }
- if (hasTextureCoords) {
- if (babylonPBRMaterial.bumpTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.bumpTexture, mimeType, images, textures, imageData);
- if (glTFTexture) {
- glTFMaterial.normalTexture = glTFTexture;
- }
- }
- if (babylonPBRMaterial.ambientTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.ambientTexture, mimeType, images, textures, imageData);
- if (glTFTexture) {
- var occlusionTexture = {
- index: glTFTexture.index
- };
- glTFMaterial.occlusionTexture = occlusionTexture;
- if (babylonPBRMaterial.ambientTextureStrength) {
- occlusionTexture.strength = babylonPBRMaterial.ambientTextureStrength;
- }
- }
- }
- if (babylonPBRMaterial.emissiveTexture) {
- var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.emissiveTexture, mimeType, images, textures, imageData);
- if (glTFTexture != null) {
- glTFMaterial.emissiveTexture = glTFTexture;
- }
- }
- }
- if (!this.FuzzyEquals(babylonPBRMaterial.emissiveColor, BABYLON.Color3.Black(), this._epsilon)) {
- glTFMaterial.emissiveFactor = babylonPBRMaterial.emissiveColor.asArray();
- }
- if (babylonPBRMaterial.transparencyMode != null) {
- var alphaMode = _GLTFMaterial._GetAlphaMode(babylonPBRMaterial);
- if (alphaMode !== "OPAQUE" /* OPAQUE */) { //glTF defaults to opaque
- glTFMaterial.alphaMode = alphaMode;
- if (alphaMode === "BLEND" /* BLEND */) {
- glTFMaterial.alphaCutoff = babylonPBRMaterial.alphaCutOff;
- }
- }
- }
- glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
- materials.push(glTFMaterial);
- };
- /**
- * Extracts a texture from a Babylon texture into file data and glTF data.
- * @param babylonTexture - Babylon texture to extract.
- * @param mimeType - Mime Type of the babylonTexture.
- * @param images - Array of glTF images.
- * @param textures - Array of glTF textures.
- * @param imageData - map of image file name and data.
- * @return - glTF texture info, or null if the texture format is not supported.
- */
- _GLTFMaterial._ExportTexture = function (babylonTexture, mimeType, images, textures, imageData) {
- var textureName = "texture_" + (textures.length - 1).toString();
- var textureData = babylonTexture.getInternalTexture();
- if (textureData != null) {
- textureName = textureData.url || textureName;
- }
- textureName = BABYLON.Tools.GetFilename(textureName);
- var baseFile = textureName.split('.')[0];
- var extension = "";
- if (mimeType === "image/jpeg" /* JPEG */) {
- extension = ".jpg";
- }
- else if (mimeType === "image/png" /* PNG */) {
- extension = ".png";
- }
- else {
- throw new Error("Unsupported mime type " + mimeType);
- }
- textureName = baseFile + extension;
- var pixels = babylonTexture.readPixels();
- var size = babylonTexture.getSize();
- var base64Data = this._CreateBase64FromCanvas(pixels, size.width, size.height, mimeType);
- return this._GetTextureInfoFromBase64(base64Data, textureName, mimeType, images, textures, imageData);
- };
- /**
- * Builds a texture from base64 string.
- * @param base64Texture - base64 texture string.
- * @param textureName - Name to use for the texture.
- * @param mimeType - image mime type for the texture.
- * @param images - array of images.
- * @param textures - array of textures.
- * @param imageData - map of image data.
- * @returns - glTF texture info, or null if the texture format is not supported.
- */
- _GLTFMaterial._GetTextureInfoFromBase64 = function (base64Texture, textureName, mimeType, images, textures, imageData) {
- var textureInfo = null;
- var glTFTexture = {
- source: images.length
- };
- var binStr = atob(base64Texture.split(',')[1]);
- var arr = new Uint8Array(binStr.length);
- for (var i = 0; i < binStr.length; ++i) {
- arr[i] = binStr.charCodeAt(i);
- }
- var imageValues = { data: arr, mimeType: mimeType };
- imageData[textureName] = imageValues;
- if (mimeType === "image/jpeg" /* JPEG */ || mimeType === "image/png" /* PNG */) {
- var glTFImage = {
- uri: textureName
- };
- var foundIndex = -1;
- for (var i = 0; i < images.length; ++i) {
- if (images[i].uri === textureName) {
- foundIndex = i;
- break;
- }
- }
- if (foundIndex === -1) {
- images.push(glTFImage);
- glTFTexture.source = images.length - 1;
- textures.push({
- source: images.length - 1
- });
- textureInfo = {
- index: images.length - 1
- };
- }
- else {
- glTFTexture.source = foundIndex;
- textureInfo = {
- index: foundIndex
- };
- }
- }
- return textureInfo;
- };
- /**
- * Represents the dielectric specular values for R, G and B.
- */
- _GLTFMaterial._dielectricSpecular = new BABYLON.Color3(0.04, 0.04, 0.04);
- /**
- * Allows the maximum specular power to be defined for material calculations.
- */
- _GLTFMaterial._maxSpecularPower = 1024;
- /**
- * Numeric tolerance value
- */
- _GLTFMaterial._epsilon = 1e-6;
- return _GLTFMaterial;
- }());
- GLTF2._GLTFMaterial = _GLTFMaterial;
- })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.glTFMaterial.js.map
-
- return BABYLON;
- });
|