babylon.glTF2FileLoader.js 163 KB

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