babylon.glTF2FileLoader.js 154 KB

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