babylon.glTFFileLoader.js 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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. })(BABYLON || (BABYLON = {}));
  91. var BABYLON;
  92. (function (BABYLON) {
  93. /**
  94. * Tokenizer. Used for shaders compatibility
  95. * Automatically map world, view, projection, worldViewProjection, attributes and so on
  96. */
  97. var ETokenType;
  98. (function (ETokenType) {
  99. ETokenType[ETokenType["IDENTIFIER"] = 1] = "IDENTIFIER";
  100. ETokenType[ETokenType["UNKNOWN"] = 2] = "UNKNOWN";
  101. ETokenType[ETokenType["END_OF_INPUT"] = 3] = "END_OF_INPUT";
  102. })(ETokenType || (ETokenType = {}));
  103. var Tokenizer = (function () {
  104. function Tokenizer(toParse) {
  105. this._pos = 0;
  106. this.isLetterOrDigitPattern = /^[a-zA-Z0-9]+$/;
  107. this._toParse = toParse;
  108. this._maxPos = toParse.length;
  109. }
  110. Tokenizer.prototype.getNextToken = function () {
  111. if (this.isEnd())
  112. return ETokenType.END_OF_INPUT;
  113. this.currentString = this.read();
  114. this.currentToken = ETokenType.UNKNOWN;
  115. if (this.currentString === "_" || this.isLetterOrDigitPattern.test(this.currentString)) {
  116. this.currentToken = ETokenType.IDENTIFIER;
  117. this.currentIdentifier = this.currentString;
  118. while (!this.isEnd() && (this.isLetterOrDigitPattern.test(this.currentString = this.peek()) || this.currentString === "_")) {
  119. this.currentIdentifier += this.currentString;
  120. this.forward();
  121. }
  122. }
  123. return this.currentToken;
  124. };
  125. Tokenizer.prototype.peek = function () {
  126. return this._toParse[this._pos];
  127. };
  128. Tokenizer.prototype.read = function () {
  129. return this._toParse[this._pos++];
  130. };
  131. Tokenizer.prototype.forward = function () {
  132. this._pos++;
  133. };
  134. Tokenizer.prototype.isEnd = function () {
  135. return this._pos >= this._maxPos;
  136. };
  137. return Tokenizer;
  138. }());
  139. /**
  140. * Values
  141. */
  142. var glTFTransforms = ["MODEL", "VIEW", "PROJECTION", "MODELVIEW", "MODELVIEWPROJECTION", "JOINTMATRIX"];
  143. var babylonTransforms = ["world", "view", "projection", "worldView", "worldViewProjection", "mBones"];
  144. var glTFAnimationPaths = ["translation", "rotation", "scale"];
  145. var babylonAnimationPaths = ["position", "rotationQuaternion", "scaling"];
  146. /**
  147. * Parse
  148. */
  149. var parseBuffers = function (parsedBuffers, gltfRuntime) {
  150. for (var buf in parsedBuffers) {
  151. var parsedBuffer = parsedBuffers[buf];
  152. gltfRuntime.buffers[buf] = parsedBuffer;
  153. gltfRuntime.buffersCount++;
  154. }
  155. };
  156. var parseShaders = function (parsedShaders, gltfRuntime) {
  157. for (var sha in parsedShaders) {
  158. var parsedShader = parsedShaders[sha];
  159. gltfRuntime.shaders[sha] = parsedShader;
  160. gltfRuntime.shaderscount++;
  161. }
  162. };
  163. var parseObject = function (parsedObjects, runtimeProperty, gltfRuntime) {
  164. for (var object in parsedObjects) {
  165. var parsedObject = parsedObjects[object];
  166. gltfRuntime[runtimeProperty][object] = parsedObject;
  167. }
  168. };
  169. /**
  170. * Utils
  171. */
  172. var normalizeUVs = function (buffer) {
  173. if (!buffer) {
  174. return;
  175. }
  176. for (var i = 0; i < buffer.length / 2; i++) {
  177. buffer[i * 2 + 1] = 1.0 - buffer[i * 2 + 1];
  178. }
  179. };
  180. var replaceInString = function (str, searchValue, replaceValue) {
  181. while (str.indexOf(searchValue) !== -1) {
  182. str = str.replace(searchValue, replaceValue);
  183. }
  184. return str;
  185. };
  186. var getAttribute = function (attributeParameter) {
  187. if (attributeParameter.semantic === "NORMAL") {
  188. return "normal";
  189. }
  190. else if (attributeParameter.semantic === "POSITION") {
  191. return "position";
  192. }
  193. else if (attributeParameter.semantic === "JOINT") {
  194. return "matricesIndices";
  195. }
  196. else if (attributeParameter.semantic === "WEIGHT") {
  197. return "matricesWeights";
  198. }
  199. else if (attributeParameter.semantic === "COLOR") {
  200. return "color";
  201. }
  202. else if (attributeParameter.semantic.indexOf("TEXCOORD_") !== -1) {
  203. var channel = Number(attributeParameter.semantic.split("_")[1]);
  204. return "uv" + (channel === 0 ? "" : channel + 1);
  205. }
  206. };
  207. /**
  208. * Returns the animation path (glTF -> Babylon)
  209. */
  210. var getAnimationPath = function (path) {
  211. var index = glTFAnimationPaths.indexOf(path);
  212. if (index !== -1) {
  213. return babylonAnimationPaths[index];
  214. }
  215. return path;
  216. };
  217. /**
  218. * Loads and creates animations
  219. */
  220. var loadAnimations = function (gltfRuntime) {
  221. for (var anim in gltfRuntime.animations) {
  222. var animation = gltfRuntime.animations[anim];
  223. var lastAnimation = null;
  224. for (var i = 0; i < animation.channels.length; i++) {
  225. // Get parameters and load buffers
  226. var channel = animation.channels[i];
  227. var sampler = animation.samplers[channel.sampler];
  228. if (!sampler) {
  229. continue;
  230. }
  231. var inputData = animation.parameters[sampler.input];
  232. var outputData = animation.parameters[sampler.output];
  233. var bufferInput = BABYLON.GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[inputData]);
  234. var bufferOutput = BABYLON.GLTFUtils.GetBufferFromAccessor(gltfRuntime, gltfRuntime.accessors[outputData]);
  235. var targetID = channel.target.id;
  236. var targetNode = gltfRuntime.scene.getNodeByID(targetID);
  237. if (targetNode === null) {
  238. BABYLON.Tools.Warn("Creating animation named " + anim + ". But cannot find node named " + targetID + " to attach to");
  239. continue;
  240. }
  241. var isBone = targetNode instanceof BABYLON.Bone;
  242. // Get target path (position, rotation or scaling)
  243. var targetPath = channel.target.path;
  244. var targetPathIndex = glTFAnimationPaths.indexOf(targetPath);
  245. if (targetPathIndex !== -1) {
  246. targetPath = babylonAnimationPaths[targetPathIndex];
  247. }
  248. // Determine animation type
  249. var animationType = BABYLON.Animation.ANIMATIONTYPE_MATRIX;
  250. if (!isBone) {
  251. if (targetPath === "rotationQuaternion") {
  252. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  253. targetNode.rotationQuaternion = new BABYLON.Quaternion();
  254. }
  255. else {
  256. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  257. }
  258. }
  259. // Create animation and key frames
  260. var babylonAnimation = null;
  261. var keys = [];
  262. var arrayOffset = 0;
  263. var modifyKey = false;
  264. if (isBone && lastAnimation && lastAnimation.getKeys().length === bufferInput.length) {
  265. babylonAnimation = lastAnimation;
  266. modifyKey = true;
  267. }
  268. if (!modifyKey) {
  269. babylonAnimation = new BABYLON.Animation(anim, isBone ? "_matrix" : targetPath, 1, animationType, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  270. }
  271. // For each frame
  272. for (var j = 0; j < bufferInput.length; j++) {
  273. var value = null;
  274. if (targetPath === "rotationQuaternion") {
  275. value = BABYLON.Quaternion.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2], bufferOutput[arrayOffset + 3]]);
  276. arrayOffset += 4;
  277. }
  278. else {
  279. value = BABYLON.Vector3.FromArray([bufferOutput[arrayOffset], bufferOutput[arrayOffset + 1], bufferOutput[arrayOffset + 2]]);
  280. arrayOffset += 3;
  281. }
  282. if (isBone) {
  283. var bone = targetNode;
  284. var translation = BABYLON.Vector3.Zero();
  285. var rotationQuaternion = new BABYLON.Quaternion();
  286. var scaling = BABYLON.Vector3.Zero();
  287. // Warning on decompose
  288. var mat = bone.getBaseMatrix();
  289. if (modifyKey) {
  290. mat = lastAnimation.getKeys()[j].value;
  291. }
  292. mat.decompose(scaling, rotationQuaternion, translation);
  293. if (targetPath === "position") {
  294. translation = value;
  295. }
  296. else if (targetPath === "rotationQuaternion") {
  297. rotationQuaternion = value;
  298. // Y is Up
  299. if (GLTFFileLoader.MakeYUP) {
  300. rotationQuaternion = rotationQuaternion.multiply(new BABYLON.Quaternion(-0.707107, 0, 0, 0.707107));
  301. }
  302. }
  303. else {
  304. scaling = value;
  305. }
  306. value = BABYLON.Matrix.Compose(scaling, rotationQuaternion, translation);
  307. }
  308. if (!modifyKey) {
  309. keys.push({
  310. frame: bufferInput[j],
  311. value: value
  312. });
  313. }
  314. else {
  315. lastAnimation.getKeys()[j].value = value;
  316. }
  317. }
  318. // Finish
  319. if (!modifyKey) {
  320. babylonAnimation.setKeys(keys);
  321. targetNode.animations.push(babylonAnimation);
  322. }
  323. lastAnimation = babylonAnimation;
  324. gltfRuntime.scene.stopAnimation(targetNode);
  325. gltfRuntime.scene.beginAnimation(targetNode, 0, bufferInput[bufferInput.length - 1], true, 1.0);
  326. }
  327. }
  328. };
  329. /**
  330. * Returns the bones transformation matrix
  331. */
  332. var configureBoneTransformation = function (node) {
  333. var mat = null;
  334. if (node.translation && node.rotation && node.scale) {
  335. var scale = BABYLON.Vector3.FromArray(node.scale);
  336. var rotation = BABYLON.Quaternion.FromArray(node.rotation);
  337. var position = BABYLON.Vector3.FromArray(node.translation);
  338. // Y is Up
  339. if (GLTFFileLoader.MakeYUP) {
  340. rotation = rotation.multiply(new BABYLON.Quaternion(-0.707107, 0, 0, 0.707107));
  341. }
  342. mat = BABYLON.Matrix.Compose(scale, rotation, position);
  343. }
  344. else {
  345. mat = BABYLON.Matrix.FromArray(node.matrix);
  346. }
  347. return mat;
  348. };
  349. /**
  350. * Returns the parent bone
  351. */
  352. var getParentBone = function (gltfRuntime, skins, jointName, newSkeleton) {
  353. // Try to find
  354. for (var i = 0; i < newSkeleton.bones.length; i++) {
  355. if (newSkeleton.bones[i].id === jointName) {
  356. return newSkeleton.bones[i];
  357. }
  358. }
  359. // Not found, search in gltf nodes
  360. var nodes = gltfRuntime.nodes;
  361. for (var nde in nodes) {
  362. var node = nodes[nde];
  363. if (!node.jointName) {
  364. continue;
  365. }
  366. var children = node.children;
  367. for (var i = 0; i < children.length; i++) {
  368. var child = gltfRuntime.nodes[children[i]];
  369. if (!child.jointName) {
  370. continue;
  371. }
  372. if (child.jointName === jointName) {
  373. var mat = configureBoneTransformation(node);
  374. var bone = new BABYLON.Bone(node.name, newSkeleton, getParentBone(gltfRuntime, skins, node.jointName, newSkeleton), mat);
  375. bone.id = nde;
  376. return bone;
  377. }
  378. }
  379. }
  380. return null;
  381. };
  382. /**
  383. * Returns the appropriate root node
  384. */
  385. var getNodeToRoot = function (nodesToRoot, id) {
  386. for (var i = 0; i < nodesToRoot.length; i++) {
  387. var nodeToRoot = nodesToRoot[i];
  388. for (var j = 0; j < nodeToRoot.node.children.length; j++) {
  389. var child = nodeToRoot.node.children[j];
  390. if (child === id) {
  391. return nodeToRoot.bone;
  392. }
  393. }
  394. }
  395. return null;
  396. };
  397. /**
  398. * Returns the node with the joint name
  399. */
  400. var getJointNode = function (gltfRuntime, jointName) {
  401. var nodes = gltfRuntime.nodes;
  402. var node = nodes[jointName];
  403. if (node) {
  404. return {
  405. node: node,
  406. id: jointName
  407. };
  408. }
  409. for (var nde in nodes) {
  410. node = nodes[nde];
  411. if (node.jointName === jointName) {
  412. return {
  413. node: node,
  414. id: nde
  415. };
  416. }
  417. }
  418. return null;
  419. };
  420. /**
  421. * Checks if a nodes is in joints
  422. */
  423. var nodeIsInJoints = function (skins, id) {
  424. for (var i = 0; i < skins.jointNames.length; i++) {
  425. if (skins.jointNames[i] === id) {
  426. return true;
  427. }
  428. }
  429. return false;
  430. };
  431. /**
  432. * Fills the nodes to root for bones and builds hierarchy
  433. */
  434. var getNodesToRoot = function (gltfRuntime, newSkeleton, skins, nodesToRoot) {
  435. // Creates nodes for root
  436. for (var nde in gltfRuntime.nodes) {
  437. var node = gltfRuntime.nodes[nde];
  438. var id = nde;
  439. if (!node.jointName || nodeIsInJoints(skins, node.jointName)) {
  440. continue;
  441. }
  442. // Create node to root bone
  443. var mat = configureBoneTransformation(node);
  444. var bone = new BABYLON.Bone(node.name, newSkeleton, null, mat);
  445. bone.id = id;
  446. nodesToRoot.push({ bone: bone, node: node, id: id });
  447. }
  448. // Parenting
  449. for (var i = 0; i < nodesToRoot.length; i++) {
  450. var nodeToRoot = nodesToRoot[i];
  451. var children = nodeToRoot.node.children;
  452. for (var j = 0; j < children.length; j++) {
  453. var child = null;
  454. for (var k = 0; k < nodesToRoot.length; k++) {
  455. if (nodesToRoot[k].id === children[j]) {
  456. child = nodesToRoot[k];
  457. break;
  458. }
  459. }
  460. if (child) {
  461. child.bone._parent = nodeToRoot.bone;
  462. nodeToRoot.bone.children.push(child.bone);
  463. }
  464. }
  465. }
  466. };
  467. var printMat = function (m) {
  468. console.log(m[0] + "\t" + m[1] + "\t" + m[2] + "\t" + m[3] + "\n" +
  469. m[4] + "\t" + m[5] + "\t" + m[6] + "\t" + m[7] + "\n" +
  470. m[8] + "\t" + m[9] + "\t" + m[10] + "\t" + m[11] + "\n" +
  471. m[12] + "\t" + m[13] + "\t" + m[14] + "\t" + m[15] + "\n");
  472. };
  473. /**
  474. * Imports a skeleton
  475. */
  476. var importSkeleton = function (gltfRuntime, skins, mesh, newSkeleton, id) {
  477. if (!newSkeleton) {
  478. newSkeleton = new BABYLON.Skeleton(skins.name, "", gltfRuntime.scene);
  479. }
  480. if (!skins.babylonSkeleton) {
  481. return newSkeleton;
  482. }
  483. // Matrices
  484. var accessor = gltfRuntime.accessors[skins.inverseBindMatrices];
  485. var buffer = BABYLON.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  486. var bindShapeMatrix = BABYLON.Matrix.FromArray(skins.bindShapeMatrix);
  487. // Find the root bones
  488. var nodesToRoot = [];
  489. var nodesToRootToAdd = [];
  490. getNodesToRoot(gltfRuntime, newSkeleton, skins, nodesToRoot);
  491. newSkeleton.bones = [];
  492. if (nodesToRoot.length === 0) {
  493. newSkeleton.needInitialSkinMatrix = true;
  494. }
  495. // Joints
  496. for (var i = 0; i < skins.jointNames.length; i++) {
  497. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  498. var node = jointNode.node;
  499. if (!node) {
  500. BABYLON.Tools.Warn("Joint named " + skins.jointNames[i] + " does not exist");
  501. continue;
  502. }
  503. var id = jointNode.id;
  504. // Optimize, if the bone already exists...
  505. var existingBone = gltfRuntime.scene.getBoneByID(id);
  506. if (existingBone) {
  507. newSkeleton.bones.push(existingBone);
  508. continue;
  509. }
  510. // Check if node already exists
  511. var foundBone = false;
  512. for (var j = 0; j < newSkeleton.bones.length; j++) {
  513. if (newSkeleton.bones[j].id === id) {
  514. foundBone = true;
  515. break;
  516. }
  517. }
  518. if (foundBone) {
  519. continue;
  520. }
  521. // Search for parent bone
  522. var parentBone = null;
  523. for (var j = 0; j < i; j++) {
  524. var joint = getJointNode(gltfRuntime, skins.jointNames[j]).node;
  525. if (!joint) {
  526. BABYLON.Tools.Warn("Joint named " + skins.jointNames[j] + " does not exist when looking for parent");
  527. continue;
  528. }
  529. var children = joint.children;
  530. foundBone = false;
  531. for (var k = 0; k < children.length; k++) {
  532. if (children[k] === id) {
  533. parentBone = getParentBone(gltfRuntime, skins, skins.jointNames[j], newSkeleton);
  534. foundBone = true;
  535. break;
  536. }
  537. }
  538. if (foundBone) {
  539. break;
  540. }
  541. }
  542. // Create bone
  543. var mat = configureBoneTransformation(node);
  544. if (!parentBone && nodesToRoot.length > 0) {
  545. parentBone = getNodeToRoot(nodesToRoot, id);
  546. if (parentBone) {
  547. if (nodesToRootToAdd.indexOf(parentBone) === -1) {
  548. nodesToRootToAdd.push(parentBone);
  549. }
  550. }
  551. }
  552. if (!parentBone && nodesToRoot.length === 0) {
  553. var inverseBindMatrix = BABYLON.Matrix.FromArray(buffer, i * 16);
  554. var invertMesh = BABYLON.Matrix.Invert(mesh.getWorldMatrix());
  555. mat = mat.multiply(mesh.getWorldMatrix());
  556. }
  557. var bone = new BABYLON.Bone(node.name, newSkeleton, parentBone, mat);
  558. bone.id = id;
  559. }
  560. // Polish
  561. var bones = newSkeleton.bones;
  562. newSkeleton.bones = [];
  563. for (var i = 0; i < skins.jointNames.length; i++) {
  564. var jointNode = getJointNode(gltfRuntime, skins.jointNames[i]);
  565. if (!jointNode) {
  566. continue;
  567. }
  568. for (var j = 0; j < bones.length; j++) {
  569. if (bones[j].id === jointNode.id) {
  570. newSkeleton.bones.push(bones[j]);
  571. break;
  572. }
  573. }
  574. }
  575. // Finish
  576. newSkeleton.prepare();
  577. for (var i = 0; i < nodesToRootToAdd.length; i++) {
  578. newSkeleton.bones.push(nodesToRootToAdd[i]);
  579. }
  580. return newSkeleton;
  581. };
  582. /**
  583. * Imports a mesh and its geometries
  584. */
  585. var importMesh = function (gltfRuntime, node, meshes, id, newMesh) {
  586. if (!newMesh) {
  587. newMesh = new BABYLON.Mesh(node.name, gltfRuntime.scene);
  588. newMesh.id = id;
  589. }
  590. if (!node.babylonNode) {
  591. return newMesh;
  592. }
  593. var multiMat = new BABYLON.MultiMaterial("multimat" + id, gltfRuntime.scene);
  594. newMesh.material = multiMat;
  595. var vertexData = new BABYLON.VertexData();
  596. var geometry = new BABYLON.Geometry(id, gltfRuntime.scene, vertexData, false, newMesh);
  597. var verticesStarts = [];
  598. var verticesCounts = [];
  599. var indexStarts = [];
  600. var indexCounts = [];
  601. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  602. var meshID = meshes[meshIndex];
  603. var mesh = gltfRuntime.meshes[meshID];
  604. if (!mesh) {
  605. continue;
  606. }
  607. // Positions, normals and UVs
  608. for (var i = 0; i < mesh.primitives.length; i++) {
  609. // Temporary vertex data
  610. var tempVertexData = new BABYLON.VertexData();
  611. var primitive = mesh.primitives[i];
  612. if (primitive.mode !== 4) {
  613. }
  614. var attributes = primitive.attributes;
  615. var accessor = null;
  616. var buffer = null;
  617. // Set positions, normal and uvs
  618. for (var semantic in attributes) {
  619. // Link accessor and buffer view
  620. accessor = gltfRuntime.accessors[attributes[semantic]];
  621. buffer = BABYLON.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  622. if (semantic === "NORMAL") {
  623. tempVertexData.normals = new Float32Array(buffer.length);
  624. tempVertexData.normals.set(buffer);
  625. }
  626. else if (semantic === "POSITION") {
  627. if (GLTFFileLoader.HomogeneousCoordinates) {
  628. tempVertexData.positions = new Float32Array(buffer.length - buffer.length / 4);
  629. for (var j = 0; j < buffer.length; j += 4) {
  630. tempVertexData.positions[j] = buffer[j];
  631. tempVertexData.positions[j + 1] = buffer[j + 1];
  632. tempVertexData.positions[j + 2] = buffer[j + 2];
  633. }
  634. }
  635. else {
  636. tempVertexData.positions = new Float32Array(buffer.length);
  637. tempVertexData.positions.set(buffer);
  638. }
  639. verticesCounts.push(tempVertexData.positions.length);
  640. }
  641. else if (semantic.indexOf("TEXCOORD_") !== -1) {
  642. var channel = Number(semantic.split("_")[1]);
  643. var uvKind = BABYLON.VertexBuffer.UVKind + (channel === 0 ? "" : (channel + 1));
  644. var uvs = new Float32Array(buffer.length);
  645. uvs.set(buffer);
  646. normalizeUVs(uvs);
  647. tempVertexData.set(uvs, uvKind);
  648. }
  649. else if (semantic === "JOINT") {
  650. tempVertexData.matricesIndices = new Float32Array(buffer.length);
  651. tempVertexData.matricesIndices.set(buffer);
  652. }
  653. else if (semantic === "WEIGHT") {
  654. tempVertexData.matricesWeights = new Float32Array(buffer.length);
  655. tempVertexData.matricesWeights.set(buffer);
  656. }
  657. else if (semantic === "COLOR") {
  658. tempVertexData.colors = new Float32Array(buffer.length);
  659. tempVertexData.colors.set(buffer);
  660. }
  661. }
  662. // Indices
  663. accessor = gltfRuntime.accessors[primitive.indices];
  664. buffer = BABYLON.GLTFUtils.GetBufferFromAccessor(gltfRuntime, accessor);
  665. tempVertexData.indices = new Int32Array(buffer.length);
  666. tempVertexData.indices.set(buffer);
  667. indexCounts.push(tempVertexData.indices.length);
  668. vertexData.merge(tempVertexData);
  669. tempVertexData = undefined;
  670. // Sub material
  671. var material = gltfRuntime.scene.getMaterialByID(primitive.material);
  672. multiMat.subMaterials.push(material === null ? gltfRuntime.scene.defaultMaterial : material);
  673. // Update vertices start and index start
  674. verticesStarts.push(verticesStarts.length === 0 ? 0 : verticesStarts[verticesStarts.length - 1] + verticesCounts[verticesCounts.length - 2]);
  675. indexStarts.push(indexStarts.length === 0 ? 0 : indexStarts[indexStarts.length - 1] + indexCounts[indexCounts.length - 2]);
  676. }
  677. }
  678. // Apply geometry
  679. geometry.setAllVerticesData(vertexData, false);
  680. newMesh.flipFaces(true);
  681. newMesh.computeWorldMatrix(true);
  682. // Apply submeshes
  683. newMesh.subMeshes = [];
  684. var index = 0;
  685. for (var meshIndex = 0; meshIndex < meshes.length; meshIndex++) {
  686. var meshID = meshes[meshIndex];
  687. var mesh = gltfRuntime.meshes[meshID];
  688. if (!mesh) {
  689. continue;
  690. }
  691. for (var i = 0; i < mesh.primitives.length; i++) {
  692. if (mesh.primitives[i].mode !== 4) {
  693. }
  694. var subMesh = new BABYLON.SubMesh(index, verticesStarts[index], verticesCounts[index], indexStarts[index], indexCounts[index], newMesh, newMesh, true);
  695. index++;
  696. }
  697. }
  698. // Finish
  699. return newMesh;
  700. };
  701. /**
  702. * Configure node transformation from position, rotation and scaling
  703. */
  704. var configureNode = function (newNode, position, rotation, scaling) {
  705. if (newNode.position) {
  706. newNode.position = position;
  707. }
  708. if (newNode.rotationQuaternion || newNode.rotation) {
  709. newNode.rotationQuaternion = rotation;
  710. }
  711. if (newNode.scaling) {
  712. newNode.scaling = scaling;
  713. }
  714. };
  715. /**
  716. * Configures node from transformation matrix
  717. */
  718. var configureNodeFromMatrix = function (newNode, node) {
  719. if (node.matrix) {
  720. var position = new BABYLON.Vector3(0, 0, 0);
  721. var rotation = new BABYLON.Quaternion();
  722. var scaling = new BABYLON.Vector3(0, 0, 0);
  723. var mat = BABYLON.Matrix.FromArray(node.matrix);
  724. mat.decompose(scaling, rotation, position);
  725. // Y is Up
  726. if (GLTFFileLoader.MakeYUP) {
  727. rotation = rotation.multiply(new BABYLON.Quaternion(-0.707107, 0, 0, 0.707107));
  728. }
  729. configureNode(newNode, position, rotation, scaling);
  730. if (newNode instanceof BABYLON.TargetCamera) {
  731. newNode.setTarget(BABYLON.Vector3.Zero());
  732. }
  733. }
  734. else {
  735. configureNode(newNode, BABYLON.Vector3.FromArray(node.translation), BABYLON.Quaternion.FromArray(node.rotation), BABYLON.Vector3.FromArray(node.scale));
  736. }
  737. };
  738. /**
  739. * Imports a node
  740. */
  741. var importNode = function (gltfRuntime, node, id) {
  742. var lastNode = null;
  743. if (gltfRuntime.importOnlyMeshes && (node.skin || node.meshes)) {
  744. if (gltfRuntime.importMeshesNames.length > 0 && gltfRuntime.importMeshesNames.indexOf(node.name) === -1) {
  745. return null;
  746. }
  747. }
  748. // Meshes
  749. if (node.skin) {
  750. if (node.meshes) {
  751. var skin = gltfRuntime.skins[node.skin];
  752. var newMesh = importMesh(gltfRuntime, node, node.meshes, id, node.babylonNode);
  753. newMesh.skeleton = gltfRuntime.scene.getLastSkeletonByID(node.skin);
  754. if (newMesh.skeleton === null) {
  755. newMesh.skeleton = importSkeleton(gltfRuntime, skin, newMesh, skin.babylonSkeleton, node.skin);
  756. if (!skin.babylonSkeleton) {
  757. skin.babylonSkeleton = newMesh.skeleton;
  758. }
  759. }
  760. if (newMesh.skeleton !== null) {
  761. newMesh.useBones = true;
  762. }
  763. lastNode = newMesh;
  764. }
  765. }
  766. else if (node.meshes) {
  767. /**
  768. * Improve meshes property
  769. */
  770. var newMesh = importMesh(gltfRuntime, node, node.mesh ? [node.mesh] : node.meshes, id, node.babylonNode);
  771. lastNode = newMesh;
  772. }
  773. else if (node.light && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  774. var light = gltfRuntime.lights[node.light];
  775. if (light) {
  776. if (light.type === "ambient") {
  777. var ambienLight = light[light.type];
  778. var hemiLight = new BABYLON.HemisphericLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  779. hemiLight.name = node.name;
  780. if (ambienLight.color) {
  781. hemiLight.diffuse = BABYLON.Color3.FromArray(ambienLight.color);
  782. }
  783. lastNode = hemiLight;
  784. }
  785. else if (light.type === "directional") {
  786. var directionalLight = light[light.type];
  787. var dirLight = new BABYLON.DirectionalLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  788. dirLight.name = node.name;
  789. if (directionalLight.color) {
  790. dirLight.diffuse = BABYLON.Color3.FromArray(directionalLight.color);
  791. }
  792. lastNode = dirLight;
  793. }
  794. else if (light.type === "point") {
  795. var pointLight = light[light.type];
  796. var ptLight = new BABYLON.PointLight(node.light, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  797. ptLight.name = node.name;
  798. if (pointLight.color) {
  799. ptLight.diffuse = BABYLON.Color3.FromArray(pointLight.color);
  800. }
  801. lastNode = ptLight;
  802. }
  803. else if (light.type === "spot") {
  804. var spotLight = light[light.type];
  805. var spLight = new BABYLON.SpotLight(node.light, BABYLON.Vector3.Zero(), BABYLON.Vector3.Zero(), 0, 0, gltfRuntime.scene);
  806. spLight.name = node.name;
  807. if (spotLight.color) {
  808. spLight.diffuse = BABYLON.Color3.FromArray(spotLight.color);
  809. }
  810. if (spotLight.fallOfAngle) {
  811. spLight.angle = spotLight.fallOfAngle;
  812. }
  813. if (spotLight.fallOffExponent) {
  814. spLight.exponent = spotLight.fallOffExponent;
  815. }
  816. lastNode = spLight;
  817. }
  818. }
  819. }
  820. else if (node.camera && !node.babylonNode && !gltfRuntime.importOnlyMeshes) {
  821. var camera = gltfRuntime.cameras[node.camera];
  822. if (camera) {
  823. if (camera.type === "orthographic") {
  824. var orthographicCamera = camera[camera.type];
  825. var orthoCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  826. orthoCamera.name = node.name;
  827. orthoCamera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
  828. orthoCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  829. lastNode = orthoCamera;
  830. }
  831. else if (camera.type === "perspective") {
  832. var perspectiveCamera = camera[camera.type];
  833. var persCamera = new BABYLON.FreeCamera(node.camera, BABYLON.Vector3.Zero(), gltfRuntime.scene);
  834. persCamera.name = node.name;
  835. persCamera.attachControl(gltfRuntime.scene.getEngine().getRenderingCanvas());
  836. if (!perspectiveCamera.aspectRatio) {
  837. perspectiveCamera.aspectRatio = gltfRuntime.scene.getEngine().getRenderWidth() / gltfRuntime.scene.getEngine().getRenderHeight();
  838. }
  839. if (perspectiveCamera.znear && perspectiveCamera.zfar) {
  840. persCamera.maxZ = perspectiveCamera.zfar;
  841. persCamera.minZ = perspectiveCamera.znear;
  842. }
  843. lastNode = persCamera;
  844. }
  845. }
  846. }
  847. // Empty node
  848. if (!node.jointName) {
  849. if (node.babylonNode) {
  850. return node.babylonNode;
  851. }
  852. else if (lastNode === null) {
  853. var dummy = new BABYLON.Mesh(node.name, gltfRuntime.scene);
  854. node.babylonNode = dummy;
  855. lastNode = dummy;
  856. }
  857. }
  858. if (lastNode !== null) {
  859. if (node.matrix) {
  860. configureNodeFromMatrix(lastNode, node);
  861. }
  862. else {
  863. configureNode(lastNode, BABYLON.Vector3.FromArray(node.translation), BABYLON.Quaternion.RotationAxis(BABYLON.Vector3.FromArray(node.rotation).normalize(), node.rotation[3]), BABYLON.Vector3.FromArray(node.scale));
  864. }
  865. lastNode.updateCache(true);
  866. node.babylonNode = lastNode;
  867. }
  868. return lastNode;
  869. };
  870. /**
  871. * Traverses nodes and creates them
  872. */
  873. var traverseNodes = function (gltfRuntime, id, parent, meshIncluded) {
  874. var node = gltfRuntime.nodes[id];
  875. var newNode = null;
  876. if (gltfRuntime.importOnlyMeshes && !meshIncluded) {
  877. if (gltfRuntime.importMeshesNames.indexOf(node.name) !== -1 || gltfRuntime.importMeshesNames.length === 0) {
  878. meshIncluded = true;
  879. }
  880. else {
  881. meshIncluded = false;
  882. }
  883. }
  884. else {
  885. meshIncluded = true;
  886. }
  887. if (!node.jointName && meshIncluded) {
  888. newNode = importNode(gltfRuntime, node, id);
  889. if (newNode !== null) {
  890. newNode.id = id;
  891. newNode.parent = parent;
  892. }
  893. }
  894. for (var i = 0; i < node.children.length; i++) {
  895. traverseNodes(gltfRuntime, node.children[i], newNode, meshIncluded);
  896. }
  897. };
  898. /**
  899. * Buffers loaded, create nodes
  900. */
  901. var onBuffersLoaded = function (gltfRuntime) {
  902. // Nodes
  903. var currentScene = gltfRuntime.currentScene;
  904. for (var i = 0; i < currentScene.nodes.length; i++) {
  905. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  906. }
  907. // Set animations
  908. loadAnimations(gltfRuntime);
  909. for (var i = 0; i < gltfRuntime.scene.skeletons.length; i++) {
  910. var skeleton = gltfRuntime.scene.skeletons[i];
  911. gltfRuntime.scene.beginAnimation(skeleton, 0, Number.MAX_VALUE, true, 1.0);
  912. }
  913. };
  914. /**
  915. * Decode array buffer from base64
  916. */
  917. var decodeArrayBuffer = function (base64) {
  918. var decodedString = atob(base64);
  919. var bufferLength = decodedString.length;
  920. var arraybuffer = new Uint8Array(new ArrayBuffer(bufferLength));
  921. for (var i = 0; i < bufferLength; i++) {
  922. arraybuffer[i] = decodedString.charCodeAt(i);
  923. }
  924. return arraybuffer.buffer;
  925. };
  926. /**
  927. * onBind shaderrs callback to set uniforms and matrices
  928. */
  929. var onBindShaderMaterial = function (mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess) {
  930. for (var unif in unTreatedUniforms) {
  931. var uniform = unTreatedUniforms[unif];
  932. var type = uniform.type;
  933. if (type === BABYLON.EParameterType.FLOAT_MAT2 || type === BABYLON.EParameterType.FLOAT_MAT3 || type === BABYLON.EParameterType.FLOAT_MAT4) {
  934. if (uniform.semantic && !uniform.source && !uniform.node) {
  935. BABYLON.GLTFUtils.SetMatrix(gltfRuntime.scene, mesh, uniform, unif, shaderMaterial.getEffect());
  936. }
  937. else if (uniform.semantic && (uniform.source || uniform.node)) {
  938. var source = gltfRuntime.scene.getNodeByName(uniform.source || uniform.node);
  939. if (source === null) {
  940. source = gltfRuntime.scene.getNodeByID(uniform.source || uniform.node);
  941. }
  942. if (source === null) {
  943. continue;
  944. }
  945. BABYLON.GLTFUtils.SetMatrix(gltfRuntime.scene, source, uniform, unif, shaderMaterial.getEffect());
  946. }
  947. }
  948. else {
  949. var value = material.values[technique.uniforms[unif]];
  950. if (!value) {
  951. continue;
  952. }
  953. if (type === BABYLON.EParameterType.SAMPLER_2D) {
  954. var texture = gltfRuntime.textures[value].babylonTexture;
  955. if (texture === null) {
  956. continue;
  957. }
  958. shaderMaterial.getEffect().setTexture(unif, texture);
  959. }
  960. else {
  961. BABYLON.GLTFUtils.SetUniform(shaderMaterial.getEffect(), unif, value, type);
  962. }
  963. }
  964. }
  965. onSuccess(shaderMaterial);
  966. };
  967. /**
  968. * Prepare uniforms to send the only one time
  969. * Loads the appropriate textures
  970. */
  971. var prepareShaderMaterialUniforms = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms) {
  972. var materialValues = material.values;
  973. var techniqueUniforms = technique.uniforms;
  974. /**
  975. * Prepare values here (not matrices)
  976. */
  977. for (var unif in unTreatedUniforms) {
  978. var uniform = unTreatedUniforms[unif];
  979. var type = uniform.type;
  980. var value = materialValues[techniqueUniforms[unif]] || uniform.value;
  981. if (!value) {
  982. continue;
  983. }
  984. var onLoadTexture = function (texture) {
  985. if (uniform.value) {
  986. // Static uniform
  987. shaderMaterial.setTexture(unif, texture);
  988. delete unTreatedUniforms[unif];
  989. }
  990. };
  991. // Texture (sampler2D)
  992. if (type === BABYLON.EParameterType.SAMPLER_2D) {
  993. BABYLON.GLTFFileLoaderExtension.loadTextureAsync(gltfRuntime, value, onLoadTexture, function () { return onLoadTexture(null); });
  994. }
  995. else {
  996. if (uniform.value && BABYLON.GLTFUtils.SetUniform(shaderMaterial, unif, value, type)) {
  997. // Static uniform
  998. delete unTreatedUniforms[unif];
  999. }
  1000. }
  1001. }
  1002. };
  1003. /**
  1004. * Shader compilation failed
  1005. */
  1006. var onShaderCompileError = function (program, shaderMaterial, onError) {
  1007. return function (effect, error) {
  1008. BABYLON.Tools.Error("Cannot compile program named " + program.name + ". Error: " + error + ". Default material will be applied");
  1009. shaderMaterial.dispose(true);
  1010. onError();
  1011. };
  1012. };
  1013. /**
  1014. * Shader compilation success
  1015. */
  1016. var onShaderCompileSuccess = function (gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess) {
  1017. return function (_) {
  1018. prepareShaderMaterialUniforms(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms);
  1019. shaderMaterial.onBind = function (mesh) {
  1020. onBindShaderMaterial(mesh, gltfRuntime, unTreatedUniforms, shaderMaterial, technique, material, onSuccess);
  1021. };
  1022. };
  1023. };
  1024. /**
  1025. * Returns the appropriate uniform if already handled by babylon
  1026. */
  1027. var parseShaderUniforms = function (tokenizer, technique, unTreatedUniforms) {
  1028. for (var unif in technique.uniforms) {
  1029. var uniform = technique.uniforms[unif];
  1030. var uniformParameter = technique.parameters[uniform];
  1031. if (tokenizer.currentIdentifier === unif) {
  1032. if (uniformParameter.semantic && !uniformParameter.source && !uniformParameter.node) {
  1033. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1034. if (transformIndex !== -1) {
  1035. delete unTreatedUniforms[unif];
  1036. return babylonTransforms[transformIndex];
  1037. }
  1038. }
  1039. }
  1040. }
  1041. return tokenizer.currentIdentifier;
  1042. };
  1043. /**
  1044. * All shaders loaded. Create materials one by one
  1045. */
  1046. var importMaterials = function (gltfRuntime) {
  1047. // Create materials
  1048. for (var mat in gltfRuntime.materials) {
  1049. BABYLON.GLTFFileLoaderExtension.loadMaterialAsync(gltfRuntime, mat, function (material) { }, function () { });
  1050. }
  1051. };
  1052. /**
  1053. * Implementation of the base glTF spec
  1054. */
  1055. var GLTFFileLoaderBase = (function () {
  1056. function GLTFFileLoaderBase() {
  1057. }
  1058. GLTFFileLoaderBase.createRuntime = function (parsedData, scene, rootUrl) {
  1059. var gltfRuntime = {
  1060. accessors: {},
  1061. buffers: {},
  1062. bufferViews: {},
  1063. meshes: {},
  1064. lights: {},
  1065. cameras: {},
  1066. nodes: {},
  1067. images: {},
  1068. textures: {},
  1069. shaders: {},
  1070. programs: {},
  1071. samplers: {},
  1072. techniques: {},
  1073. materials: {},
  1074. animations: {},
  1075. skins: {},
  1076. currentScene: {},
  1077. extensionsUsed: [],
  1078. buffersCount: 0,
  1079. shaderscount: 0,
  1080. scene: scene,
  1081. rootUrl: rootUrl,
  1082. loadedBufferCount: 0,
  1083. loadedBufferViews: {},
  1084. loadedShaderCount: 0,
  1085. importOnlyMeshes: false,
  1086. dummyNodes: []
  1087. };
  1088. // Parse
  1089. if (parsedData.extensionsUsed) {
  1090. parseObject(parsedData.extensionsUsed, "extensionsUsed", gltfRuntime);
  1091. }
  1092. if (parsedData.buffers) {
  1093. parseBuffers(parsedData.buffers, gltfRuntime);
  1094. }
  1095. if (parsedData.bufferViews) {
  1096. parseObject(parsedData.bufferViews, "bufferViews", gltfRuntime);
  1097. }
  1098. if (parsedData.accessors) {
  1099. parseObject(parsedData.accessors, "accessors", gltfRuntime);
  1100. }
  1101. if (parsedData.meshes) {
  1102. parseObject(parsedData.meshes, "meshes", gltfRuntime);
  1103. }
  1104. if (parsedData.lights) {
  1105. parseObject(parsedData.lights, "lights", gltfRuntime);
  1106. }
  1107. if (parsedData.cameras) {
  1108. parseObject(parsedData.cameras, "cameras", gltfRuntime);
  1109. }
  1110. if (parsedData.nodes) {
  1111. parseObject(parsedData.nodes, "nodes", gltfRuntime);
  1112. }
  1113. if (parsedData.images) {
  1114. parseObject(parsedData.images, "images", gltfRuntime);
  1115. }
  1116. if (parsedData.textures) {
  1117. parseObject(parsedData.textures, "textures", gltfRuntime);
  1118. }
  1119. if (parsedData.shaders) {
  1120. parseShaders(parsedData.shaders, gltfRuntime);
  1121. }
  1122. if (parsedData.programs) {
  1123. parseObject(parsedData.programs, "programs", gltfRuntime);
  1124. }
  1125. if (parsedData.samplers) {
  1126. parseObject(parsedData.samplers, "samplers", gltfRuntime);
  1127. }
  1128. if (parsedData.techniques) {
  1129. parseObject(parsedData.techniques, "techniques", gltfRuntime);
  1130. }
  1131. if (parsedData.materials) {
  1132. parseObject(parsedData.materials, "materials", gltfRuntime);
  1133. }
  1134. if (parsedData.animations) {
  1135. parseObject(parsedData.animations, "animations", gltfRuntime);
  1136. }
  1137. if (parsedData.skins) {
  1138. parseObject(parsedData.skins, "skins", gltfRuntime);
  1139. }
  1140. if (parsedData.scene && parsedData.scenes) {
  1141. gltfRuntime.currentScene = parsedData.scenes[parsedData.scene];
  1142. }
  1143. return gltfRuntime;
  1144. };
  1145. GLTFFileLoaderBase.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1146. var buffer = gltfRuntime.buffers[id];
  1147. if (BABYLON.GLTFUtils.IsBase64(buffer.uri)) {
  1148. var decodedBuffer = decodeArrayBuffer(buffer.uri.split(",")[1]);
  1149. onSuccess(new Uint8Array(decodedBuffer));
  1150. }
  1151. else {
  1152. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + buffer.uri, function (data) { return onSuccess(new Uint8Array(data)); }, null, null, true, onError);
  1153. }
  1154. };
  1155. GLTFFileLoaderBase.loadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  1156. var texture = gltfRuntime.textures[id];
  1157. if (!texture || !texture.source) {
  1158. onError();
  1159. return;
  1160. }
  1161. if (texture.babylonTexture) {
  1162. onSuccess(texture.babylonTexture);
  1163. return;
  1164. }
  1165. var sampler = gltfRuntime.samplers[texture.sampler];
  1166. var source = gltfRuntime.images[texture.source];
  1167. var newTexture;
  1168. var createMipMaps = (sampler.minFilter === BABYLON.ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  1169. (sampler.minFilter === BABYLON.ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  1170. (sampler.minFilter === BABYLON.ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  1171. (sampler.minFilter === BABYLON.ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  1172. var samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  1173. if (BABYLON.GLTFUtils.IsBase64(source.uri)) {
  1174. newTexture = new BABYLON.Texture(source.uri, gltfRuntime.scene, !createMipMaps, true, samplingMode, function () { return onSuccess(newTexture); }, onError, source.uri, true);
  1175. }
  1176. else {
  1177. newTexture = new BABYLON.Texture(gltfRuntime.rootUrl + source.uri, gltfRuntime.scene, !createMipMaps, true, samplingMode, function () { return onSuccess(newTexture); }, onError);
  1178. }
  1179. newTexture.wrapU = BABYLON.GLTFUtils.GetWrapMode(sampler.wrapS);
  1180. newTexture.wrapV = BABYLON.GLTFUtils.GetWrapMode(sampler.wrapT);
  1181. newTexture.name = id;
  1182. texture.babylonTexture = newTexture;
  1183. };
  1184. GLTFFileLoaderBase.loadShaderDataAsync = function (gltfRuntime, id, onSuccess, onError) {
  1185. var shader = gltfRuntime.shaders[id];
  1186. if (BABYLON.GLTFUtils.IsBase64(shader.uri)) {
  1187. var shaderData = atob(shader.uri.split(",")[1]);
  1188. onSuccess(shaderData);
  1189. }
  1190. else {
  1191. BABYLON.Tools.LoadFile(gltfRuntime.rootUrl + shader.uri, onSuccess, null, null, false, onError);
  1192. }
  1193. };
  1194. GLTFFileLoaderBase.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  1195. var material = gltfRuntime.materials[id];
  1196. var technique = gltfRuntime.techniques[material.technique];
  1197. var program = gltfRuntime.programs[technique.program];
  1198. var states = technique.states;
  1199. var vertexShader = BABYLON.Effect.ShadersStore[program.vertexShader + "VertexShader"];
  1200. var pixelShader = BABYLON.Effect.ShadersStore[program.fragmentShader + "PixelShader"];
  1201. var newVertexShader = "";
  1202. var newPixelShader = "";
  1203. var vertexTokenizer = new Tokenizer(vertexShader);
  1204. var pixelTokenizer = new Tokenizer(pixelShader);
  1205. var unTreatedUniforms = {};
  1206. var uniforms = [];
  1207. var attributes = [];
  1208. var samplers = [];
  1209. // Fill uniform, sampler2D and attributes
  1210. for (var unif in technique.uniforms) {
  1211. var uniform = technique.uniforms[unif];
  1212. var uniformParameter = technique.parameters[uniform];
  1213. unTreatedUniforms[unif] = uniformParameter;
  1214. if (uniformParameter.semantic && !uniformParameter.node && !uniformParameter.source) {
  1215. var transformIndex = glTFTransforms.indexOf(uniformParameter.semantic);
  1216. if (transformIndex !== -1) {
  1217. uniforms.push(babylonTransforms[transformIndex]);
  1218. delete unTreatedUniforms[unif];
  1219. }
  1220. else {
  1221. uniforms.push(unif);
  1222. }
  1223. }
  1224. else if (uniformParameter.type === BABYLON.EParameterType.SAMPLER_2D) {
  1225. samplers.push(unif);
  1226. }
  1227. else {
  1228. uniforms.push(unif);
  1229. }
  1230. }
  1231. for (var attr in technique.attributes) {
  1232. var attribute = technique.attributes[attr];
  1233. var attributeParameter = technique.parameters[attribute];
  1234. if (attributeParameter.semantic) {
  1235. attributes.push(getAttribute(attributeParameter));
  1236. }
  1237. }
  1238. // Configure vertex shader
  1239. while (!vertexTokenizer.isEnd() && vertexTokenizer.getNextToken()) {
  1240. var tokenType = vertexTokenizer.currentToken;
  1241. if (tokenType !== ETokenType.IDENTIFIER) {
  1242. newVertexShader += vertexTokenizer.currentString;
  1243. continue;
  1244. }
  1245. var foundAttribute = false;
  1246. for (var attr in technique.attributes) {
  1247. var attribute = technique.attributes[attr];
  1248. var attributeParameter = technique.parameters[attribute];
  1249. if (vertexTokenizer.currentIdentifier === attr && attributeParameter.semantic) {
  1250. newVertexShader += getAttribute(attributeParameter);
  1251. foundAttribute = true;
  1252. break;
  1253. }
  1254. }
  1255. if (foundAttribute) {
  1256. continue;
  1257. }
  1258. newVertexShader += parseShaderUniforms(vertexTokenizer, technique, unTreatedUniforms);
  1259. }
  1260. // Configure pixel shader
  1261. while (!pixelTokenizer.isEnd() && pixelTokenizer.getNextToken()) {
  1262. var tokenType = pixelTokenizer.currentToken;
  1263. if (tokenType !== ETokenType.IDENTIFIER) {
  1264. newPixelShader += pixelTokenizer.currentString;
  1265. continue;
  1266. }
  1267. newPixelShader += parseShaderUniforms(pixelTokenizer, technique, unTreatedUniforms);
  1268. }
  1269. // Create shader material
  1270. var shaderPath = {
  1271. vertex: program.vertexShader + id,
  1272. fragment: program.fragmentShader + id
  1273. };
  1274. var options = {
  1275. attributes: attributes,
  1276. uniforms: uniforms,
  1277. samplers: samplers,
  1278. needAlphaBlending: states.functions && states.functions.blendEquationSeparate
  1279. };
  1280. BABYLON.Effect.ShadersStore[program.vertexShader + id + "VertexShader"] = newVertexShader;
  1281. BABYLON.Effect.ShadersStore[program.fragmentShader + id + "PixelShader"] = newPixelShader;
  1282. var shaderMaterial = new BABYLON.ShaderMaterial(id, gltfRuntime.scene, shaderPath, options);
  1283. shaderMaterial.onError = onShaderCompileError(program, shaderMaterial, onError);
  1284. shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess);
  1285. if (states.functions) {
  1286. var functions = states.functions;
  1287. if (functions.cullFace && functions.cullFace[0] !== BABYLON.ECullingType.BACK) {
  1288. shaderMaterial.backFaceCulling = false;
  1289. }
  1290. var blendFunc = functions.blendFuncSeparate;
  1291. if (blendFunc) {
  1292. if (blendFunc[0] === BABYLON.EBlendingFunction.SRC_ALPHA && blendFunc[1] === BABYLON.EBlendingFunction.ONE_MINUS_SRC_ALPHA && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1293. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_COMBINE;
  1294. }
  1295. else if (blendFunc[0] === BABYLON.EBlendingFunction.ONE && blendFunc[1] === BABYLON.EBlendingFunction.ONE && blendFunc[2] === BABYLON.EBlendingFunction.ZERO && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1296. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ONEONE;
  1297. }
  1298. else if (blendFunc[0] === BABYLON.EBlendingFunction.SRC_ALPHA && blendFunc[1] === BABYLON.EBlendingFunction.ONE && blendFunc[2] === BABYLON.EBlendingFunction.ZERO && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1299. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ADD;
  1300. }
  1301. else if (blendFunc[0] === BABYLON.EBlendingFunction.ZERO && blendFunc[1] === BABYLON.EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1302. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_SUBTRACT;
  1303. }
  1304. else if (blendFunc[0] === BABYLON.EBlendingFunction.DST_COLOR && blendFunc[1] === BABYLON.EBlendingFunction.ZERO && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1305. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MULTIPLY;
  1306. }
  1307. else if (blendFunc[0] === BABYLON.EBlendingFunction.SRC_ALPHA && blendFunc[1] === BABYLON.EBlendingFunction.ONE_MINUS_SRC_COLOR && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1308. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MAXIMIZED;
  1309. }
  1310. }
  1311. }
  1312. };
  1313. return GLTFFileLoaderBase;
  1314. }());
  1315. BABYLON.GLTFFileLoaderBase = GLTFFileLoaderBase;
  1316. /**
  1317. * glTF File Loader Plugin
  1318. */
  1319. var GLTFFileLoader = (function () {
  1320. function GLTFFileLoader() {
  1321. /**
  1322. * Public members
  1323. */
  1324. this.extensions = {
  1325. ".gltf": { isBinary: false },
  1326. ".glb": { isBinary: true }
  1327. };
  1328. }
  1329. GLTFFileLoader.RegisterExtension = function (extension) {
  1330. if (GLTFFileLoader.Extensions[extension.name]) {
  1331. BABYLON.Tools.Error("Tool with the same name \"" + extension.name + "\" already exists");
  1332. return;
  1333. }
  1334. GLTFFileLoader.Extensions[extension.name] = extension;
  1335. };
  1336. /**
  1337. * Import meshes
  1338. */
  1339. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onError) {
  1340. var _this = this;
  1341. var gltfRuntime = BABYLON.GLTFFileLoaderExtension.loadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  1342. gltfRuntime.importOnlyMeshes = true;
  1343. if (meshesNames === "") {
  1344. gltfRuntime.importMeshesNames = [];
  1345. }
  1346. else if (typeof meshesNames === "string") {
  1347. gltfRuntime.importMeshesNames = [meshesNames];
  1348. }
  1349. else if (meshesNames && !(meshesNames instanceof Array)) {
  1350. gltfRuntime.importMeshesNames = [meshesNames];
  1351. }
  1352. else {
  1353. gltfRuntime.importMeshesNames = [];
  1354. BABYLON.Tools.Warn("Argument meshesNames must be of type string or string[]");
  1355. }
  1356. // Create nodes
  1357. _this._createNodes(gltfRuntime);
  1358. var meshes = [];
  1359. var skeletons = [];
  1360. // Fill arrays of meshes and skeletons
  1361. for (var nde in gltfRuntime.nodes) {
  1362. var node = gltfRuntime.nodes[nde];
  1363. if (node.babylonNode instanceof BABYLON.AbstractMesh) {
  1364. meshes.push(node.babylonNode);
  1365. }
  1366. }
  1367. for (var skl in gltfRuntime.skins) {
  1368. var skin = gltfRuntime.skins[skl];
  1369. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  1370. skeletons.push(skin.babylonSkeleton);
  1371. }
  1372. }
  1373. // Load shaders and then buffers
  1374. _this._loadShadersAsync(gltfRuntime, function () {
  1375. importMaterials(gltfRuntime);
  1376. _this._loadBuffersAsync(gltfRuntime, function () {
  1377. onBuffersLoaded(gltfRuntime);
  1378. });
  1379. });
  1380. if (onSuccess) {
  1381. onSuccess(meshes, null, skeletons);
  1382. }
  1383. }, onError);
  1384. return true;
  1385. };
  1386. /**
  1387. * Load scene
  1388. */
  1389. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onError) {
  1390. var _this = this;
  1391. BABYLON.GLTFFileLoaderExtension.loadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  1392. // Create nodes
  1393. _this._createNodes(gltfRuntime);
  1394. // Load shaders and then buffers
  1395. _this._loadShadersAsync(gltfRuntime, function () {
  1396. importMaterials(gltfRuntime);
  1397. _this._loadBuffersAsync(gltfRuntime, function () {
  1398. onBuffersLoaded(gltfRuntime);
  1399. });
  1400. });
  1401. onSuccess();
  1402. }, onError);
  1403. return true;
  1404. };
  1405. GLTFFileLoader.prototype._loadShadersAsync = function (gltfRuntime, onload) {
  1406. var hasShaders = false;
  1407. var processShader = function (sha, shader) {
  1408. BABYLON.GLTFFileLoaderExtension.loadShaderDataAsync(gltfRuntime, sha, function (shaderData) {
  1409. gltfRuntime.loadedShaderCount++;
  1410. if (shaderData) {
  1411. BABYLON.Effect.ShadersStore[sha + (shader.type === BABYLON.EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = shaderData;
  1412. }
  1413. if (gltfRuntime.loadedShaderCount === gltfRuntime.shaderscount) {
  1414. onload();
  1415. }
  1416. }, function () {
  1417. BABYLON.Tools.Error("Error when loading shader program named " + sha + " located at " + shader.uri);
  1418. });
  1419. };
  1420. for (var sha in gltfRuntime.shaders) {
  1421. hasShaders = true;
  1422. var shader = gltfRuntime.shaders[sha];
  1423. if (shader) {
  1424. processShader.bind(this, sha, shader)();
  1425. }
  1426. else {
  1427. BABYLON.Tools.Error("No shader named: " + sha);
  1428. }
  1429. }
  1430. if (!hasShaders) {
  1431. onload();
  1432. }
  1433. };
  1434. ;
  1435. GLTFFileLoader.prototype._loadBuffersAsync = function (gltfRuntime, onload) {
  1436. var hasBuffers = false;
  1437. var processBuffer = function (buf, buffer) {
  1438. BABYLON.GLTFFileLoaderExtension.loadBufferAsync(gltfRuntime, buf, function (bufferView) {
  1439. gltfRuntime.loadedBufferCount++;
  1440. if (bufferView) {
  1441. if (bufferView.byteLength != gltfRuntime.buffers[buf].byteLength) {
  1442. BABYLON.Tools.Error("Buffer named " + buf + " is length " + bufferView.byteLength + ". Expected: " + buffer.byteLength); // Improve error message
  1443. }
  1444. gltfRuntime.loadedBufferViews[buf] = bufferView;
  1445. }
  1446. if (gltfRuntime.loadedBufferCount === gltfRuntime.buffersCount) {
  1447. onload();
  1448. }
  1449. }, function () {
  1450. BABYLON.Tools.Error("Error when loading buffer named " + buf + " located at " + buffer.uri);
  1451. });
  1452. };
  1453. for (var buf in gltfRuntime.buffers) {
  1454. hasBuffers = true;
  1455. var buffer = gltfRuntime.buffers[buf];
  1456. if (buffer) {
  1457. processBuffer.bind(this, buf, buffer)();
  1458. }
  1459. else {
  1460. BABYLON.Tools.Error("No buffer named: " + buf);
  1461. }
  1462. }
  1463. if (!hasBuffers) {
  1464. onload();
  1465. }
  1466. };
  1467. // Creates nodes before loading buffers and shaders
  1468. GLTFFileLoader.prototype._createNodes = function (gltfRuntime) {
  1469. var currentScene = gltfRuntime.currentScene;
  1470. for (var i = 0; i < currentScene.nodes.length; i++) {
  1471. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1472. }
  1473. };
  1474. /**
  1475. * Private members
  1476. */
  1477. // None
  1478. /**
  1479. * Static members
  1480. */
  1481. GLTFFileLoader.MakeYUP = false;
  1482. GLTFFileLoader.HomogeneousCoordinates = false;
  1483. GLTFFileLoader.Extensions = {};
  1484. return GLTFFileLoader;
  1485. }());
  1486. BABYLON.GLTFFileLoader = GLTFFileLoader;
  1487. ;
  1488. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  1489. })(BABYLON || (BABYLON = {}));
  1490. var BABYLON;
  1491. (function (BABYLON) {
  1492. /**
  1493. * Utils functions for GLTF
  1494. */
  1495. var GLTFUtils = (function () {
  1496. function GLTFUtils() {
  1497. }
  1498. /**
  1499. * Sets the given "parameter" matrix
  1500. * @param scene: the {BABYLON.Scene} object
  1501. * @param source: the source node where to pick the matrix
  1502. * @param parameter: the GLTF technique parameter
  1503. * @param uniformName: the name of the shader's uniform
  1504. * @param shaderMaterial: the shader material
  1505. */
  1506. GLTFUtils.SetMatrix = function (scene, source, parameter, uniformName, shaderMaterial) {
  1507. var mat = null;
  1508. if (parameter.semantic === "MODEL") {
  1509. mat = source.getWorldMatrix();
  1510. }
  1511. else if (parameter.semantic === "PROJECTION") {
  1512. mat = scene.getProjectionMatrix();
  1513. }
  1514. else if (parameter.semantic === "VIEW") {
  1515. mat = scene.getViewMatrix();
  1516. }
  1517. else if (parameter.semantic === "MODELVIEWINVERSETRANSPOSE") {
  1518. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().multiply(scene.getViewMatrix()).invert());
  1519. }
  1520. else if (parameter.semantic === "MODELVIEW") {
  1521. mat = source.getWorldMatrix().multiply(scene.getViewMatrix());
  1522. }
  1523. else if (parameter.semantic === "MODELVIEWPROJECTION") {
  1524. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix());
  1525. }
  1526. else if (parameter.semantic === "MODELINVERSE") {
  1527. mat = source.getWorldMatrix().invert();
  1528. }
  1529. else if (parameter.semantic === "VIEWINVERSE") {
  1530. mat = scene.getViewMatrix().invert();
  1531. }
  1532. else if (parameter.semantic === "PROJECTIONINVERSE") {
  1533. mat = scene.getProjectionMatrix().invert();
  1534. }
  1535. else if (parameter.semantic === "MODELVIEWINVERSE") {
  1536. mat = source.getWorldMatrix().multiply(scene.getViewMatrix()).invert();
  1537. }
  1538. else if (parameter.semantic === "MODELVIEWPROJECTIONINVERSE") {
  1539. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix()).invert();
  1540. }
  1541. else if (parameter.semantic === "MODELINVERSETRANSPOSE") {
  1542. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().invert());
  1543. }
  1544. else {
  1545. debugger;
  1546. }
  1547. switch (parameter.type) {
  1548. case BABYLON.EParameterType.FLOAT_MAT2:
  1549. shaderMaterial.setMatrix2x2(uniformName, BABYLON.Matrix.GetAsMatrix2x2(mat));
  1550. break;
  1551. case BABYLON.EParameterType.FLOAT_MAT3:
  1552. shaderMaterial.setMatrix3x3(uniformName, BABYLON.Matrix.GetAsMatrix3x3(mat));
  1553. break;
  1554. case BABYLON.EParameterType.FLOAT_MAT4:
  1555. shaderMaterial.setMatrix(uniformName, mat);
  1556. break;
  1557. default: break;
  1558. }
  1559. };
  1560. /**
  1561. * Sets the given "parameter" matrix
  1562. * @param shaderMaterial: the shader material
  1563. * @param uniform: the name of the shader's uniform
  1564. * @param value: the value of the uniform
  1565. * @param type: the uniform's type (EParameterType FLOAT, VEC2, VEC3 or VEC4)
  1566. */
  1567. GLTFUtils.SetUniform = function (shaderMaterial, uniform, value, type) {
  1568. switch (type) {
  1569. case BABYLON.EParameterType.FLOAT:
  1570. shaderMaterial.setFloat(uniform, value);
  1571. return true;
  1572. case BABYLON.EParameterType.FLOAT_VEC2:
  1573. shaderMaterial.setVector2(uniform, BABYLON.Vector2.FromArray(value));
  1574. return true;
  1575. case BABYLON.EParameterType.FLOAT_VEC3:
  1576. shaderMaterial.setVector3(uniform, BABYLON.Vector3.FromArray(value));
  1577. return true;
  1578. case BABYLON.EParameterType.FLOAT_VEC4:
  1579. shaderMaterial.setVector4(uniform, BABYLON.Vector4.FromArray(value));
  1580. return true;
  1581. default: return false;
  1582. }
  1583. };
  1584. /**
  1585. * If the uri is a base64 string
  1586. * @param uri: the uri to test
  1587. */
  1588. GLTFUtils.IsBase64 = function (uri) {
  1589. return uri.length < 5 ? false : uri.substr(0, 5) === "data:";
  1590. };
  1591. /**
  1592. * Returns the wrap mode of the texture
  1593. * @param mode: the mode value
  1594. */
  1595. GLTFUtils.GetWrapMode = function (mode) {
  1596. switch (mode) {
  1597. case BABYLON.ETextureWrapMode.CLAMP_TO_EDGE: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  1598. case BABYLON.ETextureWrapMode.MIRRORED_REPEAT: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1599. case BABYLON.ETextureWrapMode.REPEAT: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1600. default: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1601. }
  1602. };
  1603. /**
  1604. * Returns the byte stride giving an accessor
  1605. * @param accessor: the GLTF accessor objet
  1606. */
  1607. GLTFUtils.GetByteStrideFromType = function (accessor) {
  1608. // Needs this function since "byteStride" isn't requiered in glTF format
  1609. var type = accessor.type;
  1610. switch (type) {
  1611. case "VEC2": return 2;
  1612. case "VEC3": return 3;
  1613. case "VEC4": return 4;
  1614. case "MAT2": return 4;
  1615. case "MAT3": return 9;
  1616. case "MAT4": return 16;
  1617. default: return 1;
  1618. }
  1619. };
  1620. /**
  1621. * Returns the texture filter mode giving a mode value
  1622. * @param mode: the filter mode value
  1623. */
  1624. GLTFUtils.GetTextureFilterMode = function (mode) {
  1625. switch (mode) {
  1626. case BABYLON.ETextureFilterType.LINEAR:
  1627. case BABYLON.ETextureFilterType.LINEAR_MIPMAP_NEAREST:
  1628. case BABYLON.ETextureFilterType.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  1629. case BABYLON.ETextureFilterType.NEAREST:
  1630. case BABYLON.ETextureFilterType.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_SAMPLINGMODE;
  1631. default: return BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  1632. }
  1633. };
  1634. GLTFUtils.GetBufferFromBufferView = function (gltfRuntime, bufferView, byteOffset, byteLength, componentType) {
  1635. var byteOffset = bufferView.byteOffset + byteOffset;
  1636. var loadedBufferView = gltfRuntime.loadedBufferViews[bufferView.buffer];
  1637. if (byteOffset + byteLength > loadedBufferView.byteLength) {
  1638. throw new Error("Buffer access is out of range");
  1639. }
  1640. var buffer = loadedBufferView.buffer;
  1641. byteOffset += loadedBufferView.byteOffset;
  1642. switch (componentType) {
  1643. case BABYLON.EComponentType.BYTE: return new Int8Array(buffer, byteOffset, byteLength);
  1644. case BABYLON.EComponentType.UNSIGNED_BYTE: return new Uint8Array(buffer, byteOffset, byteLength);
  1645. case BABYLON.EComponentType.SHORT: return new Int16Array(buffer, byteOffset, byteLength);
  1646. case BABYLON.EComponentType.UNSIGNED_SHORT: return new Uint16Array(buffer, byteOffset, byteLength);
  1647. default: return new Float32Array(buffer, byteOffset, byteLength);
  1648. }
  1649. };
  1650. /**
  1651. * Returns a buffer from its accessor
  1652. * @param gltfRuntime: the GLTF runtime
  1653. * @param accessor: the GLTF accessor
  1654. */
  1655. GLTFUtils.GetBufferFromAccessor = function (gltfRuntime, accessor) {
  1656. var bufferView = gltfRuntime.bufferViews[accessor.bufferView];
  1657. var byteLength = accessor.count * GLTFUtils.GetByteStrideFromType(accessor);
  1658. return GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, accessor.byteOffset, byteLength, accessor.componentType);
  1659. };
  1660. /**
  1661. * Decodes a buffer view into a string
  1662. * @param view: the buffer view
  1663. */
  1664. GLTFUtils.DecodeBufferToText = function (view) {
  1665. var result = "";
  1666. var length = view.byteLength;
  1667. for (var i = 0; i < length; ++i) {
  1668. result += String.fromCharCode(view[i]);
  1669. }
  1670. return result;
  1671. };
  1672. return GLTFUtils;
  1673. }());
  1674. BABYLON.GLTFUtils = GLTFUtils;
  1675. })(BABYLON || (BABYLON = {}));
  1676. var BABYLON;
  1677. (function (BABYLON) {
  1678. var GLTFFileLoaderExtension = (function () {
  1679. function GLTFFileLoaderExtension(name) {
  1680. this._name = name;
  1681. }
  1682. Object.defineProperty(GLTFFileLoaderExtension.prototype, "name", {
  1683. get: function () {
  1684. return this._name;
  1685. },
  1686. enumerable: true,
  1687. configurable: true
  1688. });
  1689. /**
  1690. * Defines an override for loading the runtime
  1691. * Return true to stop further extensions from loading the runtime
  1692. */
  1693. GLTFFileLoaderExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  1694. return false;
  1695. };
  1696. /**
  1697. * Defines an override for loading buffers
  1698. * Return true to stop further extensions from loading this buffer
  1699. */
  1700. GLTFFileLoaderExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1701. return false;
  1702. };
  1703. /**
  1704. * Defines an override for loading textures
  1705. * Return true to stop further extensions from loading this texture
  1706. */
  1707. GLTFFileLoaderExtension.prototype.loadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  1708. return false;
  1709. };
  1710. /**
  1711. * Defines an override for loading shader data
  1712. * Return true to stop further extensions from loading this shader data
  1713. */
  1714. GLTFFileLoaderExtension.prototype.loadShaderDataAsync = function (gltfRuntime, id, onSuccess, onError) {
  1715. return false;
  1716. };
  1717. /**
  1718. * Defines an override for loading materials
  1719. * Return true to stop further extensions from loading this material
  1720. */
  1721. GLTFFileLoaderExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  1722. return false;
  1723. };
  1724. // ---------
  1725. // Utilities
  1726. // ---------
  1727. GLTFFileLoaderExtension.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  1728. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1729. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  1730. }, function () {
  1731. onSuccess(BABYLON.GLTFFileLoaderBase.createRuntime(JSON.parse(data), scene, rootUrl));
  1732. });
  1733. };
  1734. GLTFFileLoaderExtension.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1735. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1736. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
  1737. }, function () {
  1738. BABYLON.GLTFFileLoaderBase.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
  1739. });
  1740. };
  1741. GLTFFileLoaderExtension.loadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  1742. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1743. return loaderExtension.loadTextureAsync(gltfRuntime, id, onSuccess, onError);
  1744. }, function () {
  1745. BABYLON.GLTFFileLoaderBase.loadTextureAsync(gltfRuntime, id, onSuccess, onError);
  1746. });
  1747. };
  1748. GLTFFileLoaderExtension.loadShaderDataAsync = function (gltfRuntime, id, onSuccess, onError) {
  1749. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1750. return loaderExtension.loadShaderDataAsync(gltfRuntime, id, onSuccess, onError);
  1751. }, function () {
  1752. BABYLON.GLTFFileLoaderBase.loadShaderDataAsync(gltfRuntime, id, onSuccess, onError);
  1753. });
  1754. };
  1755. GLTFFileLoaderExtension.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  1756. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1757. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  1758. }, function () {
  1759. BABYLON.GLTFFileLoaderBase.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  1760. });
  1761. };
  1762. GLTFFileLoaderExtension.applyExtensions = function (func, defaultFunc) {
  1763. for (var extensionName in BABYLON.GLTFFileLoader.Extensions) {
  1764. var loaderExtension = BABYLON.GLTFFileLoader.Extensions[extensionName];
  1765. if (func(loaderExtension)) {
  1766. return;
  1767. }
  1768. }
  1769. defaultFunc();
  1770. };
  1771. return GLTFFileLoaderExtension;
  1772. }());
  1773. BABYLON.GLTFFileLoaderExtension = GLTFFileLoaderExtension;
  1774. })(BABYLON || (BABYLON = {}));
  1775. var __extends = (this && this.__extends) || function (d, b) {
  1776. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  1777. function __() { this.constructor = d; }
  1778. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1779. };
  1780. var BABYLON;
  1781. (function (BABYLON) {
  1782. var BinaryExtensionBufferName = "binary_glTF";
  1783. var EContentFormat;
  1784. (function (EContentFormat) {
  1785. EContentFormat[EContentFormat["JSON"] = 0] = "JSON";
  1786. })(EContentFormat || (EContentFormat = {}));
  1787. ;
  1788. ;
  1789. ;
  1790. ;
  1791. var GLTFBinaryExtension = (function (_super) {
  1792. __extends(GLTFBinaryExtension, _super);
  1793. function GLTFBinaryExtension() {
  1794. _super.call(this, "KHR_binary_glTF");
  1795. }
  1796. GLTFBinaryExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  1797. if (!(data instanceof ArrayBuffer)) {
  1798. return false;
  1799. }
  1800. var binary = this._parseBinary(data);
  1801. if (!binary) {
  1802. onError();
  1803. return true;
  1804. }
  1805. var gltfRuntime = BABYLON.GLTFFileLoaderBase.createRuntime(binary.content, scene, rootUrl);
  1806. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  1807. BABYLON.Tools.Warn("glTF binary file does not have " + this.name + " specified in extensionsUsed");
  1808. gltfRuntime.extensionsUsed.push(this.name);
  1809. }
  1810. gltfRuntime.loadedBufferViews[BinaryExtensionBufferName] = binary.body;
  1811. onSuccess(gltfRuntime);
  1812. return true;
  1813. };
  1814. GLTFBinaryExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1815. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  1816. return false;
  1817. }
  1818. if (id !== BinaryExtensionBufferName) {
  1819. return false;
  1820. }
  1821. // Buffer is already loaded in loadRuntimeAsync
  1822. onSuccess(null);
  1823. return true;
  1824. };
  1825. GLTFBinaryExtension.prototype.loadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  1826. var texture = gltfRuntime.textures[id];
  1827. var source = gltfRuntime.images[texture.source];
  1828. if (!source.extensions || !(this.name in source.extensions)) {
  1829. return false;
  1830. }
  1831. var sourceExt = source.extensions[this.name];
  1832. var sampler = gltfRuntime.samplers[texture.sampler];
  1833. var createMipMaps = (sampler.minFilter === BABYLON.ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  1834. (sampler.minFilter === BABYLON.ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  1835. (sampler.minFilter === BABYLON.ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  1836. (sampler.minFilter === BABYLON.ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  1837. var samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  1838. var bufferView = gltfRuntime.bufferViews[sourceExt.bufferView];
  1839. var imageBytes = BABYLON.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, BABYLON.EComponentType.UNSIGNED_BYTE);
  1840. var blob = new Blob([imageBytes], { type: sourceExt.mimeType });
  1841. var blobURL = URL.createObjectURL(blob);
  1842. var revokeBlobURL = function () { return URL.revokeObjectURL(blobURL); };
  1843. var newTexture = new BABYLON.Texture(blobURL, gltfRuntime.scene, !createMipMaps, true, samplingMode, revokeBlobURL, revokeBlobURL);
  1844. newTexture.wrapU = BABYLON.GLTFUtils.GetWrapMode(sampler.wrapS);
  1845. newTexture.wrapV = BABYLON.GLTFUtils.GetWrapMode(sampler.wrapT);
  1846. newTexture.name = name;
  1847. texture.babylonTexture = newTexture;
  1848. onSuccess(newTexture);
  1849. return true;
  1850. };
  1851. GLTFBinaryExtension.prototype.loadShaderDataAsync = function (gltfRuntime, id, onSuccess, onError) {
  1852. var shader = gltfRuntime.shaders[id];
  1853. if (!shader.extensions || !(this.name in shader.extensions)) {
  1854. return false;
  1855. }
  1856. var binaryExtensionShader = shader.extensions[this.name];
  1857. var bufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
  1858. var shaderBytes = BABYLON.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, BABYLON.EComponentType.UNSIGNED_BYTE);
  1859. var shaderData = BABYLON.GLTFUtils.DecodeBufferToText(shaderBytes);
  1860. onSuccess(shaderData);
  1861. return true;
  1862. };
  1863. // Parses a glTF binary array buffer into its content and body
  1864. GLTFBinaryExtension.prototype._parseBinary = function (data) {
  1865. var binaryReader = new BinaryReader(data);
  1866. var magic = BABYLON.GLTFUtils.DecodeBufferToText(binaryReader.getUint8Array(4));
  1867. if (magic != "glTF") {
  1868. BABYLON.Tools.Error("Unexpected magic: " + magic);
  1869. return null;
  1870. }
  1871. var version = binaryReader.getUint32();
  1872. if (version != 1) {
  1873. BABYLON.Tools.Error("Unsupported version: " + version);
  1874. return null;
  1875. }
  1876. var length = binaryReader.getUint32();
  1877. if (length != data.byteLength) {
  1878. BABYLON.Tools.Error("Length in header does not match actual data length: " + length + " != " + data.byteLength);
  1879. return null;
  1880. }
  1881. var contentLength = binaryReader.getUint32();
  1882. var contentFormat = binaryReader.getUint32();
  1883. var content;
  1884. switch (contentFormat) {
  1885. case EContentFormat.JSON:
  1886. var jsonText = BABYLON.GLTFUtils.DecodeBufferToText(binaryReader.getUint8Array(contentLength));
  1887. content = JSON.parse(jsonText);
  1888. break;
  1889. default:
  1890. BABYLON.Tools.Error("Unexpected content format: " + contentFormat);
  1891. return null;
  1892. }
  1893. var body = binaryReader.getUint8Array();
  1894. return {
  1895. content: content,
  1896. body: body
  1897. };
  1898. };
  1899. ;
  1900. return GLTFBinaryExtension;
  1901. }(BABYLON.GLTFFileLoaderExtension));
  1902. BABYLON.GLTFBinaryExtension = GLTFBinaryExtension;
  1903. var BinaryReader = (function () {
  1904. function BinaryReader(arrayBuffer) {
  1905. this._arrayBuffer = arrayBuffer;
  1906. this._dataView = new DataView(arrayBuffer);
  1907. this._byteOffset = 0;
  1908. }
  1909. BinaryReader.prototype.getUint32 = function () {
  1910. var value = this._dataView.getUint32(this._byteOffset, true);
  1911. this._byteOffset += 4;
  1912. return value;
  1913. };
  1914. BinaryReader.prototype.getUint8Array = function (length) {
  1915. if (!length) {
  1916. length = this._arrayBuffer.byteLength - this._byteOffset;
  1917. }
  1918. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  1919. this._byteOffset += length;
  1920. return value;
  1921. };
  1922. return BinaryReader;
  1923. }());
  1924. BABYLON.GLTFFileLoader.RegisterExtension(new GLTFBinaryExtension());
  1925. })(BABYLON || (BABYLON = {}));