babylon.glTFFileLoader.js 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  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 = name;
  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(material.name, gltfRuntime.scene, shaderPath, options);
  1283. shaderMaterial.id = id;
  1284. shaderMaterial.onError = onShaderCompileError(program, shaderMaterial, onError);
  1285. shaderMaterial.onCompiled = onShaderCompileSuccess(gltfRuntime, shaderMaterial, technique, material, unTreatedUniforms, onSuccess);
  1286. if (states.functions) {
  1287. var functions = states.functions;
  1288. if (functions.cullFace && functions.cullFace[0] !== BABYLON.ECullingType.BACK) {
  1289. shaderMaterial.backFaceCulling = false;
  1290. }
  1291. var blendFunc = functions.blendFuncSeparate;
  1292. if (blendFunc) {
  1293. 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) {
  1294. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_COMBINE;
  1295. }
  1296. else if (blendFunc[0] === BABYLON.EBlendingFunction.ONE && blendFunc[1] === BABYLON.EBlendingFunction.ONE && blendFunc[2] === BABYLON.EBlendingFunction.ZERO && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1297. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ONEONE;
  1298. }
  1299. else if (blendFunc[0] === BABYLON.EBlendingFunction.SRC_ALPHA && blendFunc[1] === BABYLON.EBlendingFunction.ONE && blendFunc[2] === BABYLON.EBlendingFunction.ZERO && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1300. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_ADD;
  1301. }
  1302. 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) {
  1303. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_SUBTRACT;
  1304. }
  1305. else if (blendFunc[0] === BABYLON.EBlendingFunction.DST_COLOR && blendFunc[1] === BABYLON.EBlendingFunction.ZERO && blendFunc[2] === BABYLON.EBlendingFunction.ONE && blendFunc[3] === BABYLON.EBlendingFunction.ONE) {
  1306. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MULTIPLY;
  1307. }
  1308. 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) {
  1309. shaderMaterial.alphaMode = BABYLON.Engine.ALPHA_MAXIMIZED;
  1310. }
  1311. }
  1312. }
  1313. };
  1314. return GLTFFileLoaderBase;
  1315. }());
  1316. BABYLON.GLTFFileLoaderBase = GLTFFileLoaderBase;
  1317. /**
  1318. * glTF File Loader Plugin
  1319. */
  1320. var GLTFFileLoader = (function () {
  1321. function GLTFFileLoader() {
  1322. /**
  1323. * Public members
  1324. */
  1325. this.extensions = {
  1326. ".gltf": { isBinary: false },
  1327. ".glb": { isBinary: true }
  1328. };
  1329. }
  1330. GLTFFileLoader.RegisterExtension = function (extension) {
  1331. if (GLTFFileLoader.Extensions[extension.name]) {
  1332. BABYLON.Tools.Error("Tool with the same name \"" + extension.name + "\" already exists");
  1333. return;
  1334. }
  1335. GLTFFileLoader.Extensions[extension.name] = extension;
  1336. };
  1337. /**
  1338. * Import meshes
  1339. */
  1340. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onSuccess, onError) {
  1341. var _this = this;
  1342. var gltfRuntime = BABYLON.GLTFFileLoaderExtension.loadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  1343. gltfRuntime.importOnlyMeshes = true;
  1344. if (meshesNames === "") {
  1345. gltfRuntime.importMeshesNames = [];
  1346. }
  1347. else if (typeof meshesNames === "string") {
  1348. gltfRuntime.importMeshesNames = [meshesNames];
  1349. }
  1350. else if (meshesNames && !(meshesNames instanceof Array)) {
  1351. gltfRuntime.importMeshesNames = [meshesNames];
  1352. }
  1353. else {
  1354. gltfRuntime.importMeshesNames = [];
  1355. BABYLON.Tools.Warn("Argument meshesNames must be of type string or string[]");
  1356. }
  1357. // Create nodes
  1358. _this._createNodes(gltfRuntime);
  1359. var meshes = [];
  1360. var skeletons = [];
  1361. // Fill arrays of meshes and skeletons
  1362. for (var nde in gltfRuntime.nodes) {
  1363. var node = gltfRuntime.nodes[nde];
  1364. if (node.babylonNode instanceof BABYLON.AbstractMesh) {
  1365. meshes.push(node.babylonNode);
  1366. }
  1367. }
  1368. for (var skl in gltfRuntime.skins) {
  1369. var skin = gltfRuntime.skins[skl];
  1370. if (skin.babylonSkeleton instanceof BABYLON.Skeleton) {
  1371. skeletons.push(skin.babylonSkeleton);
  1372. }
  1373. }
  1374. // Load shaders and then buffers
  1375. _this._loadShadersAsync(gltfRuntime, function () {
  1376. importMaterials(gltfRuntime);
  1377. _this._loadBuffersAsync(gltfRuntime, function () {
  1378. onBuffersLoaded(gltfRuntime);
  1379. });
  1380. });
  1381. if (onSuccess) {
  1382. onSuccess(meshes, null, skeletons);
  1383. }
  1384. }, onError);
  1385. return true;
  1386. };
  1387. /**
  1388. * Load scene
  1389. */
  1390. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onSuccess, onError) {
  1391. var _this = this;
  1392. BABYLON.GLTFFileLoaderExtension.loadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
  1393. // Create nodes
  1394. _this._createNodes(gltfRuntime);
  1395. // Load shaders and then buffers
  1396. _this._loadShadersAsync(gltfRuntime, function () {
  1397. importMaterials(gltfRuntime);
  1398. _this._loadBuffersAsync(gltfRuntime, function () {
  1399. onBuffersLoaded(gltfRuntime);
  1400. });
  1401. });
  1402. onSuccess();
  1403. }, onError);
  1404. return true;
  1405. };
  1406. GLTFFileLoader.prototype._loadShadersAsync = function (gltfRuntime, onload) {
  1407. var hasShaders = false;
  1408. var processShader = function (sha, shader) {
  1409. BABYLON.GLTFFileLoaderExtension.loadShaderDataAsync(gltfRuntime, sha, function (shaderData) {
  1410. gltfRuntime.loadedShaderCount++;
  1411. BABYLON.Effect.ShadersStore[sha + (shader.type === BABYLON.EShaderType.VERTEX ? "VertexShader" : "PixelShader")] = shaderData;
  1412. if (gltfRuntime.loadedShaderCount === gltfRuntime.shaderscount) {
  1413. onload();
  1414. }
  1415. }, function () {
  1416. BABYLON.Tools.Error("Error when loading shader program named " + sha + " located at " + shader.uri);
  1417. });
  1418. };
  1419. for (var sha in gltfRuntime.shaders) {
  1420. hasShaders = true;
  1421. var shader = gltfRuntime.shaders[sha];
  1422. if (shader) {
  1423. processShader.bind(this, sha, shader)();
  1424. }
  1425. else {
  1426. BABYLON.Tools.Error("No shader named: " + sha);
  1427. }
  1428. }
  1429. if (!hasShaders) {
  1430. onload();
  1431. }
  1432. };
  1433. ;
  1434. GLTFFileLoader.prototype._loadBuffersAsync = function (gltfRuntime, onload) {
  1435. var hasBuffers = false;
  1436. var processBuffer = function (buf, buffer) {
  1437. BABYLON.GLTFFileLoaderExtension.loadBufferAsync(gltfRuntime, buf, function (bufferView) {
  1438. gltfRuntime.loadedBufferCount++;
  1439. if (bufferView.byteLength != gltfRuntime.buffers[buf].byteLength) {
  1440. BABYLON.Tools.Error("Buffer named " + buf + " is length " + bufferView.byteLength + ". Expected: " + buffer.byteLength); // Improve error message
  1441. }
  1442. gltfRuntime.loadedBufferViews[buf] = bufferView;
  1443. if (gltfRuntime.loadedBufferCount === gltfRuntime.buffersCount) {
  1444. onload();
  1445. }
  1446. }, function () {
  1447. BABYLON.Tools.Error("Error when loading buffer named " + buf + " located at " + buffer.uri);
  1448. });
  1449. };
  1450. for (var buf in gltfRuntime.buffers) {
  1451. hasBuffers = true;
  1452. var buffer = gltfRuntime.buffers[buf];
  1453. if (buffer) {
  1454. processBuffer.bind(this, buf, buffer)();
  1455. }
  1456. else {
  1457. BABYLON.Tools.Error("No buffer named: " + buf);
  1458. }
  1459. }
  1460. if (!hasBuffers) {
  1461. onload();
  1462. }
  1463. };
  1464. // Creates nodes before loading buffers and shaders
  1465. GLTFFileLoader.prototype._createNodes = function (gltfRuntime) {
  1466. var currentScene = gltfRuntime.currentScene;
  1467. for (var i = 0; i < currentScene.nodes.length; i++) {
  1468. traverseNodes(gltfRuntime, currentScene.nodes[i], null);
  1469. }
  1470. };
  1471. /**
  1472. * Private members
  1473. */
  1474. // None
  1475. /**
  1476. * Static members
  1477. */
  1478. GLTFFileLoader.MakeYUP = false;
  1479. GLTFFileLoader.HomogeneousCoordinates = false;
  1480. GLTFFileLoader.Extensions = {};
  1481. return GLTFFileLoader;
  1482. }());
  1483. BABYLON.GLTFFileLoader = GLTFFileLoader;
  1484. ;
  1485. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  1486. })(BABYLON || (BABYLON = {}));
  1487. var BABYLON;
  1488. (function (BABYLON) {
  1489. /**
  1490. * Utils functions for GLTF
  1491. */
  1492. var GLTFUtils = (function () {
  1493. function GLTFUtils() {
  1494. }
  1495. /**
  1496. * Sets the given "parameter" matrix
  1497. * @param scene: the {BABYLON.Scene} object
  1498. * @param source: the source node where to pick the matrix
  1499. * @param parameter: the GLTF technique parameter
  1500. * @param uniformName: the name of the shader's uniform
  1501. * @param shaderMaterial: the shader material
  1502. */
  1503. GLTFUtils.SetMatrix = function (scene, source, parameter, uniformName, shaderMaterial) {
  1504. var mat = null;
  1505. if (parameter.semantic === "MODEL") {
  1506. mat = source.getWorldMatrix();
  1507. }
  1508. else if (parameter.semantic === "PROJECTION") {
  1509. mat = scene.getProjectionMatrix();
  1510. }
  1511. else if (parameter.semantic === "VIEW") {
  1512. mat = scene.getViewMatrix();
  1513. }
  1514. else if (parameter.semantic === "MODELVIEWINVERSETRANSPOSE") {
  1515. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().multiply(scene.getViewMatrix()).invert());
  1516. }
  1517. else if (parameter.semantic === "MODELVIEW") {
  1518. mat = source.getWorldMatrix().multiply(scene.getViewMatrix());
  1519. }
  1520. else if (parameter.semantic === "MODELVIEWPROJECTION") {
  1521. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix());
  1522. }
  1523. else if (parameter.semantic === "MODELINVERSE") {
  1524. mat = source.getWorldMatrix().invert();
  1525. }
  1526. else if (parameter.semantic === "VIEWINVERSE") {
  1527. mat = scene.getViewMatrix().invert();
  1528. }
  1529. else if (parameter.semantic === "PROJECTIONINVERSE") {
  1530. mat = scene.getProjectionMatrix().invert();
  1531. }
  1532. else if (parameter.semantic === "MODELVIEWINVERSE") {
  1533. mat = source.getWorldMatrix().multiply(scene.getViewMatrix()).invert();
  1534. }
  1535. else if (parameter.semantic === "MODELVIEWPROJECTIONINVERSE") {
  1536. mat = source.getWorldMatrix().multiply(scene.getTransformMatrix()).invert();
  1537. }
  1538. else if (parameter.semantic === "MODELINVERSETRANSPOSE") {
  1539. mat = BABYLON.Matrix.Transpose(source.getWorldMatrix().invert());
  1540. }
  1541. else {
  1542. debugger;
  1543. }
  1544. switch (parameter.type) {
  1545. case BABYLON.EParameterType.FLOAT_MAT2:
  1546. shaderMaterial.setMatrix2x2(uniformName, BABYLON.Matrix.GetAsMatrix2x2(mat));
  1547. break;
  1548. case BABYLON.EParameterType.FLOAT_MAT3:
  1549. shaderMaterial.setMatrix3x3(uniformName, BABYLON.Matrix.GetAsMatrix3x3(mat));
  1550. break;
  1551. case BABYLON.EParameterType.FLOAT_MAT4:
  1552. shaderMaterial.setMatrix(uniformName, mat);
  1553. break;
  1554. default: break;
  1555. }
  1556. };
  1557. /**
  1558. * Sets the given "parameter" matrix
  1559. * @param shaderMaterial: the shader material
  1560. * @param uniform: the name of the shader's uniform
  1561. * @param value: the value of the uniform
  1562. * @param type: the uniform's type (EParameterType FLOAT, VEC2, VEC3 or VEC4)
  1563. */
  1564. GLTFUtils.SetUniform = function (shaderMaterial, uniform, value, type) {
  1565. switch (type) {
  1566. case BABYLON.EParameterType.FLOAT:
  1567. shaderMaterial.setFloat(uniform, value);
  1568. return true;
  1569. case BABYLON.EParameterType.FLOAT_VEC2:
  1570. shaderMaterial.setVector2(uniform, BABYLON.Vector2.FromArray(value));
  1571. return true;
  1572. case BABYLON.EParameterType.FLOAT_VEC3:
  1573. shaderMaterial.setVector3(uniform, BABYLON.Vector3.FromArray(value));
  1574. return true;
  1575. case BABYLON.EParameterType.FLOAT_VEC4:
  1576. shaderMaterial.setVector4(uniform, BABYLON.Vector4.FromArray(value));
  1577. return true;
  1578. default: return false;
  1579. }
  1580. };
  1581. /**
  1582. * If the uri is a base64 string
  1583. * @param uri: the uri to test
  1584. */
  1585. GLTFUtils.IsBase64 = function (uri) {
  1586. return uri.length < 5 ? false : uri.substr(0, 5) === "data:";
  1587. };
  1588. /**
  1589. * Returns the wrap mode of the texture
  1590. * @param mode: the mode value
  1591. */
  1592. GLTFUtils.GetWrapMode = function (mode) {
  1593. switch (mode) {
  1594. case BABYLON.ETextureWrapMode.CLAMP_TO_EDGE: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  1595. case BABYLON.ETextureWrapMode.MIRRORED_REPEAT: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1596. case BABYLON.ETextureWrapMode.REPEAT: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1597. default: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1598. }
  1599. };
  1600. /**
  1601. * Returns the byte stride giving an accessor
  1602. * @param accessor: the GLTF accessor objet
  1603. */
  1604. GLTFUtils.GetByteStrideFromType = function (accessor) {
  1605. // Needs this function since "byteStride" isn't requiered in glTF format
  1606. var type = accessor.type;
  1607. switch (type) {
  1608. case "VEC2": return 2;
  1609. case "VEC3": return 3;
  1610. case "VEC4": return 4;
  1611. case "MAT2": return 4;
  1612. case "MAT3": return 9;
  1613. case "MAT4": return 16;
  1614. default: return 1;
  1615. }
  1616. };
  1617. /**
  1618. * Returns the texture filter mode giving a mode value
  1619. * @param mode: the filter mode value
  1620. */
  1621. GLTFUtils.GetTextureFilterMode = function (mode) {
  1622. switch (mode) {
  1623. case BABYLON.ETextureFilterType.LINEAR:
  1624. case BABYLON.ETextureFilterType.LINEAR_MIPMAP_NEAREST:
  1625. case BABYLON.ETextureFilterType.LINEAR_MIPMAP_LINEAR: return BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  1626. case BABYLON.ETextureFilterType.NEAREST:
  1627. case BABYLON.ETextureFilterType.NEAREST_MIPMAP_NEAREST: return BABYLON.Texture.NEAREST_SAMPLINGMODE;
  1628. default: return BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  1629. }
  1630. };
  1631. GLTFUtils.GetBufferFromBufferView = function (gltfRuntime, bufferView, byteOffset, byteLength, componentType) {
  1632. var byteOffset = bufferView.byteOffset + byteOffset;
  1633. var loadedBufferView = gltfRuntime.loadedBufferViews[bufferView.buffer];
  1634. if (byteOffset + byteLength > loadedBufferView.byteLength) {
  1635. throw new Error("Buffer access is out of range");
  1636. }
  1637. var buffer = loadedBufferView.buffer;
  1638. byteOffset += loadedBufferView.byteOffset;
  1639. switch (componentType) {
  1640. case BABYLON.EComponentType.BYTE: return new Int8Array(buffer, byteOffset, byteLength);
  1641. case BABYLON.EComponentType.UNSIGNED_BYTE: return new Uint8Array(buffer, byteOffset, byteLength);
  1642. case BABYLON.EComponentType.SHORT: return new Int16Array(buffer, byteOffset, byteLength);
  1643. case BABYLON.EComponentType.UNSIGNED_SHORT: return new Uint16Array(buffer, byteOffset, byteLength);
  1644. default: return new Float32Array(buffer, byteOffset, byteLength);
  1645. }
  1646. };
  1647. /**
  1648. * Returns a buffer from its accessor
  1649. * @param gltfRuntime: the GLTF runtime
  1650. * @param accessor: the GLTF accessor
  1651. */
  1652. GLTFUtils.GetBufferFromAccessor = function (gltfRuntime, accessor) {
  1653. var bufferView = gltfRuntime.bufferViews[accessor.bufferView];
  1654. var byteLength = accessor.count * GLTFUtils.GetByteStrideFromType(accessor);
  1655. return GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, accessor.byteOffset, byteLength, accessor.componentType);
  1656. };
  1657. /**
  1658. * Decodes a buffer view into a string
  1659. * @param view: the buffer view
  1660. */
  1661. GLTFUtils.DecodeBufferToText = function (view) {
  1662. var result = "";
  1663. var length = view.byteLength;
  1664. for (var i = 0; i < length; ++i) {
  1665. result += String.fromCharCode(view[i]);
  1666. }
  1667. return result;
  1668. };
  1669. return GLTFUtils;
  1670. }());
  1671. BABYLON.GLTFUtils = GLTFUtils;
  1672. })(BABYLON || (BABYLON = {}));
  1673. var BABYLON;
  1674. (function (BABYLON) {
  1675. var GLTFFileLoaderExtension = (function () {
  1676. function GLTFFileLoaderExtension(name) {
  1677. this._name = name;
  1678. }
  1679. Object.defineProperty(GLTFFileLoaderExtension.prototype, "name", {
  1680. get: function () {
  1681. return this._name;
  1682. },
  1683. enumerable: true,
  1684. configurable: true
  1685. });
  1686. /**
  1687. * Defines an override for loading the runtime
  1688. * Return true to stop further extensions from loading the runtime
  1689. */
  1690. GLTFFileLoaderExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  1691. return false;
  1692. };
  1693. /**
  1694. * Defines an override for loading buffers
  1695. * Return true to stop further extensions from loading this buffer
  1696. */
  1697. GLTFFileLoaderExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1698. return false;
  1699. };
  1700. /**
  1701. * Defines an override for loading textures
  1702. * Return true to stop further extensions from loading this texture
  1703. */
  1704. GLTFFileLoaderExtension.prototype.loadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  1705. return false;
  1706. };
  1707. /**
  1708. * Defines an override for loading shader data
  1709. * Return true to stop further extensions from loading this shader data
  1710. */
  1711. GLTFFileLoaderExtension.prototype.loadShaderDataAsync = function (gltfRuntime, id, onSuccess, onError) {
  1712. return false;
  1713. };
  1714. /**
  1715. * Defines an override for loading materials
  1716. * Return true to stop further extensions from loading this material
  1717. */
  1718. GLTFFileLoaderExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  1719. return false;
  1720. };
  1721. // ---------
  1722. // Utilities
  1723. // ---------
  1724. GLTFFileLoaderExtension.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  1725. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1726. return loaderExtension.loadRuntimeAsync(scene, data, rootUrl, onSuccess, onError);
  1727. }, function () {
  1728. onSuccess(BABYLON.GLTFFileLoaderBase.createRuntime(JSON.parse(data), scene, rootUrl));
  1729. });
  1730. };
  1731. GLTFFileLoaderExtension.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1732. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1733. return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
  1734. }, function () {
  1735. BABYLON.GLTFFileLoaderBase.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
  1736. });
  1737. };
  1738. GLTFFileLoaderExtension.loadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  1739. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1740. return loaderExtension.loadTextureAsync(gltfRuntime, id, onSuccess, onError);
  1741. }, function () {
  1742. BABYLON.GLTFFileLoaderBase.loadTextureAsync(gltfRuntime, id, onSuccess, onError);
  1743. });
  1744. };
  1745. GLTFFileLoaderExtension.loadShaderDataAsync = function (gltfRuntime, id, onSuccess, onError) {
  1746. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1747. return loaderExtension.loadShaderDataAsync(gltfRuntime, id, onSuccess, onError);
  1748. }, function () {
  1749. BABYLON.GLTFFileLoaderBase.loadShaderDataAsync(gltfRuntime, id, onSuccess, onError);
  1750. });
  1751. };
  1752. GLTFFileLoaderExtension.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) {
  1753. GLTFFileLoaderExtension.applyExtensions(function (loaderExtension) {
  1754. return loaderExtension.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  1755. }, function () {
  1756. BABYLON.GLTFFileLoaderBase.loadMaterialAsync(gltfRuntime, id, onSuccess, onError);
  1757. });
  1758. };
  1759. GLTFFileLoaderExtension.applyExtensions = function (func, defaultFunc) {
  1760. for (var extensionName in BABYLON.GLTFFileLoader.Extensions) {
  1761. var loaderExtension = BABYLON.GLTFFileLoader.Extensions[extensionName];
  1762. if (func(loaderExtension)) {
  1763. return;
  1764. }
  1765. }
  1766. defaultFunc();
  1767. };
  1768. return GLTFFileLoaderExtension;
  1769. }());
  1770. BABYLON.GLTFFileLoaderExtension = GLTFFileLoaderExtension;
  1771. })(BABYLON || (BABYLON = {}));
  1772. var __extends = (this && this.__extends) || function (d, b) {
  1773. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  1774. function __() { this.constructor = d; }
  1775. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1776. };
  1777. var BABYLON;
  1778. (function (BABYLON) {
  1779. var BinaryExtensionBufferName = "binary_glTF";
  1780. var EContentFormat;
  1781. (function (EContentFormat) {
  1782. EContentFormat[EContentFormat["JSON"] = 0] = "JSON";
  1783. })(EContentFormat || (EContentFormat = {}));
  1784. ;
  1785. ;
  1786. ;
  1787. ;
  1788. var GLTFBinaryExtension = (function (_super) {
  1789. __extends(GLTFBinaryExtension, _super);
  1790. function GLTFBinaryExtension() {
  1791. _super.call(this, "KHR_binary_glTF");
  1792. }
  1793. GLTFBinaryExtension.prototype.loadRuntimeAsync = function (scene, data, rootUrl, onSuccess, onError) {
  1794. if (!(data instanceof ArrayBuffer)) {
  1795. return false;
  1796. }
  1797. var binary = this._parseBinary(data);
  1798. if (!binary) {
  1799. onError();
  1800. return true;
  1801. }
  1802. var gltfRuntime = BABYLON.GLTFFileLoaderBase.createRuntime(binary.content, scene, rootUrl);
  1803. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  1804. BABYLON.Tools.Warn("glTF binary file does not have " + this.name + " specified in extensionsUsed");
  1805. gltfRuntime.extensionsUsed.push(this.name);
  1806. }
  1807. gltfRuntime.loadedBufferViews[BinaryExtensionBufferName] = binary.body;
  1808. onSuccess(gltfRuntime);
  1809. return true;
  1810. };
  1811. GLTFBinaryExtension.prototype.loadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
  1812. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  1813. return false;
  1814. }
  1815. if (id !== BinaryExtensionBufferName) {
  1816. return false;
  1817. }
  1818. onSuccess(gltfRuntime.loadedBufferViews[BinaryExtensionBufferName]);
  1819. return true;
  1820. };
  1821. GLTFBinaryExtension.prototype.loadTextureAsync = function (gltfRuntime, id, onSuccess, onError) {
  1822. var texture = gltfRuntime.textures[id];
  1823. var source = gltfRuntime.images[texture.source];
  1824. if (!source.extensions || !(this.name in source.extensions)) {
  1825. return false;
  1826. }
  1827. var sourceExt = source.extensions[this.name];
  1828. var sampler = gltfRuntime.samplers[texture.sampler];
  1829. var createMipMaps = (sampler.minFilter === BABYLON.ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  1830. (sampler.minFilter === BABYLON.ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  1831. (sampler.minFilter === BABYLON.ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  1832. (sampler.minFilter === BABYLON.ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  1833. var samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE;
  1834. var bufferView = gltfRuntime.bufferViews[sourceExt.bufferView];
  1835. var imageBytes = BABYLON.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, BABYLON.EComponentType.UNSIGNED_BYTE);
  1836. var blob = new Blob([imageBytes], { type: sourceExt.mimeType });
  1837. var blobURL = URL.createObjectURL(blob);
  1838. var revokeBlobURL = function () { return URL.revokeObjectURL(blobURL); };
  1839. var newTexture = new BABYLON.Texture(blobURL, gltfRuntime.scene, !createMipMaps, true, samplingMode, revokeBlobURL, revokeBlobURL);
  1840. newTexture.wrapU = BABYLON.GLTFUtils.GetWrapMode(sampler.wrapS);
  1841. newTexture.wrapV = BABYLON.GLTFUtils.GetWrapMode(sampler.wrapT);
  1842. newTexture.name = name;
  1843. texture.babylonTexture = newTexture;
  1844. onSuccess(newTexture);
  1845. return true;
  1846. };
  1847. GLTFBinaryExtension.prototype.loadShaderDataAsync = function (gltfRuntime, id, onSuccess, onError) {
  1848. var shader = gltfRuntime.shaders[id];
  1849. if (!shader.extensions || !(this.name in shader.extensions)) {
  1850. return false;
  1851. }
  1852. var binaryExtensionShader = shader.extensions[this.name];
  1853. var bufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
  1854. var shaderBytes = BABYLON.GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, BABYLON.EComponentType.UNSIGNED_BYTE);
  1855. var shaderData = BABYLON.GLTFUtils.DecodeBufferToText(shaderBytes);
  1856. onSuccess(shaderData);
  1857. return true;
  1858. };
  1859. // Parses a glTF binary array buffer into its content and body
  1860. GLTFBinaryExtension.prototype._parseBinary = function (data) {
  1861. var binaryReader = new BinaryReader(data);
  1862. var magic = BABYLON.GLTFUtils.DecodeBufferToText(binaryReader.getUint8Array(4));
  1863. if (magic != "glTF") {
  1864. BABYLON.Tools.Error("Unexpected magic: " + magic);
  1865. return null;
  1866. }
  1867. var version = binaryReader.getUint32();
  1868. if (version != 1) {
  1869. BABYLON.Tools.Error("Unsupported version: " + version);
  1870. return null;
  1871. }
  1872. var length = binaryReader.getUint32();
  1873. if (length != data.byteLength) {
  1874. BABYLON.Tools.Error("Length in header does not match actual data length: " + length + " != " + data.byteLength);
  1875. return null;
  1876. }
  1877. var contentLength = binaryReader.getUint32();
  1878. var contentFormat = binaryReader.getUint32();
  1879. var content;
  1880. switch (contentFormat) {
  1881. case EContentFormat.JSON:
  1882. var jsonText = BABYLON.GLTFUtils.DecodeBufferToText(binaryReader.getUint8Array(contentLength));
  1883. content = JSON.parse(jsonText);
  1884. break;
  1885. default:
  1886. BABYLON.Tools.Error("Unexpected content format: " + contentFormat);
  1887. return null;
  1888. }
  1889. var body = binaryReader.getUint8Array();
  1890. return {
  1891. content: content,
  1892. body: body
  1893. };
  1894. };
  1895. ;
  1896. return GLTFBinaryExtension;
  1897. }(BABYLON.GLTFFileLoaderExtension));
  1898. BABYLON.GLTFBinaryExtension = GLTFBinaryExtension;
  1899. var BinaryReader = (function () {
  1900. function BinaryReader(arrayBuffer) {
  1901. this._arrayBuffer = arrayBuffer;
  1902. this._dataView = new DataView(arrayBuffer);
  1903. this._byteOffset = 0;
  1904. }
  1905. BinaryReader.prototype.getUint32 = function () {
  1906. var value = this._dataView.getUint32(this._byteOffset, true);
  1907. this._byteOffset += 4;
  1908. return value;
  1909. };
  1910. BinaryReader.prototype.getUint8Array = function (length) {
  1911. if (!length) {
  1912. length = this._arrayBuffer.byteLength - this._byteOffset;
  1913. }
  1914. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  1915. this._byteOffset += length;
  1916. return value;
  1917. };
  1918. return BinaryReader;
  1919. }());
  1920. BABYLON.GLTFFileLoader.RegisterExtension(new GLTFBinaryExtension());
  1921. })(BABYLON || (BABYLON = {}));