babylon.glTF2FileLoader.js 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427
  1. "use strict";
  2. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  3. var BABYLON;
  4. (function (BABYLON) {
  5. var GLTFLoaderCoordinateSystemMode;
  6. (function (GLTFLoaderCoordinateSystemMode) {
  7. /**
  8. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  9. */
  10. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
  11. /**
  12. * Sets the useRightHandedSystem flag on the scene.
  13. */
  14. GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
  15. })(GLTFLoaderCoordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode || (BABYLON.GLTFLoaderCoordinateSystemMode = {}));
  16. var GLTFLoaderAnimationStartMode;
  17. (function (GLTFLoaderAnimationStartMode) {
  18. /**
  19. * No animation will start.
  20. */
  21. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
  22. /**
  23. * The first animation will start.
  24. */
  25. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
  26. /**
  27. * All animations will start.
  28. */
  29. GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
  30. })(GLTFLoaderAnimationStartMode = BABYLON.GLTFLoaderAnimationStartMode || (BABYLON.GLTFLoaderAnimationStartMode = {}));
  31. var GLTFLoaderState;
  32. (function (GLTFLoaderState) {
  33. GLTFLoaderState[GLTFLoaderState["Loading"] = 0] = "Loading";
  34. GLTFLoaderState[GLTFLoaderState["Ready"] = 1] = "Ready";
  35. GLTFLoaderState[GLTFLoaderState["Complete"] = 2] = "Complete";
  36. })(GLTFLoaderState = BABYLON.GLTFLoaderState || (BABYLON.GLTFLoaderState = {}));
  37. var GLTFFileLoader = /** @class */ (function () {
  38. function GLTFFileLoader() {
  39. // #region Common options
  40. /**
  41. * Raised when the asset has been parsed.
  42. * The data.json property stores the glTF JSON.
  43. * The data.bin property stores the BIN chunk from a glTF binary or null if the input is not a glTF binary.
  44. */
  45. this.onParsedObservable = new BABYLON.Observable();
  46. // #endregion
  47. // #region V2 options
  48. /**
  49. * The coordinate system mode (AUTO, FORCE_RIGHT_HANDED).
  50. */
  51. this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
  52. /**
  53. * The animation start mode (NONE, FIRST, ALL).
  54. */
  55. this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
  56. /**
  57. * Set to true to compile materials before raising the success callback.
  58. */
  59. this.compileMaterials = false;
  60. /**
  61. * Set to true to also compile materials with clip planes.
  62. */
  63. this.useClipPlane = false;
  64. /**
  65. * Set to true to compile shadow generators before raising the success callback.
  66. */
  67. this.compileShadowGenerators = false;
  68. /**
  69. * Raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  70. */
  71. this.onMeshLoadedObservable = new BABYLON.Observable();
  72. /**
  73. * Raised when the loader creates a texture after parsing the glTF properties of the texture.
  74. */
  75. this.onTextureLoadedObservable = new BABYLON.Observable();
  76. /**
  77. * Raised when the loader creates a material after parsing the glTF properties of the material.
  78. */
  79. this.onMaterialLoadedObservable = new BABYLON.Observable();
  80. /**
  81. * Raised when the loader creates an animation group after parsing the glTF properties of the animation.
  82. */
  83. this.onAnimationGroupLoadedObservable = new BABYLON.Observable();
  84. /**
  85. * Raised when the asset is completely loaded, immediately before the loader is disposed.
  86. * For assets with LODs, raised when all of the LODs are complete.
  87. * For assets without LODs, raised when the model is complete, immediately after onSuccess.
  88. */
  89. this.onCompleteObservable = new BABYLON.Observable();
  90. /**
  91. * Raised when the loader is disposed.
  92. */
  93. this.onDisposeObservable = new BABYLON.Observable();
  94. /**
  95. * Raised after a loader extension is created.
  96. * Set additional options for a loader extension in this event.
  97. */
  98. this.onExtensionLoadedObservable = new BABYLON.Observable();
  99. // #endregion
  100. this._loader = null;
  101. this.name = "gltf";
  102. this.extensions = {
  103. ".gltf": { isBinary: false },
  104. ".glb": { isBinary: true }
  105. };
  106. }
  107. Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
  108. set: function (callback) {
  109. if (this._onParsedObserver) {
  110. this.onParsedObservable.remove(this._onParsedObserver);
  111. }
  112. this._onParsedObserver = this.onParsedObservable.add(callback);
  113. },
  114. enumerable: true,
  115. configurable: true
  116. });
  117. Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
  118. set: function (callback) {
  119. if (this._onMeshLoadedObserver) {
  120. this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
  121. }
  122. this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
  123. },
  124. enumerable: true,
  125. configurable: true
  126. });
  127. Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
  128. set: function (callback) {
  129. if (this._onTextureLoadedObserver) {
  130. this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
  131. }
  132. this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
  133. },
  134. enumerable: true,
  135. configurable: true
  136. });
  137. Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
  138. set: function (callback) {
  139. if (this._onMaterialLoadedObserver) {
  140. this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
  141. }
  142. this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
  143. },
  144. enumerable: true,
  145. configurable: true
  146. });
  147. Object.defineProperty(GLTFFileLoader.prototype, "onAnimationGroupLoaded", {
  148. set: function (callback) {
  149. if (this._onAnimationGroupLoadedObserver) {
  150. this.onAnimationGroupLoadedObservable.remove(this._onAnimationGroupLoadedObserver);
  151. }
  152. this._onAnimationGroupLoadedObserver = this.onAnimationGroupLoadedObservable.add(callback);
  153. },
  154. enumerable: true,
  155. configurable: true
  156. });
  157. Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
  158. set: function (callback) {
  159. if (this._onCompleteObserver) {
  160. this.onCompleteObservable.remove(this._onCompleteObserver);
  161. }
  162. this._onCompleteObserver = this.onCompleteObservable.add(callback);
  163. },
  164. enumerable: true,
  165. configurable: true
  166. });
  167. Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
  168. set: function (callback) {
  169. if (this._onDisposeObserver) {
  170. this.onDisposeObservable.remove(this._onDisposeObserver);
  171. }
  172. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  173. },
  174. enumerable: true,
  175. configurable: true
  176. });
  177. Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
  178. set: function (callback) {
  179. if (this._onExtensionLoadedObserver) {
  180. this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
  181. }
  182. this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
  183. },
  184. enumerable: true,
  185. configurable: true
  186. });
  187. /**
  188. * Gets a promise that resolves when the asset is completely loaded.
  189. * @returns A promise that resolves when the asset is completely loaded.
  190. */
  191. GLTFFileLoader.prototype.whenCompleteAsync = function () {
  192. var _this = this;
  193. return new Promise(function (resolve) {
  194. _this.onCompleteObservable.add(function () {
  195. resolve();
  196. }, undefined, undefined, undefined, true);
  197. });
  198. };
  199. Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
  200. /**
  201. * The loader state or null if not active.
  202. */
  203. get: function () {
  204. return this._loader ? this._loader.state : null;
  205. },
  206. enumerable: true,
  207. configurable: true
  208. });
  209. /**
  210. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  211. */
  212. GLTFFileLoader.prototype.dispose = function () {
  213. if (this._loader) {
  214. this._loader.dispose();
  215. this._loader = null;
  216. }
  217. this.onMeshLoadedObservable.clear();
  218. this.onTextureLoadedObservable.clear();
  219. this.onMaterialLoadedObservable.clear();
  220. this.onDisposeObservable.notifyObservers(this);
  221. this.onDisposeObservable.clear();
  222. };
  223. GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  224. var _this = this;
  225. return Promise.resolve().then(function () {
  226. var loaderData = _this._parse(data);
  227. _this._loader = _this._getLoader(loaderData);
  228. return _this._loader.importMeshAsync(meshesNames, scene, loaderData, rootUrl, onProgress);
  229. });
  230. };
  231. GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  232. var _this = this;
  233. return Promise.resolve().then(function () {
  234. var loaderData = _this._parse(data);
  235. _this._loader = _this._getLoader(loaderData);
  236. return _this._loader.loadAsync(scene, loaderData, rootUrl, onProgress);
  237. });
  238. };
  239. GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress) {
  240. var _this = this;
  241. return Promise.resolve().then(function () {
  242. var loaderData = _this._parse(data);
  243. _this._loader = _this._getLoader(loaderData);
  244. return _this._loader.importMeshAsync(null, scene, loaderData, rootUrl, onProgress).then(function (result) {
  245. var container = new BABYLON.AssetContainer(scene);
  246. Array.prototype.push.apply(container.meshes, result.meshes);
  247. Array.prototype.push.apply(container.particleSystems, result.particleSystems);
  248. Array.prototype.push.apply(container.skeletons, result.skeletons);
  249. container.removeAllFromScene();
  250. return container;
  251. });
  252. });
  253. };
  254. GLTFFileLoader.prototype.canDirectLoad = function (data) {
  255. return ((data.indexOf("scene") !== -1) && (data.indexOf("node") !== -1));
  256. };
  257. GLTFFileLoader.prototype.createPlugin = function () {
  258. return new GLTFFileLoader();
  259. };
  260. GLTFFileLoader.prototype._parse = function (data) {
  261. var parsedData;
  262. if (data instanceof ArrayBuffer) {
  263. parsedData = GLTFFileLoader._parseBinary(data);
  264. }
  265. else {
  266. parsedData = {
  267. json: JSON.parse(data),
  268. bin: null
  269. };
  270. }
  271. this.onParsedObservable.notifyObservers(parsedData);
  272. this.onParsedObservable.clear();
  273. return parsedData;
  274. };
  275. GLTFFileLoader.prototype._getLoader = function (loaderData) {
  276. var _this = this;
  277. var loaderVersion = { major: 2, minor: 0 };
  278. var asset = loaderData.json.asset || {};
  279. var version = GLTFFileLoader._parseVersion(asset.version);
  280. if (!version) {
  281. throw new Error("Invalid version: " + asset.version);
  282. }
  283. if (asset.minVersion !== undefined) {
  284. var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
  285. if (!minVersion) {
  286. throw new Error("Invalid minimum version: " + asset.minVersion);
  287. }
  288. if (GLTFFileLoader._compareVersion(minVersion, loaderVersion) > 0) {
  289. throw new Error("Incompatible minimum version: " + asset.minVersion);
  290. }
  291. }
  292. var createLoaders = {
  293. 1: GLTFFileLoader.CreateGLTFLoaderV1,
  294. 2: GLTFFileLoader.CreateGLTFLoaderV2
  295. };
  296. var createLoader = createLoaders[version.major];
  297. if (!createLoader) {
  298. throw new Error("Unsupported version: " + asset.version);
  299. }
  300. var loader = createLoader();
  301. loader.coordinateSystemMode = this.coordinateSystemMode;
  302. loader.animationStartMode = this.animationStartMode;
  303. loader.compileMaterials = this.compileMaterials;
  304. loader.useClipPlane = this.useClipPlane;
  305. loader.compileShadowGenerators = this.compileShadowGenerators;
  306. loader.onMeshLoadedObservable.add(function (mesh) { return _this.onMeshLoadedObservable.notifyObservers(mesh); });
  307. loader.onTextureLoadedObservable.add(function (texture) { return _this.onTextureLoadedObservable.notifyObservers(texture); });
  308. loader.onMaterialLoadedObservable.add(function (material) { return _this.onMaterialLoadedObservable.notifyObservers(material); });
  309. loader.onExtensionLoadedObservable.add(function (extension) { return _this.onExtensionLoadedObservable.notifyObservers(extension); });
  310. loader.onCompleteObservable.add(function () {
  311. _this.onMeshLoadedObservable.clear();
  312. _this.onTextureLoadedObservable.clear();
  313. _this.onMaterialLoadedObservable.clear();
  314. _this.onCompleteObservable.notifyObservers(_this);
  315. _this.onCompleteObservable.clear();
  316. });
  317. return loader;
  318. };
  319. GLTFFileLoader._parseBinary = function (data) {
  320. var Binary = {
  321. Magic: 0x46546C67
  322. };
  323. var binaryReader = new BinaryReader(data);
  324. var magic = binaryReader.readUint32();
  325. if (magic !== Binary.Magic) {
  326. throw new Error("Unexpected magic: " + magic);
  327. }
  328. var version = binaryReader.readUint32();
  329. switch (version) {
  330. case 1: return GLTFFileLoader._parseV1(binaryReader);
  331. case 2: return GLTFFileLoader._parseV2(binaryReader);
  332. }
  333. throw new Error("Unsupported version: " + version);
  334. };
  335. GLTFFileLoader._parseV1 = function (binaryReader) {
  336. var ContentFormat = {
  337. JSON: 0
  338. };
  339. var length = binaryReader.readUint32();
  340. if (length != binaryReader.getLength()) {
  341. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  342. }
  343. var contentLength = binaryReader.readUint32();
  344. var contentFormat = binaryReader.readUint32();
  345. var content;
  346. switch (contentFormat) {
  347. case ContentFormat.JSON: {
  348. content = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(contentLength)));
  349. break;
  350. }
  351. default: {
  352. throw new Error("Unexpected content format: " + contentFormat);
  353. }
  354. }
  355. var bytesRemaining = binaryReader.getLength() - binaryReader.getPosition();
  356. var body = binaryReader.readUint8Array(bytesRemaining);
  357. return {
  358. json: content,
  359. bin: body
  360. };
  361. };
  362. GLTFFileLoader._parseV2 = function (binaryReader) {
  363. var ChunkFormat = {
  364. JSON: 0x4E4F534A,
  365. BIN: 0x004E4942
  366. };
  367. var length = binaryReader.readUint32();
  368. if (length !== binaryReader.getLength()) {
  369. throw new Error("Length in header does not match actual data length: " + length + " != " + binaryReader.getLength());
  370. }
  371. // JSON chunk
  372. var chunkLength = binaryReader.readUint32();
  373. var chunkFormat = binaryReader.readUint32();
  374. if (chunkFormat !== ChunkFormat.JSON) {
  375. throw new Error("First chunk format is not JSON");
  376. }
  377. var json = JSON.parse(GLTFFileLoader._decodeBufferToText(binaryReader.readUint8Array(chunkLength)));
  378. // Look for BIN chunk
  379. var bin = null;
  380. while (binaryReader.getPosition() < binaryReader.getLength()) {
  381. var chunkLength_1 = binaryReader.readUint32();
  382. var chunkFormat_1 = binaryReader.readUint32();
  383. switch (chunkFormat_1) {
  384. case ChunkFormat.JSON: {
  385. throw new Error("Unexpected JSON chunk");
  386. }
  387. case ChunkFormat.BIN: {
  388. bin = binaryReader.readUint8Array(chunkLength_1);
  389. break;
  390. }
  391. default: {
  392. // ignore unrecognized chunkFormat
  393. binaryReader.skipBytes(chunkLength_1);
  394. break;
  395. }
  396. }
  397. }
  398. return {
  399. json: json,
  400. bin: bin
  401. };
  402. };
  403. GLTFFileLoader._parseVersion = function (version) {
  404. if (version === "1.0" || version === "1.0.1") {
  405. return {
  406. major: 1,
  407. minor: 0
  408. };
  409. }
  410. var match = (version + "").match(/^(\d+)\.(\d+)/);
  411. if (!match) {
  412. return null;
  413. }
  414. return {
  415. major: parseInt(match[1]),
  416. minor: parseInt(match[2])
  417. };
  418. };
  419. GLTFFileLoader._compareVersion = function (a, b) {
  420. if (a.major > b.major)
  421. return 1;
  422. if (a.major < b.major)
  423. return -1;
  424. if (a.minor > b.minor)
  425. return 1;
  426. if (a.minor < b.minor)
  427. return -1;
  428. return 0;
  429. };
  430. GLTFFileLoader._decodeBufferToText = function (buffer) {
  431. var result = "";
  432. var length = buffer.byteLength;
  433. for (var i = 0; i < length; i++) {
  434. result += String.fromCharCode(buffer[i]);
  435. }
  436. return result;
  437. };
  438. // #endregion
  439. // #region V1 options
  440. GLTFFileLoader.IncrementalLoading = true;
  441. GLTFFileLoader.HomogeneousCoordinates = false;
  442. return GLTFFileLoader;
  443. }());
  444. BABYLON.GLTFFileLoader = GLTFFileLoader;
  445. var BinaryReader = /** @class */ (function () {
  446. function BinaryReader(arrayBuffer) {
  447. this._arrayBuffer = arrayBuffer;
  448. this._dataView = new DataView(arrayBuffer);
  449. this._byteOffset = 0;
  450. }
  451. BinaryReader.prototype.getPosition = function () {
  452. return this._byteOffset;
  453. };
  454. BinaryReader.prototype.getLength = function () {
  455. return this._arrayBuffer.byteLength;
  456. };
  457. BinaryReader.prototype.readUint32 = function () {
  458. var value = this._dataView.getUint32(this._byteOffset, true);
  459. this._byteOffset += 4;
  460. return value;
  461. };
  462. BinaryReader.prototype.readUint8Array = function (length) {
  463. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  464. this._byteOffset += length;
  465. return value;
  466. };
  467. BinaryReader.prototype.skipBytes = function (length) {
  468. this._byteOffset += length;
  469. };
  470. return BinaryReader;
  471. }());
  472. if (BABYLON.SceneLoader) {
  473. BABYLON.SceneLoader.RegisterPlugin(new GLTFFileLoader());
  474. }
  475. })(BABYLON || (BABYLON = {}));
  476. //# sourceMappingURL=babylon.glTFFileLoader.js.map
  477. "use strict";
  478. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  479. var BABYLON;
  480. (function (BABYLON) {
  481. var GLTF2;
  482. (function (GLTF2) {
  483. var ArrayItem = /** @class */ (function () {
  484. function ArrayItem() {
  485. }
  486. ArrayItem.Assign = function (values) {
  487. if (values) {
  488. for (var index = 0; index < values.length; index++) {
  489. values[index]._index = index;
  490. }
  491. }
  492. };
  493. return ArrayItem;
  494. }());
  495. GLTF2.ArrayItem = ArrayItem;
  496. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  497. })(BABYLON || (BABYLON = {}));
  498. //# sourceMappingURL=babylon.glTFLoaderUtilities.js.map
  499. "use strict";
  500. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  501. /// <reference path="../../../../dist/babylon.glTF2Interface.d.ts"/>
  502. //# sourceMappingURL=babylon.glTFLoaderInterfaces.js.map
  503. "use strict";
  504. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  505. var BABYLON;
  506. (function (BABYLON) {
  507. var GLTF2;
  508. (function (GLTF2) {
  509. var GLTFLoader = /** @class */ (function () {
  510. function GLTFLoader() {
  511. this._completePromises = new Array();
  512. this._disposed = false;
  513. this._state = null;
  514. this._extensions = {};
  515. this._defaultSampler = {};
  516. this._defaultBabylonMaterials = {};
  517. this._requests = new Array();
  518. this.coordinateSystemMode = BABYLON.GLTFLoaderCoordinateSystemMode.AUTO;
  519. this.animationStartMode = BABYLON.GLTFLoaderAnimationStartMode.FIRST;
  520. this.compileMaterials = false;
  521. this.useClipPlane = false;
  522. this.compileShadowGenerators = false;
  523. this.onDisposeObservable = new BABYLON.Observable();
  524. this.onMeshLoadedObservable = new BABYLON.Observable();
  525. this.onTextureLoadedObservable = new BABYLON.Observable();
  526. this.onMaterialLoadedObservable = new BABYLON.Observable();
  527. this.onAnimationGroupLoadedObservable = new BABYLON.Observable();
  528. this.onExtensionLoadedObservable = new BABYLON.Observable();
  529. this.onCompleteObservable = new BABYLON.Observable();
  530. }
  531. GLTFLoader._Register = function (name, factory) {
  532. if (GLTFLoader._Factories[name]) {
  533. BABYLON.Tools.Error("Extension with the name '" + name + "' already exists");
  534. return;
  535. }
  536. GLTFLoader._Factories[name] = factory;
  537. // Keep the order of registration so that extensions registered first are called first.
  538. GLTFLoader._Names.push(name);
  539. };
  540. Object.defineProperty(GLTFLoader.prototype, "state", {
  541. get: function () {
  542. return this._state;
  543. },
  544. enumerable: true,
  545. configurable: true
  546. });
  547. GLTFLoader.prototype.dispose = function () {
  548. if (this._disposed) {
  549. return;
  550. }
  551. this._disposed = true;
  552. this.onDisposeObservable.notifyObservers(this);
  553. this.onDisposeObservable.clear();
  554. this._clear();
  555. };
  556. GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress) {
  557. var _this = this;
  558. return Promise.resolve().then(function () {
  559. var nodes = null;
  560. if (meshesNames) {
  561. var nodeMap_1 = {};
  562. if (_this._gltf.nodes) {
  563. for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
  564. var node = _a[_i];
  565. if (node.name) {
  566. nodeMap_1[node.name] = node;
  567. }
  568. }
  569. }
  570. var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
  571. nodes = names.map(function (name) {
  572. var node = nodeMap_1[name];
  573. if (!node) {
  574. throw new Error("Failed to find node '" + name + "'");
  575. }
  576. return node;
  577. });
  578. }
  579. return _this._loadAsync(nodes, scene, data, rootUrl, onProgress).then(function () {
  580. return {
  581. meshes: _this._getMeshes(),
  582. particleSystems: [],
  583. skeletons: _this._getSkeletons(),
  584. };
  585. });
  586. });
  587. };
  588. GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress) {
  589. return this._loadAsync(null, scene, data, rootUrl, onProgress);
  590. };
  591. GLTFLoader.prototype._loadAsync = function (nodes, scene, data, rootUrl, onProgress) {
  592. var _this = this;
  593. return Promise.resolve().then(function () {
  594. _this._loadExtensions();
  595. _this._babylonScene = scene;
  596. _this._rootUrl = rootUrl;
  597. _this._progressCallback = onProgress;
  598. _this._state = BABYLON.GLTFLoaderState.Loading;
  599. _this._loadData(data);
  600. _this._checkExtensions();
  601. var promises = new Array();
  602. if (nodes) {
  603. promises.push(_this._loadNodesAsync(nodes));
  604. }
  605. else {
  606. var scene_1 = GLTFLoader._GetProperty("#/scene", _this._gltf.scenes, _this._gltf.scene || 0);
  607. promises.push(_this._loadSceneAsync("#/scenes/" + scene_1._index, scene_1));
  608. }
  609. if (_this.compileMaterials) {
  610. promises.push(_this._compileMaterialsAsync());
  611. }
  612. if (_this.compileShadowGenerators) {
  613. promises.push(_this._compileShadowGeneratorsAsync());
  614. }
  615. var resultPromise = Promise.all(promises).then(function () {
  616. _this._state = BABYLON.GLTFLoaderState.Ready;
  617. _this._startAnimations();
  618. });
  619. resultPromise.then(function () {
  620. _this._rootBabylonMesh.setEnabled(true);
  621. BABYLON.Tools.SetImmediate(function () {
  622. if (!_this._disposed) {
  623. Promise.all(_this._completePromises).then(function () {
  624. _this._state = BABYLON.GLTFLoaderState.Complete;
  625. _this.onCompleteObservable.notifyObservers(_this);
  626. _this.onCompleteObservable.clear();
  627. _this._clear();
  628. }).catch(function (error) {
  629. BABYLON.Tools.Error("glTF Loader: " + error.message);
  630. _this._clear();
  631. });
  632. }
  633. });
  634. });
  635. return resultPromise;
  636. }).catch(function (error) {
  637. BABYLON.Tools.Error("glTF Loader: " + error.message);
  638. _this._clear();
  639. throw error;
  640. });
  641. };
  642. GLTFLoader.prototype._loadExtensions = function () {
  643. for (var _i = 0, _a = GLTFLoader._Names; _i < _a.length; _i++) {
  644. var name_1 = _a[_i];
  645. var extension = GLTFLoader._Factories[name_1](this);
  646. this._extensions[name_1] = extension;
  647. this.onExtensionLoadedObservable.notifyObservers(extension);
  648. }
  649. this.onExtensionLoadedObservable.clear();
  650. };
  651. GLTFLoader.prototype._loadData = function (data) {
  652. this._gltf = data.json;
  653. this._setupData();
  654. if (data.bin) {
  655. var buffers = this._gltf.buffers;
  656. if (buffers && buffers[0] && !buffers[0].uri) {
  657. var binaryBuffer = buffers[0];
  658. if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
  659. BABYLON.Tools.Warn("Binary buffer length (" + binaryBuffer.byteLength + ") from JSON does not match chunk length (" + data.bin.byteLength + ")");
  660. }
  661. binaryBuffer._data = Promise.resolve(data.bin);
  662. }
  663. else {
  664. BABYLON.Tools.Warn("Unexpected BIN chunk");
  665. }
  666. }
  667. };
  668. GLTFLoader.prototype._setupData = function () {
  669. GLTF2.ArrayItem.Assign(this._gltf.accessors);
  670. GLTF2.ArrayItem.Assign(this._gltf.animations);
  671. GLTF2.ArrayItem.Assign(this._gltf.buffers);
  672. GLTF2.ArrayItem.Assign(this._gltf.bufferViews);
  673. GLTF2.ArrayItem.Assign(this._gltf.cameras);
  674. GLTF2.ArrayItem.Assign(this._gltf.images);
  675. GLTF2.ArrayItem.Assign(this._gltf.materials);
  676. GLTF2.ArrayItem.Assign(this._gltf.meshes);
  677. GLTF2.ArrayItem.Assign(this._gltf.nodes);
  678. GLTF2.ArrayItem.Assign(this._gltf.samplers);
  679. GLTF2.ArrayItem.Assign(this._gltf.scenes);
  680. GLTF2.ArrayItem.Assign(this._gltf.skins);
  681. GLTF2.ArrayItem.Assign(this._gltf.textures);
  682. if (this._gltf.nodes) {
  683. var nodeParents = {};
  684. for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
  685. var node = _a[_i];
  686. if (node.children) {
  687. for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
  688. var index = _c[_b];
  689. nodeParents[index] = node._index;
  690. }
  691. }
  692. }
  693. var rootNode = this._createRootNode();
  694. for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
  695. var node = _e[_d];
  696. var parentIndex = nodeParents[node._index];
  697. node._parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
  698. }
  699. }
  700. };
  701. GLTFLoader.prototype._checkExtensions = function () {
  702. if (this._gltf.extensionsRequired) {
  703. for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
  704. var name_2 = _a[_i];
  705. var extension = this._extensions[name_2];
  706. if (!extension || !extension.enabled) {
  707. throw new Error("Require extension " + name_2 + " is not available");
  708. }
  709. }
  710. }
  711. };
  712. GLTFLoader.prototype._createRootNode = function () {
  713. this._rootBabylonMesh = new BABYLON.Mesh("__root__", this._babylonScene);
  714. this._rootBabylonMesh.setEnabled(false);
  715. var rootNode = { _babylonMesh: this._rootBabylonMesh };
  716. switch (this.coordinateSystemMode) {
  717. case BABYLON.GLTFLoaderCoordinateSystemMode.AUTO: {
  718. if (!this._babylonScene.useRightHandedSystem) {
  719. rootNode.rotation = [0, 1, 0, 0];
  720. rootNode.scale = [1, 1, -1];
  721. GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
  722. }
  723. break;
  724. }
  725. case BABYLON.GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
  726. this._babylonScene.useRightHandedSystem = true;
  727. break;
  728. }
  729. default: {
  730. throw new Error("Invalid coordinate system mode (" + this.coordinateSystemMode + ")");
  731. }
  732. }
  733. this.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
  734. return rootNode;
  735. };
  736. GLTFLoader.prototype._loadNodesAsync = function (nodes) {
  737. var promises = new Array();
  738. for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
  739. var node = nodes_1[_i];
  740. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  741. }
  742. promises.push(this._loadAnimationsAsync());
  743. return Promise.all(promises).then(function () { });
  744. };
  745. GLTFLoader.prototype._loadSceneAsync = function (context, scene) {
  746. var promise = GLTF2.GLTFLoaderExtension._LoadSceneAsync(this, context, scene);
  747. if (promise) {
  748. return promise;
  749. }
  750. var promises = new Array();
  751. for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
  752. var index = _a[_i];
  753. var node = GLTFLoader._GetProperty(context + "/nodes/" + index, this._gltf.nodes, index);
  754. promises.push(this._loadNodeAsync("#/nodes/" + node._index, node));
  755. }
  756. promises.push(this._loadAnimationsAsync());
  757. return Promise.all(promises).then(function () { });
  758. };
  759. GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
  760. if (node._primitiveBabylonMeshes) {
  761. for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
  762. var babylonMesh = _a[_i];
  763. callback(babylonMesh);
  764. }
  765. }
  766. else {
  767. callback(node._babylonMesh);
  768. }
  769. };
  770. GLTFLoader.prototype._getMeshes = function () {
  771. var meshes = new Array();
  772. // Root mesh is always first.
  773. meshes.push(this._rootBabylonMesh);
  774. var nodes = this._gltf.nodes;
  775. if (nodes) {
  776. for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
  777. var node = nodes_2[_i];
  778. if (node._babylonMesh) {
  779. meshes.push(node._babylonMesh);
  780. }
  781. if (node._primitiveBabylonMeshes) {
  782. for (var _a = 0, _b = node._primitiveBabylonMeshes; _a < _b.length; _a++) {
  783. var babylonMesh = _b[_a];
  784. meshes.push(babylonMesh);
  785. }
  786. }
  787. }
  788. }
  789. return meshes;
  790. };
  791. GLTFLoader.prototype._getSkeletons = function () {
  792. var skeletons = new Array();
  793. var skins = this._gltf.skins;
  794. if (skins) {
  795. for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
  796. var skin = skins_1[_i];
  797. if (skin._babylonSkeleton) {
  798. skeletons.push(skin._babylonSkeleton);
  799. }
  800. }
  801. }
  802. return skeletons;
  803. };
  804. GLTFLoader.prototype._startAnimations = function () {
  805. var animations = this._gltf.animations;
  806. if (!animations) {
  807. return;
  808. }
  809. switch (this.animationStartMode) {
  810. case BABYLON.GLTFLoaderAnimationStartMode.NONE: {
  811. // do nothing
  812. break;
  813. }
  814. case BABYLON.GLTFLoaderAnimationStartMode.FIRST: {
  815. var animation = animations[0];
  816. animation._babylonAnimationGroup.start(true);
  817. break;
  818. }
  819. case BABYLON.GLTFLoaderAnimationStartMode.ALL: {
  820. for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
  821. var animation = animations_1[_i];
  822. animation._babylonAnimationGroup.start(true);
  823. }
  824. break;
  825. }
  826. default: {
  827. BABYLON.Tools.Error("Invalid animation start mode (" + this.animationStartMode + ")");
  828. return;
  829. }
  830. }
  831. };
  832. GLTFLoader.prototype._loadNodeAsync = function (context, node) {
  833. var promise = GLTF2.GLTFLoaderExtension._LoadNodeAsync(this, context, node);
  834. if (promise) {
  835. return promise;
  836. }
  837. if (node._babylonMesh) {
  838. throw new Error(context + ": Invalid recursive node hierarchy");
  839. }
  840. var promises = new Array();
  841. var babylonMesh = new BABYLON.Mesh(node.name || "node" + node._index, this._babylonScene, node._parent._babylonMesh);
  842. node._babylonMesh = babylonMesh;
  843. node._babylonAnimationTargets = node._babylonAnimationTargets || [];
  844. node._babylonAnimationTargets.push(babylonMesh);
  845. GLTFLoader._LoadTransform(node, babylonMesh);
  846. if (node.mesh != undefined) {
  847. var mesh = GLTFLoader._GetProperty(context + "/mesh", this._gltf.meshes, node.mesh);
  848. promises.push(this._loadMeshAsync("#/meshes/" + mesh._index, node, mesh, babylonMesh));
  849. }
  850. if (node.children) {
  851. for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
  852. var index = _a[_i];
  853. var childNode = GLTFLoader._GetProperty(context + "/children/" + index, this._gltf.nodes, index);
  854. promises.push(this._loadNodeAsync("#/nodes/" + index, childNode));
  855. }
  856. }
  857. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  858. return Promise.all(promises).then(function () { });
  859. };
  860. GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, babylonMesh) {
  861. // TODO: instancing
  862. var _this = this;
  863. var promises = new Array();
  864. var primitives = mesh.primitives;
  865. if (!primitives || primitives.length === 0) {
  866. throw new Error(context + ": Primitives are missing");
  867. }
  868. GLTF2.ArrayItem.Assign(primitives);
  869. if (primitives.length === 1) {
  870. var primitive = primitives[0];
  871. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, babylonMesh));
  872. }
  873. else {
  874. node._primitiveBabylonMeshes = [];
  875. for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
  876. var primitive = primitives_1[_i];
  877. var primitiveBabylonMesh = new BABYLON.Mesh((mesh.name || babylonMesh.name) + "_" + primitive._index, this._babylonScene, babylonMesh);
  878. node._primitiveBabylonMeshes.push(primitiveBabylonMesh);
  879. promises.push(this._loadPrimitiveAsync(context + "/primitives/" + primitive._index, node, mesh, primitive, primitiveBabylonMesh));
  880. this.onMeshLoadedObservable.notifyObservers(babylonMesh);
  881. }
  882. }
  883. if (node.skin != undefined) {
  884. var skin = GLTFLoader._GetProperty(context + "/skin", this._gltf.skins, node.skin);
  885. promises.push(this._loadSkinAsync("#/skins/" + skin._index, node, mesh, skin));
  886. }
  887. return Promise.all(promises).then(function () {
  888. _this._forEachPrimitive(node, function (babylonMesh) {
  889. babylonMesh._refreshBoundingInfo(true);
  890. });
  891. });
  892. };
  893. GLTFLoader.prototype._loadPrimitiveAsync = function (context, node, mesh, primitive, babylonMesh) {
  894. var _this = this;
  895. var promises = new Array();
  896. this._createMorphTargets(context, node, mesh, primitive, babylonMesh);
  897. promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh).then(function (babylonVertexData) {
  898. new BABYLON.Geometry(babylonMesh.name, _this._babylonScene, babylonVertexData, false, babylonMesh);
  899. return _this._loadMorphTargetsAsync(context, primitive, babylonMesh, babylonVertexData);
  900. }));
  901. var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
  902. if (primitive.material == undefined) {
  903. babylonMesh.material = this._getDefaultMaterial(babylonDrawMode);
  904. }
  905. else {
  906. var material = GLTFLoader._GetProperty(context + "/material}", this._gltf.materials, primitive.material);
  907. promises.push(this._loadMaterialAsync("#/materials/" + material._index, material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
  908. babylonMesh.material = babylonMaterial;
  909. }));
  910. }
  911. return Promise.all(promises).then(function () { });
  912. };
  913. GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  914. var _this = this;
  915. var promise = GLTF2.GLTFLoaderExtension._LoadVertexDataAsync(this, context, primitive, babylonMesh);
  916. if (promise) {
  917. return promise;
  918. }
  919. var attributes = primitive.attributes;
  920. if (!attributes) {
  921. throw new Error(context + ": Attributes are missing");
  922. }
  923. var promises = new Array();
  924. var babylonVertexData = new BABYLON.VertexData();
  925. if (primitive.indices == undefined) {
  926. var positionAccessorIndex = attributes["POSITION"];
  927. if (positionAccessorIndex != undefined) {
  928. var accessor = GLTFLoader._GetProperty(context + "/attributes/POSITION", this._gltf.accessors, positionAccessorIndex);
  929. babylonVertexData.indices = new Uint32Array(accessor.count);
  930. for (var i = 0; i < babylonVertexData.indices.length; i++) {
  931. babylonVertexData.indices[i] = i;
  932. }
  933. }
  934. }
  935. else {
  936. var indicesAccessor = GLTFLoader._GetProperty(context + "/indices", this._gltf.accessors, primitive.indices);
  937. promises.push(this._loadAccessorAsync("#/accessors/" + indicesAccessor._index, indicesAccessor).then(function (data) {
  938. babylonVertexData.indices = data;
  939. }));
  940. }
  941. var loadAttribute = function (attribute, kind) {
  942. if (attributes[attribute] == undefined) {
  943. return;
  944. }
  945. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  946. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  947. babylonMesh._delayInfo.push(kind);
  948. }
  949. if (attribute === "COLOR_0") {
  950. // Assume vertex color has alpha on the mesh. The alphaMode of the material controls whether the material should use alpha or not.
  951. babylonMesh.hasVertexAlpha = true;
  952. }
  953. var accessor = GLTFLoader._GetProperty(context + "/attributes/" + attribute, _this._gltf.accessors, attributes[attribute]);
  954. promises.push(_this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  955. var attributeData = GLTFLoader._ConvertToFloat32Array(context, accessor, data);
  956. if (attribute === "COLOR_0" && accessor.type === "VEC3") {
  957. attributeData = GLTFLoader._ConvertVec3ToVec4(context, attributeData);
  958. }
  959. babylonVertexData.set(attributeData, kind);
  960. }));
  961. };
  962. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  963. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  964. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  965. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  966. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  967. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  968. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  969. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  970. return Promise.all(promises).then(function () {
  971. return babylonVertexData;
  972. });
  973. };
  974. GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
  975. if (!primitive.targets) {
  976. return;
  977. }
  978. if (node._numMorphTargets == undefined) {
  979. node._numMorphTargets = primitive.targets.length;
  980. }
  981. else if (primitive.targets.length !== node._numMorphTargets) {
  982. throw new Error(context + ": Primitives do not have the same number of targets");
  983. }
  984. babylonMesh.morphTargetManager = new BABYLON.MorphTargetManager();
  985. for (var index = 0; index < primitive.targets.length; index++) {
  986. var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
  987. babylonMesh.morphTargetManager.addTarget(new BABYLON.MorphTarget("morphTarget" + index, weight));
  988. // TODO: tell the target whether it has positions, normals, tangents
  989. }
  990. };
  991. GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonVertexData) {
  992. if (!primitive.targets) {
  993. return Promise.resolve();
  994. }
  995. var promises = new Array();
  996. var morphTargetManager = babylonMesh.morphTargetManager;
  997. for (var index = 0; index < morphTargetManager.numTargets; index++) {
  998. var babylonMorphTarget = morphTargetManager.getTarget(index);
  999. promises.push(this._loadMorphTargetVertexDataAsync(context + "/targets/" + index, babylonVertexData, primitive.targets[index], babylonMorphTarget));
  1000. }
  1001. return Promise.all(promises).then(function () { });
  1002. };
  1003. GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonVertexData, attributes, babylonMorphTarget) {
  1004. var _this = this;
  1005. var promises = new Array();
  1006. var loadAttribute = function (attribute, setData) {
  1007. if (attributes[attribute] == undefined) {
  1008. return;
  1009. }
  1010. var accessor = GLTFLoader._GetProperty(context + "/" + attribute, _this._gltf.accessors, attributes[attribute]);
  1011. promises.push(_this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1012. setData(data);
  1013. }));
  1014. };
  1015. loadAttribute("POSITION", function (data) {
  1016. if (babylonVertexData.positions) {
  1017. for (var i = 0; i < data.length; i++) {
  1018. data[i] += babylonVertexData.positions[i];
  1019. }
  1020. babylonMorphTarget.setPositions(data);
  1021. }
  1022. });
  1023. loadAttribute("NORMAL", function (data) {
  1024. if (babylonVertexData.normals) {
  1025. for (var i = 0; i < data.length; i++) {
  1026. data[i] += babylonVertexData.normals[i];
  1027. }
  1028. babylonMorphTarget.setNormals(data);
  1029. }
  1030. });
  1031. loadAttribute("TANGENT", function (data) {
  1032. if (babylonVertexData.tangents) {
  1033. // Tangent data for morph targets is stored as xyz delta.
  1034. // The vertexData.tangent is stored as xyzw.
  1035. // So we need to skip every fourth vertexData.tangent.
  1036. for (var i = 0, j = 0; i < data.length; i++) {
  1037. data[i] += babylonVertexData.tangents[j++];
  1038. if ((i + 1) % 3 == 0) {
  1039. j++;
  1040. }
  1041. }
  1042. babylonMorphTarget.setTangents(data);
  1043. }
  1044. });
  1045. return Promise.all(promises).then(function () { });
  1046. };
  1047. GLTFLoader._ConvertToFloat32Array = function (context, accessor, data) {
  1048. if (accessor.componentType == 5126 /* FLOAT */) {
  1049. return data;
  1050. }
  1051. var factor = 1;
  1052. if (accessor.normalized) {
  1053. switch (accessor.componentType) {
  1054. case 5121 /* UNSIGNED_BYTE */: {
  1055. factor = 1 / 255;
  1056. break;
  1057. }
  1058. case 5123 /* UNSIGNED_SHORT */: {
  1059. factor = 1 / 65535;
  1060. break;
  1061. }
  1062. default: {
  1063. throw new Error(context + ": Invalid component type (" + accessor.componentType + ")");
  1064. }
  1065. }
  1066. }
  1067. var result = new Float32Array(accessor.count * GLTFLoader._GetNumComponents(context, accessor.type));
  1068. for (var i = 0; i < result.length; i++) {
  1069. result[i] = data[i] * factor;
  1070. }
  1071. return result;
  1072. };
  1073. GLTFLoader._ConvertVec3ToVec4 = function (context, data) {
  1074. var result = new Float32Array(data.length / 3 * 4);
  1075. var offset = 0;
  1076. for (var i = 0; i < result.length; i++) {
  1077. if ((i + 1) % 4 === 0) {
  1078. result[i] = 1;
  1079. }
  1080. else {
  1081. result[i] = data[offset++];
  1082. }
  1083. }
  1084. return result;
  1085. };
  1086. GLTFLoader._LoadTransform = function (node, babylonNode) {
  1087. var position = BABYLON.Vector3.Zero();
  1088. var rotation = BABYLON.Quaternion.Identity();
  1089. var scaling = BABYLON.Vector3.One();
  1090. if (node.matrix) {
  1091. var matrix = BABYLON.Matrix.FromArray(node.matrix);
  1092. matrix.decompose(scaling, rotation, position);
  1093. }
  1094. else {
  1095. if (node.translation)
  1096. position = BABYLON.Vector3.FromArray(node.translation);
  1097. if (node.rotation)
  1098. rotation = BABYLON.Quaternion.FromArray(node.rotation);
  1099. if (node.scale)
  1100. scaling = BABYLON.Vector3.FromArray(node.scale);
  1101. }
  1102. babylonNode.position = position;
  1103. babylonNode.rotationQuaternion = rotation;
  1104. babylonNode.scaling = scaling;
  1105. };
  1106. GLTFLoader.prototype._loadSkinAsync = function (context, node, mesh, skin) {
  1107. var _this = this;
  1108. var assignSkeleton = function () {
  1109. _this._forEachPrimitive(node, function (babylonMesh) {
  1110. babylonMesh.skeleton = skin._babylonSkeleton;
  1111. });
  1112. node._babylonMesh.parent = _this._rootBabylonMesh;
  1113. node._babylonMesh.position = BABYLON.Vector3.Zero();
  1114. node._babylonMesh.rotationQuaternion = BABYLON.Quaternion.Identity();
  1115. node._babylonMesh.scaling = BABYLON.Vector3.One();
  1116. };
  1117. if (skin._loaded) {
  1118. return skin._loaded.then(function () {
  1119. assignSkeleton();
  1120. });
  1121. }
  1122. // TODO: split into two parts so that bones are created before inverseBindMatricesData is loaded (for compiling materials).
  1123. return (skin._loaded = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
  1124. var skeletonId = "skeleton" + skin._index;
  1125. var babylonSkeleton = new BABYLON.Skeleton(skin.name || skeletonId, skeletonId, _this._babylonScene);
  1126. skin._babylonSkeleton = babylonSkeleton;
  1127. _this._loadBones(context, skin, inverseBindMatricesData);
  1128. assignSkeleton();
  1129. }));
  1130. };
  1131. GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
  1132. if (skin.inverseBindMatrices == undefined) {
  1133. return Promise.resolve(null);
  1134. }
  1135. var accessor = GLTFLoader._GetProperty(context + "/inverseBindMatrices", this._gltf.accessors, skin.inverseBindMatrices);
  1136. return this._loadAccessorAsync("#/accessors/" + accessor._index, accessor).then(function (data) {
  1137. return data;
  1138. });
  1139. };
  1140. GLTFLoader.prototype._createBone = function (node, skin, parent, localMatrix, baseMatrix, index) {
  1141. var babylonBone = new BABYLON.Bone(node.name || "joint" + node._index, skin._babylonSkeleton, parent, localMatrix, null, baseMatrix, index);
  1142. node._babylonAnimationTargets = node._babylonAnimationTargets || [];
  1143. node._babylonAnimationTargets.push(babylonBone);
  1144. return babylonBone;
  1145. };
  1146. GLTFLoader.prototype._loadBones = function (context, skin, inverseBindMatricesData) {
  1147. var babylonBones = {};
  1148. for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
  1149. var index = _a[_i];
  1150. var node = GLTFLoader._GetProperty(context + "/joints/" + index, this._gltf.nodes, index);
  1151. this._loadBone(node, skin, inverseBindMatricesData, babylonBones);
  1152. }
  1153. };
  1154. GLTFLoader.prototype._loadBone = function (node, skin, inverseBindMatricesData, babylonBones) {
  1155. var babylonBone = babylonBones[node._index];
  1156. if (babylonBone) {
  1157. return babylonBone;
  1158. }
  1159. var boneIndex = skin.joints.indexOf(node._index);
  1160. var baseMatrix = BABYLON.Matrix.Identity();
  1161. if (inverseBindMatricesData && boneIndex !== -1) {
  1162. baseMatrix = BABYLON.Matrix.FromArray(inverseBindMatricesData, boneIndex * 16);
  1163. baseMatrix.invertToRef(baseMatrix);
  1164. }
  1165. var babylonParentBone = null;
  1166. if (node._parent._babylonMesh !== this._rootBabylonMesh) {
  1167. babylonParentBone = this._loadBone(node._parent, skin, inverseBindMatricesData, babylonBones);
  1168. baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
  1169. }
  1170. babylonBone = this._createBone(node, skin, babylonParentBone, this._getNodeMatrix(node), baseMatrix, boneIndex);
  1171. babylonBones[node._index] = babylonBone;
  1172. return babylonBone;
  1173. };
  1174. GLTFLoader.prototype._getNodeMatrix = function (node) {
  1175. return node.matrix ?
  1176. BABYLON.Matrix.FromArray(node.matrix) :
  1177. BABYLON.Matrix.Compose(node.scale ? BABYLON.Vector3.FromArray(node.scale) : BABYLON.Vector3.One(), node.rotation ? BABYLON.Quaternion.FromArray(node.rotation) : BABYLON.Quaternion.Identity(), node.translation ? BABYLON.Vector3.FromArray(node.translation) : BABYLON.Vector3.Zero());
  1178. };
  1179. GLTFLoader.prototype._loadAnimationsAsync = function () {
  1180. var animations = this._gltf.animations;
  1181. if (!animations) {
  1182. return Promise.resolve();
  1183. }
  1184. var promises = new Array();
  1185. for (var index = 0; index < animations.length; index++) {
  1186. var animation = animations[index];
  1187. promises.push(this._loadAnimationAsync("#/animations/" + index, animation));
  1188. }
  1189. return Promise.all(promises).then(function () { });
  1190. };
  1191. GLTFLoader.prototype._loadAnimationAsync = function (context, animation) {
  1192. var babylonAnimationGroup = new BABYLON.AnimationGroup(animation.name || "animation" + animation._index, this._babylonScene);
  1193. animation._babylonAnimationGroup = babylonAnimationGroup;
  1194. var promises = new Array();
  1195. GLTF2.ArrayItem.Assign(animation.channels);
  1196. GLTF2.ArrayItem.Assign(animation.samplers);
  1197. for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
  1198. var channel = _a[_i];
  1199. promises.push(this._loadAnimationChannelAsync(context + "/channels/" + channel._index, context, animation, channel, babylonAnimationGroup));
  1200. }
  1201. this.onAnimationGroupLoadedObservable.notifyObservers(babylonAnimationGroup);
  1202. return Promise.all(promises).then(function () {
  1203. babylonAnimationGroup.normalize();
  1204. });
  1205. };
  1206. GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup) {
  1207. var _this = this;
  1208. var targetNode = GLTFLoader._GetProperty(context + "/target/node", this._gltf.nodes, channel.target.node);
  1209. if (!targetNode._babylonMesh || targetNode.skin != undefined) {
  1210. return Promise.resolve();
  1211. }
  1212. var sampler = GLTFLoader._GetProperty(context + "/sampler", animation.samplers, channel.sampler);
  1213. return this._loadAnimationSamplerAsync(animationContext + "/samplers/" + channel.sampler, sampler).then(function (data) {
  1214. var targetPath;
  1215. var animationType;
  1216. switch (channel.target.path) {
  1217. case "translation" /* TRANSLATION */: {
  1218. targetPath = "position";
  1219. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1220. break;
  1221. }
  1222. case "rotation" /* ROTATION */: {
  1223. targetPath = "rotationQuaternion";
  1224. animationType = BABYLON.Animation.ANIMATIONTYPE_QUATERNION;
  1225. break;
  1226. }
  1227. case "scale" /* SCALE */: {
  1228. targetPath = "scaling";
  1229. animationType = BABYLON.Animation.ANIMATIONTYPE_VECTOR3;
  1230. break;
  1231. }
  1232. case "weights" /* WEIGHTS */: {
  1233. targetPath = "influence";
  1234. animationType = BABYLON.Animation.ANIMATIONTYPE_FLOAT;
  1235. break;
  1236. }
  1237. default: {
  1238. throw new Error(context + ": Invalid target path (" + channel.target.path + ")");
  1239. }
  1240. }
  1241. var outputBufferOffset = 0;
  1242. var getNextOutputValue;
  1243. switch (targetPath) {
  1244. case "position": {
  1245. getNextOutputValue = function () {
  1246. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1247. outputBufferOffset += 3;
  1248. return value;
  1249. };
  1250. break;
  1251. }
  1252. case "rotationQuaternion": {
  1253. getNextOutputValue = function () {
  1254. var value = BABYLON.Quaternion.FromArray(data.output, outputBufferOffset);
  1255. outputBufferOffset += 4;
  1256. return value;
  1257. };
  1258. break;
  1259. }
  1260. case "scaling": {
  1261. getNextOutputValue = function () {
  1262. var value = BABYLON.Vector3.FromArray(data.output, outputBufferOffset);
  1263. outputBufferOffset += 3;
  1264. return value;
  1265. };
  1266. break;
  1267. }
  1268. case "influence": {
  1269. getNextOutputValue = function () {
  1270. var value = new Array(targetNode._numMorphTargets);
  1271. for (var i = 0; i < targetNode._numMorphTargets; i++) {
  1272. value[i] = data.output[outputBufferOffset++];
  1273. }
  1274. return value;
  1275. };
  1276. break;
  1277. }
  1278. }
  1279. var getNextKey;
  1280. switch (data.interpolation) {
  1281. case "STEP" /* STEP */: {
  1282. getNextKey = function (frameIndex) { return ({
  1283. frame: data.input[frameIndex],
  1284. value: getNextOutputValue(),
  1285. interpolation: BABYLON.AnimationKeyInterpolation.STEP
  1286. }); };
  1287. break;
  1288. }
  1289. case "LINEAR" /* LINEAR */: {
  1290. getNextKey = function (frameIndex) { return ({
  1291. frame: data.input[frameIndex],
  1292. value: getNextOutputValue()
  1293. }); };
  1294. break;
  1295. }
  1296. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1297. getNextKey = function (frameIndex) { return ({
  1298. frame: data.input[frameIndex],
  1299. inTangent: getNextOutputValue(),
  1300. value: getNextOutputValue(),
  1301. outTangent: getNextOutputValue()
  1302. }); };
  1303. break;
  1304. }
  1305. }
  1306. var keys = new Array(data.input.length);
  1307. for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
  1308. keys[frameIndex] = getNextKey(frameIndex);
  1309. }
  1310. if (targetPath === "influence") {
  1311. var _loop_1 = function (targetIndex) {
  1312. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1313. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1314. babylonAnimation.setKeys(keys.map(function (key) { return ({
  1315. frame: key.frame,
  1316. inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
  1317. value: key.value[targetIndex],
  1318. outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
  1319. }); }));
  1320. _this._forEachPrimitive(targetNode, function (babylonMesh) {
  1321. var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
  1322. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, morphTarget);
  1323. });
  1324. };
  1325. for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
  1326. _loop_1(targetIndex);
  1327. }
  1328. }
  1329. else {
  1330. var animationName = babylonAnimationGroup.name + "_channel" + babylonAnimationGroup.targetedAnimations.length;
  1331. var babylonAnimation = new BABYLON.Animation(animationName, targetPath, 1, animationType);
  1332. babylonAnimation.setKeys(keys);
  1333. if (targetNode._babylonAnimationTargets) {
  1334. for (var _i = 0, _a = targetNode._babylonAnimationTargets; _i < _a.length; _i++) {
  1335. var target = _a[_i];
  1336. babylonAnimationGroup.addTargetedAnimation(babylonAnimation, target);
  1337. }
  1338. }
  1339. }
  1340. });
  1341. };
  1342. GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
  1343. if (sampler._data) {
  1344. return sampler._data;
  1345. }
  1346. var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
  1347. switch (interpolation) {
  1348. case "STEP" /* STEP */:
  1349. case "LINEAR" /* LINEAR */:
  1350. case "CUBICSPLINE" /* CUBICSPLINE */: {
  1351. break;
  1352. }
  1353. default: {
  1354. throw new Error(context + ": Invalid interpolation (" + sampler.interpolation + ")");
  1355. }
  1356. }
  1357. var inputData;
  1358. var outputData;
  1359. var inputAccessor = GLTFLoader._GetProperty(context + "/input", this._gltf.accessors, sampler.input);
  1360. var outputAccessor = GLTFLoader._GetProperty(context + "/output", this._gltf.accessors, sampler.output);
  1361. sampler._data = Promise.all([
  1362. this._loadAccessorAsync("#/accessors/" + inputAccessor._index, inputAccessor).then(function (data) {
  1363. inputData = data;
  1364. }),
  1365. this._loadAccessorAsync("#/accessors/" + outputAccessor._index, outputAccessor).then(function (data) {
  1366. outputData = data;
  1367. })
  1368. ]).then(function () {
  1369. return {
  1370. input: inputData,
  1371. interpolation: interpolation,
  1372. output: outputData,
  1373. };
  1374. });
  1375. return sampler._data;
  1376. };
  1377. GLTFLoader.prototype._loadBufferAsync = function (context, buffer) {
  1378. if (buffer._data) {
  1379. return buffer._data;
  1380. }
  1381. if (!buffer.uri) {
  1382. throw new Error(context + ": Uri is missing");
  1383. }
  1384. buffer._data = this._loadUriAsync(context, buffer.uri);
  1385. return buffer._data;
  1386. };
  1387. GLTFLoader.prototype._loadBufferViewAsync = function (context, bufferView) {
  1388. if (bufferView._data) {
  1389. return bufferView._data;
  1390. }
  1391. var buffer = GLTFLoader._GetProperty(context + "/buffer", this._gltf.buffers, bufferView.buffer);
  1392. bufferView._data = this._loadBufferAsync("#/buffers/" + buffer._index, buffer).then(function (bufferData) {
  1393. try {
  1394. return new Uint8Array(bufferData.buffer, bufferData.byteOffset + (bufferView.byteOffset || 0), bufferView.byteLength);
  1395. }
  1396. catch (e) {
  1397. throw new Error(context + ": " + e.message);
  1398. }
  1399. });
  1400. return bufferView._data;
  1401. };
  1402. GLTFLoader.prototype._loadAccessorAsync = function (context, accessor) {
  1403. var _this = this;
  1404. if (accessor.sparse) {
  1405. throw new Error(context + ": Sparse accessors are not currently supported");
  1406. }
  1407. if (accessor._data) {
  1408. return accessor._data;
  1409. }
  1410. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, accessor.bufferView);
  1411. accessor._data = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (bufferViewData) {
  1412. var numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
  1413. var byteOffset = accessor.byteOffset || 0;
  1414. var byteStride = bufferView.byteStride;
  1415. if (byteStride === 0) {
  1416. BABYLON.Tools.Warn(context + ": Byte stride of 0 is not valid");
  1417. }
  1418. try {
  1419. switch (accessor.componentType) {
  1420. case 5120 /* BYTE */: {
  1421. return _this._buildArrayBuffer(Float32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1422. }
  1423. case 5121 /* UNSIGNED_BYTE */: {
  1424. return _this._buildArrayBuffer(Uint8Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1425. }
  1426. case 5122 /* SHORT */: {
  1427. return _this._buildArrayBuffer(Int16Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1428. }
  1429. case 5123 /* UNSIGNED_SHORT */: {
  1430. return _this._buildArrayBuffer(Uint16Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1431. }
  1432. case 5125 /* UNSIGNED_INT */: {
  1433. return _this._buildArrayBuffer(Uint32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1434. }
  1435. case 5126 /* FLOAT */: {
  1436. return _this._buildArrayBuffer(Float32Array, bufferViewData, byteOffset, accessor.count, numComponents, byteStride);
  1437. }
  1438. default: {
  1439. throw new Error(context + ": Invalid component type (" + accessor.componentType + ")");
  1440. }
  1441. }
  1442. }
  1443. catch (e) {
  1444. throw new Error(context + ": " + e.messsage);
  1445. }
  1446. });
  1447. return accessor._data;
  1448. };
  1449. GLTFLoader.prototype._buildArrayBuffer = function (typedArray, data, byteOffset, count, numComponents, byteStride) {
  1450. byteOffset += data.byteOffset;
  1451. var targetLength = count * numComponents;
  1452. if (!byteStride || byteStride === numComponents * typedArray.BYTES_PER_ELEMENT) {
  1453. return new typedArray(data.buffer, byteOffset, targetLength);
  1454. }
  1455. var elementStride = byteStride / typedArray.BYTES_PER_ELEMENT;
  1456. var sourceBuffer = new typedArray(data.buffer, byteOffset, elementStride * count);
  1457. var targetBuffer = new typedArray(targetLength);
  1458. var sourceIndex = 0;
  1459. var targetIndex = 0;
  1460. while (targetIndex < targetLength) {
  1461. for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
  1462. targetBuffer[targetIndex] = sourceBuffer[sourceIndex + componentIndex];
  1463. targetIndex++;
  1464. }
  1465. sourceIndex += elementStride;
  1466. }
  1467. return targetBuffer;
  1468. };
  1469. GLTFLoader.prototype._getDefaultMaterial = function (drawMode) {
  1470. var babylonMaterial = this._defaultBabylonMaterials[drawMode];
  1471. if (!babylonMaterial) {
  1472. babylonMaterial = this._createMaterial(BABYLON.PBRMaterial, "__gltf_default", drawMode);
  1473. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1474. babylonMaterial.metallic = 1;
  1475. babylonMaterial.roughness = 1;
  1476. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1477. }
  1478. return babylonMaterial;
  1479. };
  1480. GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, material, babylonMaterial) {
  1481. var promises = new Array();
  1482. // Ensure metallic workflow
  1483. babylonMaterial.metallic = 1;
  1484. babylonMaterial.roughness = 1;
  1485. var properties = material.pbrMetallicRoughness;
  1486. if (properties) {
  1487. if (properties.baseColorFactor) {
  1488. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.baseColorFactor);
  1489. babylonMaterial.alpha = properties.baseColorFactor[3];
  1490. }
  1491. else {
  1492. babylonMaterial.albedoColor = BABYLON.Color3.White();
  1493. }
  1494. babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
  1495. babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
  1496. if (properties.baseColorTexture) {
  1497. promises.push(this._loadTextureAsync(context + "/baseColorTexture", properties.baseColorTexture, function (texture) {
  1498. babylonMaterial.albedoTexture = texture;
  1499. }));
  1500. }
  1501. if (properties.metallicRoughnessTexture) {
  1502. promises.push(this._loadTextureAsync(context + "/metallicRoughnessTexture", properties.metallicRoughnessTexture, function (texture) {
  1503. babylonMaterial.metallicTexture = texture;
  1504. }));
  1505. babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
  1506. babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
  1507. babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
  1508. }
  1509. }
  1510. this._loadMaterialAlphaProperties(context, material, babylonMaterial);
  1511. return Promise.all(promises).then(function () { });
  1512. };
  1513. GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  1514. var promise = GLTF2.GLTFLoaderExtension._LoadMaterialAsync(this, context, material, babylonMesh, babylonDrawMode, assign);
  1515. if (promise) {
  1516. return promise;
  1517. }
  1518. material._babylonData = material._babylonData || {};
  1519. var babylonData = material._babylonData[babylonDrawMode];
  1520. if (!babylonData) {
  1521. var promises = new Array();
  1522. var name_3 = material.name || "materialSG_" + material._index;
  1523. var babylonMaterial = this._createMaterial(BABYLON.PBRMaterial, name_3, babylonDrawMode);
  1524. promises.push(this._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  1525. promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial));
  1526. this.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  1527. babylonData = {
  1528. material: babylonMaterial,
  1529. meshes: [],
  1530. loaded: Promise.all(promises).then(function () { })
  1531. };
  1532. material._babylonData[babylonDrawMode] = babylonData;
  1533. }
  1534. babylonData.meshes.push(babylonMesh);
  1535. assign(babylonData.material);
  1536. return babylonData.loaded;
  1537. };
  1538. GLTFLoader.prototype._createMaterial = function (type, name, drawMode) {
  1539. var babylonMaterial = new type(name, this._babylonScene);
  1540. babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? BABYLON.Material.CounterClockWiseSideOrientation : BABYLON.Material.ClockWiseSideOrientation;
  1541. babylonMaterial.fillMode = drawMode;
  1542. return babylonMaterial;
  1543. };
  1544. GLTFLoader.prototype._loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
  1545. var promises = new Array();
  1546. babylonMaterial.emissiveColor = material.emissiveFactor ? BABYLON.Color3.FromArray(material.emissiveFactor) : new BABYLON.Color3(0, 0, 0);
  1547. if (material.doubleSided) {
  1548. babylonMaterial.backFaceCulling = false;
  1549. babylonMaterial.twoSidedLighting = true;
  1550. }
  1551. if (material.normalTexture) {
  1552. promises.push(this._loadTextureAsync(context + "/normalTexture", material.normalTexture, function (texture) {
  1553. babylonMaterial.bumpTexture = texture;
  1554. }));
  1555. babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
  1556. babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
  1557. if (material.normalTexture.scale != undefined) {
  1558. babylonMaterial.bumpTexture.level = material.normalTexture.scale;
  1559. }
  1560. }
  1561. if (material.occlusionTexture) {
  1562. promises.push(this._loadTextureAsync(context + "/occlusionTexture", material.occlusionTexture, function (texture) {
  1563. babylonMaterial.ambientTexture = texture;
  1564. }));
  1565. babylonMaterial.useAmbientInGrayScale = true;
  1566. if (material.occlusionTexture.strength != undefined) {
  1567. babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
  1568. }
  1569. }
  1570. if (material.emissiveTexture) {
  1571. promises.push(this._loadTextureAsync(context + "/emissiveTexture", material.emissiveTexture, function (texture) {
  1572. babylonMaterial.emissiveTexture = texture;
  1573. }));
  1574. }
  1575. return Promise.all(promises).then(function () { });
  1576. };
  1577. GLTFLoader.prototype._loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
  1578. var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
  1579. switch (alphaMode) {
  1580. case "OPAQUE" /* OPAQUE */: {
  1581. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  1582. break;
  1583. }
  1584. case "MASK" /* MASK */: {
  1585. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  1586. babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
  1587. if (babylonMaterial.albedoTexture) {
  1588. babylonMaterial.albedoTexture.hasAlpha = true;
  1589. }
  1590. break;
  1591. }
  1592. case "BLEND" /* BLEND */: {
  1593. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  1594. if (babylonMaterial.albedoTexture) {
  1595. babylonMaterial.albedoTexture.hasAlpha = true;
  1596. babylonMaterial.useAlphaFromAlbedoTexture = true;
  1597. }
  1598. break;
  1599. }
  1600. default: {
  1601. throw new Error(context + ": Invalid alpha mode (" + material.alphaMode + ")");
  1602. }
  1603. }
  1604. };
  1605. GLTFLoader.prototype._loadTextureAsync = function (context, textureInfo, assign) {
  1606. var _this = this;
  1607. var texture = GLTFLoader._GetProperty(context + "/index", this._gltf.textures, textureInfo.index);
  1608. context = "#/textures/" + textureInfo.index;
  1609. var promises = new Array();
  1610. var sampler = (texture.sampler == undefined ? this._defaultSampler : GLTFLoader._GetProperty(context + "/sampler", this._gltf.samplers, texture.sampler));
  1611. var samplerData = this._loadSampler("#/samplers/" + sampler._index, sampler);
  1612. var deferred = new BABYLON.Deferred();
  1613. var babylonTexture = new BABYLON.Texture(null, this._babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, function () {
  1614. if (!_this._disposed) {
  1615. deferred.resolve();
  1616. }
  1617. }, function (message, exception) {
  1618. if (!_this._disposed) {
  1619. deferred.reject(new Error(context + ": " + ((exception && exception.message) ? exception.message : message || "Failed to load texture")));
  1620. }
  1621. });
  1622. promises.push(deferred.promise);
  1623. babylonTexture.name = texture.name || "texture" + texture._index;
  1624. babylonTexture.wrapU = samplerData.wrapU;
  1625. babylonTexture.wrapV = samplerData.wrapV;
  1626. babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
  1627. var image = GLTFLoader._GetProperty(context + "/source", this._gltf.images, texture.source);
  1628. promises.push(this._loadImageAsync("#/images/" + image._index, image).then(function (objectURL) {
  1629. babylonTexture.updateURL(objectURL);
  1630. }));
  1631. assign(babylonTexture);
  1632. this.onTextureLoadedObservable.notifyObservers(babylonTexture);
  1633. return Promise.all(promises).then(function () { });
  1634. };
  1635. GLTFLoader.prototype._loadSampler = function (context, sampler) {
  1636. if (!sampler._data) {
  1637. sampler._data = {
  1638. noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
  1639. samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler.magFilter, sampler.minFilter),
  1640. wrapU: GLTFLoader._GetTextureWrapMode(context, sampler.wrapS),
  1641. wrapV: GLTFLoader._GetTextureWrapMode(context, sampler.wrapT)
  1642. };
  1643. }
  1644. ;
  1645. return sampler._data;
  1646. };
  1647. GLTFLoader.prototype._loadImageAsync = function (context, image) {
  1648. if (image._objectURL) {
  1649. return image._objectURL;
  1650. }
  1651. var promise;
  1652. if (image.uri) {
  1653. promise = this._loadUriAsync(context, image.uri);
  1654. }
  1655. else {
  1656. var bufferView = GLTFLoader._GetProperty(context + "/bufferView", this._gltf.bufferViews, image.bufferView);
  1657. promise = this._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView);
  1658. }
  1659. image._objectURL = promise.then(function (data) {
  1660. return URL.createObjectURL(new Blob([data], { type: image.mimeType }));
  1661. });
  1662. return image._objectURL;
  1663. };
  1664. GLTFLoader.prototype._loadUriAsync = function (context, uri) {
  1665. var _this = this;
  1666. var promise = GLTF2.GLTFLoaderExtension._LoadUriAsync(this, context, uri);
  1667. if (promise) {
  1668. return promise;
  1669. }
  1670. if (!GLTFLoader._ValidateUri(uri)) {
  1671. throw new Error(context + ": Uri '" + uri + "' is invalid");
  1672. }
  1673. if (BABYLON.Tools.IsBase64(uri)) {
  1674. return Promise.resolve(new Uint8Array(BABYLON.Tools.DecodeBase64(uri)));
  1675. }
  1676. return new Promise(function (resolve, reject) {
  1677. var request = BABYLON.Tools.LoadFile(_this._rootUrl + uri, function (data) {
  1678. if (!_this._disposed) {
  1679. resolve(new Uint8Array(data));
  1680. }
  1681. }, function (event) {
  1682. if (!_this._disposed) {
  1683. try {
  1684. if (request && _this._state === BABYLON.GLTFLoaderState.Loading) {
  1685. request._lengthComputable = event.lengthComputable;
  1686. request._loaded = event.loaded;
  1687. request._total = event.total;
  1688. _this._onProgress();
  1689. }
  1690. }
  1691. catch (e) {
  1692. reject(e);
  1693. }
  1694. }
  1695. }, _this._babylonScene.database, true, function (request, exception) {
  1696. if (!_this._disposed) {
  1697. reject(new BABYLON.LoadFileError(context + ": Failed to load '" + uri + "'" + (request ? ": " + request.status + " " + request.statusText : ""), request));
  1698. }
  1699. });
  1700. _this._requests.push(request);
  1701. });
  1702. };
  1703. GLTFLoader.prototype._onProgress = function () {
  1704. if (!this._progressCallback) {
  1705. return;
  1706. }
  1707. var lengthComputable = true;
  1708. var loaded = 0;
  1709. var total = 0;
  1710. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  1711. var request = _a[_i];
  1712. if (request._lengthComputable === undefined || request._loaded === undefined || request._total === undefined) {
  1713. return;
  1714. }
  1715. lengthComputable = lengthComputable && request._lengthComputable;
  1716. loaded += request._loaded;
  1717. total += request._total;
  1718. }
  1719. this._progressCallback(new BABYLON.SceneLoaderProgressEvent(lengthComputable, loaded, lengthComputable ? total : 0));
  1720. };
  1721. GLTFLoader._GetProperty = function (context, array, index) {
  1722. if (!array || index == undefined || !array[index]) {
  1723. throw new Error(context + ": Failed to find index (" + index + ")");
  1724. }
  1725. return array[index];
  1726. };
  1727. GLTFLoader._GetTextureWrapMode = function (context, mode) {
  1728. // Set defaults if undefined
  1729. mode = mode == undefined ? 10497 /* REPEAT */ : mode;
  1730. switch (mode) {
  1731. case 33071 /* CLAMP_TO_EDGE */: return BABYLON.Texture.CLAMP_ADDRESSMODE;
  1732. case 33648 /* MIRRORED_REPEAT */: return BABYLON.Texture.MIRROR_ADDRESSMODE;
  1733. case 10497 /* REPEAT */: return BABYLON.Texture.WRAP_ADDRESSMODE;
  1734. default:
  1735. BABYLON.Tools.Warn(context + ": Invalid texture wrap mode (" + mode + ")");
  1736. return BABYLON.Texture.WRAP_ADDRESSMODE;
  1737. }
  1738. };
  1739. GLTFLoader._GetTextureSamplingMode = function (context, magFilter, minFilter) {
  1740. // Set defaults if undefined
  1741. magFilter = magFilter == undefined ? 9729 /* LINEAR */ : magFilter;
  1742. minFilter = minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : minFilter;
  1743. if (magFilter === 9729 /* LINEAR */) {
  1744. switch (minFilter) {
  1745. case 9728 /* NEAREST */: return BABYLON.Texture.LINEAR_NEAREST;
  1746. case 9729 /* LINEAR */: return BABYLON.Texture.LINEAR_LINEAR;
  1747. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_NEAREST_MIPNEAREST;
  1748. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.LINEAR_LINEAR_MIPNEAREST;
  1749. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_NEAREST_MIPLINEAR;
  1750. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  1751. default:
  1752. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  1753. return BABYLON.Texture.LINEAR_LINEAR_MIPLINEAR;
  1754. }
  1755. }
  1756. else {
  1757. if (magFilter !== 9728 /* NEAREST */) {
  1758. BABYLON.Tools.Warn(context + ": Invalid texture magnification filter (" + magFilter + ")");
  1759. }
  1760. switch (minFilter) {
  1761. case 9728 /* NEAREST */: return BABYLON.Texture.NEAREST_NEAREST;
  1762. case 9729 /* LINEAR */: return BABYLON.Texture.NEAREST_LINEAR;
  1763. case 9984 /* NEAREST_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  1764. case 9985 /* LINEAR_MIPMAP_NEAREST */: return BABYLON.Texture.NEAREST_LINEAR_MIPNEAREST;
  1765. case 9986 /* NEAREST_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_NEAREST_MIPLINEAR;
  1766. case 9987 /* LINEAR_MIPMAP_LINEAR */: return BABYLON.Texture.NEAREST_LINEAR_MIPLINEAR;
  1767. default:
  1768. BABYLON.Tools.Warn(context + ": Invalid texture minification filter (" + minFilter + ")");
  1769. return BABYLON.Texture.NEAREST_NEAREST_MIPNEAREST;
  1770. }
  1771. }
  1772. };
  1773. GLTFLoader._GetNumComponents = function (context, type) {
  1774. switch (type) {
  1775. case "SCALAR": return 1;
  1776. case "VEC2": return 2;
  1777. case "VEC3": return 3;
  1778. case "VEC4": return 4;
  1779. case "MAT2": return 4;
  1780. case "MAT3": return 9;
  1781. case "MAT4": return 16;
  1782. }
  1783. throw new Error(context + ": Invalid type (" + type + ")");
  1784. };
  1785. GLTFLoader._ValidateUri = function (uri) {
  1786. return (BABYLON.Tools.IsBase64(uri) || uri.indexOf("..") === -1);
  1787. };
  1788. GLTFLoader._GetDrawMode = function (context, mode) {
  1789. mode = mode || 4 /* TRIANGLES */;
  1790. switch (mode) {
  1791. case 0 /* POINTS */: return BABYLON.Material.PointListDrawMode;
  1792. case 1 /* LINES */: return BABYLON.Material.LineListDrawMode;
  1793. case 2 /* LINE_LOOP */: return BABYLON.Material.LineLoopDrawMode;
  1794. case 3 /* LINE_STRIP */: return BABYLON.Material.LineStripDrawMode;
  1795. case 4 /* TRIANGLES */: return BABYLON.Material.TriangleFillMode;
  1796. case 5 /* TRIANGLE_STRIP */: return BABYLON.Material.TriangleStripDrawMode;
  1797. case 6 /* TRIANGLE_FAN */: return BABYLON.Material.TriangleFanDrawMode;
  1798. }
  1799. throw new Error(context + ": Invalid mesh primitive mode (" + mode + ")");
  1800. };
  1801. GLTFLoader.prototype._compileMaterialsAsync = function () {
  1802. var promises = new Array();
  1803. if (this._gltf.materials) {
  1804. for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
  1805. var material = _a[_i];
  1806. if (material._babylonData) {
  1807. for (var babylonDrawMode in material._babylonData) {
  1808. var babylonData = material._babylonData[babylonDrawMode];
  1809. for (var _b = 0, _c = babylonData.meshes; _b < _c.length; _b++) {
  1810. var babylonMesh = _c[_b];
  1811. // Ensure nonUniformScaling is set if necessary.
  1812. babylonMesh.computeWorldMatrix(true);
  1813. var babylonMaterial = babylonData.material;
  1814. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
  1815. if (this.useClipPlane) {
  1816. promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
  1817. }
  1818. }
  1819. }
  1820. }
  1821. }
  1822. }
  1823. return Promise.all(promises).then(function () { });
  1824. };
  1825. GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
  1826. var promises = new Array();
  1827. var lights = this._babylonScene.lights;
  1828. for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
  1829. var light = lights_1[_i];
  1830. var generator = light.getShadowGenerator();
  1831. if (generator) {
  1832. promises.push(generator.forceCompilationAsync());
  1833. }
  1834. }
  1835. return Promise.all(promises).then(function () { });
  1836. };
  1837. GLTFLoader.prototype._clear = function () {
  1838. for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
  1839. var request = _a[_i];
  1840. request.abort();
  1841. }
  1842. this._requests.length = 0;
  1843. if (this._gltf && this._gltf.images) {
  1844. for (var _b = 0, _c = this._gltf.images; _b < _c.length; _b++) {
  1845. var image = _c[_b];
  1846. if (image._objectURL) {
  1847. image._objectURL.then(function (value) {
  1848. URL.revokeObjectURL(value);
  1849. });
  1850. image._objectURL = undefined;
  1851. }
  1852. }
  1853. }
  1854. delete this._gltf;
  1855. delete this._babylonScene;
  1856. this._completePromises.length = 0;
  1857. for (var name_4 in this._extensions) {
  1858. this._extensions[name_4].dispose();
  1859. }
  1860. this._extensions = {};
  1861. delete this._rootBabylonMesh;
  1862. delete this._progressCallback;
  1863. this.onMeshLoadedObservable.clear();
  1864. this.onTextureLoadedObservable.clear();
  1865. this.onMaterialLoadedObservable.clear();
  1866. };
  1867. GLTFLoader.prototype._applyExtensions = function (actionAsync) {
  1868. for (var _i = 0, _a = GLTFLoader._Names; _i < _a.length; _i++) {
  1869. var name_5 = _a[_i];
  1870. var extension = this._extensions[name_5];
  1871. if (extension.enabled) {
  1872. var promise = actionAsync(extension);
  1873. if (promise) {
  1874. return promise;
  1875. }
  1876. }
  1877. }
  1878. return null;
  1879. };
  1880. GLTFLoader._Names = new Array();
  1881. GLTFLoader._Factories = {};
  1882. return GLTFLoader;
  1883. }());
  1884. GLTF2.GLTFLoader = GLTFLoader;
  1885. BABYLON.GLTFFileLoader.CreateGLTFLoaderV2 = function () { return new GLTFLoader(); };
  1886. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1887. })(BABYLON || (BABYLON = {}));
  1888. //# sourceMappingURL=babylon.glTFLoader.js.map
  1889. "use strict";
  1890. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  1891. var BABYLON;
  1892. (function (BABYLON) {
  1893. var GLTF2;
  1894. (function (GLTF2) {
  1895. var GLTFLoaderExtension = /** @class */ (function () {
  1896. function GLTFLoaderExtension(loader) {
  1897. this.enabled = true;
  1898. this._loader = loader;
  1899. }
  1900. GLTFLoaderExtension.prototype.dispose = function () {
  1901. delete this._loader;
  1902. };
  1903. // #region Overridable Methods
  1904. /** Override this method to modify the default behavior for loading scenes. */
  1905. GLTFLoaderExtension.prototype._loadSceneAsync = function (context, node) { return null; };
  1906. /** Override this method to modify the default behavior for loading nodes. */
  1907. GLTFLoaderExtension.prototype._loadNodeAsync = function (context, node) { return null; };
  1908. /** Override this method to modify the default behavior for loading mesh primitive vertex data. */
  1909. GLTFLoaderExtension.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) { return null; };
  1910. /** Override this method to modify the default behavior for loading materials. */
  1911. GLTFLoaderExtension.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) { return null; };
  1912. /** Override this method to modify the default behavior for loading uris. */
  1913. GLTFLoaderExtension.prototype._loadUriAsync = function (context, uri) { return null; };
  1914. // #endregion
  1915. /** Helper method called by a loader extension to load an glTF extension. */
  1916. GLTFLoaderExtension.prototype._loadExtensionAsync = function (context, property, actionAsync) {
  1917. if (!property.extensions) {
  1918. return null;
  1919. }
  1920. var extensions = property.extensions;
  1921. var extension = extensions[this.name];
  1922. if (!extension) {
  1923. return null;
  1924. }
  1925. // Clear out the extension before executing the action to avoid recursing into the same property.
  1926. delete extensions[this.name];
  1927. try {
  1928. return actionAsync(context + "/extensions/" + this.name, extension);
  1929. }
  1930. finally {
  1931. // Restore the extension after executing the action.
  1932. extensions[this.name] = extension;
  1933. }
  1934. };
  1935. /** Helper method called by the loader to allow extensions to override loading scenes. */
  1936. GLTFLoaderExtension._LoadSceneAsync = function (loader, context, scene) {
  1937. return loader._applyExtensions(function (extension) { return extension._loadSceneAsync(context, scene); });
  1938. };
  1939. /** Helper method called by the loader to allow extensions to override loading nodes. */
  1940. GLTFLoaderExtension._LoadNodeAsync = function (loader, context, node) {
  1941. return loader._applyExtensions(function (extension) { return extension._loadNodeAsync(context, node); });
  1942. };
  1943. /** Helper method called by the loader to allow extensions to override loading mesh primitive vertex data. */
  1944. GLTFLoaderExtension._LoadVertexDataAsync = function (loader, context, primitive, babylonMesh) {
  1945. return loader._applyExtensions(function (extension) { return extension._loadVertexDataAsync(context, primitive, babylonMesh); });
  1946. };
  1947. /** Helper method called by the loader to allow extensions to override loading materials. */
  1948. GLTFLoaderExtension._LoadMaterialAsync = function (loader, context, material, babylonMesh, babylonDrawMode, assign) {
  1949. return loader._applyExtensions(function (extension) { return extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
  1950. };
  1951. /** Helper method called by the loader to allow extensions to override loading uris. */
  1952. GLTFLoaderExtension._LoadUriAsync = function (loader, context, uri) {
  1953. return loader._applyExtensions(function (extension) { return extension._loadUriAsync(context, uri); });
  1954. };
  1955. return GLTFLoaderExtension;
  1956. }());
  1957. GLTF2.GLTFLoaderExtension = GLTFLoaderExtension;
  1958. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1959. })(BABYLON || (BABYLON = {}));
  1960. //# sourceMappingURL=babylon.glTFLoaderExtension.js.map
  1961. "use strict";
  1962. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  1963. var __extends = (this && this.__extends) || (function () {
  1964. var extendStatics = Object.setPrototypeOf ||
  1965. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  1966. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  1967. return function (d, b) {
  1968. extendStatics(d, b);
  1969. function __() { this.constructor = d; }
  1970. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1971. };
  1972. })();
  1973. var BABYLON;
  1974. (function (BABYLON) {
  1975. var GLTF2;
  1976. (function (GLTF2) {
  1977. var Extensions;
  1978. (function (Extensions) {
  1979. // https://github.com/sbtron/glTF/tree/MSFT_lod/extensions/Vendor/MSFT_lod
  1980. var NAME = "MSFT_lod";
  1981. var MSFT_lod = /** @class */ (function (_super) {
  1982. __extends(MSFT_lod, _super);
  1983. function MSFT_lod() {
  1984. var _this = _super !== null && _super.apply(this, arguments) || this;
  1985. _this.name = NAME;
  1986. /**
  1987. * Maximum number of LODs to load, starting from the lowest LOD.
  1988. */
  1989. _this.maxLODsToLoad = Number.MAX_VALUE;
  1990. _this._loadingNodeLOD = null;
  1991. _this._loadNodeSignals = {};
  1992. _this._loadingMaterialLOD = null;
  1993. _this._loadMaterialSignals = {};
  1994. return _this;
  1995. }
  1996. MSFT_lod.prototype._loadNodeAsync = function (context, node) {
  1997. var _this = this;
  1998. return this._loadExtensionAsync(context, node, function (context, extension) {
  1999. var firstPromise;
  2000. var nodeLODs = _this._getLODs(context, node, _this._loader._gltf.nodes, extension.ids);
  2001. var _loop_1 = function (indexLOD) {
  2002. var nodeLOD = nodeLODs[indexLOD];
  2003. if (indexLOD !== 0) {
  2004. _this._loadingNodeLOD = nodeLOD;
  2005. if (!_this._loadNodeSignals[nodeLOD._index]) {
  2006. _this._loadNodeSignals[nodeLOD._index] = new BABYLON.Deferred();
  2007. }
  2008. }
  2009. var promise = _this._loader._loadNodeAsync("#/nodes/" + nodeLOD._index, nodeLOD).then(function () {
  2010. if (indexLOD !== 0) {
  2011. var previousNodeLOD = nodeLODs[indexLOD - 1];
  2012. if (previousNodeLOD._babylonMesh) {
  2013. previousNodeLOD._babylonMesh.dispose();
  2014. delete previousNodeLOD._babylonMesh;
  2015. }
  2016. }
  2017. if (indexLOD !== nodeLODs.length - 1) {
  2018. var nodeIndex = nodeLODs[indexLOD + 1]._index;
  2019. if (_this._loadNodeSignals[nodeIndex]) {
  2020. _this._loadNodeSignals[nodeIndex].resolve();
  2021. delete _this._loadNodeSignals[nodeIndex];
  2022. }
  2023. }
  2024. });
  2025. if (indexLOD === 0) {
  2026. firstPromise = promise;
  2027. }
  2028. else {
  2029. _this._loader._completePromises.push(promise);
  2030. _this._loadingNodeLOD = null;
  2031. }
  2032. };
  2033. for (var indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  2034. _loop_1(indexLOD);
  2035. }
  2036. return firstPromise;
  2037. });
  2038. };
  2039. MSFT_lod.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2040. var _this = this;
  2041. // Don't load material LODs if already loading a node LOD.
  2042. if (this._loadingNodeLOD) {
  2043. return null;
  2044. }
  2045. return this._loadExtensionAsync(context, material, function (context, extension) {
  2046. var firstPromise;
  2047. var materialLODs = _this._getLODs(context, material, _this._loader._gltf.materials, extension.ids);
  2048. var _loop_2 = function (indexLOD) {
  2049. var materialLOD = materialLODs[indexLOD];
  2050. if (indexLOD !== 0) {
  2051. _this._loadingMaterialLOD = materialLOD;
  2052. if (!_this._loadMaterialSignals[materialLOD._index]) {
  2053. _this._loadMaterialSignals[materialLOD._index] = new BABYLON.Deferred();
  2054. }
  2055. }
  2056. var promise = _this._loader._loadMaterialAsync("#/materials/" + materialLOD._index, materialLOD, babylonMesh, babylonDrawMode, indexLOD === 0 ? assign : function () { }).then(function () {
  2057. if (indexLOD !== 0) {
  2058. var babylonDataLOD = materialLOD._babylonData;
  2059. assign(babylonDataLOD[babylonDrawMode].material);
  2060. var previousBabylonDataLOD = materialLODs[indexLOD - 1]._babylonData;
  2061. if (previousBabylonDataLOD[babylonDrawMode]) {
  2062. previousBabylonDataLOD[babylonDrawMode].material.dispose();
  2063. delete previousBabylonDataLOD[babylonDrawMode];
  2064. }
  2065. }
  2066. if (indexLOD !== materialLODs.length - 1) {
  2067. var materialIndex = materialLODs[indexLOD + 1]._index;
  2068. if (_this._loadMaterialSignals[materialIndex]) {
  2069. _this._loadMaterialSignals[materialIndex].resolve();
  2070. delete _this._loadMaterialSignals[materialIndex];
  2071. }
  2072. }
  2073. });
  2074. if (indexLOD === 0) {
  2075. firstPromise = promise;
  2076. }
  2077. else {
  2078. _this._loader._completePromises.push(promise);
  2079. _this._loadingMaterialLOD = null;
  2080. }
  2081. };
  2082. for (var indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  2083. _loop_2(indexLOD);
  2084. }
  2085. return firstPromise;
  2086. });
  2087. };
  2088. MSFT_lod.prototype._loadUriAsync = function (context, uri) {
  2089. var _this = this;
  2090. // Defer the loading of uris if loading a material or node LOD.
  2091. if (this._loadingMaterialLOD) {
  2092. var index = this._loadingMaterialLOD._index;
  2093. return this._loadMaterialSignals[index].promise.then(function () {
  2094. return _this._loader._loadUriAsync(context, uri);
  2095. });
  2096. }
  2097. else if (this._loadingNodeLOD) {
  2098. var index = this._loadingNodeLOD._index;
  2099. return this._loadNodeSignals[index].promise.then(function () {
  2100. return _this._loader._loadUriAsync(context, uri);
  2101. });
  2102. }
  2103. return null;
  2104. };
  2105. /**
  2106. * Gets an array of LOD properties from lowest to highest.
  2107. */
  2108. MSFT_lod.prototype._getLODs = function (context, property, array, ids) {
  2109. if (this.maxLODsToLoad <= 0) {
  2110. throw new Error("maxLODsToLoad must be greater than zero");
  2111. }
  2112. var properties = new Array();
  2113. for (var i = ids.length - 1; i >= 0; i--) {
  2114. properties.push(GLTF2.GLTFLoader._GetProperty(context + "/ids/" + ids[i], array, ids[i]));
  2115. if (properties.length === this.maxLODsToLoad) {
  2116. return properties;
  2117. }
  2118. }
  2119. properties.push(property);
  2120. return properties;
  2121. };
  2122. return MSFT_lod;
  2123. }(GLTF2.GLTFLoaderExtension));
  2124. Extensions.MSFT_lod = MSFT_lod;
  2125. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new MSFT_lod(loader); });
  2126. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2127. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2128. })(BABYLON || (BABYLON = {}));
  2129. //# sourceMappingURL=MSFT_lod.js.map
  2130. "use strict";
  2131. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2132. var __extends = (this && this.__extends) || (function () {
  2133. var extendStatics = Object.setPrototypeOf ||
  2134. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2135. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2136. return function (d, b) {
  2137. extendStatics(d, b);
  2138. function __() { this.constructor = d; }
  2139. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2140. };
  2141. })();
  2142. var BABYLON;
  2143. (function (BABYLON) {
  2144. var GLTF2;
  2145. (function (GLTF2) {
  2146. var Extensions;
  2147. (function (Extensions) {
  2148. // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression
  2149. var NAME = "KHR_draco_mesh_compression";
  2150. var KHR_draco_mesh_compression = /** @class */ (function (_super) {
  2151. __extends(KHR_draco_mesh_compression, _super);
  2152. function KHR_draco_mesh_compression(loader) {
  2153. var _this = _super.call(this, loader) || this;
  2154. _this.name = NAME;
  2155. _this._dracoCompression = null;
  2156. // Disable extension if decoder is not available.
  2157. if (!BABYLON.DracoCompression.DecoderUrl) {
  2158. _this.enabled = false;
  2159. }
  2160. return _this;
  2161. }
  2162. KHR_draco_mesh_compression.prototype.dispose = function () {
  2163. if (this._dracoCompression) {
  2164. this._dracoCompression.dispose();
  2165. }
  2166. _super.prototype.dispose.call(this);
  2167. };
  2168. KHR_draco_mesh_compression.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
  2169. var _this = this;
  2170. return this._loadExtensionAsync(context, primitive, function (extensionContext, extension) {
  2171. if (primitive.mode != undefined) {
  2172. if (primitive.mode !== 5 /* TRIANGLE_STRIP */ &&
  2173. primitive.mode !== 4 /* TRIANGLES */) {
  2174. throw new Error(context + ": Unsupported mode " + primitive.mode);
  2175. }
  2176. // TODO: handle triangle strips
  2177. if (primitive.mode === 5 /* TRIANGLE_STRIP */) {
  2178. throw new Error(context + ": Mode " + primitive.mode + " is not currently supported");
  2179. }
  2180. }
  2181. var attributes = {};
  2182. var loadAttribute = function (name, kind) {
  2183. var uniqueId = extension.attributes[name];
  2184. if (uniqueId == undefined) {
  2185. return;
  2186. }
  2187. babylonMesh._delayInfo = babylonMesh._delayInfo || [];
  2188. if (babylonMesh._delayInfo.indexOf(kind) === -1) {
  2189. babylonMesh._delayInfo.push(kind);
  2190. }
  2191. attributes[kind] = uniqueId;
  2192. };
  2193. loadAttribute("POSITION", BABYLON.VertexBuffer.PositionKind);
  2194. loadAttribute("NORMAL", BABYLON.VertexBuffer.NormalKind);
  2195. loadAttribute("TANGENT", BABYLON.VertexBuffer.TangentKind);
  2196. loadAttribute("TEXCOORD_0", BABYLON.VertexBuffer.UVKind);
  2197. loadAttribute("TEXCOORD_1", BABYLON.VertexBuffer.UV2Kind);
  2198. loadAttribute("JOINTS_0", BABYLON.VertexBuffer.MatricesIndicesKind);
  2199. loadAttribute("WEIGHTS_0", BABYLON.VertexBuffer.MatricesWeightsKind);
  2200. loadAttribute("COLOR_0", BABYLON.VertexBuffer.ColorKind);
  2201. var bufferView = GLTF2.GLTFLoader._GetProperty(extensionContext, _this._loader._gltf.bufferViews, extension.bufferView);
  2202. return _this._loader._loadBufferViewAsync("#/bufferViews/" + bufferView._index, bufferView).then(function (data) {
  2203. try {
  2204. if (!_this._dracoCompression) {
  2205. _this._dracoCompression = new BABYLON.DracoCompression();
  2206. }
  2207. return _this._dracoCompression.decodeMeshAsync(data, attributes);
  2208. }
  2209. catch (e) {
  2210. throw new Error(context + ": " + e.message);
  2211. }
  2212. });
  2213. });
  2214. };
  2215. return KHR_draco_mesh_compression;
  2216. }(GLTF2.GLTFLoaderExtension));
  2217. Extensions.KHR_draco_mesh_compression = KHR_draco_mesh_compression;
  2218. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_draco_mesh_compression(loader); });
  2219. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2220. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2221. })(BABYLON || (BABYLON = {}));
  2222. //# sourceMappingURL=KHR_draco_mesh_compression.js.map
  2223. "use strict";
  2224. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2225. var __extends = (this && this.__extends) || (function () {
  2226. var extendStatics = Object.setPrototypeOf ||
  2227. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2228. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2229. return function (d, b) {
  2230. extendStatics(d, b);
  2231. function __() { this.constructor = d; }
  2232. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2233. };
  2234. })();
  2235. var BABYLON;
  2236. (function (BABYLON) {
  2237. var GLTF2;
  2238. (function (GLTF2) {
  2239. var Extensions;
  2240. (function (Extensions) {
  2241. // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness
  2242. var NAME = "KHR_materials_pbrSpecularGlossiness";
  2243. var KHR_materials_pbrSpecularGlossiness = /** @class */ (function (_super) {
  2244. __extends(KHR_materials_pbrSpecularGlossiness, _super);
  2245. function KHR_materials_pbrSpecularGlossiness() {
  2246. var _this = _super !== null && _super.apply(this, arguments) || this;
  2247. _this.name = NAME;
  2248. return _this;
  2249. }
  2250. KHR_materials_pbrSpecularGlossiness.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
  2251. var _this = this;
  2252. return this._loadExtensionAsync(context, material, function (context, extension) {
  2253. material._babylonData = material._babylonData || {};
  2254. var babylonData = material._babylonData[babylonDrawMode];
  2255. if (!babylonData) {
  2256. var promises = new Array();
  2257. var name_1 = material.name || "materialSG_" + material._index;
  2258. var babylonMaterial = _this._loader._createMaterial(BABYLON.PBRMaterial, name_1, babylonDrawMode);
  2259. promises.push(_this._loader._loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  2260. promises.push(_this._loadSpecularGlossinessPropertiesAsync(context, material, extension, babylonMaterial));
  2261. _this._loader.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
  2262. babylonData = {
  2263. material: babylonMaterial,
  2264. meshes: [],
  2265. loaded: Promise.all(promises).then(function () { })
  2266. };
  2267. material._babylonData[babylonDrawMode] = babylonData;
  2268. }
  2269. babylonData.meshes.push(babylonMesh);
  2270. assign(babylonData.material);
  2271. return babylonData.loaded;
  2272. });
  2273. };
  2274. KHR_materials_pbrSpecularGlossiness.prototype._loadSpecularGlossinessPropertiesAsync = function (context, material, properties, babylonMaterial) {
  2275. var promises = new Array();
  2276. if (properties.diffuseFactor) {
  2277. babylonMaterial.albedoColor = BABYLON.Color3.FromArray(properties.diffuseFactor);
  2278. babylonMaterial.alpha = properties.diffuseFactor[3];
  2279. }
  2280. else {
  2281. babylonMaterial.albedoColor = BABYLON.Color3.White();
  2282. }
  2283. babylonMaterial.reflectivityColor = properties.specularFactor ? BABYLON.Color3.FromArray(properties.specularFactor) : BABYLON.Color3.White();
  2284. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  2285. if (properties.diffuseTexture) {
  2286. promises.push(this._loader._loadTextureAsync(context + "/diffuseTexture", properties.diffuseTexture, function (texture) {
  2287. babylonMaterial.albedoTexture = texture;
  2288. }));
  2289. }
  2290. if (properties.specularGlossinessTexture) {
  2291. promises.push(this._loader._loadTextureAsync(context + "/specularGlossinessTexture", properties.specularGlossinessTexture, function (texture) {
  2292. babylonMaterial.reflectivityTexture = texture;
  2293. }));
  2294. babylonMaterial.reflectivityTexture.hasAlpha = true;
  2295. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  2296. }
  2297. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  2298. return Promise.all(promises).then(function () { });
  2299. };
  2300. return KHR_materials_pbrSpecularGlossiness;
  2301. }(GLTF2.GLTFLoaderExtension));
  2302. Extensions.KHR_materials_pbrSpecularGlossiness = KHR_materials_pbrSpecularGlossiness;
  2303. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_materials_pbrSpecularGlossiness(loader); });
  2304. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2305. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2306. })(BABYLON || (BABYLON = {}));
  2307. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map
  2308. "use strict";
  2309. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2310. var __extends = (this && this.__extends) || (function () {
  2311. var extendStatics = Object.setPrototypeOf ||
  2312. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  2313. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  2314. return function (d, b) {
  2315. extendStatics(d, b);
  2316. function __() { this.constructor = d; }
  2317. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  2318. };
  2319. })();
  2320. var BABYLON;
  2321. (function (BABYLON) {
  2322. var GLTF2;
  2323. (function (GLTF2) {
  2324. var Extensions;
  2325. (function (Extensions) {
  2326. // https://github.com/MiiBond/glTF/tree/khr_lights_v1/extensions/Khronos/KHR_lights
  2327. var NAME = "KHR_lights";
  2328. var LightType;
  2329. (function (LightType) {
  2330. LightType["AMBIENT"] = "ambient";
  2331. LightType["DIRECTIONAL"] = "directional";
  2332. LightType["POINT"] = "point";
  2333. LightType["SPOT"] = "spot";
  2334. })(LightType || (LightType = {}));
  2335. var KHR_lights = /** @class */ (function (_super) {
  2336. __extends(KHR_lights, _super);
  2337. function KHR_lights() {
  2338. var _this = _super !== null && _super.apply(this, arguments) || this;
  2339. _this.name = NAME;
  2340. return _this;
  2341. }
  2342. KHR_lights.prototype._loadSceneAsync = function (context, scene) {
  2343. var _this = this;
  2344. return this._loadExtensionAsync(context, scene, function (context, extension) {
  2345. var promise = _this._loader._loadSceneAsync(context, scene);
  2346. var light = GLTF2.GLTFLoader._GetProperty(context, _this._lights, extension.light);
  2347. if (light.type !== LightType.AMBIENT) {
  2348. throw new Error(context + ": Only ambient lights are allowed on a scene");
  2349. }
  2350. _this._loader._babylonScene.ambientColor = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.Black();
  2351. return promise;
  2352. });
  2353. };
  2354. KHR_lights.prototype._loadNodeAsync = function (context, node) {
  2355. var _this = this;
  2356. return this._loadExtensionAsync(context, node, function (context, extension) {
  2357. var promise = _this._loader._loadNodeAsync(context, node);
  2358. var babylonLight;
  2359. var light = GLTF2.GLTFLoader._GetProperty(context, _this._lights, extension.light);
  2360. var name = node._babylonMesh.name;
  2361. switch (light.type) {
  2362. case LightType.AMBIENT: {
  2363. throw new Error(context + ": Ambient lights are not allowed on a node");
  2364. }
  2365. case LightType.DIRECTIONAL: {
  2366. babylonLight = new BABYLON.DirectionalLight(name, BABYLON.Vector3.Forward(), _this._loader._babylonScene);
  2367. break;
  2368. }
  2369. case LightType.POINT: {
  2370. babylonLight = new BABYLON.PointLight(name, BABYLON.Vector3.Zero(), _this._loader._babylonScene);
  2371. break;
  2372. }
  2373. case LightType.SPOT: {
  2374. var spotLight = light;
  2375. // TODO: support inner and outer cone angles
  2376. //const innerConeAngle = spotLight.innerConeAngle || 0;
  2377. var outerConeAngle = spotLight.outerConeAngle || Math.PI / 4;
  2378. babylonLight = new BABYLON.SpotLight(name, BABYLON.Vector3.Zero(), BABYLON.Vector3.Forward(), outerConeAngle, 2, _this._loader._babylonScene);
  2379. break;
  2380. }
  2381. default: {
  2382. throw new Error(context + ": Invalid light type (" + light.type + ")");
  2383. }
  2384. }
  2385. babylonLight.diffuse = light.color ? BABYLON.Color3.FromArray(light.color) : BABYLON.Color3.White();
  2386. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  2387. babylonLight.parent = node._babylonMesh;
  2388. return promise;
  2389. });
  2390. };
  2391. Object.defineProperty(KHR_lights.prototype, "_lights", {
  2392. get: function () {
  2393. var extensions = this._loader._gltf.extensions;
  2394. if (!extensions || !extensions[this.name]) {
  2395. throw new Error("#/extensions: '" + this.name + "' not found");
  2396. }
  2397. var extension = extensions[this.name];
  2398. return extension.lights;
  2399. },
  2400. enumerable: true,
  2401. configurable: true
  2402. });
  2403. return KHR_lights;
  2404. }(GLTF2.GLTFLoaderExtension));
  2405. Extensions.KHR_lights = KHR_lights;
  2406. GLTF2.GLTFLoader._Register(NAME, function (loader) { return new KHR_lights(loader); });
  2407. })(Extensions = GLTF2.Extensions || (GLTF2.Extensions = {}));
  2408. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  2409. })(BABYLON || (BABYLON = {}));
  2410. //# sourceMappingURL=KHR_lights.js.map