babylon.glTF2FileLoader.js 128 KB

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