babylon.glTF2FileLoader.js 153 KB

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