babylon.glTF2FileLoader.js 163 KB

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