babylon.glTF2FileLoader.js 158 KB

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