babylon.glTFFileLoader.js 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. var BABYLON;
  2. (function (BABYLON) {
  3. /**
  4. * Enums
  5. */
  6. (function (EComponentType) {
  7. EComponentType[EComponentType["BYTE"] = 5120] = "BYTE";
  8. EComponentType[EComponentType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  9. EComponentType[EComponentType["SHORT"] = 5122] = "SHORT";
  10. EComponentType[EComponentType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  11. EComponentType[EComponentType["FLOAT"] = 5126] = "FLOAT";
  12. })(BABYLON.EComponentType || (BABYLON.EComponentType = {}));
  13. var EComponentType = BABYLON.EComponentType;
  14. (function (EShaderType) {
  15. EShaderType[EShaderType["FRAGMENT"] = 35632] = "FRAGMENT";
  16. EShaderType[EShaderType["VERTEX"] = 35633] = "VERTEX";
  17. })(BABYLON.EShaderType || (BABYLON.EShaderType = {}));
  18. var EShaderType = BABYLON.EShaderType;
  19. (function (EParameterType) {
  20. EParameterType[EParameterType["BYTE"] = 5120] = "BYTE";
  21. EParameterType[EParameterType["UNSIGNED_BYTE"] = 5121] = "UNSIGNED_BYTE";
  22. EParameterType[EParameterType["SHORT"] = 5122] = "SHORT";
  23. EParameterType[EParameterType["UNSIGNED_SHORT"] = 5123] = "UNSIGNED_SHORT";
  24. EParameterType[EParameterType["INT"] = 5124] = "INT";
  25. EParameterType[EParameterType["UNSIGNED_INT"] = 5125] = "UNSIGNED_INT";
  26. EParameterType[EParameterType["FLOAT"] = 5126] = "FLOAT";
  27. EParameterType[EParameterType["FLOAT_VEC2"] = 35664] = "FLOAT_VEC2";
  28. EParameterType[EParameterType["FLOAT_VEC3"] = 35665] = "FLOAT_VEC3";
  29. EParameterType[EParameterType["FLOAT_VEC4"] = 35666] = "FLOAT_VEC4";
  30. EParameterType[EParameterType["INT_VEC2"] = 35667] = "INT_VEC2";
  31. EParameterType[EParameterType["INT_VEC3"] = 35668] = "INT_VEC3";
  32. EParameterType[EParameterType["INT_VEC4"] = 35669] = "INT_VEC4";
  33. EParameterType[EParameterType["BOOL"] = 35670] = "BOOL";
  34. EParameterType[EParameterType["BOOL_VEC2"] = 35671] = "BOOL_VEC2";
  35. EParameterType[EParameterType["BOOL_VEC3"] = 35672] = "BOOL_VEC3";
  36. EParameterType[EParameterType["BOOL_VEC4"] = 35673] = "BOOL_VEC4";
  37. EParameterType[EParameterType["FLOAT_MAT2"] = 35674] = "FLOAT_MAT2";
  38. EParameterType[EParameterType["FLOAT_MAT3"] = 35675] = "FLOAT_MAT3";
  39. EParameterType[EParameterType["FLOAT_MAT4"] = 35676] = "FLOAT_MAT4";
  40. EParameterType[EParameterType["SAMPLER_2D"] = 35678] = "SAMPLER_2D";
  41. })(BABYLON.EParameterType || (BABYLON.EParameterType = {}));
  42. var EParameterType = BABYLON.EParameterType;
  43. (function (ETextureWrapMode) {
  44. ETextureWrapMode[ETextureWrapMode["CLAMP_TO_EDGE"] = 33071] = "CLAMP_TO_EDGE";
  45. ETextureWrapMode[ETextureWrapMode["MIRRORED_REPEAT"] = 33648] = "MIRRORED_REPEAT";
  46. ETextureWrapMode[ETextureWrapMode["REPEAT"] = 10497] = "REPEAT";
  47. })(BABYLON.ETextureWrapMode || (BABYLON.ETextureWrapMode = {}));
  48. var ETextureWrapMode = BABYLON.ETextureWrapMode;
  49. (function (ETextureFilterType) {
  50. ETextureFilterType[ETextureFilterType["NEAREST"] = 9728] = "NEAREST";
  51. ETextureFilterType[ETextureFilterType["LINEAR"] = 9728] = "LINEAR";
  52. ETextureFilterType[ETextureFilterType["NEAREST_MIPMAP_NEAREST"] = 9984] = "NEAREST_MIPMAP_NEAREST";
  53. ETextureFilterType[ETextureFilterType["LINEAR_MIPMAP_NEAREST"] = 9985] = "LINEAR_MIPMAP_NEAREST";
  54. ETextureFilterType[ETextureFilterType["NEAREST_MIPMAP_LINEAR"] = 9986] = "NEAREST_MIPMAP_LINEAR";
  55. ETextureFilterType[ETextureFilterType["LINEAR_MIPMAP_LINEAR"] = 9987] = "LINEAR_MIPMAP_LINEAR";
  56. })(BABYLON.ETextureFilterType || (BABYLON.ETextureFilterType = {}));
  57. var ETextureFilterType = BABYLON.ETextureFilterType;
  58. (function (ETextureFormat) {
  59. ETextureFormat[ETextureFormat["ALPHA"] = 6406] = "ALPHA";
  60. ETextureFormat[ETextureFormat["RGB"] = 6407] = "RGB";
  61. ETextureFormat[ETextureFormat["RGBA"] = 6408] = "RGBA";
  62. ETextureFormat[ETextureFormat["LUMINANCE"] = 6409] = "LUMINANCE";
  63. ETextureFormat[ETextureFormat["LUMINANCE_ALPHA"] = 6410] = "LUMINANCE_ALPHA";
  64. })(BABYLON.ETextureFormat || (BABYLON.ETextureFormat = {}));
  65. var ETextureFormat = BABYLON.ETextureFormat;
  66. (function (ECullingType) {
  67. ECullingType[ECullingType["FRONT"] = 1028] = "FRONT";
  68. ECullingType[ECullingType["BACK"] = 1029] = "BACK";
  69. ECullingType[ECullingType["FRONT_AND_BACK"] = 1032] = "FRONT_AND_BACK";
  70. })(BABYLON.ECullingType || (BABYLON.ECullingType = {}));
  71. var ECullingType = BABYLON.ECullingType;
  72. (function (EBlendingFunction) {
  73. EBlendingFunction[EBlendingFunction["ZERO"] = 0] = "ZERO";
  74. EBlendingFunction[EBlendingFunction["ONE"] = 1] = "ONE";
  75. EBlendingFunction[EBlendingFunction["SRC_COLOR"] = 768] = "SRC_COLOR";
  76. EBlendingFunction[EBlendingFunction["ONE_MINUS_SRC_COLOR"] = 769] = "ONE_MINUS_SRC_COLOR";
  77. EBlendingFunction[EBlendingFunction["DST_COLOR"] = 774] = "DST_COLOR";
  78. EBlendingFunction[EBlendingFunction["ONE_MINUS_DST_COLOR"] = 775] = "ONE_MINUS_DST_COLOR";
  79. EBlendingFunction[EBlendingFunction["SRC_ALPHA"] = 770] = "SRC_ALPHA";
  80. EBlendingFunction[EBlendingFunction["ONE_MINUS_SRC_ALPHA"] = 771] = "ONE_MINUS_SRC_ALPHA";
  81. EBlendingFunction[EBlendingFunction["DST_ALPHA"] = 772] = "DST_ALPHA";
  82. EBlendingFunction[EBlendingFunction["ONE_MINUS_DST_ALPHA"] = 773] = "ONE_MINUS_DST_ALPHA";
  83. EBlendingFunction[EBlendingFunction["CONSTANT_COLOR"] = 32769] = "CONSTANT_COLOR";
  84. EBlendingFunction[EBlendingFunction["ONE_MINUS_CONSTANT_COLOR"] = 32770] = "ONE_MINUS_CONSTANT_COLOR";
  85. EBlendingFunction[EBlendingFunction["CONSTANT_ALPHA"] = 32771] = "CONSTANT_ALPHA";
  86. EBlendingFunction[EBlendingFunction["ONE_MINUS_CONSTANT_ALPHA"] = 32772] = "ONE_MINUS_CONSTANT_ALPHA";
  87. EBlendingFunction[EBlendingFunction["SRC_ALPHA_SATURATE"] = 776] = "SRC_ALPHA_SATURATE";
  88. })(BABYLON.EBlendingFunction || (BABYLON.EBlendingFunction = {}));
  89. var EBlendingFunction = BABYLON.EBlendingFunction;
  90. /**
  91. * Tokenizer. Used for shaders compatibility
  92. * Automatically map world, view, projection, worldViewProjection, attributes and so on
  93. */
  94. var ETokenType;
  95. (function (ETokenType) {
  96. ETokenType[ETokenType["IDENTIFIER"] = 1] = "IDENTIFIER";
  97. ETokenType[ETokenType["UNKNOWN"] = 2] = "UNKNOWN";
  98. ETokenType[ETokenType["END_OF_INPUT"] = 3] = "END_OF_INPUT";
  99. })(ETokenType || (ETokenType = {}));
  100. var Tokenizer = (function () {
  101. function Tokenizer(toParse) {
  102. this._pos = 0;
  103. this.isLetterOrDigitPattern = /^[a-zA-Z0-9]+$/;
  104. this._toParse = toParse;
  105. this._maxPos = toParse.length;
  106. }
  107. Tokenizer.prototype.getNextToken = function () {
  108. if (this.isEnd())
  109. return ETokenType.END_OF_INPUT;
  110. this.currentString = this.read();
  111. this.currentToken = ETokenType.UNKNOWN;
  112. if (this.currentString === "_" || this.isLetterOrDigitPattern.test(this.currentString)) {
  113. this.currentToken = ETokenType.IDENTIFIER;
  114. this.currentIdentifier = this.currentString;
  115. while (!this.isEnd() && (this.isLetterOrDigitPattern.test(this.currentString = this.peek()) || this.currentString === "_")) {
  116. this.currentIdentifier += this.currentString;
  117. this.forward();
  118. }
  119. }
  120. return this.currentToken;
  121. };
  122. Tokenizer.prototype.peek = function () {
  123. return this._toParse[this._pos];
  124. };
  125. Tokenizer.prototype.read = function () {
  126. return this._toParse[this._pos++];
  127. };
  128. Tokenizer.prototype.forward = function () {
  129. this._pos++;
  130. };
  131. Tokenizer.prototype.isEnd = function () {
  132. return this._pos >= this._maxPos;
  133. };
  134. return Tokenizer;
  135. })();
  136. /**
  137. * Values
  138. */
  139. var glTFTransforms = ["MODEL", "VIEW", "PROJECTION", "MODELVIEW", "MODELVIEWPROJECTION", "JOINTMATRIX"];
  140. var babylonTransforms = ["world", "view", "projection", "worldView", "worldViewProjection", "mBones"];
  141. var glTFAnimationPaths = ["translation", "rotation", "scale"];
  142. var babylonAnimationPaths = ["position", "rotationQuaternion", "scaling"];
  143. /**
  144. * Parse
  145. */
  146. var parseBuffers = function (parsedBuffers, gltfRuntime) {
  147. for (var buf in parsedBuffers) {
  148. var parsedBuffer = parsedBuffers[buf];
  149. gltfRuntime.buffers[buf] = parsedBuffer;
  150. gltfRuntime.buffersCount++;
  151. }
  152. };
  153. var parseShaders = function (parsedShaders, gltfRuntime) {
  154. for (var sha in parsedShaders) {
  155. var parsedShader = parsedShaders[sha];
  156. gltfRuntime.shaders[sha] = parsedShader;
  157. gltfRuntime.shaderscount++;
  158. }
  159. };
  160. var parseObject = function (parsedObjects, runtimeProperty, gltfRuntime) {
  161. for (var object in parsedObjects) {
  162. var parsedObject = parsedObjects[object];
  163. gltfRuntime[runtimeProperty][object] = parsedObject;
  164. }
  165. };
  166. /**
  167. * Utils
  168. */
  169. var getByteStrideFromType = function (accessor) {
  170. // Needs this function since "byteStride" isn't requiered in glTF format
  171. var type = accessor.type;
  172. switch (type) {
  173. case "VEC2": return 2;
  174. case "VEC3": return 3;
  175. case "VEC4": return 4;
  176. case "MAT2": return 4;
  177. case "MAT3": return 9;
  178. case "MAT4": return 16;
  179. default: return 1;
  180. }
  181. };
  182. var setMatrix = function (scene, source, parameter, uniformName, shaderMaterial) {
  183. var mat = null;
  184. if (parameter.semantic === "MODEL") {
  185. mat = source.getWorldMatrix();
  186. }
  187. else if (parameter.semantic === "PROJECTION") {
  188. mat = scene.getProjectionMatrix();
  189. }
  190. else if (parameter.semantic === "VIEW") {
  191. mat = scene.getViewMatrix();
  192. }
  193. else if (parameter.semantic === "MODELVIEWINVERSETRANSPOSE") {
  194. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().multiply(scene.getViewMatrix()).invert());
  195. }
  196. else if (parameter.semantic === "MODELVIEW") {
  197. mat = source.getWorldMatrix().multiply(scene.getViewMatrix());
  198. }
  199. else if (parameter.semantic === "MODELVIEWPROJECTION") {
  200. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix());
  201. }
  202. else if (parameter.semantic === "MODELINVERSE") {
  203. mat = source.getWorldMatrix().invert();
  204. }
  205. else if (parameter.semantic === "VIEWINVERSE") {
  206. mat = scene.getViewMatrix().invert();
  207. }
  208. else if (parameter.semantic === "PROJECTIONINVERSE") {
  209. mat = scene.getProjectionMatrix().invert();
  210. }
  211. else if (parameter.semantic === "MODELVIEWINVERSE") {
  212. mat = source.getWorldMatrix().multiply(scene.getViewMatrix()).invert();
  213. }
  214. else if (parameter.semantic === "MODELVIEWPROJECTIONINVERSE") {
  215. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix()).invert();
  216. }
  217. else if (parameter.semantic === "MODELINVERSETRANSPOSE") {
  218. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().invert());
  219. }
  220. else {
  221. debugger;
  222. }
  223. switch (parameter.type) {
  224. case EParameterType.FLOAT_MAT2:
  225. shaderMaterial.setMatrix2x2(uniformName, BABYLON.Matrix.GetAsMatrix2x2(mat));
  226. break;
  227. case EParameterType.FLOAT_MAT3:
  228. shaderMaterial.setMatrix3x3(uniformName, BABYLON.Matrix.GetAsMatrix3x3(mat));
  229. break;
  230. case EParameterType.FLOAT_MAT4:
  231. shaderMaterial.setMatrix(uniformName, mat);
  232. break;
  233. default: break;
  234. }
  235. };
  236. var setUniform = function (shaderMaterial, uniform, value, type) {
  237. switch (type) {
  238. case EParameterType.FLOAT:
  239. shaderMaterial.setFloat(uniform, value);
  240. return true;
  241. case EParameterType.FLOAT_VEC2:
  242. shaderMaterial.setVector2(uniform, BABYLON.Vector2.FromArray(value));
  243. return true;
  244. case EParameterType.FLOAT_VEC3:
  245. shaderMaterial.setVector3(uniform, BABYLON.Vector3.FromArray(value));
  246. return true;
  247. case EParameterType.FLOAT_VEC4:
  248. shaderMaterial.setVector4(uniform, BABYLON.Vector4.FromArray(value));
  249. return true;
  250. default: return false;
  251. }
  252. };
  253. var getWrapMode = function (mode) {
  254. switch (mode) {
  255. case ETextureWrapMode.CLAMP_TO_EDGE: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  256. case ETextureWrapMode.MIRRORED_REPEAT: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  257. case ETextureWrapMode.REPEAT: return BABYLON.Texture.WRAP_ADDRESSMODE;
  258. default: return BABYLON.Texture.WRAP_ADDRESSMODE;
  259. }
  260. };
  261. var getTextureFilterMode = function (mode) {
  262. switch (mode) {
  263. case ETextureFilterType.LINEAR:
  264. case ETextureFilterType.LINEAR_MIPMAP_NEAREST:
  265. case ETextureFilterType.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  266. case ETextureFilterType.NEAREST:
  267. case ETextureFilterType.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_SAMPLINGMODE;
  268. default: return BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  269. }
  270. };
  271. var getBufferFromAccessor = function (gltfRuntime, accessor) {
  272. var bufferView = gltfRuntime.bufferViews[accessor.bufferView];
  273. var arrayBuffer = gltfRuntime.arrayBuffers[bufferView.buffer];
  274. var byteOffset = accessor.byteOffset + bufferView.byteOffset;
  275. var count = accessor.count * getByteStrideFromType(accessor);
  276. switch (accessor.componentType) {
  277. case EComponentType.BYTE: return new Int8Array(arrayBuffer, byteOffset, count);
  278. case EComponentType.UNSIGNED_BYTE: return new Uint8Array(arrayBuffer, byteOffset, count);
  279. case EComponentType.SHORT: return new Int16Array(arrayBuffer, byteOffset, count);
  280. case EComponentType.UNSIGNED_SHORT: return new Uint16Array(arrayBuffer, byteOffset, count);
  281. default: return new Float32Array(arrayBuffer, byteOffset, count);
  282. }
  283. };
  284. var normalizeUVs = function (buffer) {
  285. if (!buffer) {
  286. return;
  287. }
  288. for (var i = 0; i < buffer.length / 2; i++) {
  289. buffer[i * 2 + 1] = 1.0 - buffer[i * 2 + 1];
  290. }
  291. };
  292. var replaceInString = function (str, searchValue, replaceValue) {
  293. while (str.indexOf(searchValue) !== -1) {
  294. str = str.replace(searchValue, replaceValue);
  295. }
  296. return str;
  297. };
  298. var getAttribute = function (attributeParameter) {
  299. if (attributeParameter.semantic === "NORMAL") {
  300. return "normal";
  301. }
  302. else if (attributeParameter.semantic === "POSITION") {
  303. return "position";
  304. }
  305. else if (attributeParameter.semantic === "JOINT") {
  306. return "matricesIndices";
  307. }
  308. else if (attributeParameter.semantic === "WEIGHT") {
  309. return "matricesWeights";
  310. }
  311. else if (attributeParameter.semantic === "COLOR") {
  312. return "color";
  313. }
  314. else if (attributeParameter.semantic.indexOf("TEXCOORD_") !== -1) {
  315. var channel = Number(attributeParameter.semantic.split("_")[1]);
  316. return "uv" + (channel === 0 ? "" : channel + 1);
  317. }
  318. };
  319. var isBase64 = function (uri) {
  320. return uri.length < 5 ? false : uri.substr(0, 5) === "data:";
  321. };
  322. /**
  323. * Returns the animation path (glTF -> Babylon)
  324. */
  325. var getAnimationPath = function (path) {
  326. var index = glTFAnimationPaths.indexOf(path);
  327. if (index !== -1) {
  328. return babylonAnimationPaths[index];
  329. }
  330. return path;
  331. };
  332. /**
  333. * Loads and creates animations
  334. */
  335. var loadAnimations = function (gltfRuntime) {
  336. for (var anim in gltfRuntime.animations) {
  337. var animation = gltfRuntime.animations[anim];
  338. var lastAnimation = null;
  339. for (var i = 0; i < animation.channels.length; i++) {
  340. // Get parameters and load buffers
  341. var channel = animation.channels[i];
  342. var sampler = animation.samplers[channel.sampler];
  343. if (!sampler) {
  344. continue;
  345. }
  346. var inputData = animation.parameters[sampler.input];
  347. var outputData = animation.parameters[sampler.output];
  348. var bufferInput = getBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[inputData]);
  349. var bufferOutput = getBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[outputData]);
  350. var targetID = channel.target.id;
  351. var targetNode = gltfRuntime.scene.getNodeByID(targetID);
  352. if (targetNode === null) {
  353. BABYLON.Tools.Warn("Creating animation named " + anim + ". But cannot find node named " + targetID + " to attach to");
  354. continue;
  355. }
  356. var isBone = targetNode instanceof BABYLON.Bone;
  357. // Get target path (position, rotation or scaling)
  358. var targetPath = channel.target.path;
  359. var targetPathIndex = glTFAnimationPaths.indexOf(targetPath);
  360. if (targetPathIndex !== -1) {
  361. targetPath = babylonAnimationPaths[targetPathIndex];
  362. }
  363. // Determine animation type
  364. var animationType = BABYLON.Animation.ANIMATIONTYPE_MATRIX;
  365. if (!isBone) {
  366. if (targetPath === "rotationQuaternion") {
  367. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  368. targetNode.rotationQuaternion = new BABYLON.Quaternion();
  369. }
  370. else {
  371. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  372. }
  373. }
  374. // Create animation and key frames
  375. var babylonAnimation = null;
  376. var keys = [];
  377. var arrayOffset = 0;
  378. var modifyKey = false;
  379. if (isBone && lastAnimation && lastAnimation.getKeys().length === bufferInput.length) {
  380. babylonAnimation = lastAnimation;
  381. modifyKey = true;
  382. }
  383. if (!modifyKey) {
  384. babylonAnimation = new BABYLON.Animation(anim, isBone ? "_matrix" : targetPath, 1, animationType, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  385. }
  386. // For each frame
  387. for (var j = 0; j < bufferInput.length; j++) {
  388. var value = null;
  389. if (targetPath === "rotationQuaternion") {
  390. value = BABYLON.Quaternion.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2], bufferOutput[arrayOffset + 3]]);
  391. arrayOffset += 4;
  392. }
  393. else {
  394. value = BABYLON.Vector3.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2]]);
  395. arrayOffset += 3;
  396. }
  397. if (isBone) {
  398. var bone = targetNode;
  399. var translation = BABYLON.Vector3.Zero();
  400. var rotationQuaternion = new BABYLON.Quaternion();
  401. var scaling = BABYLON.Vector3.Zero();
  402. // Warning on decompose
  403. var mat = bone.getBaseMatrix();
  404. if (modifyKey) {
  405. mat = lastAnimation.getKeys()[j].value;
  406. }
  407. mat.decompose(scaling, rotationQuaternion, translation);
  408. if (targetPath === "position") {
  409. translation = value;
  410. }
  411. else if (targetPath === "rotationQuaternion") {
  412. rotationQuaternion = value;
  413. // Y is Up
  414. if (GLTFFileLoader.MakeYUP) {
  415. rotationQuaternion = rotationQuaternion.multiply(new BABYLON.Quaternion(-0.707107, 0, 0, 0.707107));
  416. }
  417. }
  418. else {
  419. scaling = value;
  420. }
  421. value = BABYLON.Matrix.Compose(scaling, rotationQuaternion, translation);
  422. }
  423. if (!modifyKey) {
  424. keys.push({
  425. frame: bufferInput[j],
  426. value: value
  427. });
  428. }
  429. else {
  430. lastAnimation.getKeys()[j].value = value;
  431. }
  432. }
  433. // Finish
  434. if (!modifyKey) {
  435. babylonAnimation.setKeys(keys);
  436. targetNode.animations.push(babylonAnimation);
  437. }
  438. lastAnimation = babylonAnimation;
  439. gltfRuntime.scene.stopAnimation(targetNode);
  440. gltfRuntime.scene.beginAnimation(targetNode, 0, bufferInput[bufferInput.length - 1], true, 1.0);
  441. }
  442. }
  443. };
  444. /**
  445. * Returns the bones transformation matrix
  446. */
  447. var configureBoneTransformation = function (node) {
  448. var mat = null;
  449. if (node.translation && node.rotation && node.scale) {
  450. var scale = BABYLON.Vector3.FromArray(node.scale);
  451. var rotation = BABYLON.Quaternion.FromArray(node.rotation);
  452. var position = BABYLON.Vector3.FromArray(node.translation);
  453. // Y is Up
  454. if (GLTFFileLoader.MakeYUP) {
  455. rotation = rotation.multiply(new BABYLON.Quaternion(-0.707107, 0, 0, 0.707107));
  456. }
  457. mat = BABYLON.Matrix.Compose(scale, rotation, position);
  458. }
  459. else {
  460. mat = BABYLON.Matrix.FromArray(node.matrix);
  461. }
  462. return mat;
  463. };
  464. /**
  465. * Returns the parent bone
  466. */
  467. var getParentBone = function (gltfRuntime, skins, jointName, newSkeleton) {
  468. // Try to find
  469. for (var i = 0; i < newSkeleton.bones.length; i++) {
  470. if (newSkeleton.bones[i].id === jointName) {
  471. return newSkeleton.bones[i];
  472. }
  473. }
  474. // Not found, search in gltf nodes
  475. var nodes = gltfRuntime.nodes;
  476. for (var nde in nodes) {
  477. var node = nodes[nde];
  478. if (!node.jointName) {
  479. continue;
  480. }
  481. var children = node.children;
  482. for (var i = 0; i < children.length; i++) {
  483. var child = gltfRuntime.nodes[children[i]];
  484. if (!child.jointName) {
  485. continue;
  486. }
  487. if (child.jointName === jointName) {
  488. var mat = configureBoneTransformation(node);
  489. var bone = new BABYLON.Bone(node.name, newSkeleton, getParentBone(gltfRuntime, skins, node.jointName, newSkeleton), mat);
  490. bone.id = nde;
  491. return bone;
  492. }
  493. }
  494. }
  495. return null;
  496. };
  497. /**
  498. * Returns the appropriate root node
  499. */
  500. var getNodeToRoot = function (nodesToRoot, id) {
  501. for (var i = 0; i < nodesToRoot.length; i++) {
  502. var nodeToRoot = nodesToRoot[i];
  503. for (var j = 0; j < nodeToRoot.node.children.length; j++) {
  504. var child = nodeToRoot.node.children[j];
  505. if (child === id) {
  506. return nodeToRoot.bone;
  507. }
  508. }
  509. }
  510. return null;
  511. };
  512. /**
  513. * Returns the node with the joint name
  514. */
  515. var getJointNode = function (gltfRuntime, jointName) {
  516. var nodes = gltfRuntime.nodes;
  517. var node = nodes[jointName];
  518. if (node) {
  519. return {
  520. node: node,
  521. id: jointName
  522. };
  523. }
  524. for (var nde in nodes) {
  525. node = nodes[nde];
  526. if (node.jointName === jointName) {
  527. return {
  528. node: node,
  529. id: nde
  530. };
  531. }
  532. }
  533. return null;
  534. };
  535. /**
  536. * Checks if a nodes is in joints
  537. */
  538. var nodeIsInJoints = function (skins, id) {
  539. for (var i = 0; i < skins.jointNames.length; i++) {
  540. if (skins.jointNames[i] === id) {
  541. return true;
  542. }
  543. }
  544. return false;
  545. };
  546. /**
  547. * Fills the nodes to root for bones and builds hierarchy
  548. */
  549. var getNodesToRoot = function (gltfRuntime, newSkeleton, skins, nodesToRoot) {
  550. // Creates nodes for root
  551. for (var nde in gltfRuntime.nodes) {
  552. var node = gltfRuntime.nodes[nde];
  553. var id = nde;
  554. if (!node.jointName || nodeIsInJoints(skins, node.jointName)) {
  555. continue;
  556. }
  557. // Create node to root bone
  558. var mat = configureBoneTransformation(node);
  559. var bone = new BABYLON.Bone(node.name, newSkeleton, null, mat);
  560. bone.id = id;
  561. nodesToRoot.push({ bone: bone, node: node, id: id });
  562. }
  563. // Parenting
  564. for (var i = 0; i < nodesToRoot.length; i++) {
  565. var nodeToRoot = nodesToRoot[i];
  566. var children = nodeToRoot.node.children;
  567. for (var j = 0; j < children.length; j++) {
  568. var child = null;
  569. for (var k = 0; k < nodesToRoot.length; k++) {
  570. if (nodesToRoot[k].id === children[j]) {
  571. child = nodesToRoot[k];
  572. break;
  573. }
  574. }
  575. if (child) {
  576. child.bone._parent = nodeToRoot.bone;
  577. nodeToRoot.bone.children.push(child.bone);
  578. }
  579. }
  580. }
  581. };
  582. var printMat = function (m) {
  583. console.log(m[0] + "\t" + m[1] + "\t" + m[2] + "\t" + m[3] + "\n" +
  584. m[4] + "\t" + m[5] + "\t" + m[6] + "\t" + m[7] + "\n" +
  585. m[8] + "\t" + m[9] + "\t" + m[10] + "\t" + m[11] + "\n" +
  586. m[12] + "\t" + m[13] + "\t" + m[14] + "\t" + m[15] + "\n");
  587. };
  588. /**
  589. * Imports a skeleton
  590. */
  591. var importSkeleton = function (gltfRuntime, skins, mesh, newSkeleton) {
  592. if (!newSkeleton) {
  593. newSkeleton = new BABYLON.Skeleton(skins.name, "", gltfRuntime.scene);
  594. }
  595. if (!skins.babylonSkeleton) {
  596. return newSkeleton;
  597. }
  598. // Matrices
  599. var accessor = gltfRuntime.accessors[skins.inverseBindMatrices];
  600. var buffer = getBufferFromAccessor(gltfRuntime, accessor);
  601. var bindShapeMatrix = BABYLON.Matrix.FromArray(skins.bindShapeMatrix);
  602. // Find the root bones
  603. var nodesToRoot = [];
  604. var nodesToRootToAdd = [];
  605. getNodesToRoot(gltfRuntime, newSkeleton, skins, nodesToRoot);
  606. newSkeleton.bones = [];
  607. if (nodesToRoot.length === 0) {
  608. newSkeleton.needInitialSkinMatrix = true;
  609. }
  610. // Joints
  611. for (var i = 0; i < skins.jointNames.length; i++) {
  612. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  613. var node = jointNode.node;
  614. if (!node) {
  615. BABYLON.Tools.Warn("Joint named " + skins.jointNames[i] + " does not exist");
  616. continue;
  617. }
  618. var id = jointNode.id;
  619. // Optimize, if the bone already exists...
  620. var existingBone = gltfRuntime.scene.getBoneByID(id);
  621. if (existingBone) {
  622. newSkeleton.bones.push(existingBone);
  623. continue;
  624. }
  625. // Check if node already exists
  626. var foundBone = false;
  627. for (var j = 0; j < newSkeleton.bones.length; j++) {
  628. if (newSkeleton.bones[j].id === id) {
  629. foundBone = true;
  630. break;
  631. }
  632. }
  633. if (foundBone) {
  634. continue;
  635. }
  636. // Search for parent bone
  637. var parentBone = null;
  638. for (var j = 0; j < i; j++) {
  639. var joint = getJointNode(gltfRuntime, skins.jointNames[j]).node;
  640. if (!joint) {
  641. BABYLON.Tools.Warn("Joint named " + skins.jointNames[j] + " does not exist when looking for parent");
  642. continue;
  643. }
  644. var children = joint.children;
  645. foundBone = false;
  646. for (var k = 0; k < children.length; k++) {
  647. if (children[k] === id) {
  648. parentBone = getParentBone(gltfRuntime, skins, skins.jointNames[j], newSkeleton);
  649. foundBone = true;
  650. break;
  651. }
  652. }
  653. if (foundBone) {
  654. break;
  655. }
  656. }
  657. // Create bone
  658. var mat = configureBoneTransformation(node);
  659. if (!parentBone && nodesToRoot.length > 0) {
  660. parentBone = getNodeToRoot(nodesToRoot, id);
  661. if (parentBone) {
  662. if (nodesToRootToAdd.indexOf(parentBone) === -1) {
  663. nodesToRootToAdd.push(parentBone);
  664. }
  665. }
  666. }
  667. if (!parentBone && nodesToRoot.length === 0) {
  668. var inverseBindMatrix = BABYLON.Matrix.FromArray(buffer, i * 16);
  669. var invertMesh = BABYLON.Matrix.Invert(mesh.getWorldMatrix());
  670. mat = mat.multiply(mesh.getWorldMatrix());
  671. }
  672. var bone = new BABYLON.Bone(node.name, newSkeleton, parentBone, mat);
  673. bone.id = id;
  674. }
  675. // Polish
  676. var bones = newSkeleton.bones;
  677. newSkeleton.bones = [];
  678. for (var i = 0; i < skins.jointNames.length; i++) {
  679. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  680. if (!jointNode) {
  681. continue;
  682. }
  683. for (var j = 0; j < bones.length; j++) {
  684. if (bones[j].id === jointNode.id) {
  685. newSkeleton.bones.push(bones[j]);
  686. break;
  687. }
  688. }
  689. }
  690. // Finish
  691. newSkeleton.prepare();
  692. for (var i = 0; i < nodesToRootToAdd.length; i++) {
  693. newSkeleton.bones.push(nodesToRootToAdd[i]);
  694. }
  695. return newSkeleton;
  696. };
  697. /**
  698. * Imports a mesh and its geometries
  699. */
  700. var importMesh = function (gltfRuntime, node, meshes, id, newMesh) {
  701. if (!newMesh) {
  702. newMesh = new BABYLON.Mesh(node.name, gltfRuntime.scene);
  703. newMesh.id = id;
  704. }
  705. if (!node.babylonNode) {
  706. return newMesh;
  707. }
  708. var multiMat = new BABYLON.MultiMaterial("multimat" + id, gltfRuntime.scene);
  709. newMesh.material = multiMat;
  710. var vertexData = new BABYLON.VertexData();
  711. var geometry = new BABYLON.Geometry(id, gltfRuntime.scene, vertexData, false, newMesh);
  712. var verticesStarts = [];
  713. var verticesCounts = [];
  714. var indexStarts = [];
  715. var indexCounts = [];
  716. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  717. var meshID = meshes[meshIndex];
  718. var mesh = gltfRuntime.meshes[meshID];
  719. if (!mesh) {
  720. continue;
  721. }
  722. // Positions, normals and UVs
  723. for (var i = 0; i < mesh.primitives.length; i++) {
  724. // Temporary vertex data
  725. var tempVertexData = new BABYLON.VertexData();
  726. var primitive = mesh.primitives[i];
  727. if (primitive.mode !== 4) {
  728. }
  729. var attributes = primitive.attributes;
  730. var accessor = null;
  731. var buffer = null;
  732. // Set positions, normal and uvs
  733. for (var semantic in attributes) {
  734. // Link accessor and buffer view
  735. accessor = gltfRuntime.accessors[attributes[semantic]];
  736. buffer = getBufferFromAccessor(gltfRuntime, accessor);
  737. if (semantic === "NORMAL") {
  738. tempVertexData.normals = [];
  739. for (var j = 0; j < buffer.length; j++) {
  740. tempVertexData.normals.push(buffer[j]);
  741. }
  742. }
  743. else if (semantic === "POSITION") {
  744. tempVertexData.positions = [];
  745. for (var j = 0; j < buffer.length; j++) {
  746. tempVertexData.positions.push(buffer[j]);
  747. }
  748. verticesCounts.push(tempVertexData.positions.length);
  749. }
  750. else if (semantic.indexOf("TEXCOORD_") !== -1) {
  751. var channel = Number(semantic.split("_")[1]);
  752. var uvKind = BABYLON.VertexBuffer.UVKind + (channel === 0 ? "" : (channel + 1));
  753. var uvs = [];
  754. for (var j = 0; j < buffer.length; j++) {
  755. uvs.push(buffer[j]);
  756. }
  757. normalizeUVs(uvs);
  758. tempVertexData.set(uvs, uvKind);
  759. }
  760. else if (semantic === "JOINT") {
  761. tempVertexData.matricesIndices = [];
  762. for (var j = 0; j < buffer.length; j++) {
  763. tempVertexData.matricesIndices.push(buffer[j]);
  764. }
  765. }
  766. else if (semantic === "WEIGHT") {
  767. tempVertexData.matricesWeights = [];
  768. for (var j = 0; j < buffer.length; j++) {
  769. tempVertexData.matricesWeights.push(buffer[j]);
  770. }
  771. }
  772. else if (semantic === "COLOR") {
  773. tempVertexData.colors = [];
  774. for (var j = 0; j < buffer.length; j++) {
  775. tempVertexData.colors.push(buffer[j]);
  776. }
  777. }
  778. }
  779. // Indices
  780. accessor = gltfRuntime.accessors[primitive.indices];
  781. buffer = getBufferFromAccessor(gltfRuntime, accessor);
  782. tempVertexData.indices = [];
  783. for (var j = 0; j < buffer.length; j++) {
  784. tempVertexData.indices.push(buffer[j]);
  785. }
  786. indexCounts.push(buffer.length);
  787. vertexData.merge(tempVertexData);
  788. tempVertexData = undefined;
  789. // Sub material
  790. var material = gltfRuntime.scene.getMaterialByID(primitive.material);
  791. multiMat.subMaterials.push(material === null ? gltfRuntime.scene.defaultMaterial : material);
  792. // Update vertices start and index start
  793. verticesStarts.push(verticesStarts.length === 0 ? 0 : verticesStarts[verticesStarts.length - 1] + verticesCounts[verticesCounts.length - 2]);
  794. indexStarts.push(indexStarts.length === 0 ? 0 : indexStarts[indexStarts.length - 1] + indexCounts[indexCounts.length - 2]);
  795. }
  796. }
  797. // Apply geometry
  798. geometry.setAllVerticesData(vertexData, false);
  799. newMesh.flipFaces(true);
  800. newMesh.computeWorldMatrix(true);
  801. // Apply submeshes
  802. newMesh.subMeshes = [];
  803. var index = 0;
  804. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  805. var meshID = meshes[meshIndex];
  806. var mesh = gltfRuntime.meshes[meshID];
  807. if (!mesh) {
  808. continue;
  809. }
  810. for (var i = 0; i < mesh.primitives.length; i++) {
  811. if (mesh.primitives[i].mode !== 4) {
  812. }
  813. var subMesh = new BABYLON.SubMesh(index, verticesStarts[index], verticesCounts[index], indexStarts[index], indexCounts[index], newMesh, newMesh, true);
  814. index++;
  815. }
  816. }
  817. // Finish
  818. return newMesh;
  819. };
  820. /**
  821. * Configure node transformation from position, rotation and scaling
  822. */
  823. var configureNode = function (newNode, position, rotation, scaling) {
  824. if (newNode.position) {
  825. newNode.position = position;
  826. }
  827. if (newNode.rotationQuaternion || newNode.rotation) {
  828. newNode.rotationQuaternion = rotation;
  829. }
  830. if (newNode.scaling) {
  831. newNode.scaling = scaling;
  832. }
  833. };
  834. /**
  835. * Configures node from transformation matrix
  836. */
  837. var configureNodeFromMatrix = function (newNode, node) {
  838. if (node.matrix) {
  839. var position = new BABYLON.Vector3(0, 0, 0);
  840. var rotation = new BABYLON.Quaternion();
  841. var scaling = new BABYLON.Vector3(0, 0, 0);
  842. var mat = BABYLON.Matrix.FromArray(node.matrix);
  843. mat.decompose(scaling, rotation, position);
  844. // Y is Up
  845. if (GLTFFileLoader.MakeYUP) {
  846. rotation = rotation.multiply(new BABYLON.Quaternion(-0.707107, 0, 0, 0.707107));
  847. }
  848. configureNode(newNode, position, rotation, scaling);
  849. if (newNode instanceof BABYLON.TargetCamera) {
  850. newNode.setTarget(BABYLON.Vector3.Zero());
  851. }
  852. }
  853. else {
  854. configureNode(newNode, BABYLON.Vector3.FromArray(node.translation), BABYLON.Quaternion.FromArray(node.rotation), BABYLON.Vector3.FromArray(node.scale));
  855. }
  856. };
  857. /**
  858. * Imports a node
  859. */
  860. var importNode = function (gltfRuntime, node, id) {
  861. var lastNode = null;
  862. if (gltfRuntime.importOnlyMeshes && (node.skin || node.meshes)) {
  863. if (gltfRuntime.importMeshesNames.length > 0 && gltfRuntime.importMeshesNames.indexOf(node.name) === -1) {
  864. return null;
  865. }
  866. }
  867. // Meshes
  868. if (node.skin) {
  869. if (node.meshes) {
  870. var skin = gltfRuntime.skins[node.skin];
  871. var newMesh = importMesh(gltfRuntime, node, node.meshes, id, node.babylonNode);
  872. newMesh.skeleton = gltfRuntime.scene.getLastSkeletonByID(node.skin);
  873. if (newMesh.skeleton === null) {
  874. newMesh.skeleton = importSkeleton(gltfRuntime, skin, newMesh, skin.babylonSkeleton);
  875. if (!skin.babylonSkeleton) {
  876. skin.babylonSkeleton = newMesh.skeleton;
  877. }
  878. }
  879. if (newMesh.skeleton !== null) {
  880. newMesh.useBones = true;
  881. }
  882. lastNode = newMesh;
  883. }
  884. }
  885. else if (node.meshes) {
  886. /**
  887. * Improve meshes property
  888. */
  889. var newMesh = importMesh(gltfRuntime, node, node.mesh ? [node.mesh] : node.meshes, id, node.babylonNode);
  890. lastNode = newMesh;
  891. }
  892. else if (node.light && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  893. var light = gltfRuntime.lights[node.light];
  894. if (light) {
  895. if (light.type === "ambient") {
  896. var ambienLight = light[light.type];
  897. var hemiLight = new BABYLON.HemisphericLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  898. hemiLight.name = node.name;
  899. if (ambienLight.color) {
  900. hemiLight.diffuse = BABYLON.Color3.FromArray(ambienLight.color);
  901. }
  902. lastNode = hemiLight;
  903. }
  904. else if (light.type === "directional") {
  905. var directionalLight = light[light.type];
  906. var dirLight = new BABYLON.DirectionalLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  907. dirLight.name = node.name;
  908. if (directionalLight.color) {
  909. dirLight.diffuse = BABYLON.Color3.FromArray(directionalLight.color);
  910. }
  911. lastNode = dirLight;
  912. }
  913. else if (light.type === "point") {
  914. var pointLight = light[light.type];
  915. var ptLight = new BABYLON.PointLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  916. ptLight.name = node.name;
  917. if (pointLight.color) {
  918. ptLight.diffuse = BABYLON.Color3.FromArray(pointLight.color);
  919. }
  920. lastNode = ptLight;
  921. }
  922. else if (light.type === "spot") {
  923. var spotLight = light[light.type];
  924. var spLight = new BABYLON.SpotLight(node.light, BABYLON.Vector3.Zero(), BABYLON.Vector3.Zero(), 0, 0, gltfRuntime.scene);
  925. spLight.name = node.name;
  926. if (spotLight.color) {
  927. spLight.diffuse = BABYLON.Color3.FromArray(spotLight.color);
  928. }
  929. if (spotLight.fallOfAngle) {
  930. spLight.angle = spotLight.fallOfAngle;
  931. }
  932. if (spotLight.fallOffExponent) {
  933. spLight.exponent = spotLight.fallOffExponent;
  934. }
  935. lastNode = spLight;
  936. }
  937. }
  938. }
  939. else if (node.camera && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  940. var camera = gltfRuntime.cameras[node.camera];
  941. if (camera) {
  942. if (camera.type === "orthographic") {
  943. var orthographicCamera = camera[camera.type];
  944. var orthoCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  945. orthoCamera.name = node.name;
  946. orthoCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
  947. orthoCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  948. lastNode = orthoCamera;
  949. }
  950. else if (camera.type === "perspective") {
  951. var perspectiveCamera = camera[camera.type];
  952. var persCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  953. persCamera.name = node.name;
  954. persCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  955. if (!perspectiveCamera.aspectRatio) {
  956. perspectiveCamera.aspectRatio = gltfRuntime.scene.getEngine().getRenderWidth() / gltfRuntime.scene.getEngine().getRenderHeight();
  957. }
  958. if (perspectiveCamera.znear && perspectiveCamera.zfar) {
  959. persCamera.maxZ = perspectiveCamera.zfar;
  960. persCamera.minZ = perspectiveCamera.znear;
  961. }
  962. lastNode = persCamera;
  963. }
  964. }
  965. }
  966. // Empty node
  967. if (!node.jointName) {
  968. if (node.babylonNode) {
  969. return node.babylonNode;
  970. }
  971. else if (lastNode === null) {
  972. var dummy = new BABYLON.Mesh(node.name, gltfRuntime.scene);
  973. node.babylonNode = dummy;
  974. lastNode = dummy;
  975. }
  976. }
  977. if (lastNode !== null) {
  978. if (node.matrix) {
  979. configureNodeFromMatrix(lastNode, node);
  980. }
  981. else {
  982. configureNode(lastNode, BABYLON.Vector3.FromArray(node.translation), BABYLON.Quaternion.RotationAxis(BABYLON.Vector3.FromArray(node.rotation).normalize(), node.rotation[3]), BABYLON.Vector3.FromArray(node.scale));
  983. }
  984. lastNode.updateCache(true);
  985. node.babylonNode = lastNode;
  986. }
  987. return lastNode;
  988. };
  989. /**
  990. * Traverses nodes and creates them
  991. */
  992. var traverseNodes = function (gltfRuntime, id, parent, meshIncluded) {
  993. var node = gltfRuntime.nodes[id];
  994. var newNode = null;
  995. if (gltfRuntime.importOnlyMeshes && !meshIncluded) {
  996. if (gltfRuntime.importMeshesNames.indexOf(node.name) !== -1 || gltfRuntime.importMeshesNames.length === 0) {
  997. meshIncluded = true;
  998. }
  999. else {
  1000. meshIncluded = false;
  1001. }
  1002. }
  1003. else {
  1004. meshIncluded = true;
  1005. }
  1006. if (!node.jointName && meshIncluded) {
  1007. newNode = importNode(gltfRuntime, node, id);
  1008. if (newNode !== null) {
  1009. newNode.id = id;
  1010. newNode.parent = parent;
  1011. }
  1012. }
  1013. for (var i = 0; i < node.children.length; i++) {
  1014. traverseNodes(gltfRuntime, node.children[i], newNode, meshIncluded);
  1015. }
  1016. };
  1017. /**
  1018. * Buffers loaded, create nodes
  1019. */
  1020. var onBuffersLoaded = function (gltfRuntime) {
  1021. // Nodes
  1022. var currentScene = gltfRuntime.currentScene;
  1023. for (var i = 0; i < currentScene.nodes.length; i++) {
  1024. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1025. }
  1026. // Set animations
  1027. loadAnimations(gltfRuntime);
  1028. for (var i = 0; i < gltfRuntime.scene.skeletons.length; i++) {
  1029. var skeleton = gltfRuntime.scene.skeletons[i];
  1030. gltfRuntime.scene.beginAnimation(skeleton, 0, Number.MAX_VALUE, true, 1.0);
  1031. }
  1032. };
  1033. /**
  1034. * On a buffer is loaded
  1035. */
  1036. var onLoadBuffer = function (gltfRuntime, buf) {
  1037. return function (data) {
  1038. gltfRuntime.loadedBuffers++;
  1039. if (!(data instanceof ArrayBuffer)) {
  1040. BABYLON.Tools.Error("Buffer named " + buf + " is not an array buffer");
  1041. }
  1042. else if (data.byteLength != gltfRuntime.buffers[buf].byteLength) {
  1043. BABYLON.Tools.Error("Buffer named " + buf + " is length " + data.byteLength + ". Expected: " + gltfRuntime.buffers[buf].byteLength); // Improve error message
  1044. }
  1045. gltfRuntime.arrayBuffers[buf] = data;
  1046. if (gltfRuntime.loadedBuffers === gltfRuntime.buffersCount) {
  1047. onBuffersLoaded(gltfRuntime);
  1048. }
  1049. };
  1050. };
  1051. /**
  1052. * Error when loaded buffer
  1053. */
  1054. var onLoadBufferError = function (gltfRuntime, buf) {
  1055. return function () {
  1056. BABYLON.Tools.Error("Error when loading buffer named " + buf + " located at " + gltfRuntime.buffers[buf].uri);
  1057. };
  1058. };
  1059. /**
  1060. * Decode array buffer from base64
  1061. */
  1062. var decodeArrayBuffer = function (base64) {
  1063. var decodedString = atob(base64);
  1064. var bufferLength = decodedString.length;
  1065. var arraybuffer = new Uint8Array(new ArrayBuffer(bufferLength));
  1066. for (var i = 0; i < bufferLength; i++) {
  1067. arraybuffer[i] = decodedString.charCodeAt(i);
  1068. }
  1069. return arraybuffer.buffer;
  1070. };
  1071. /**
  1072. * Loads buffers (geometries)
  1073. */
  1074. var loadBuffers = function (gltfRuntime) {
  1075. for (var buf in gltfRuntime.buffers) {
  1076. var buffer = gltfRuntime.buffers[buf];
  1077. if (buffer) {
  1078. if (isBase64(buffer.uri)) {
  1079. var arrayBuffer = decodeArrayBuffer(buffer.uri.split(",")[1]);
  1080. onLoadBuffer(gltfRuntime, buf)(arrayBuffer);
  1081. }
  1082. else {
  1083. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, onLoadBuffer(gltfRuntime, buf), null, null, true, onLoadBufferError(gltfRuntime, buf));
  1084. }
  1085. }
  1086. else {
  1087. BABYLON.Tools.Error("No buffer named : " + buf);
  1088. }
  1089. }
  1090. };
  1091. /**
  1092. * onBind shaderrs callback to set uniforms and matrices
  1093. */
  1094. var onBindShaderMaterial = function (mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material) {
  1095. for (var unif in unTreatedUniforms) {
  1096. var uniform = unTreatedUniforms[unif];
  1097. var type = uniform.type;
  1098. if (type === EParameterType.FLOAT_MAT2 || type === EParameterType.FLOAT_MAT3 || type === EParameterType.FLOAT_MAT4) {
  1099. if (uniform.semantic && !uniform.source && !uniform.node) {
  1100. setMatrix(gltfRuntime.scene, mesh, uniform, unif, shaderMaterial.getEffect());
  1101. }
  1102. else if (uniform.semantic && (uniform.source || uniform.node)) {
  1103. var source = gltfRuntime.scene.getNodeByName(uniform.source || uniform.node);
  1104. if (source === null) {
  1105. source = gltfRuntime.scene.getNodeByID(uniform.source || uniform.node);
  1106. }
  1107. if (source === null) {
  1108. continue;
  1109. }
  1110. setMatrix(gltfRuntime.scene, source, uniform, unif, shaderMaterial.getEffect());
  1111. }
  1112. }
  1113. else {
  1114. var value = material.values[technique.uniforms[unif]];
  1115. if (!value) {
  1116. continue;
  1117. }
  1118. if (type === EParameterType.SAMPLER_2D) {
  1119. var texture = gltfRuntime.textures[value].babylonTexture;
  1120. if (texture === null) {
  1121. continue;
  1122. }
  1123. shaderMaterial.getEffect().setTexture(unif, texture);
  1124. }
  1125. else {
  1126. setUniform(shaderMaterial.getEffect(), unif, value, type);
  1127. }
  1128. }
  1129. }
  1130. };
  1131. /**
  1132. * Prepare uniforms to send the only one time
  1133. * Loads the appropriate textures
  1134. */
  1135. var prepareShaderMaterialUniforms = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms) {
  1136. var materialValues = material.values;
  1137. var techniqueUniforms = technique.uniforms;
  1138. /**
  1139. * Prepare values here (not matrices)
  1140. */
  1141. for (var unif in unTreatedUniforms) {
  1142. var uniform = unTreatedUniforms[unif];
  1143. var type = uniform.type;
  1144. var value = materialValues[techniqueUniforms[unif]] || uniform.value;
  1145. if (!value) {
  1146. continue;
  1147. }
  1148. // Texture (sampler2D)
  1149. if (type === EParameterType.SAMPLER_2D) {
  1150. var texture = gltfRuntime.textures[value];
  1151. var sampler = gltfRuntime.samplers[texture.sampler];
  1152. if (!texture || !texture.source) {
  1153. continue;
  1154. }
  1155. var source = gltfRuntime.images[texture.source];
  1156. var newTexture = null;
  1157. var createMipMaps = (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  1158. (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  1159. (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  1160. (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  1161. var samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  1162. if (isBase64(source.uri)) {
  1163. newTexture = new BABYLON.Texture(source.uri, gltfRuntime.scene, !createMipMaps, true, samplingMode, null, null, source.uri, true);
  1164. }
  1165. else {
  1166. newTexture = new BABYLON.Texture(gltfRuntime.rootUrl + source.uri, gltfRuntime.scene, !createMipMaps, true, samplingMode);
  1167. }
  1168. newTexture.wrapU = getWrapMode(sampler.wrapS);
  1169. newTexture.wrapV = getWrapMode(sampler.wrapT);
  1170. newTexture.name = value;
  1171. texture.babylonTexture = newTexture;
  1172. if (uniform.value) {
  1173. // Static uniform
  1174. shaderMaterial.setTexture(unif, newTexture);
  1175. delete unTreatedUniforms[unif];
  1176. }
  1177. }
  1178. else {
  1179. if (uniform.value && setUniform(shaderMaterial, unif, value, type)) {
  1180. // Static uniform
  1181. delete unTreatedUniforms[unif];
  1182. }
  1183. }
  1184. }
  1185. };
  1186. /**
  1187. * Shader compilation failed
  1188. */
  1189. var onShaderCompileError = function (program, shaderMaterial) {
  1190. return function (effect, error) {
  1191. BABYLON.Tools.Error("Cannot compile program named " + program.name + ". Error: " + error + ". Default material will be applied");
  1192. shaderMaterial.dispose(true);
  1193. };
  1194. };
  1195. /**
  1196. * Shader compilation success
  1197. */
  1198. var onShaderCompileSuccess = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms) {
  1199. return function (_) {
  1200. prepareShaderMaterialUniforms(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
  1201. shaderMaterial.onBind = function (mat, mesh) {
  1202. onBindShaderMaterial(mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material);
  1203. };
  1204. };
  1205. };
  1206. /**
  1207. * Returns the appropriate uniform if already handled by babylon
  1208. */
  1209. var parseShaderUniforms = function (tokenizer, technique, unTreatedUniforms) {
  1210. for (var unif in technique.uniforms) {
  1211. var uniform = technique.uniforms[unif];
  1212. var uniformParameter = technique.parameters[uniform];
  1213. if (tokenizer.currentIdentifier === unif) {
  1214. if (uniformParameter.semantic && !uniformParameter.source && !uniformParameter.node) {
  1215. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1216. if (transformIndex !== -1) {
  1217. delete unTreatedUniforms[unif];
  1218. return babylonTransforms[transformIndex];
  1219. }
  1220. }
  1221. }
  1222. }
  1223. return tokenizer.currentIdentifier;
  1224. };
  1225. /**
  1226. * All shaders loaded. Create materials one by one
  1227. */
  1228. var onShadersLoaded = function (gltfRuntime) {
  1229. // Create materials
  1230. for (var mat in gltfRuntime.materials) {
  1231. var material = gltfRuntime.materials[mat];
  1232. var technique = gltfRuntime.techniques[material.technique];
  1233. var program = gltfRuntime.programs[technique.program];
  1234. var states = technique.states;
  1235. var vertexShader = BABYLON.Effect.ShadersStore[program.vertexShader + "VertexShader"];
  1236. var pixelShader = BABYLON.Effect.ShadersStore[program.fragmentShader + "PixelShader"];
  1237. var newVertexShader = "";
  1238. var newPixelShader = "";
  1239. var vertexTokenizer = new Tokenizer(vertexShader);
  1240. var pixelTokenizer = new Tokenizer(pixelShader);
  1241. var unTreatedUniforms = {};
  1242. var uniforms = [];
  1243. var attributes = [];
  1244. var samplers = [];
  1245. // Fill uniform, sampler2D and attributes
  1246. for (var unif in technique.uniforms) {
  1247. var uniform = technique.uniforms[unif];
  1248. var uniformParameter = technique.parameters[uniform];
  1249. unTreatedUniforms[unif] = uniformParameter;
  1250. if (uniformParameter.semantic && !uniformParameter.node && !uniformParameter.source) {
  1251. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1252. if (transformIndex !== -1) {
  1253. uniforms.push(babylonTransforms[transformIndex]);
  1254. delete unTreatedUniforms[unif];
  1255. }
  1256. else {
  1257. uniforms.push(unif);
  1258. }
  1259. }
  1260. else if (uniformParameter.type === EParameterType.SAMPLER_2D) {
  1261. samplers.push(unif);
  1262. }
  1263. else {
  1264. uniforms.push(unif);
  1265. }
  1266. }
  1267. for (var attr in technique.attributes) {
  1268. var attribute = technique.attributes[attr];
  1269. var attributeParameter = technique.parameters[attribute];
  1270. if (attributeParameter.semantic) {
  1271. attributes.push(getAttribute(attributeParameter));
  1272. }
  1273. }
  1274. // Configure vertex shader
  1275. while (!vertexTokenizer.isEnd() && vertexTokenizer.getNextToken()) {
  1276. var tokenType = vertexTokenizer.currentToken;
  1277. if (tokenType !== ETokenType.IDENTIFIER) {
  1278. newVertexShader += vertexTokenizer.currentString;
  1279. continue;
  1280. }
  1281. var foundAttribute = false;
  1282. for (var attr in technique.attributes) {
  1283. var attribute = technique.attributes[attr];
  1284. var attributeParameter = technique.parameters[attribute];
  1285. if (vertexTokenizer.currentIdentifier === attr && attributeParameter.semantic) {
  1286. newVertexShader += getAttribute(attributeParameter);
  1287. foundAttribute = true;
  1288. break;
  1289. }
  1290. }
  1291. if (foundAttribute) {
  1292. continue;
  1293. }
  1294. newVertexShader += parseShaderUniforms(vertexTokenizer, technique, unTreatedUniforms);
  1295. }
  1296. // Configure pixel shader
  1297. while (!pixelTokenizer.isEnd() && pixelTokenizer.getNextToken()) {
  1298. var tokenType = pixelTokenizer.currentToken;
  1299. if (tokenType !== ETokenType.IDENTIFIER) {
  1300. newPixelShader += pixelTokenizer.currentString;
  1301. continue;
  1302. }
  1303. newPixelShader += parseShaderUniforms(pixelTokenizer, technique, unTreatedUniforms);
  1304. }
  1305. // Create shader material
  1306. var shaderPath = {
  1307. vertex: program.vertexShader + mat,
  1308. fragment: program.fragmentShader + mat
  1309. };
  1310. var options = {
  1311. attributes: attributes,
  1312. uniforms: uniforms,
  1313. samplers: samplers,
  1314. needAlphaBlending: states.functions && states.functions.blendEquationSeparate
  1315. };
  1316. BABYLON.Effect.ShadersStore[program.vertexShader + mat + "VertexShader"] = newVertexShader;
  1317. BABYLON.Effect.ShadersStore[program.fragmentShader + mat + "PixelShader"] = newPixelShader;
  1318. var shaderMaterial = new BABYLON.ShaderMaterial(material.name, gltfRuntime.scene, shaderPath, options);
  1319. shaderMaterial.id = mat;
  1320. shaderMaterial.onError = onShaderCompileError(program, shaderMaterial);
  1321. shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
  1322. if (states.functions) {
  1323. var functions = states.functions;
  1324. if (functions.cullFace && functions.cullFace[0] !== ECullingType.BACK) {
  1325. shaderMaterial.backFaceCulling = false;
  1326. }
  1327. var blendFunc = functions.blendFuncSeparate;
  1328. if (blendFunc) {
  1329. if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_ALPHA && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1330. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_COMBINE;
  1331. }
  1332. else if (blendFunc[0] === EBlendingFunction.ONE && blendFunc[1] === EBlendingFunction.ONE && blendFunc[2] === EBlendingFunction.ZERO && blendFunc[3] === EBlendingFunction.ONE) {
  1333. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ONEONE;
  1334. }
  1335. else if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE && blendFunc[2] === EBlendingFunction.ZERO && blendFunc[3] === EBlendingFunction.ONE) {
  1336. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ADD;
  1337. }
  1338. else if (blendFunc[0] === EBlendingFunction.ZERO && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1339. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_SUBTRACT;
  1340. }
  1341. else if (blendFunc[0] === EBlendingFunction.DST_COLOR && blendFunc[1] === EBlendingFunction.ZERO && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1342. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MULTIPLY;
  1343. }
  1344. else if (blendFunc[0] === EBlendingFunction.SRC_ALPHA && blendFunc[1] === EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === EBlendingFunction.ONE && blendFunc[3] === EBlendingFunction.ONE) {
  1345. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MAXIMIZED;
  1346. }
  1347. }
  1348. }
  1349. }
  1350. // Finish
  1351. loadBuffers(gltfRuntime);
  1352. };
  1353. /**
  1354. * Shader loaded
  1355. */
  1356. var onLoadShader = function (gltfRuntime, sha) {
  1357. return function (data) {
  1358. gltfRuntime.loadedShaders++;
  1359. BABYLON.Effect.ShadersStore[sha + (gltfRuntime.shaders[sha].type === EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = data;
  1360. if (gltfRuntime.loadedShaders === gltfRuntime.shaderscount) {
  1361. onShadersLoaded(gltfRuntime);
  1362. }
  1363. };
  1364. };
  1365. /**
  1366. * Error callback when loading a shader
  1367. */
  1368. var onLoadShaderError = function (gltfRuntime, sha) {
  1369. return function () {
  1370. BABYLON.Tools.Error("Error when loading shader program named " + sha + " located at " + gltfRuntime.shaders[sha].uri);
  1371. };
  1372. };
  1373. /**
  1374. * Load shaders
  1375. */
  1376. var load = function (gltfRuntime) {
  1377. // Begin with shaders
  1378. for (var sha in gltfRuntime.shaders) {
  1379. var shader = gltfRuntime.shaders[sha];
  1380. if (shader) {
  1381. if (isBase64(shader.uri)) {
  1382. var shaderString = atob(shader.uri.split(",")[1]);
  1383. onLoadShader(gltfRuntime, sha)(shaderString);
  1384. }
  1385. else {
  1386. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + shader.uri, onLoadShader(gltfRuntime, sha), null, null, false, onLoadShaderError(gltfRuntime, sha));
  1387. }
  1388. }
  1389. else {
  1390. BABYLON.Tools.Error("No shader file named " + shader.uri);
  1391. }
  1392. }
  1393. };
  1394. /**
  1395. * glTF File Loader Plugin
  1396. */
  1397. var GLTFFileLoader = (function () {
  1398. function GLTFFileLoader() {
  1399. /**
  1400. * Public members
  1401. */
  1402. this.extensions = ".gltf";
  1403. }
  1404. /**
  1405. * Import meshes
  1406. */
  1407. GLTFFileLoader.prototype.importMesh = function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  1408. var parsedData = JSON.parse(data);
  1409. var gltfRuntime = this._createGlTFRuntime(parsedData, scene, rootUrl);
  1410. gltfRuntime.importOnlyMeshes = true;
  1411. if (meshesNames === "") {
  1412. gltfRuntime.importMeshesNames = [];
  1413. }
  1414. else if (typeof meshesNames === "string") {
  1415. gltfRuntime.importMeshesNames = [meshesNames];
  1416. }
  1417. else if (meshesNames && !(meshesNames instanceof Array)) {
  1418. gltfRuntime.importMeshesNames = [meshesNames];
  1419. }
  1420. else {
  1421. gltfRuntime.importMeshesNames = [];
  1422. BABYLON.Tools.Warn("Argument meshesNames must be of type string or string[]");
  1423. }
  1424. // Create nodes
  1425. this._createNodes(gltfRuntime);
  1426. // Fill arrays of meshes and skeletons
  1427. for (var nde in gltfRuntime.nodes) {
  1428. var node = gltfRuntime.nodes[nde];
  1429. if (node.babylonNode instanceof BABYLON.AbstractMesh) {
  1430. meshes.push(node.babylonNode);
  1431. }
  1432. }
  1433. for (var skl in gltfRuntime.skins) {
  1434. var skin = gltfRuntime.skins[skl];
  1435. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  1436. skeletons.push(skin.babylonSkeleton);
  1437. }
  1438. }
  1439. // Load shaders and buffers to apply
  1440. load(gltfRuntime);
  1441. return true;
  1442. };
  1443. /**
  1444. * Load scene
  1445. */
  1446. GLTFFileLoader.prototype.load = function (scene, data, rootUrl) {
  1447. var parsedData = JSON.parse(data);
  1448. var gltfRuntime = this._createGlTFRuntime(parsedData, scene, rootUrl);
  1449. // Create nodes
  1450. this._createNodes(gltfRuntime);
  1451. // Load shaders and buffers to apply
  1452. load(gltfRuntime);
  1453. // Finish
  1454. return true;
  1455. };
  1456. // Creates nodes before loading buffers and shaders
  1457. GLTFFileLoader.prototype._createNodes = function (gltfRuntime) {
  1458. var currentScene = gltfRuntime.currentScene;
  1459. for (var i = 0; i < currentScene.nodes.length; i++) {
  1460. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1461. }
  1462. };
  1463. // Creates the gltfRuntime
  1464. GLTFFileLoader.prototype._createGlTFRuntime = function (parsedData, scene, rootUrl) {
  1465. var gltfRuntime = {
  1466. accessors: {},
  1467. buffers: {},
  1468. bufferViews: {},
  1469. meshes: {},
  1470. lights: {},
  1471. cameras: {},
  1472. nodes: {},
  1473. images: {},
  1474. textures: {},
  1475. shaders: {},
  1476. programs: {},
  1477. samplers: {},
  1478. techniques: {},
  1479. materials: {},
  1480. animations: {},
  1481. skins: {},
  1482. currentScene: {},
  1483. buffersCount: 0,
  1484. shaderscount: 0,
  1485. scene: scene,
  1486. dummyNodes: [],
  1487. loadedBuffers: 0,
  1488. loadedShaders: 0,
  1489. rootUrl: rootUrl,
  1490. importOnlyMeshes: false,
  1491. arrayBuffers: []
  1492. };
  1493. // Parse
  1494. if (parsedData.buffers) {
  1495. parseBuffers(parsedData.buffers, gltfRuntime);
  1496. }
  1497. if (parsedData.bufferViews) {
  1498. parseObject(parsedData.bufferViews, "bufferViews", gltfRuntime);
  1499. }
  1500. if (parsedData.accessors) {
  1501. parseObject(parsedData.accessors, "accessors", gltfRuntime);
  1502. }
  1503. if (parsedData.meshes) {
  1504. parseObject(parsedData.meshes, "meshes", gltfRuntime);
  1505. }
  1506. if (parsedData.lights) {
  1507. parseObject(parsedData.lights, "lights", gltfRuntime);
  1508. }
  1509. if (parsedData.cameras) {
  1510. parseObject(parsedData.cameras, "cameras", gltfRuntime);
  1511. }
  1512. if (parsedData.nodes) {
  1513. parseObject(parsedData.nodes, "nodes", gltfRuntime);
  1514. }
  1515. if (parsedData.images) {
  1516. parseObject(parsedData.images, "images", gltfRuntime);
  1517. }
  1518. if (parsedData.textures) {
  1519. parseObject(parsedData.textures, "textures", gltfRuntime);
  1520. }
  1521. if (parsedData.shaders) {
  1522. parseShaders(parsedData.shaders, gltfRuntime);
  1523. }
  1524. if (parsedData.programs) {
  1525. parseObject(parsedData.programs, "programs", gltfRuntime);
  1526. }
  1527. if (parsedData.samplers) {
  1528. parseObject(parsedData.samplers, "samplers", gltfRuntime);
  1529. }
  1530. if (parsedData.techniques) {
  1531. parseObject(parsedData.techniques, "techniques", gltfRuntime);
  1532. }
  1533. if (parsedData.materials) {
  1534. parseObject(parsedData.materials, "materials", gltfRuntime);
  1535. }
  1536. if (parsedData.animations) {
  1537. parseObject(parsedData.animations, "animations", gltfRuntime);
  1538. }
  1539. if (parsedData.skins) {
  1540. parseObject(parsedData.skins, "skins", gltfRuntime);
  1541. }
  1542. if (parsedData.scene && parsedData.scenes) {
  1543. gltfRuntime.currentScene = parsedData.scenes[parsedData.scene];
  1544. }
  1545. return gltfRuntime;
  1546. };
  1547. /**
  1548. * Private members
  1549. */
  1550. // None
  1551. /**
  1552. * Static members
  1553. */
  1554. GLTFFileLoader.MakeYUP = false;
  1555. return GLTFFileLoader;
  1556. })();
  1557. BABYLON.GLTFFileLoader = GLTFFileLoader;
  1558. ;
  1559. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  1560. })(BABYLON || (BABYLON = {}));
  1561. //# sourceMappingURL=babylon.glTFFileLoader.js.map