GLTF2Loader.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  1. /**
  2. * @author Rich Tibbett / https://github.com/richtr
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author Tony Parisi / http://www.tonyparisi.com/
  5. * @author Takahiro / https://github.com/takahirox
  6. * @author Don McCurdy / https://www.donmccurdy.com
  7. */
  8. THREE.GLTF2Loader = ( function () {
  9. function GLTF2Loader( manager ) {
  10. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  11. }
  12. GLTF2Loader.prototype = {
  13. constructor: GLTF2Loader,
  14. crossOrigin: 'Anonymous',
  15. load: function ( url, onLoad, onProgress, onError ) {
  16. var scope = this;
  17. var path = this.path && ( typeof this.path === 'string' ) ? this.path : THREE.Loader.prototype.extractUrlBase( url );
  18. var loader = new THREE.FileLoader( scope.manager );
  19. loader.setResponseType( 'arraybuffer' );
  20. loader.load( url, function ( data ) {
  21. try {
  22. scope.parse( data, path, onLoad, onError );
  23. } catch ( e ) {
  24. // For SyntaxError or TypeError, return a generic failure message.
  25. onError( e.constructor === Error ? e : new Error( 'THREE.GLTF2Loader: Unable to parse model.' ) );
  26. }
  27. }, onProgress, onError );
  28. },
  29. setCrossOrigin: function ( value ) {
  30. this.crossOrigin = value;
  31. },
  32. setPath: function ( value ) {
  33. this.path = value;
  34. },
  35. parse: function ( data, path, onLoad, onError ) {
  36. var content;
  37. var extensions = {};
  38. var magic = convertUint8ArrayToString( new Uint8Array( data, 0, 4 ) );
  39. if ( magic === BINARY_EXTENSION_HEADER_MAGIC ) {
  40. extensions[ EXTENSIONS.KHR_BINARY_GLTF ] = new GLTFBinaryExtension( data );
  41. content = extensions[ EXTENSIONS.KHR_BINARY_GLTF ].content;
  42. } else {
  43. content = convertUint8ArrayToString( new Uint8Array( data ) );
  44. }
  45. var json = JSON.parse( content );
  46. if ( json.asset.version[0] < 2 ) {
  47. onError( new Error( 'THREE.GLTF2Loader: Legacy glTF detected. Use THREE.GLTFLoader instead.' ) );
  48. return;
  49. }
  50. if ( json.extensionsUsed ) {
  51. if( json.extensionsUsed.indexOf( EXTENSIONS.KHR_LIGHTS ) >= 0 ) {
  52. extensions[ EXTENSIONS.KHR_LIGHTS ] = new GLTFLightsExtension( json );
  53. }
  54. if( json.extensionsUsed.indexOf( EXTENSIONS.KHR_MATERIALS_COMMON ) >= 0 ) {
  55. extensions[ EXTENSIONS.KHR_MATERIALS_COMMON ] = new GLTFMaterialsCommonExtension( json );
  56. }
  57. if( json.extensionsUsed.indexOf( EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ) >= 0 ) {
  58. extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] = new GLTFMaterialsPbrSpecularGlossinessExtension();
  59. }
  60. }
  61. console.time( 'GLTF2Loader' );
  62. var parser = new GLTFParser( json, extensions, {
  63. path: path || this.path,
  64. crossOrigin: this.crossOrigin
  65. } );
  66. parser.parse( function ( scene, scenes, cameras, animations ) {
  67. console.timeEnd( 'GLTF2Loader' );
  68. var glTF = {
  69. scene: scene,
  70. scenes: scenes,
  71. cameras: cameras,
  72. animations: animations
  73. };
  74. onLoad( glTF );
  75. }, onError );
  76. }
  77. };
  78. /* GLTFREGISTRY */
  79. function GLTFRegistry() {
  80. var objects = {};
  81. return {
  82. get: function ( key ) {
  83. return objects[ key ];
  84. },
  85. add: function ( key, object ) {
  86. objects[ key ] = object;
  87. },
  88. remove: function ( key ) {
  89. delete objects[ key ];
  90. },
  91. removeAll: function () {
  92. objects = {};
  93. },
  94. update: function ( scene, camera ) {
  95. for ( var name in objects ) {
  96. var object = objects[ name ];
  97. if ( object.update ) {
  98. object.update( scene, camera );
  99. }
  100. }
  101. }
  102. };
  103. }
  104. /*********************************/
  105. /********** EXTENSIONS ***********/
  106. /*********************************/
  107. var EXTENSIONS = {
  108. KHR_BINARY_GLTF: 'KHR_binary_glTF',
  109. KHR_LIGHTS: 'KHR_lights',
  110. KHR_MATERIALS_COMMON: 'KHR_materials_common',
  111. KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness',
  112. KHR_TECHNIQUE_WEBGL: 'KHR_technique_webgl',
  113. };
  114. /**
  115. * Lights Extension
  116. *
  117. * Specification: PENDING
  118. */
  119. function GLTFLightsExtension( json ) {
  120. this.name = EXTENSIONS.KHR_LIGHTS;
  121. this.lights = {};
  122. var extension = ( json.extensions && json.extensions[ EXTENSIONS.KHR_LIGHTS ] ) || {};
  123. var lights = extension.lights || {};
  124. for ( var lightId in lights ) {
  125. var light = lights[ lightId ];
  126. var lightNode;
  127. var color = new THREE.Color().fromArray( light.color );
  128. switch ( light.type ) {
  129. case 'directional':
  130. lightNode = new THREE.DirectionalLight( color );
  131. lightNode.position.set( 0, 0, 1 );
  132. break;
  133. case 'point':
  134. lightNode = new THREE.PointLight( color );
  135. break;
  136. case 'spot':
  137. lightNode = new THREE.SpotLight( color );
  138. lightNode.position.set( 0, 0, 1 );
  139. break;
  140. case 'ambient':
  141. lightNode = new THREE.AmbientLight( color );
  142. break;
  143. }
  144. if ( lightNode ) {
  145. if ( light.constantAttenuation !== undefined ) {
  146. lightNode.intensity = light.constantAttenuation;
  147. }
  148. if ( light.linearAttenuation !== undefined ) {
  149. lightNode.distance = 1 / light.linearAttenuation;
  150. }
  151. if ( light.quadraticAttenuation !== undefined ) {
  152. lightNode.decay = light.quadraticAttenuation;
  153. }
  154. if ( light.fallOffAngle !== undefined ) {
  155. lightNode.angle = light.fallOffAngle;
  156. }
  157. if ( light.fallOffExponent !== undefined ) {
  158. console.warn( 'THREE.GLTF2Loader:: light.fallOffExponent not currently supported.' );
  159. }
  160. lightNode.name = light.name || ( 'light_' + lightId );
  161. this.lights[ lightId ] = lightNode;
  162. }
  163. }
  164. }
  165. /**
  166. * Common Materials Extension
  167. *
  168. * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/Khronos/KHR_materials_common
  169. */
  170. function GLTFMaterialsCommonExtension( json ) {
  171. this.name = EXTENSIONS.KHR_MATERIALS_COMMON;
  172. }
  173. GLTFMaterialsCommonExtension.prototype.getMaterialType = function ( material ) {
  174. var khrMaterial = material.extensions[ this.name ];
  175. switch ( khrMaterial.type ) {
  176. case 'commonBlinn' :
  177. case 'commonPhong' :
  178. return THREE.MeshPhongMaterial;
  179. case 'commonLambert' :
  180. return THREE.MeshLambertMaterial;
  181. case 'commonConstant' :
  182. default :
  183. return THREE.MeshBasicMaterial;
  184. }
  185. };
  186. GLTFMaterialsCommonExtension.prototype.extendParams = function ( materialParams, material, dependencies ) {
  187. var khrMaterial = material.extensions[ this.name ];
  188. var keys = [];
  189. // TODO: Currently ignored: 'ambientFactor', 'ambientTexture'
  190. switch ( khrMaterial.type ) {
  191. case 'commonBlinn' :
  192. case 'commonPhong' :
  193. keys.push( 'diffuseFactor', 'diffuseTexture', 'specularFactor', 'specularTexture', 'shininessFactor' );
  194. break;
  195. case 'commonLambert' :
  196. keys.push( 'diffuseFactor', 'diffuseTexture' );
  197. break;
  198. case 'commonConstant' :
  199. default :
  200. break;
  201. }
  202. var materialValues = {};
  203. keys.forEach( function( v ) {
  204. if ( khrMaterial[ v ] !== undefined ) materialValues[ v ] = khrMaterial[ v ];
  205. } );
  206. if ( materialValues.diffuseFactor !== undefined ) {
  207. materialParams.color = new THREE.Color().fromArray( materialValues.diffuseFactor );
  208. materialParams.opacity = materialValues.diffuseFactor[ 3 ];
  209. }
  210. if ( materialValues.diffuseTexture !== undefined ) {
  211. materialParams.map = dependencies.textures[ materialValues.diffuseTexture.index ];
  212. }
  213. if ( materialValues.specularFactor !== undefined ) {
  214. materialParams.specular = new THREE.Color().fromArray( materialValues.specularFactor );
  215. }
  216. if ( materialValues.specularTexture !== undefined ) {
  217. materialParams.specularMap = dependencies.textures[ materialValues.specularTexture.index ];
  218. }
  219. if ( materialValues.shininessFactor !== undefined ) {
  220. materialParams.shininess = materialValues.shininessFactor;
  221. }
  222. };
  223. /* BINARY EXTENSION */
  224. var BINARY_EXTENSION_BUFFER_NAME = 'binary_glTF';
  225. var BINARY_EXTENSION_HEADER_MAGIC = 'glTF';
  226. var BINARY_EXTENSION_HEADER_LENGTH = 12;
  227. var BINARY_EXTENSION_CHUNK_TYPES = { JSON: 0x4E4F534A, BIN: 0x004E4942 };
  228. function GLTFBinaryExtension( data ) {
  229. this.name = EXTENSIONS.KHR_BINARY_GLTF;
  230. this.content = null;
  231. this.body = null;
  232. var headerView = new DataView( data, 0, BINARY_EXTENSION_HEADER_LENGTH );
  233. this.header = {
  234. magic: convertUint8ArrayToString( new Uint8Array( data.slice( 0, 4 ) ) ),
  235. version: headerView.getUint32( 4, true ),
  236. length: headerView.getUint32( 8, true )
  237. };
  238. if ( this.header.magic !== BINARY_EXTENSION_HEADER_MAGIC ) {
  239. throw new Error( 'THREE.GLTF2Loader: Unsupported glTF-Binary header.' );
  240. } else if ( this.header.version < 2.0 ) {
  241. throw new Error( 'THREE.GLTF2Loader: Legacy binary file detected. Use GLTFLoader instead.' );
  242. }
  243. var chunkView = new DataView( data, BINARY_EXTENSION_HEADER_LENGTH );
  244. var chunkIndex = 0;
  245. while ( chunkIndex < chunkView.byteLength ) {
  246. var chunkLength = chunkView.getUint32( chunkIndex, true );
  247. chunkIndex += 4;
  248. var chunkType = chunkView.getUint32( chunkIndex, true );
  249. chunkIndex += 4;
  250. if ( chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON ) {
  251. var contentArray = new Uint8Array( data, BINARY_EXTENSION_HEADER_LENGTH + chunkIndex, chunkLength );
  252. this.content = convertUint8ArrayToString( contentArray );
  253. } else if ( chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN ) {
  254. var byteOffset = BINARY_EXTENSION_HEADER_LENGTH + chunkIndex;
  255. this.body = data.slice( byteOffset, byteOffset + chunkLength );
  256. }
  257. // Clients must ignore chunks with unknown types.
  258. chunkIndex += chunkLength;
  259. }
  260. if ( this.content === null ) {
  261. throw new Error( 'THREE.GLTF2Loader: JSON content not found.' );
  262. }
  263. }
  264. /**
  265. * Specular-Glossiness Extension
  266. *
  267. * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/Khronos/KHR_materials_pbrSpecularGlossiness
  268. */
  269. function GLTFMaterialsPbrSpecularGlossinessExtension() {
  270. return {
  271. name: EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS,
  272. getMaterialType: function () {
  273. return THREE.ShaderMaterial;
  274. },
  275. extendParams: function ( params, material, dependencies ) {
  276. // specification
  277. // https://github.com/sbtron/glTF/tree/KHRpbrSpecGloss/extensions/Khronos/KHR_materials_pbrSpecularGlossiness
  278. var pbrSpecularGlossiness = material.extensions[ this.name ];
  279. var shader = THREE.ShaderLib[ 'standard' ];
  280. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  281. var specularMapParsFragmentChunk = [
  282. '#ifdef USE_SPECULARMAP',
  283. ' uniform sampler2D specularMap;',
  284. '#endif'
  285. ].join( '\n' );
  286. var glossinessMapParsFragmentChunk = [
  287. '#ifdef USE_GLOSSINESSMAP',
  288. ' uniform sampler2D glossinessMap;',
  289. '#endif'
  290. ].join( '\n' );
  291. var specularMapFragmentChunk = [
  292. 'vec3 specularFactor = specular;',
  293. '#ifdef USE_SPECULARMAP',
  294. ' vec4 texelSpecular = texture2D( specularMap, vUv );',
  295. ' // reads channel RGB, compatible with a glTF Specular-Glossiness (RGBA) texture',
  296. ' specularFactor *= texelSpecular.rgb;',
  297. '#endif'
  298. ].join( '\n' );
  299. var glossinessMapFragmentChunk = [
  300. 'float glossinessFactor = glossiness;',
  301. '#ifdef USE_GLOSSINESSMAP',
  302. ' vec4 texelGlossiness = texture2D( glossinessMap, vUv );',
  303. ' // reads channel A, compatible with a glTF Specular-Glossiness (RGBA) texture',
  304. ' glossinessFactor *= texelGlossiness.a;',
  305. '#endif'
  306. ].join( '\n' );
  307. var lightPhysicalFragmentChunk = [
  308. 'PhysicalMaterial material;',
  309. 'material.diffuseColor = diffuseColor.rgb;',
  310. 'material.specularRoughness = clamp( 1.0 - glossinessFactor, 0.04, 1.0 );',
  311. 'material.specularColor = specularFactor.rgb;',
  312. ].join( '\n' );
  313. var fragmentShader = shader.fragmentShader
  314. .replace( '#include <specularmap_fragment>', '' )
  315. .replace( 'uniform float roughness;', 'uniform vec3 specular;' )
  316. .replace( 'uniform float metalness;', 'uniform float glossiness;' )
  317. .replace( '#include <roughnessmap_pars_fragment>', specularMapParsFragmentChunk )
  318. .replace( '#include <metalnessmap_pars_fragment>', glossinessMapParsFragmentChunk )
  319. .replace( '#include <roughnessmap_fragment>', specularMapFragmentChunk )
  320. .replace( '#include <metalnessmap_fragment>', glossinessMapFragmentChunk )
  321. .replace( '#include <lights_physical_fragment>', lightPhysicalFragmentChunk );
  322. delete uniforms.roughness;
  323. delete uniforms.metalness;
  324. delete uniforms.roughnessMap;
  325. delete uniforms.metalnessMap;
  326. uniforms.specular = { value: new THREE.Color().setHex( 0x111111 ) };
  327. uniforms.glossiness = { value: 0.5 };
  328. uniforms.specularMap = { value: null };
  329. uniforms.glossinessMap = { value: null };
  330. params.vertexShader = shader.vertexShader;
  331. params.fragmentShader = fragmentShader;
  332. params.uniforms = uniforms;
  333. params.defines = { 'STANDARD': '' };
  334. params.color = new THREE.Color( 1.0, 1.0, 1.0 );
  335. params.opacity = 1.0;
  336. if ( Array.isArray( pbrSpecularGlossiness.diffuseFactor ) ) {
  337. var array = pbrSpecularGlossiness.diffuseFactor;
  338. params.color.fromArray( array );
  339. params.opacity = array[ 3 ];
  340. }
  341. if ( pbrSpecularGlossiness.diffuseTexture !== undefined ) {
  342. params.map = dependencies.textures[ pbrSpecularGlossiness.diffuseTexture.index ];
  343. }
  344. params.emissive = new THREE.Color( 0.0, 0.0, 0.0 );
  345. params.glossiness = pbrSpecularGlossiness.glossinessFactor !== undefined ? pbrSpecularGlossiness.glossinessFactor : 1.0;
  346. params.specular = new THREE.Color( 1.0, 1.0, 1.0 );
  347. if ( Array.isArray( pbrSpecularGlossiness.specularFactor ) ) {
  348. params.specular.fromArray( pbrSpecularGlossiness.specularFactor );
  349. }
  350. if ( pbrSpecularGlossiness.specularGlossinessTexture !== undefined ) {
  351. params.glossinessMap = dependencies.textures[ pbrSpecularGlossiness.specularGlossinessTexture.index ];
  352. params.specularMap = dependencies.textures[ pbrSpecularGlossiness.specularGlossinessTexture.index ];
  353. }
  354. },
  355. createMaterial: function ( params ) {
  356. // setup material properties based on MeshStandardMaterial for Specular-Glossiness
  357. var material = new THREE.ShaderMaterial( {
  358. defines: params.defines,
  359. vertexShader: params.vertexShader,
  360. fragmentShader: params.fragmentShader,
  361. uniforms: params.uniforms,
  362. fog: true,
  363. lights: true,
  364. opacity: params.opacity,
  365. transparent: params.transparent
  366. } );
  367. material.isGLTFSpecularGlossinessMaterial = true;
  368. material.color = params.color;
  369. material.map = params.map === undefined ? null : params.map;
  370. material.lightMap = null;
  371. material.lightMapIntensity = 1.0;
  372. material.aoMap = params.aoMap === undefined ? null : params.aoMap;
  373. material.aoMapIntensity = 1.0;
  374. material.emissive = params.emissive;
  375. material.emissiveIntensity = 1.0;
  376. material.emissiveMap = params.emissiveMap === undefined ? null : params.emissiveMap;
  377. material.bumpMap = params.bumpMap === undefined ? null : params.bumpMap;
  378. material.bumpScale = 1;
  379. material.normalMap = params.normalMap === undefined ? null : params.normalMap;
  380. material.normalScale = new THREE.Vector2( 1, 1 );
  381. material.displacementMap = null;
  382. material.displacementScale = 1;
  383. material.displacementBias = 0;
  384. material.specularMap = params.specularMap === undefined ? null : params.specularMap;
  385. material.specular = params.specular;
  386. material.glossinessMap = params.glossinessMap === undefined ? null : params.glossinessMap;
  387. material.glossiness = params.glossiness;
  388. material.alphaMap = null;
  389. material.envMap = params.envMap === undefined ? null : params.envMap;
  390. material.envMapIntensity = 1.0;
  391. material.refractionRatio = 0.98;
  392. material.extensions.derivatives = true;
  393. return material;
  394. },
  395. // Here's based on refreshUniformsCommon() and refreshUniformsStandard() in WebGLRenderer.
  396. refreshUniforms: function ( renderer, scene, camera, geometry, material, group ) {
  397. var uniforms = material.uniforms;
  398. var defines = material.defines;
  399. uniforms.opacity.value = material.opacity;
  400. uniforms.diffuse.value.copy( material.color );
  401. uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity );
  402. uniforms.map.value = material.map;
  403. uniforms.specularMap.value = material.specularMap;
  404. uniforms.alphaMap.value = material.alphaMap;
  405. uniforms.lightMap.value = material.lightMap;
  406. uniforms.lightMapIntensity.value = material.lightMapIntensity;
  407. uniforms.aoMap.value = material.aoMap;
  408. uniforms.aoMapIntensity.value = material.aoMapIntensity;
  409. // uv repeat and offset setting priorities
  410. // 1. color map
  411. // 2. specular map
  412. // 3. normal map
  413. // 4. bump map
  414. // 5. alpha map
  415. // 6. emissive map
  416. var uvScaleMap;
  417. if ( material.map ) {
  418. uvScaleMap = material.map;
  419. } else if ( material.specularMap ) {
  420. uvScaleMap = material.specularMap;
  421. } else if ( material.displacementMap ) {
  422. uvScaleMap = material.displacementMap;
  423. } else if ( material.normalMap ) {
  424. uvScaleMap = material.normalMap;
  425. } else if ( material.bumpMap ) {
  426. uvScaleMap = material.bumpMap;
  427. } else if ( material.glossinessMap ) {
  428. uvScaleMap = material.glossinessMap;
  429. } else if ( material.alphaMap ) {
  430. uvScaleMap = material.alphaMap;
  431. } else if ( material.emissiveMap ) {
  432. uvScaleMap = material.emissiveMap;
  433. }
  434. if ( uvScaleMap !== undefined ) {
  435. // backwards compatibility
  436. if ( uvScaleMap.isWebGLRenderTarget ) {
  437. uvScaleMap = uvScaleMap.texture;
  438. }
  439. var offset = uvScaleMap.offset;
  440. var repeat = uvScaleMap.repeat;
  441. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  442. }
  443. uniforms.envMap.value = material.envMap;
  444. uniforms.envMapIntensity.value = material.envMapIntensity;
  445. uniforms.flipEnvMap.value = ( material.envMap && material.envMap.isCubeTexture ) ? -1 : 1;
  446. uniforms.refractionRatio.value = material.refractionRatio;
  447. uniforms.specular.value.copy( material.specular );
  448. uniforms.glossiness.value = material.glossiness;
  449. uniforms.glossinessMap.value = material.glossinessMap;
  450. uniforms.emissiveMap.value = material.emissiveMap;
  451. uniforms.bumpMap.value = material.bumpMap;
  452. uniforms.normalMap.value = material.normalMap;
  453. uniforms.displacementMap.value = material.displacementMap;
  454. uniforms.displacementScale.value = material.displacementScale;
  455. uniforms.displacementBias.value = material.displacementBias;
  456. if ( uniforms.glossinessMap.value !== null && defines.USE_GLOSSINESSMAP === undefined ) {
  457. defines.USE_GLOSSINESSMAP = '';
  458. // set USE_ROUGHNESSMAP to enable vUv
  459. defines.USE_ROUGHNESSMAP = ''
  460. }
  461. if ( uniforms.glossinessMap.value === null && defines.USE_GLOSSINESSMAP !== undefined ) {
  462. delete defines.USE_GLOSSINESSMAP;
  463. delete defines.USE_ROUGHNESSMAP;
  464. }
  465. }
  466. };
  467. }
  468. /*********************************/
  469. /********** INTERNALS ************/
  470. /*********************************/
  471. /* CONSTANTS */
  472. var WEBGL_CONSTANTS = {
  473. FLOAT: 5126,
  474. //FLOAT_MAT2: 35674,
  475. FLOAT_MAT3: 35675,
  476. FLOAT_MAT4: 35676,
  477. FLOAT_VEC2: 35664,
  478. FLOAT_VEC3: 35665,
  479. FLOAT_VEC4: 35666,
  480. LINEAR: 9729,
  481. REPEAT: 10497,
  482. SAMPLER_2D: 35678,
  483. LINES: 1,
  484. LINE_LOOP: 2,
  485. LINE_STRIP: 3,
  486. TRIANGLES: 4,
  487. TRIANGLE_STRIP: 5,
  488. TRIANGLE_FAN: 6,
  489. UNSIGNED_BYTE: 5121,
  490. UNSIGNED_SHORT: 5123
  491. };
  492. var WEBGL_TYPE = {
  493. 5126: Number,
  494. //35674: THREE.Matrix2,
  495. 35675: THREE.Matrix3,
  496. 35676: THREE.Matrix4,
  497. 35664: THREE.Vector2,
  498. 35665: THREE.Vector3,
  499. 35666: THREE.Vector4,
  500. 35678: THREE.Texture
  501. };
  502. var WEBGL_COMPONENT_TYPES = {
  503. 5120: Int8Array,
  504. 5121: Uint8Array,
  505. 5122: Int16Array,
  506. 5123: Uint16Array,
  507. 5125: Uint32Array,
  508. 5126: Float32Array
  509. };
  510. var WEBGL_FILTERS = {
  511. 9728: THREE.NearestFilter,
  512. 9729: THREE.LinearFilter,
  513. 9984: THREE.NearestMipMapNearestFilter,
  514. 9985: THREE.LinearMipMapNearestFilter,
  515. 9986: THREE.NearestMipMapLinearFilter,
  516. 9987: THREE.LinearMipMapLinearFilter
  517. };
  518. var WEBGL_WRAPPINGS = {
  519. 33071: THREE.ClampToEdgeWrapping,
  520. 33648: THREE.MirroredRepeatWrapping,
  521. 10497: THREE.RepeatWrapping
  522. };
  523. var WEBGL_TEXTURE_FORMATS = {
  524. 6406: THREE.AlphaFormat,
  525. 6407: THREE.RGBFormat,
  526. 6408: THREE.RGBAFormat,
  527. 6409: THREE.LuminanceFormat,
  528. 6410: THREE.LuminanceAlphaFormat
  529. };
  530. var WEBGL_TEXTURE_DATATYPES = {
  531. 5121: THREE.UnsignedByteType,
  532. 32819: THREE.UnsignedShort4444Type,
  533. 32820: THREE.UnsignedShort5551Type,
  534. 33635: THREE.UnsignedShort565Type
  535. };
  536. var WEBGL_SIDES = {
  537. 1028: THREE.BackSide, // Culling front
  538. 1029: THREE.FrontSide // Culling back
  539. //1032: THREE.NoSide // Culling front and back, what to do?
  540. };
  541. var WEBGL_DEPTH_FUNCS = {
  542. 512: THREE.NeverDepth,
  543. 513: THREE.LessDepth,
  544. 514: THREE.EqualDepth,
  545. 515: THREE.LessEqualDepth,
  546. 516: THREE.GreaterEqualDepth,
  547. 517: THREE.NotEqualDepth,
  548. 518: THREE.GreaterEqualDepth,
  549. 519: THREE.AlwaysDepth
  550. };
  551. var WEBGL_BLEND_EQUATIONS = {
  552. 32774: THREE.AddEquation,
  553. 32778: THREE.SubtractEquation,
  554. 32779: THREE.ReverseSubtractEquation
  555. };
  556. var WEBGL_BLEND_FUNCS = {
  557. 0: THREE.ZeroFactor,
  558. 1: THREE.OneFactor,
  559. 768: THREE.SrcColorFactor,
  560. 769: THREE.OneMinusSrcColorFactor,
  561. 770: THREE.SrcAlphaFactor,
  562. 771: THREE.OneMinusSrcAlphaFactor,
  563. 772: THREE.DstAlphaFactor,
  564. 773: THREE.OneMinusDstAlphaFactor,
  565. 774: THREE.DstColorFactor,
  566. 775: THREE.OneMinusDstColorFactor,
  567. 776: THREE.SrcAlphaSaturateFactor
  568. // The followings are not supported by Three.js yet
  569. //32769: CONSTANT_COLOR,
  570. //32770: ONE_MINUS_CONSTANT_COLOR,
  571. //32771: CONSTANT_ALPHA,
  572. //32772: ONE_MINUS_CONSTANT_COLOR
  573. };
  574. var WEBGL_TYPE_SIZES = {
  575. 'SCALAR': 1,
  576. 'VEC2': 2,
  577. 'VEC3': 3,
  578. 'VEC4': 4,
  579. 'MAT2': 4,
  580. 'MAT3': 9,
  581. 'MAT4': 16
  582. };
  583. var PATH_PROPERTIES = {
  584. scale: 'scale',
  585. translation: 'position',
  586. rotation: 'quaternion',
  587. weights: 'morphTargetInfluences'
  588. };
  589. var INTERPOLATION = {
  590. CATMULLROMSPLINE: THREE.InterpolateSmooth,
  591. CUBICSPLINE: THREE.InterpolateSmooth,
  592. LINEAR: THREE.InterpolateLinear,
  593. STEP: THREE.InterpolateDiscrete
  594. };
  595. var STATES_ENABLES = {
  596. 2884: 'CULL_FACE',
  597. 2929: 'DEPTH_TEST',
  598. 3042: 'BLEND',
  599. 3089: 'SCISSOR_TEST',
  600. 32823: 'POLYGON_OFFSET_FILL',
  601. 32926: 'SAMPLE_ALPHA_TO_COVERAGE'
  602. };
  603. var ALPHA_MODES = {
  604. OPAQUE: 'OPAQUE',
  605. MASK: 'MASK',
  606. BLEND: 'BLEND'
  607. };
  608. /* UTILITY FUNCTIONS */
  609. function _each( object, callback, thisObj ) {
  610. if ( !object ) {
  611. return Promise.resolve();
  612. }
  613. var results;
  614. var fns = [];
  615. if ( Object.prototype.toString.call( object ) === '[object Array]' ) {
  616. results = [];
  617. var length = object.length;
  618. for ( var idx = 0; idx < length; idx ++ ) {
  619. var value = callback.call( thisObj || this, object[ idx ], idx );
  620. if ( value ) {
  621. fns.push( value );
  622. if ( value instanceof Promise ) {
  623. value.then( function( key, value ) {
  624. results[ key ] = value;
  625. }.bind( this, idx ));
  626. } else {
  627. results[ idx ] = value;
  628. }
  629. }
  630. }
  631. } else {
  632. results = {};
  633. for ( var key in object ) {
  634. if ( object.hasOwnProperty( key ) ) {
  635. var value = callback.call( thisObj || this, object[ key ], key );
  636. if ( value ) {
  637. fns.push( value );
  638. if ( value instanceof Promise ) {
  639. value.then( function( key, value ) {
  640. results[ key ] = value;
  641. }.bind( this, key ));
  642. } else {
  643. results[ key ] = value;
  644. }
  645. }
  646. }
  647. }
  648. }
  649. return Promise.all( fns ).then( function() {
  650. return results;
  651. });
  652. }
  653. function resolveURL( url, path ) {
  654. // Invalid URL
  655. if ( typeof url !== 'string' || url === '' )
  656. return '';
  657. // Absolute URL http://,https://,//
  658. if ( /^(https?:)?\/\//i.test( url ) ) {
  659. return url;
  660. }
  661. // Data URI
  662. if ( /^data:.*,.*$/i.test( url ) ) {
  663. return url;
  664. }
  665. // Blob URL
  666. if ( /^blob:.*$/i.test( url ) ) {
  667. return url;
  668. }
  669. // Relative URL
  670. return ( path || '' ) + url;
  671. }
  672. function convertUint8ArrayToString( array ) {
  673. if ( window.TextDecoder !== undefined ) {
  674. return new TextDecoder().decode( array );
  675. }
  676. // Avoid the String.fromCharCode.apply(null, array) shortcut, which
  677. // throws a "maximum call stack size exceeded" error for large arrays.
  678. var s = '';
  679. for ( var i = 0, il = array.length; i < il; i ++ ) {
  680. s += String.fromCharCode( array[ i ] );
  681. }
  682. return s;
  683. }
  684. /**
  685. * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#default-material
  686. */
  687. function createDefaultMaterial() {
  688. return new THREE.MeshStandardMaterial( {
  689. color: 0xFFFFFF,
  690. emissive: 0x000000,
  691. metalness: 1,
  692. roughness: 1,
  693. transparent: false,
  694. depthTest: true,
  695. side: THREE.FrontSide
  696. } );
  697. }
  698. /**
  699. * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#morph-targets
  700. * @param {THREE.Mesh} mesh
  701. * @param {GLTF.Mesh} meshDef
  702. * @param {GLTF.Primitive} primitiveDef
  703. * @param {Object} dependencies
  704. */
  705. function addMorphTargets ( mesh, meshDef, primitiveDef, dependencies ) {
  706. var geometry = mesh.geometry;
  707. var material = mesh.material;
  708. var targets = primitiveDef.targets;
  709. var morphAttributes = geometry.morphAttributes;
  710. morphAttributes.position = [];
  711. morphAttributes.normal = [];
  712. material.morphTargets = true;
  713. for ( var i = 0, il = targets.length; i < il; i ++ ) {
  714. var target = targets[ i ];
  715. var attributeName = 'morphTarget' + i;
  716. var positionAttribute, normalAttribute;
  717. if ( target.POSITION !== undefined ) {
  718. // Three.js morph formula is
  719. // position
  720. // + weight0 * ( morphTarget0 - position )
  721. // + weight1 * ( morphTarget1 - position )
  722. // ...
  723. // while the glTF one is
  724. // position
  725. // + weight0 * morphTarget0
  726. // + weight1 * morphTarget1
  727. // ...
  728. // then adding position to morphTarget.
  729. // So morphTarget value will depend on mesh's position, then cloning attribute
  730. // for the case if attribute is shared among two or more meshes.
  731. positionAttribute = dependencies.accessors[ target.POSITION ].clone();
  732. var position = geometry.attributes.position;
  733. for ( var j = 0, jl = positionAttribute.count; j < jl; j ++ ) {
  734. positionAttribute.setXYZ(
  735. j,
  736. positionAttribute.getX( j ) + position.getX( j ),
  737. positionAttribute.getY( j ) + position.getY( j ),
  738. positionAttribute.getZ( j ) + position.getZ( j )
  739. );
  740. }
  741. } else {
  742. // Copying the original position not to affect the final position.
  743. // See the formula above.
  744. positionAttribute = geometry.attributes.position.clone();
  745. }
  746. if ( target.NORMAL !== undefined ) {
  747. material.morphNormals = true;
  748. // see target.POSITION's comment
  749. normalAttribute = dependencies.accessors[ target.NORMAL ].clone();
  750. var normal = geometry.attributes.normal;
  751. for ( var j = 0, jl = normalAttribute.count; j < jl; j ++ ) {
  752. normalAttribute.setXYZ(
  753. j,
  754. normalAttribute.getX( j ) + normal.getX( j ),
  755. normalAttribute.getY( j ) + normal.getY( j ),
  756. normalAttribute.getZ( j ) + normal.getZ( j )
  757. );
  758. }
  759. } else {
  760. normalAttribute = geometry.attributes.normal.clone();
  761. }
  762. if ( target.TANGENT !== undefined ) {
  763. // TODO: implement
  764. }
  765. positionAttribute.name = attributeName;
  766. normalAttribute.name = attributeName;
  767. morphAttributes.position.push( positionAttribute );
  768. morphAttributes.normal.push( normalAttribute );
  769. }
  770. mesh.updateMorphTargets();
  771. if ( meshDef.weights !== undefined ) {
  772. for ( var i = 0, il = meshDef.weights.length; i < il; i ++ ) {
  773. mesh.morphTargetInfluences[ i ] = meshDef.weights[ i ];
  774. }
  775. }
  776. }
  777. /* GLTF PARSER */
  778. function GLTFParser( json, extensions, options ) {
  779. this.json = json || {};
  780. this.extensions = extensions || {};
  781. this.options = options || {};
  782. // loader object cache
  783. this.cache = new GLTFRegistry();
  784. }
  785. GLTFParser.prototype._withDependencies = function ( dependencies ) {
  786. var _dependencies = {};
  787. for ( var i = 0; i < dependencies.length; i ++ ) {
  788. var dependency = dependencies[ i ];
  789. var fnName = 'load' + dependency.charAt( 0 ).toUpperCase() + dependency.slice( 1 );
  790. var cached = this.cache.get( dependency );
  791. if ( cached !== undefined ) {
  792. _dependencies[ dependency ] = cached;
  793. } else if ( this[ fnName ] ) {
  794. var fn = this[ fnName ]();
  795. this.cache.add( dependency, fn );
  796. _dependencies[ dependency ] = fn;
  797. }
  798. }
  799. return _each( _dependencies, function ( dependency ) {
  800. return dependency;
  801. } );
  802. };
  803. GLTFParser.prototype.parse = function ( onLoad, onError ) {
  804. var json = this.json;
  805. // Clear the loader cache
  806. this.cache.removeAll();
  807. // Fire the callback on complete
  808. this._withDependencies( [
  809. 'scenes',
  810. 'cameras',
  811. 'animations'
  812. ] ).then( function ( dependencies ) {
  813. var scenes = [];
  814. for ( var name in dependencies.scenes ) {
  815. scenes.push( dependencies.scenes[ name ] );
  816. }
  817. var scene = json.scene !== undefined ? dependencies.scenes[ json.scene ] : scenes[ 0 ];
  818. var cameras = [];
  819. for ( var name in dependencies.cameras ) {
  820. var camera = dependencies.cameras[ name ];
  821. cameras.push( camera );
  822. }
  823. var animations = [];
  824. for ( var name in dependencies.animations ) {
  825. animations.push( dependencies.animations[ name ] );
  826. }
  827. onLoad( scene, scenes, cameras, animations );
  828. } ).catch( onError );
  829. };
  830. GLTFParser.prototype.loadBuffers = function () {
  831. var json = this.json;
  832. var extensions = this.extensions;
  833. var options = this.options;
  834. return _each( json.buffers, function ( buffer, name ) {
  835. if ( buffer.type === 'arraybuffer' || buffer.type === undefined ) {
  836. // If present, GLB container is required to be the first buffer.
  837. if ( buffer.uri === undefined && name === 0 ) {
  838. return extensions[ EXTENSIONS.KHR_BINARY_GLTF ].body;
  839. }
  840. return new Promise( function ( resolve ) {
  841. var loader = new THREE.FileLoader();
  842. loader.setResponseType( 'arraybuffer' );
  843. loader.load( resolveURL( buffer.uri, options.path ), function ( buffer ) {
  844. resolve( buffer );
  845. } );
  846. } );
  847. } else {
  848. console.warn( 'THREE.GLTF2Loader: %s buffer type is not supported.', buffer.type );
  849. }
  850. } );
  851. };
  852. GLTFParser.prototype.loadBufferViews = function () {
  853. var json = this.json;
  854. return this._withDependencies( [
  855. 'buffers'
  856. ] ).then( function ( dependencies ) {
  857. return _each( json.bufferViews, function ( bufferView ) {
  858. var arraybuffer = dependencies.buffers[ bufferView.buffer ];
  859. var byteLength = bufferView.byteLength || 0;
  860. var byteOffset = bufferView.byteOffset || 0;
  861. return arraybuffer.slice( byteOffset, byteOffset + byteLength );
  862. } );
  863. } );
  864. };
  865. GLTFParser.prototype.loadAccessors = function () {
  866. var json = this.json;
  867. return this._withDependencies( [
  868. 'bufferViews'
  869. ] ).then( function ( dependencies ) {
  870. return _each( json.accessors, function ( accessor ) {
  871. var arraybuffer = dependencies.bufferViews[ accessor.bufferView ];
  872. var itemSize = WEBGL_TYPE_SIZES[ accessor.type ];
  873. var TypedArray = WEBGL_COMPONENT_TYPES[ accessor.componentType ];
  874. // For VEC3: itemSize is 3, elementBytes is 4, itemBytes is 12.
  875. var elementBytes = TypedArray.BYTES_PER_ELEMENT;
  876. var itemBytes = elementBytes * itemSize;
  877. var byteStride = json.bufferViews[ accessor.bufferView ].byteStride;
  878. var array;
  879. // The buffer is not interleaved if the stride is the item size in bytes.
  880. if ( byteStride && byteStride !== itemBytes ) {
  881. // Use the full buffer if it's interleaved.
  882. array = new TypedArray( arraybuffer );
  883. // Integer parameters to IB/IBA are in array elements, not bytes.
  884. var ib = new THREE.InterleavedBuffer( array, byteStride / elementBytes );
  885. return new THREE.InterleavedBufferAttribute( ib, itemSize, accessor.byteOffset / elementBytes );
  886. } else {
  887. array = new TypedArray( arraybuffer, accessor.byteOffset, accessor.count * itemSize );
  888. return new THREE.BufferAttribute( array, itemSize );
  889. }
  890. } );
  891. } );
  892. };
  893. GLTFParser.prototype.loadTextures = function () {
  894. var json = this.json;
  895. var options = this.options;
  896. return this._withDependencies( [
  897. 'bufferViews'
  898. ] ).then( function ( dependencies ) {
  899. return _each( json.textures, function ( texture ) {
  900. if ( texture.source !== undefined ) {
  901. return new Promise( function ( resolve ) {
  902. var source = json.images[ texture.source ];
  903. var sourceUri = source.uri;
  904. var urlCreator;
  905. if ( source.bufferView !== undefined ) {
  906. var bufferView = dependencies.bufferViews[ source.bufferView ];
  907. var blob = new Blob( [ bufferView ], { type: source.mimeType } );
  908. urlCreator = window.URL || window.webkitURL;
  909. sourceUri = urlCreator.createObjectURL( blob );
  910. }
  911. var textureLoader = THREE.Loader.Handlers.get( sourceUri );
  912. if ( textureLoader === null ) {
  913. textureLoader = new THREE.TextureLoader();
  914. }
  915. textureLoader.setCrossOrigin( options.crossOrigin );
  916. textureLoader.load( resolveURL( sourceUri, options.path ), function ( _texture ) {
  917. if ( urlCreator !== undefined ) {
  918. urlCreator.revokeObjectURL( sourceUri );
  919. }
  920. _texture.flipY = false;
  921. if ( texture.name !== undefined ) _texture.name = texture.name;
  922. _texture.format = texture.format !== undefined ? WEBGL_TEXTURE_FORMATS[ texture.format ] : THREE.RGBAFormat;
  923. if ( texture.internalFormat !== undefined && _texture.format !== WEBGL_TEXTURE_FORMATS[ texture.internalFormat ] ) {
  924. console.warn( 'THREE.GLTF2Loader: Three.js does not support texture internalFormat which is different from texture format. ' +
  925. 'internalFormat will be forced to be the same value as format.' );
  926. }
  927. _texture.type = texture.type !== undefined ? WEBGL_TEXTURE_DATATYPES[ texture.type ] : THREE.UnsignedByteType;
  928. var samplers = json.samplers || {};
  929. var sampler = samplers[ texture.sampler ] || {};
  930. _texture.magFilter = WEBGL_FILTERS[ sampler.magFilter ] || THREE.LinearFilter;
  931. _texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ] || THREE.LinearMipMapLinearFilter;
  932. _texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || THREE.RepeatWrapping;
  933. _texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || THREE.RepeatWrapping;
  934. resolve( _texture );
  935. }, undefined, function () {
  936. resolve();
  937. } );
  938. } );
  939. }
  940. } );
  941. } );
  942. };
  943. GLTFParser.prototype.loadMaterials = function () {
  944. var json = this.json;
  945. var extensions = this.extensions;
  946. return this._withDependencies( [
  947. 'textures'
  948. ] ).then( function ( dependencies ) {
  949. return _each( json.materials, function ( material ) {
  950. var materialType;
  951. var materialParams = {};
  952. var materialExtensions = material.extensions || {};
  953. if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_COMMON ] ) {
  954. materialType = extensions[ EXTENSIONS.KHR_MATERIALS_COMMON ].getMaterialType( material );
  955. extensions[ EXTENSIONS.KHR_MATERIALS_COMMON ].extendParams( materialParams, material, dependencies );
  956. } else if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] ) {
  957. materialType = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].getMaterialType( material );
  958. extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].extendParams( materialParams, material, dependencies );
  959. } else if ( materialExtensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ] ) {
  960. materialType = extensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ].getMaterialType( material );
  961. extensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ].extendParams( materialParams, material, dependencies );
  962. } else if ( material.pbrMetallicRoughness !== undefined ) {
  963. // Specification:
  964. // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material
  965. materialType = THREE.MeshStandardMaterial;
  966. var metallicRoughness = material.pbrMetallicRoughness;
  967. materialParams.color = new THREE.Color( 1.0, 1.0, 1.0 );
  968. materialParams.opacity = 1.0;
  969. if ( Array.isArray( metallicRoughness.baseColorFactor ) ) {
  970. var array = metallicRoughness.baseColorFactor;
  971. materialParams.color.fromArray( array );
  972. materialParams.opacity = array[ 3 ];
  973. }
  974. if ( metallicRoughness.baseColorTexture !== undefined ) {
  975. materialParams.map = dependencies.textures[ metallicRoughness.baseColorTexture.index ];
  976. }
  977. materialParams.metalness = metallicRoughness.metallicFactor !== undefined ? metallicRoughness.metallicFactor : 1.0;
  978. materialParams.roughness = metallicRoughness.roughnessFactor !== undefined ? metallicRoughness.roughnessFactor : 1.0;
  979. if ( metallicRoughness.metallicRoughnessTexture !== undefined ) {
  980. var textureIndex = metallicRoughness.metallicRoughnessTexture.index;
  981. materialParams.metalnessMap = dependencies.textures[ textureIndex ];
  982. materialParams.roughnessMap = dependencies.textures[ textureIndex ];
  983. }
  984. } else {
  985. materialType = THREE.MeshPhongMaterial;
  986. }
  987. if ( material.doubleSided === true ) {
  988. materialParams.side = THREE.DoubleSide;
  989. }
  990. var alphaMode = material.alphaMode || ALPHA_MODES.OPAQUE;
  991. if ( alphaMode !== ALPHA_MODES.OPAQUE ) {
  992. materialParams.transparent = true;
  993. } else {
  994. materialParams.transparent = false;
  995. }
  996. if ( material.normalTexture !== undefined ) {
  997. materialParams.normalMap = dependencies.textures[ material.normalTexture.index ];
  998. }
  999. if ( material.occlusionTexture !== undefined ) {
  1000. materialParams.aoMap = dependencies.textures[ material.occlusionTexture.index ];
  1001. }
  1002. if ( material.emissiveFactor !== undefined ) {
  1003. if ( materialType === THREE.MeshBasicMaterial ) {
  1004. materialParams.color = new THREE.Color().fromArray( material.emissiveFactor );
  1005. } else {
  1006. materialParams.emissive = new THREE.Color().fromArray( material.emissiveFactor );
  1007. }
  1008. }
  1009. if ( material.emissiveTexture !== undefined ) {
  1010. if ( materialType === THREE.MeshBasicMaterial ) {
  1011. materialParams.map = dependencies.textures[ material.emissiveTexture.index ];
  1012. } else {
  1013. materialParams.emissiveMap = dependencies.textures[ material.emissiveTexture.index ];
  1014. }
  1015. }
  1016. var _material;
  1017. if ( materialType === THREE.ShaderMaterial ) {
  1018. _material = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].createMaterial( materialParams );
  1019. } else {
  1020. _material = new materialType( materialParams );
  1021. }
  1022. if ( material.name !== undefined ) _material.name = material.name;
  1023. // Normal map textures use OpenGL conventions:
  1024. // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#materialnormaltexture
  1025. _material.normalScale.x = -1;
  1026. _material.userData = material.extras;
  1027. return _material;
  1028. } );
  1029. } );
  1030. };
  1031. GLTFParser.prototype.loadGeometries = function ( primitives ) {
  1032. return this._withDependencies( [
  1033. 'accessors',
  1034. 'bufferViews',
  1035. ] ).then( function ( dependencies ) {
  1036. return _each( primitives, function ( primitive ) {
  1037. var geometry = new THREE.BufferGeometry();
  1038. var attributes = primitive.attributes;
  1039. for ( var attributeId in attributes ) {
  1040. var attributeEntry = attributes[ attributeId ];
  1041. if ( attributeEntry === undefined ) return;
  1042. var bufferAttribute = dependencies.accessors[ attributeEntry ];
  1043. switch ( attributeId ) {
  1044. case 'POSITION':
  1045. geometry.addAttribute( 'position', bufferAttribute );
  1046. break;
  1047. case 'NORMAL':
  1048. geometry.addAttribute( 'normal', bufferAttribute );
  1049. break;
  1050. case 'TEXCOORD_0':
  1051. case 'TEXCOORD0':
  1052. case 'TEXCOORD':
  1053. geometry.addAttribute( 'uv', bufferAttribute );
  1054. break;
  1055. case 'TEXCOORD_1':
  1056. geometry.addAttribute( 'uv2', bufferAttribute );
  1057. break;
  1058. case 'COLOR_0':
  1059. case 'COLOR0':
  1060. case 'COLOR':
  1061. geometry.addAttribute( 'color', bufferAttribute );
  1062. break;
  1063. case 'WEIGHTS_0':
  1064. case 'WEIGHT': // WEIGHT semantic deprecated.
  1065. geometry.addAttribute( 'skinWeight', bufferAttribute );
  1066. break;
  1067. case 'JOINTS_0':
  1068. case 'JOINT': // JOINT semantic deprecated.
  1069. geometry.addAttribute( 'skinIndex', bufferAttribute );
  1070. break;
  1071. }
  1072. }
  1073. if ( primitive.indices !== undefined ) {
  1074. geometry.setIndex( dependencies.accessors[ primitive.indices ] );
  1075. }
  1076. return geometry;
  1077. } );
  1078. } );
  1079. };
  1080. /**
  1081. * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#meshes
  1082. */
  1083. GLTFParser.prototype.loadMeshes = function () {
  1084. var scope = this;
  1085. var json = this.json;
  1086. return this._withDependencies( [
  1087. 'accessors',
  1088. 'materials'
  1089. ] ).then( function ( dependencies ) {
  1090. return _each( json.meshes, function ( meshDef ) {
  1091. var group = new THREE.Group();
  1092. if ( meshDef.name !== undefined ) group.name = meshDef.name;
  1093. if ( meshDef.extras ) group.userData = meshDef.extras;
  1094. var primitives = meshDef.primitives || [];
  1095. return scope.loadGeometries( primitives ).then( function ( geometries ) {
  1096. for ( var name in primitives ) {
  1097. var primitive = primitives[ name ];
  1098. var geometry = geometries[ name ];
  1099. var material = primitive.material === undefined
  1100. ? createDefaultMaterial()
  1101. : dependencies.materials[ primitive.material ];
  1102. if ( material.aoMap
  1103. && geometry.attributes.uv2 === undefined
  1104. && geometry.attributes.uv !== undefined ) {
  1105. console.log( 'THREE.GLTF2Loader: Duplicating UVs to support aoMap.' );
  1106. geometry.addAttribute( 'uv2', new THREE.BufferAttribute( geometry.attributes.uv.array, 2 ) );
  1107. }
  1108. if ( geometry.attributes.color !== undefined ) {
  1109. material.vertexColors = THREE.VertexColors;
  1110. material.needsUpdate = true;
  1111. }
  1112. if ( geometry.attributes.normal === undefined ) {
  1113. if ( material.flatShading !== undefined ) {
  1114. material.flatShading = true;
  1115. } else {
  1116. // TODO: Remove this backwards-compatibility fix after r87 release.
  1117. material.shading = THREE.FlatShading;
  1118. }
  1119. }
  1120. var mesh;
  1121. if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLES || primitive.mode === undefined ) {
  1122. mesh = new THREE.Mesh( geometry, material );
  1123. } else if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP ) {
  1124. mesh = new THREE.Mesh( geometry, material );
  1125. mesh.drawMode = THREE.TriangleStripDrawMode;
  1126. } else if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN ) {
  1127. mesh = new THREE.Mesh( geometry, material );
  1128. mesh.drawMode = THREE.TriangleFanDrawMode;
  1129. } else if ( primitive.mode === WEBGL_CONSTANTS.LINES ) {
  1130. mesh = new THREE.LineSegments( geometry, material );
  1131. } else if ( primitive.mode === WEBGL_CONSTANTS.LINE_STRIP ) {
  1132. mesh = new THREE.Line( geometry, material );
  1133. } else if ( primitive.mode === WEBGL_CONSTANTS.LINE_LOOP ) {
  1134. mesh = new THREE.LineLoop( geometry, material );
  1135. } else {
  1136. throw new Error( 'THREE.GLTF2Loader: Primitive mode unsupported: ', primitive.mode );
  1137. }
  1138. mesh.name = group.name + '_' + name;
  1139. if ( primitive.targets !== undefined ) {
  1140. addMorphTargets( mesh, meshDef, primitive, dependencies );
  1141. }
  1142. if ( primitive.extras ) mesh.userData = primitive.extras;
  1143. group.add( mesh );
  1144. }
  1145. return group;
  1146. } );
  1147. } );
  1148. } );
  1149. };
  1150. /**
  1151. * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#cameras
  1152. */
  1153. GLTFParser.prototype.loadCameras = function () {
  1154. var json = this.json;
  1155. return _each( json.cameras, function ( camera ) {
  1156. var _camera;
  1157. var params = camera[ camera.type ];
  1158. if ( !params ) {
  1159. console.warn( 'THREE.GLTF2Loader: Missing camera parameters.' );
  1160. return;
  1161. }
  1162. if ( camera.type === 'perspective' ) {
  1163. var aspectRatio = params.aspectRatio || 1;
  1164. var xfov = params.yfov * aspectRatio;
  1165. _camera = new THREE.PerspectiveCamera( THREE.Math.radToDeg( xfov ), aspectRatio, params.znear || 1, params.zfar || 2e6 );
  1166. } else if ( camera.type === 'orthographic' ) {
  1167. _camera = new THREE.OrthographicCamera( params.xmag / -2, params.xmag / 2, params.ymag / 2, params.ymag / -2, params.znear, params.zfar );
  1168. }
  1169. if ( camera.name !== undefined ) _camera.name = camera.name;
  1170. if ( camera.extras ) _camera.userData = camera.extras;
  1171. return _camera;
  1172. } );
  1173. };
  1174. GLTFParser.prototype.loadSkins = function () {
  1175. var json = this.json;
  1176. return this._withDependencies( [
  1177. 'accessors'
  1178. ] ).then( function ( dependencies ) {
  1179. return _each( json.skins, function ( skin ) {
  1180. var _skin = {
  1181. joints: skin.joints,
  1182. inverseBindMatrices: dependencies.accessors[ skin.inverseBindMatrices ]
  1183. };
  1184. return _skin;
  1185. } );
  1186. } );
  1187. };
  1188. GLTFParser.prototype.loadAnimations = function () {
  1189. var json = this.json;
  1190. return this._withDependencies( [
  1191. 'accessors',
  1192. 'nodes'
  1193. ] ).then( function ( dependencies ) {
  1194. return _each( json.animations, function ( animation, animationId ) {
  1195. var tracks = [];
  1196. for ( var channelId in animation.channels ) {
  1197. var channel = animation.channels[ channelId ];
  1198. var sampler = animation.samplers[ channel.sampler ];
  1199. if ( sampler ) {
  1200. var target = channel.target;
  1201. var name = target.node !== undefined ? target.node : target.id; // NOTE: target.id is deprecated.
  1202. var input = animation.parameters !== undefined ? animation.parameters[ sampler.input ] : sampler.input;
  1203. var output = animation.parameters !== undefined ? animation.parameters[ sampler.output ] : sampler.output;
  1204. var inputAccessor = dependencies.accessors[ input ];
  1205. var outputAccessor = dependencies.accessors[ output ];
  1206. var node = dependencies.nodes[ name ];
  1207. if ( node ) {
  1208. node.updateMatrix();
  1209. node.matrixAutoUpdate = true;
  1210. var TypedKeyframeTrack;
  1211. switch ( PATH_PROPERTIES[ target.path ] ) {
  1212. case PATH_PROPERTIES.weights:
  1213. TypedKeyframeTrack = THREE.NumberKeyframeTrack;
  1214. break;
  1215. case PATH_PROPERTIES.rotation:
  1216. TypedKeyframeTrack = THREE.QuaternionKeyframeTrack;
  1217. break;
  1218. case PATH_PROPERTIES.position:
  1219. case PATH_PROPERTIES.scale:
  1220. default:
  1221. TypedKeyframeTrack = THREE.VectorKeyframeTrack;
  1222. break;
  1223. }
  1224. var targetName = node.name ? node.name : node.uuid;
  1225. if ( sampler.interpolation === 'CATMULLROMSPLINE' ) {
  1226. console.warn( 'THREE.GLTF2Loader: CATMULLROMSPLINE interpolation is not supported. Using CUBICSPLINE instead.' );
  1227. }
  1228. var interpolation = sampler.interpolation !== undefined ? INTERPOLATION[ sampler.interpolation ] : THREE.InterpolateLinear;
  1229. var targetNames = [];
  1230. if ( PATH_PROPERTIES[ target.path ] === PATH_PROPERTIES.weights ) {
  1231. // node should be THREE.Group here but
  1232. // PATH_PROPERTIES.weights(morphTargetInfluences) should be
  1233. // the property of a mesh object under node.
  1234. // So finding targets here.
  1235. node.traverse( function ( object ) {
  1236. if ( object.isMesh === true && object.material.morphTargets === true ) {
  1237. targetNames.push( object.name ? object.name : object.uuid );
  1238. }
  1239. } );
  1240. } else {
  1241. targetNames.push( targetName );
  1242. }
  1243. // KeyframeTrack.optimize() will modify given 'times' and 'values'
  1244. // buffers before creating a truncated copy to keep. Because buffers may
  1245. // be reused by other tracks, make copies here.
  1246. for ( var i = 0, il = targetNames.length; i < il; i ++ ) {
  1247. tracks.push( new TypedKeyframeTrack(
  1248. targetNames[ i ] + '.' + PATH_PROPERTIES[ target.path ],
  1249. THREE.AnimationUtils.arraySlice( inputAccessor.array, 0 ),
  1250. THREE.AnimationUtils.arraySlice( outputAccessor.array, 0 ),
  1251. interpolation
  1252. ) );
  1253. }
  1254. }
  1255. }
  1256. }
  1257. var name = animation.name !== undefined ? animation.name : 'animation_' + animationId;
  1258. return new THREE.AnimationClip( name, undefined, tracks );
  1259. } );
  1260. } );
  1261. };
  1262. GLTFParser.prototype.loadNodes = function () {
  1263. var json = this.json;
  1264. var extensions = this.extensions;
  1265. var scope = this;
  1266. var nodes = json.nodes || [];
  1267. var skins = json.skins || [];
  1268. // Nothing in the node definition indicates whether it is a Bone or an
  1269. // Object3D. Use the skins' joint references to mark bones.
  1270. skins.forEach( function ( skin ) {
  1271. skin.joints.forEach( function ( id ) {
  1272. nodes[ id ].isBone = true;
  1273. } );
  1274. } );
  1275. return _each( json.nodes, function ( node ) {
  1276. var matrix = new THREE.Matrix4();
  1277. var _node = node.isBone === true ? new THREE.Bone() : new THREE.Object3D();
  1278. if ( node.name !== undefined ) {
  1279. _node.name = THREE.PropertyBinding.sanitizeNodeName( node.name );
  1280. }
  1281. if ( node.extras ) _node.userData = node.extras;
  1282. if ( node.matrix !== undefined ) {
  1283. matrix.fromArray( node.matrix );
  1284. _node.applyMatrix( matrix );
  1285. } else {
  1286. if ( node.translation !== undefined ) {
  1287. _node.position.fromArray( node.translation );
  1288. }
  1289. if ( node.rotation !== undefined ) {
  1290. _node.quaternion.fromArray( node.rotation );
  1291. }
  1292. if ( node.scale !== undefined ) {
  1293. _node.scale.fromArray( node.scale );
  1294. }
  1295. }
  1296. return _node;
  1297. } ).then( function ( __nodes ) {
  1298. return scope._withDependencies( [
  1299. 'meshes',
  1300. 'skins',
  1301. 'cameras'
  1302. ] ).then( function ( dependencies ) {
  1303. return _each( __nodes, function ( _node, nodeId ) {
  1304. var node = json.nodes[ nodeId ];
  1305. var meshes;
  1306. if ( node.mesh !== undefined) {
  1307. meshes = [ node.mesh ];
  1308. } else if ( node.meshes !== undefined ) {
  1309. console.warn( 'THREE.GLTF2Loader: Legacy glTF file detected. Nodes may have no more than one mesh.' );
  1310. meshes = node.meshes;
  1311. }
  1312. if ( meshes !== undefined ) {
  1313. for ( var meshId in meshes ) {
  1314. var mesh = meshes[ meshId ];
  1315. var group = dependencies.meshes[ mesh ];
  1316. if ( group === undefined ) {
  1317. console.warn( 'THREE.GLTF2Loader: Could not find node "' + mesh + '".' );
  1318. continue;
  1319. }
  1320. // do not clone children as they will be replaced anyway
  1321. var clonedgroup = group.clone( false );
  1322. for ( var childrenId in group.children ) {
  1323. var child = group.children[ childrenId ];
  1324. // clone Mesh to add to _node
  1325. var originalMaterial = child.material;
  1326. var originalGeometry = child.geometry;
  1327. var originalInfluences = child.morphTargetInfluences;
  1328. var originalUserData = child.userData;
  1329. var originalName = child.name;
  1330. var material = originalMaterial;
  1331. switch ( child.type ) {
  1332. case 'LineSegments':
  1333. child = new THREE.LineSegments( originalGeometry, material );
  1334. break;
  1335. case 'LineLoop':
  1336. child = new THREE.LineLoop( originalGeometry, material );
  1337. break;
  1338. case 'Line':
  1339. child = new THREE.Line( originalGeometry, material );
  1340. break;
  1341. default:
  1342. child = new THREE.Mesh( originalGeometry, material );
  1343. }
  1344. child.castShadow = true;
  1345. child.morphTargetInfluences = originalInfluences;
  1346. child.userData = originalUserData;
  1347. child.name = originalName;
  1348. var skinEntry;
  1349. if ( node.skin !== undefined ) {
  1350. skinEntry = dependencies.skins[ node.skin ];
  1351. }
  1352. // Replace Mesh with SkinnedMesh in library
  1353. if ( skinEntry ) {
  1354. var geometry = originalGeometry;
  1355. material = originalMaterial;
  1356. material.skinning = true;
  1357. child = new THREE.SkinnedMesh( geometry, material );
  1358. child.castShadow = true;
  1359. child.userData = originalUserData;
  1360. child.name = originalName;
  1361. var bones = [];
  1362. var boneInverses = [];
  1363. for ( var i = 0, l = skinEntry.joints.length; i < l; i ++ ) {
  1364. var jointId = skinEntry.joints[ i ];
  1365. var jointNode = __nodes[ jointId ];
  1366. if ( jointNode ) {
  1367. bones.push( jointNode );
  1368. var m = skinEntry.inverseBindMatrices.array;
  1369. var mat = new THREE.Matrix4().fromArray( m, i * 16 );
  1370. boneInverses.push( mat );
  1371. } else {
  1372. console.warn( 'THREE.GLTF2Loader: Joint "%s" could not be found.', jointId );
  1373. }
  1374. }
  1375. child.bind( new THREE.Skeleton( bones, boneInverses ), child.matrixWorld );
  1376. }
  1377. clonedgroup.add( child );
  1378. }
  1379. _node.add( clonedgroup );
  1380. }
  1381. }
  1382. if ( node.camera !== undefined ) {
  1383. var camera = dependencies.cameras[ node.camera ];
  1384. _node.add( camera );
  1385. }
  1386. if ( node.extensions
  1387. && node.extensions[ EXTENSIONS.KHR_LIGHTS ]
  1388. && node.extensions[ EXTENSIONS.KHR_LIGHTS ].light !== undefined ) {
  1389. var lights = extensions[ EXTENSIONS.KHR_LIGHTS ].lights;
  1390. _node.add( lights[ node.extensions[ EXTENSIONS.KHR_LIGHTS ].light ] );
  1391. }
  1392. return _node;
  1393. } );
  1394. } );
  1395. } );
  1396. };
  1397. GLTFParser.prototype.loadScenes = function () {
  1398. var json = this.json;
  1399. var extensions = this.extensions;
  1400. // scene node hierachy builder
  1401. function buildNodeHierachy( nodeId, parentObject, allNodes ) {
  1402. var _node = allNodes[ nodeId ];
  1403. parentObject.add( _node );
  1404. var node = json.nodes[ nodeId ];
  1405. if ( node.children ) {
  1406. var children = node.children;
  1407. for ( var i = 0, l = children.length; i < l; i ++ ) {
  1408. var child = children[ i ];
  1409. buildNodeHierachy( child, _node, allNodes );
  1410. }
  1411. }
  1412. }
  1413. return this._withDependencies( [
  1414. 'nodes'
  1415. ] ).then( function ( dependencies ) {
  1416. return _each( json.scenes, function ( scene ) {
  1417. var _scene = new THREE.Scene();
  1418. if ( scene.name !== undefined ) _scene.name = scene.name;
  1419. if ( scene.extras ) _scene.userData = scene.extras;
  1420. var nodes = scene.nodes || [];
  1421. for ( var i = 0, l = nodes.length; i < l; i ++ ) {
  1422. var nodeId = nodes[ i ];
  1423. buildNodeHierachy( nodeId, _scene, dependencies.nodes );
  1424. }
  1425. _scene.traverse( function ( child ) {
  1426. // for Specular-Glossiness.
  1427. if ( child.material && child.material.isGLTFSpecularGlossinessMaterial ) {
  1428. child.onBeforeRender = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].refreshUniforms;
  1429. }
  1430. } );
  1431. // Ambient lighting, if present, is always attached to the scene root.
  1432. if ( scene.extensions
  1433. && scene.extensions[ EXTENSIONS.KHR_LIGHTS ]
  1434. && scene.extensions[ EXTENSIONS.KHR_LIGHTS ].light !== undefined ) {
  1435. var lights = extensions[ EXTENSIONS.KHR_LIGHTS ].lights;
  1436. _scene.add( lights[ scene.extensions[ EXTENSIONS.KHR_LIGHTS ].light ] );
  1437. }
  1438. return _scene;
  1439. } );
  1440. } );
  1441. };
  1442. return GLTF2Loader;
  1443. } )();