GLTF2Loader.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  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. POINTS: 0,
  484. LINES: 1,
  485. LINE_LOOP: 2,
  486. LINE_STRIP: 3,
  487. TRIANGLES: 4,
  488. TRIANGLE_STRIP: 5,
  489. TRIANGLE_FAN: 6,
  490. UNSIGNED_BYTE: 5121,
  491. UNSIGNED_SHORT: 5123
  492. };
  493. var WEBGL_TYPE = {
  494. 5126: Number,
  495. //35674: THREE.Matrix2,
  496. 35675: THREE.Matrix3,
  497. 35676: THREE.Matrix4,
  498. 35664: THREE.Vector2,
  499. 35665: THREE.Vector3,
  500. 35666: THREE.Vector4,
  501. 35678: THREE.Texture
  502. };
  503. var WEBGL_COMPONENT_TYPES = {
  504. 5120: Int8Array,
  505. 5121: Uint8Array,
  506. 5122: Int16Array,
  507. 5123: Uint16Array,
  508. 5125: Uint32Array,
  509. 5126: Float32Array
  510. };
  511. var WEBGL_FILTERS = {
  512. 9728: THREE.NearestFilter,
  513. 9729: THREE.LinearFilter,
  514. 9984: THREE.NearestMipMapNearestFilter,
  515. 9985: THREE.LinearMipMapNearestFilter,
  516. 9986: THREE.NearestMipMapLinearFilter,
  517. 9987: THREE.LinearMipMapLinearFilter
  518. };
  519. var WEBGL_WRAPPINGS = {
  520. 33071: THREE.ClampToEdgeWrapping,
  521. 33648: THREE.MirroredRepeatWrapping,
  522. 10497: THREE.RepeatWrapping
  523. };
  524. var WEBGL_TEXTURE_FORMATS = {
  525. 6406: THREE.AlphaFormat,
  526. 6407: THREE.RGBFormat,
  527. 6408: THREE.RGBAFormat,
  528. 6409: THREE.LuminanceFormat,
  529. 6410: THREE.LuminanceAlphaFormat
  530. };
  531. var WEBGL_TEXTURE_DATATYPES = {
  532. 5121: THREE.UnsignedByteType,
  533. 32819: THREE.UnsignedShort4444Type,
  534. 32820: THREE.UnsignedShort5551Type,
  535. 33635: THREE.UnsignedShort565Type
  536. };
  537. var WEBGL_SIDES = {
  538. 1028: THREE.BackSide, // Culling front
  539. 1029: THREE.FrontSide // Culling back
  540. //1032: THREE.NoSide // Culling front and back, what to do?
  541. };
  542. var WEBGL_DEPTH_FUNCS = {
  543. 512: THREE.NeverDepth,
  544. 513: THREE.LessDepth,
  545. 514: THREE.EqualDepth,
  546. 515: THREE.LessEqualDepth,
  547. 516: THREE.GreaterEqualDepth,
  548. 517: THREE.NotEqualDepth,
  549. 518: THREE.GreaterEqualDepth,
  550. 519: THREE.AlwaysDepth
  551. };
  552. var WEBGL_BLEND_EQUATIONS = {
  553. 32774: THREE.AddEquation,
  554. 32778: THREE.SubtractEquation,
  555. 32779: THREE.ReverseSubtractEquation
  556. };
  557. var WEBGL_BLEND_FUNCS = {
  558. 0: THREE.ZeroFactor,
  559. 1: THREE.OneFactor,
  560. 768: THREE.SrcColorFactor,
  561. 769: THREE.OneMinusSrcColorFactor,
  562. 770: THREE.SrcAlphaFactor,
  563. 771: THREE.OneMinusSrcAlphaFactor,
  564. 772: THREE.DstAlphaFactor,
  565. 773: THREE.OneMinusDstAlphaFactor,
  566. 774: THREE.DstColorFactor,
  567. 775: THREE.OneMinusDstColorFactor,
  568. 776: THREE.SrcAlphaSaturateFactor
  569. // The followings are not supported by Three.js yet
  570. //32769: CONSTANT_COLOR,
  571. //32770: ONE_MINUS_CONSTANT_COLOR,
  572. //32771: CONSTANT_ALPHA,
  573. //32772: ONE_MINUS_CONSTANT_COLOR
  574. };
  575. var WEBGL_TYPE_SIZES = {
  576. 'SCALAR': 1,
  577. 'VEC2': 2,
  578. 'VEC3': 3,
  579. 'VEC4': 4,
  580. 'MAT2': 4,
  581. 'MAT3': 9,
  582. 'MAT4': 16
  583. };
  584. var PATH_PROPERTIES = {
  585. scale: 'scale',
  586. translation: 'position',
  587. rotation: 'quaternion',
  588. weights: 'morphTargetInfluences'
  589. };
  590. var INTERPOLATION = {
  591. CATMULLROMSPLINE: THREE.InterpolateSmooth,
  592. CUBICSPLINE: THREE.InterpolateSmooth,
  593. LINEAR: THREE.InterpolateLinear,
  594. STEP: THREE.InterpolateDiscrete
  595. };
  596. var STATES_ENABLES = {
  597. 2884: 'CULL_FACE',
  598. 2929: 'DEPTH_TEST',
  599. 3042: 'BLEND',
  600. 3089: 'SCISSOR_TEST',
  601. 32823: 'POLYGON_OFFSET_FILL',
  602. 32926: 'SAMPLE_ALPHA_TO_COVERAGE'
  603. };
  604. var ALPHA_MODES = {
  605. OPAQUE: 'OPAQUE',
  606. MASK: 'MASK',
  607. BLEND: 'BLEND'
  608. };
  609. /* UTILITY FUNCTIONS */
  610. function _each( object, callback, thisObj ) {
  611. if ( !object ) {
  612. return Promise.resolve();
  613. }
  614. var results;
  615. var fns = [];
  616. if ( Object.prototype.toString.call( object ) === '[object Array]' ) {
  617. results = [];
  618. var length = object.length;
  619. for ( var idx = 0; idx < length; idx ++ ) {
  620. var value = callback.call( thisObj || this, object[ idx ], idx );
  621. if ( value ) {
  622. fns.push( value );
  623. if ( value instanceof Promise ) {
  624. value.then( function( key, value ) {
  625. results[ key ] = value;
  626. }.bind( this, idx ));
  627. } else {
  628. results[ idx ] = value;
  629. }
  630. }
  631. }
  632. } else {
  633. results = {};
  634. for ( var key in object ) {
  635. if ( object.hasOwnProperty( key ) ) {
  636. var value = callback.call( thisObj || this, object[ key ], key );
  637. if ( value ) {
  638. fns.push( value );
  639. if ( value instanceof Promise ) {
  640. value.then( function( key, value ) {
  641. results[ key ] = value;
  642. }.bind( this, key ));
  643. } else {
  644. results[ key ] = value;
  645. }
  646. }
  647. }
  648. }
  649. }
  650. return Promise.all( fns ).then( function() {
  651. return results;
  652. });
  653. }
  654. function resolveURL( url, path ) {
  655. // Invalid URL
  656. if ( typeof url !== 'string' || url === '' )
  657. return '';
  658. // Absolute URL http://,https://,//
  659. if ( /^(https?:)?\/\//i.test( url ) ) {
  660. return url;
  661. }
  662. // Data URI
  663. if ( /^data:.*,.*$/i.test( url ) ) {
  664. return url;
  665. }
  666. // Blob URL
  667. if ( /^blob:.*$/i.test( url ) ) {
  668. return url;
  669. }
  670. // Relative URL
  671. return ( path || '' ) + url;
  672. }
  673. function convertUint8ArrayToString( array ) {
  674. if ( window.TextDecoder !== undefined ) {
  675. return new TextDecoder().decode( array );
  676. }
  677. // Avoid the String.fromCharCode.apply(null, array) shortcut, which
  678. // throws a "maximum call stack size exceeded" error for large arrays.
  679. var s = '';
  680. for ( var i = 0, il = array.length; i < il; i ++ ) {
  681. s += String.fromCharCode( array[ i ] );
  682. }
  683. return s;
  684. }
  685. /**
  686. * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#default-material
  687. */
  688. function createDefaultMaterial() {
  689. return new THREE.MeshStandardMaterial( {
  690. color: 0xFFFFFF,
  691. emissive: 0x000000,
  692. metalness: 1,
  693. roughness: 1,
  694. transparent: false,
  695. depthTest: true,
  696. side: THREE.FrontSide
  697. } );
  698. }
  699. /**
  700. * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#morph-targets
  701. * @param {THREE.Mesh} mesh
  702. * @param {GLTF.Mesh} meshDef
  703. * @param {GLTF.Primitive} primitiveDef
  704. * @param {Object} dependencies
  705. */
  706. function addMorphTargets ( mesh, meshDef, primitiveDef, dependencies ) {
  707. var geometry = mesh.geometry;
  708. var material = mesh.material;
  709. var targets = primitiveDef.targets;
  710. var morphAttributes = geometry.morphAttributes;
  711. morphAttributes.position = [];
  712. morphAttributes.normal = [];
  713. material.morphTargets = true;
  714. for ( var i = 0, il = targets.length; i < il; i ++ ) {
  715. var target = targets[ i ];
  716. var attributeName = 'morphTarget' + i;
  717. var positionAttribute, normalAttribute;
  718. if ( target.POSITION !== undefined ) {
  719. // Three.js morph formula is
  720. // position
  721. // + weight0 * ( morphTarget0 - position )
  722. // + weight1 * ( morphTarget1 - position )
  723. // ...
  724. // while the glTF one is
  725. // position
  726. // + weight0 * morphTarget0
  727. // + weight1 * morphTarget1
  728. // ...
  729. // then adding position to morphTarget.
  730. // So morphTarget value will depend on mesh's position, then cloning attribute
  731. // for the case if attribute is shared among two or more meshes.
  732. positionAttribute = dependencies.accessors[ target.POSITION ].clone();
  733. var position = geometry.attributes.position;
  734. for ( var j = 0, jl = positionAttribute.count; j < jl; j ++ ) {
  735. positionAttribute.setXYZ(
  736. j,
  737. positionAttribute.getX( j ) + position.getX( j ),
  738. positionAttribute.getY( j ) + position.getY( j ),
  739. positionAttribute.getZ( j ) + position.getZ( j )
  740. );
  741. }
  742. } else {
  743. // Copying the original position not to affect the final position.
  744. // See the formula above.
  745. positionAttribute = geometry.attributes.position.clone();
  746. }
  747. if ( target.NORMAL !== undefined ) {
  748. material.morphNormals = true;
  749. // see target.POSITION's comment
  750. normalAttribute = dependencies.accessors[ target.NORMAL ].clone();
  751. var normal = geometry.attributes.normal;
  752. for ( var j = 0, jl = normalAttribute.count; j < jl; j ++ ) {
  753. normalAttribute.setXYZ(
  754. j,
  755. normalAttribute.getX( j ) + normal.getX( j ),
  756. normalAttribute.getY( j ) + normal.getY( j ),
  757. normalAttribute.getZ( j ) + normal.getZ( j )
  758. );
  759. }
  760. } else {
  761. normalAttribute = geometry.attributes.normal.clone();
  762. }
  763. if ( target.TANGENT !== undefined ) {
  764. // TODO: implement
  765. }
  766. positionAttribute.name = attributeName;
  767. normalAttribute.name = attributeName;
  768. morphAttributes.position.push( positionAttribute );
  769. morphAttributes.normal.push( normalAttribute );
  770. }
  771. mesh.updateMorphTargets();
  772. if ( meshDef.weights !== undefined ) {
  773. for ( var i = 0, il = meshDef.weights.length; i < il; i ++ ) {
  774. mesh.morphTargetInfluences[ i ] = meshDef.weights[ i ];
  775. }
  776. }
  777. }
  778. /* GLTF PARSER */
  779. function GLTFParser( json, extensions, options ) {
  780. this.json = json || {};
  781. this.extensions = extensions || {};
  782. this.options = options || {};
  783. // loader object cache
  784. this.cache = new GLTFRegistry();
  785. }
  786. GLTFParser.prototype._withDependencies = function ( dependencies ) {
  787. var _dependencies = {};
  788. for ( var i = 0; i < dependencies.length; i ++ ) {
  789. var dependency = dependencies[ i ];
  790. var fnName = 'load' + dependency.charAt( 0 ).toUpperCase() + dependency.slice( 1 );
  791. var cached = this.cache.get( dependency );
  792. if ( cached !== undefined ) {
  793. _dependencies[ dependency ] = cached;
  794. } else if ( this[ fnName ] ) {
  795. var fn = this[ fnName ]();
  796. this.cache.add( dependency, fn );
  797. _dependencies[ dependency ] = fn;
  798. }
  799. }
  800. return _each( _dependencies, function ( dependency ) {
  801. return dependency;
  802. } );
  803. };
  804. GLTFParser.prototype.parse = function ( onLoad, onError ) {
  805. var json = this.json;
  806. // Clear the loader cache
  807. this.cache.removeAll();
  808. // Fire the callback on complete
  809. this._withDependencies( [
  810. 'scenes',
  811. 'cameras',
  812. 'animations'
  813. ] ).then( function ( dependencies ) {
  814. var scenes = [];
  815. for ( var name in dependencies.scenes ) {
  816. scenes.push( dependencies.scenes[ name ] );
  817. }
  818. var scene = json.scene !== undefined ? dependencies.scenes[ json.scene ] : scenes[ 0 ];
  819. var cameras = [];
  820. for ( var name in dependencies.cameras ) {
  821. var camera = dependencies.cameras[ name ];
  822. cameras.push( camera );
  823. }
  824. var animations = [];
  825. for ( var name in dependencies.animations ) {
  826. animations.push( dependencies.animations[ name ] );
  827. }
  828. onLoad( scene, scenes, cameras, animations );
  829. } ).catch( onError );
  830. };
  831. GLTFParser.prototype.loadBuffers = function () {
  832. var json = this.json;
  833. var extensions = this.extensions;
  834. var options = this.options;
  835. return _each( json.buffers, function ( buffer, name ) {
  836. if ( buffer.type === 'arraybuffer' || buffer.type === undefined ) {
  837. // If present, GLB container is required to be the first buffer.
  838. if ( buffer.uri === undefined && name === 0 ) {
  839. return extensions[ EXTENSIONS.KHR_BINARY_GLTF ].body;
  840. }
  841. return new Promise( function ( resolve ) {
  842. var loader = new THREE.FileLoader();
  843. loader.setResponseType( 'arraybuffer' );
  844. loader.load( resolveURL( buffer.uri, options.path ), function ( buffer ) {
  845. resolve( buffer );
  846. } );
  847. } );
  848. } else {
  849. console.warn( 'THREE.GLTF2Loader: %s buffer type is not supported.', buffer.type );
  850. }
  851. } );
  852. };
  853. GLTFParser.prototype.loadBufferViews = function () {
  854. var json = this.json;
  855. return this._withDependencies( [
  856. 'buffers'
  857. ] ).then( function ( dependencies ) {
  858. return _each( json.bufferViews, function ( bufferView ) {
  859. var arraybuffer = dependencies.buffers[ bufferView.buffer ];
  860. var byteLength = bufferView.byteLength || 0;
  861. var byteOffset = bufferView.byteOffset || 0;
  862. return arraybuffer.slice( byteOffset, byteOffset + byteLength );
  863. } );
  864. } );
  865. };
  866. GLTFParser.prototype.loadAccessors = function () {
  867. var json = this.json;
  868. return this._withDependencies( [
  869. 'bufferViews'
  870. ] ).then( function ( dependencies ) {
  871. return _each( json.accessors, function ( accessor ) {
  872. var arraybuffer = dependencies.bufferViews[ accessor.bufferView ];
  873. var itemSize = WEBGL_TYPE_SIZES[ accessor.type ];
  874. var TypedArray = WEBGL_COMPONENT_TYPES[ accessor.componentType ];
  875. // For VEC3: itemSize is 3, elementBytes is 4, itemBytes is 12.
  876. var elementBytes = TypedArray.BYTES_PER_ELEMENT;
  877. var itemBytes = elementBytes * itemSize;
  878. var byteStride = json.bufferViews[ accessor.bufferView ].byteStride;
  879. var array;
  880. // The buffer is not interleaved if the stride is the item size in bytes.
  881. if ( byteStride && byteStride !== itemBytes ) {
  882. // Use the full buffer if it's interleaved.
  883. array = new TypedArray( arraybuffer );
  884. // Integer parameters to IB/IBA are in array elements, not bytes.
  885. var ib = new THREE.InterleavedBuffer( array, byteStride / elementBytes );
  886. return new THREE.InterleavedBufferAttribute( ib, itemSize, accessor.byteOffset / elementBytes );
  887. } else {
  888. array = new TypedArray( arraybuffer, accessor.byteOffset, accessor.count * itemSize );
  889. return new THREE.BufferAttribute( array, itemSize );
  890. }
  891. } );
  892. } );
  893. };
  894. GLTFParser.prototype.loadTextures = function () {
  895. var json = this.json;
  896. var options = this.options;
  897. return this._withDependencies( [
  898. 'bufferViews'
  899. ] ).then( function ( dependencies ) {
  900. return _each( json.textures, function ( texture ) {
  901. if ( texture.source !== undefined ) {
  902. return new Promise( function ( resolve ) {
  903. var source = json.images[ texture.source ];
  904. var sourceUri = source.uri;
  905. var urlCreator;
  906. if ( source.bufferView !== undefined ) {
  907. var bufferView = dependencies.bufferViews[ source.bufferView ];
  908. var blob = new Blob( [ bufferView ], { type: source.mimeType } );
  909. urlCreator = window.URL || window.webkitURL;
  910. sourceUri = urlCreator.createObjectURL( blob );
  911. }
  912. var textureLoader = THREE.Loader.Handlers.get( sourceUri );
  913. if ( textureLoader === null ) {
  914. textureLoader = new THREE.TextureLoader();
  915. }
  916. textureLoader.setCrossOrigin( options.crossOrigin );
  917. textureLoader.load( resolveURL( sourceUri, options.path ), function ( _texture ) {
  918. if ( urlCreator !== undefined ) {
  919. urlCreator.revokeObjectURL( sourceUri );
  920. }
  921. _texture.flipY = false;
  922. if ( texture.name !== undefined ) _texture.name = texture.name;
  923. _texture.format = texture.format !== undefined ? WEBGL_TEXTURE_FORMATS[ texture.format ] : THREE.RGBAFormat;
  924. if ( texture.internalFormat !== undefined && _texture.format !== WEBGL_TEXTURE_FORMATS[ texture.internalFormat ] ) {
  925. console.warn( 'THREE.GLTF2Loader: Three.js does not support texture internalFormat which is different from texture format. ' +
  926. 'internalFormat will be forced to be the same value as format.' );
  927. }
  928. _texture.type = texture.type !== undefined ? WEBGL_TEXTURE_DATATYPES[ texture.type ] : THREE.UnsignedByteType;
  929. var samplers = json.samplers || {};
  930. var sampler = samplers[ texture.sampler ] || {};
  931. _texture.magFilter = WEBGL_FILTERS[ sampler.magFilter ] || THREE.LinearFilter;
  932. _texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ] || THREE.LinearMipMapLinearFilter;
  933. _texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || THREE.RepeatWrapping;
  934. _texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || THREE.RepeatWrapping;
  935. resolve( _texture );
  936. }, undefined, function () {
  937. resolve();
  938. } );
  939. } );
  940. }
  941. } );
  942. } );
  943. };
  944. GLTFParser.prototype.loadMaterials = function () {
  945. var json = this.json;
  946. var extensions = this.extensions;
  947. return this._withDependencies( [
  948. 'textures'
  949. ] ).then( function ( dependencies ) {
  950. return _each( json.materials, function ( material ) {
  951. var materialType;
  952. var materialParams = {};
  953. var materialExtensions = material.extensions || {};
  954. if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_COMMON ] ) {
  955. materialType = extensions[ EXTENSIONS.KHR_MATERIALS_COMMON ].getMaterialType( material );
  956. extensions[ EXTENSIONS.KHR_MATERIALS_COMMON ].extendParams( materialParams, material, dependencies );
  957. } else if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] ) {
  958. materialType = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].getMaterialType( material );
  959. extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].extendParams( materialParams, material, dependencies );
  960. } else if ( materialExtensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ] ) {
  961. materialType = extensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ].getMaterialType( material );
  962. extensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ].extendParams( materialParams, material, dependencies );
  963. } else if ( material.pbrMetallicRoughness !== undefined ) {
  964. // Specification:
  965. // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material
  966. materialType = THREE.MeshStandardMaterial;
  967. var metallicRoughness = material.pbrMetallicRoughness;
  968. materialParams.color = new THREE.Color( 1.0, 1.0, 1.0 );
  969. materialParams.opacity = 1.0;
  970. if ( Array.isArray( metallicRoughness.baseColorFactor ) ) {
  971. var array = metallicRoughness.baseColorFactor;
  972. materialParams.color.fromArray( array );
  973. materialParams.opacity = array[ 3 ];
  974. }
  975. if ( metallicRoughness.baseColorTexture !== undefined ) {
  976. materialParams.map = dependencies.textures[ metallicRoughness.baseColorTexture.index ];
  977. }
  978. materialParams.metalness = metallicRoughness.metallicFactor !== undefined ? metallicRoughness.metallicFactor : 1.0;
  979. materialParams.roughness = metallicRoughness.roughnessFactor !== undefined ? metallicRoughness.roughnessFactor : 1.0;
  980. if ( metallicRoughness.metallicRoughnessTexture !== undefined ) {
  981. var textureIndex = metallicRoughness.metallicRoughnessTexture.index;
  982. materialParams.metalnessMap = dependencies.textures[ textureIndex ];
  983. materialParams.roughnessMap = dependencies.textures[ textureIndex ];
  984. }
  985. } else {
  986. materialType = THREE.MeshPhongMaterial;
  987. }
  988. if ( material.doubleSided === true ) {
  989. materialParams.side = THREE.DoubleSide;
  990. }
  991. var alphaMode = material.alphaMode || ALPHA_MODES.OPAQUE;
  992. if ( alphaMode !== ALPHA_MODES.OPAQUE ) {
  993. materialParams.transparent = true;
  994. } else {
  995. materialParams.transparent = false;
  996. }
  997. if ( material.normalTexture !== undefined ) {
  998. materialParams.normalMap = dependencies.textures[ material.normalTexture.index ];
  999. }
  1000. if ( material.occlusionTexture !== undefined ) {
  1001. materialParams.aoMap = dependencies.textures[ material.occlusionTexture.index ];
  1002. }
  1003. if ( material.emissiveFactor !== undefined ) {
  1004. if ( materialType === THREE.MeshBasicMaterial ) {
  1005. materialParams.color = new THREE.Color().fromArray( material.emissiveFactor );
  1006. } else {
  1007. materialParams.emissive = new THREE.Color().fromArray( material.emissiveFactor );
  1008. }
  1009. }
  1010. if ( material.emissiveTexture !== undefined ) {
  1011. if ( materialType === THREE.MeshBasicMaterial ) {
  1012. materialParams.map = dependencies.textures[ material.emissiveTexture.index ];
  1013. } else {
  1014. materialParams.emissiveMap = dependencies.textures[ material.emissiveTexture.index ];
  1015. }
  1016. }
  1017. var _material;
  1018. if ( materialType === THREE.ShaderMaterial ) {
  1019. _material = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].createMaterial( materialParams );
  1020. } else {
  1021. _material = new materialType( materialParams );
  1022. }
  1023. if ( material.name !== undefined ) _material.name = material.name;
  1024. // Normal map textures use OpenGL conventions:
  1025. // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#materialnormaltexture
  1026. _material.normalScale.x = -1;
  1027. _material.userData = material.extras;
  1028. return _material;
  1029. } );
  1030. } );
  1031. };
  1032. GLTFParser.prototype.loadGeometries = function ( primitives ) {
  1033. return this._withDependencies( [
  1034. 'accessors',
  1035. 'bufferViews',
  1036. ] ).then( function ( dependencies ) {
  1037. return _each( primitives, function ( primitive ) {
  1038. var geometry = new THREE.BufferGeometry();
  1039. var attributes = primitive.attributes;
  1040. for ( var attributeId in attributes ) {
  1041. var attributeEntry = attributes[ attributeId ];
  1042. if ( attributeEntry === undefined ) return;
  1043. var bufferAttribute = dependencies.accessors[ attributeEntry ];
  1044. switch ( attributeId ) {
  1045. case 'POSITION':
  1046. geometry.addAttribute( 'position', bufferAttribute );
  1047. break;
  1048. case 'NORMAL':
  1049. geometry.addAttribute( 'normal', bufferAttribute );
  1050. break;
  1051. case 'TEXCOORD_0':
  1052. case 'TEXCOORD0':
  1053. case 'TEXCOORD':
  1054. geometry.addAttribute( 'uv', bufferAttribute );
  1055. break;
  1056. case 'TEXCOORD_1':
  1057. geometry.addAttribute( 'uv2', bufferAttribute );
  1058. break;
  1059. case 'COLOR_0':
  1060. case 'COLOR0':
  1061. case 'COLOR':
  1062. geometry.addAttribute( 'color', bufferAttribute );
  1063. break;
  1064. case 'WEIGHTS_0':
  1065. case 'WEIGHT': // WEIGHT semantic deprecated.
  1066. geometry.addAttribute( 'skinWeight', bufferAttribute );
  1067. break;
  1068. case 'JOINTS_0':
  1069. case 'JOINT': // JOINT semantic deprecated.
  1070. geometry.addAttribute( 'skinIndex', bufferAttribute );
  1071. break;
  1072. }
  1073. }
  1074. if ( primitive.indices !== undefined ) {
  1075. geometry.setIndex( dependencies.accessors[ primitive.indices ] );
  1076. }
  1077. return geometry;
  1078. } );
  1079. } );
  1080. };
  1081. /**
  1082. * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#meshes
  1083. */
  1084. GLTFParser.prototype.loadMeshes = function () {
  1085. var scope = this;
  1086. var json = this.json;
  1087. return this._withDependencies( [
  1088. 'accessors',
  1089. 'materials'
  1090. ] ).then( function ( dependencies ) {
  1091. return _each( json.meshes, function ( meshDef ) {
  1092. var group = new THREE.Group();
  1093. if ( meshDef.name !== undefined ) group.name = meshDef.name;
  1094. if ( meshDef.extras ) group.userData = meshDef.extras;
  1095. var primitives = meshDef.primitives || [];
  1096. return scope.loadGeometries( primitives ).then( function ( geometries ) {
  1097. for ( var name in primitives ) {
  1098. var primitive = primitives[ name ];
  1099. var geometry = geometries[ name ];
  1100. var material = primitive.material === undefined
  1101. ? createDefaultMaterial()
  1102. : dependencies.materials[ primitive.material ];
  1103. if ( material.aoMap
  1104. && geometry.attributes.uv2 === undefined
  1105. && geometry.attributes.uv !== undefined ) {
  1106. console.log( 'THREE.GLTF2Loader: Duplicating UVs to support aoMap.' );
  1107. geometry.addAttribute( 'uv2', new THREE.BufferAttribute( geometry.attributes.uv.array, 2 ) );
  1108. }
  1109. if ( geometry.attributes.color !== undefined ) {
  1110. material.vertexColors = THREE.VertexColors;
  1111. material.needsUpdate = true;
  1112. }
  1113. if ( geometry.attributes.normal === undefined ) {
  1114. if ( material.flatShading !== undefined ) {
  1115. material.flatShading = true;
  1116. } else {
  1117. // TODO: Remove this backwards-compatibility fix after r87 release.
  1118. material.shading = THREE.FlatShading;
  1119. }
  1120. }
  1121. var mesh;
  1122. if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLES || primitive.mode === undefined ) {
  1123. mesh = new THREE.Mesh( geometry, material );
  1124. } else if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP ) {
  1125. mesh = new THREE.Mesh( geometry, material );
  1126. mesh.drawMode = THREE.TriangleStripDrawMode;
  1127. } else if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN ) {
  1128. mesh = new THREE.Mesh( geometry, material );
  1129. mesh.drawMode = THREE.TriangleFanDrawMode;
  1130. } else if ( primitive.mode === WEBGL_CONSTANTS.LINES ) {
  1131. mesh = new THREE.LineSegments( geometry, material );
  1132. } else if ( primitive.mode === WEBGL_CONSTANTS.LINE_STRIP ) {
  1133. mesh = new THREE.Line( geometry, material );
  1134. } else if ( primitive.mode === WEBGL_CONSTANTS.LINE_LOOP ) {
  1135. mesh = new THREE.LineLoop( geometry, material );
  1136. } else if ( primitive.mode === WEBGL_CONSTANTS.POINTS ) {
  1137. mesh = new THREE.Points( geometry, material );
  1138. } else {
  1139. throw new Error( 'THREE.GLTF2Loader: Primitive mode unsupported: ', primitive.mode );
  1140. }
  1141. mesh.name = group.name + '_' + name;
  1142. if ( primitive.targets !== undefined ) {
  1143. addMorphTargets( mesh, meshDef, primitive, dependencies );
  1144. }
  1145. if ( primitive.extras ) mesh.userData = primitive.extras;
  1146. group.add( mesh );
  1147. }
  1148. return group;
  1149. } );
  1150. } );
  1151. } );
  1152. };
  1153. /**
  1154. * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#cameras
  1155. */
  1156. GLTFParser.prototype.loadCameras = function () {
  1157. var json = this.json;
  1158. return _each( json.cameras, function ( camera ) {
  1159. var _camera;
  1160. var params = camera[ camera.type ];
  1161. if ( !params ) {
  1162. console.warn( 'THREE.GLTF2Loader: Missing camera parameters.' );
  1163. return;
  1164. }
  1165. if ( camera.type === 'perspective' ) {
  1166. var aspectRatio = params.aspectRatio || 1;
  1167. var xfov = params.yfov * aspectRatio;
  1168. _camera = new THREE.PerspectiveCamera( THREE.Math.radToDeg( xfov ), aspectRatio, params.znear || 1, params.zfar || 2e6 );
  1169. } else if ( camera.type === 'orthographic' ) {
  1170. _camera = new THREE.OrthographicCamera( params.xmag / -2, params.xmag / 2, params.ymag / 2, params.ymag / -2, params.znear, params.zfar );
  1171. }
  1172. if ( camera.name !== undefined ) _camera.name = camera.name;
  1173. if ( camera.extras ) _camera.userData = camera.extras;
  1174. return _camera;
  1175. } );
  1176. };
  1177. GLTFParser.prototype.loadSkins = function () {
  1178. var json = this.json;
  1179. return this._withDependencies( [
  1180. 'accessors'
  1181. ] ).then( function ( dependencies ) {
  1182. return _each( json.skins, function ( skin ) {
  1183. var _skin = {
  1184. joints: skin.joints,
  1185. inverseBindMatrices: dependencies.accessors[ skin.inverseBindMatrices ]
  1186. };
  1187. return _skin;
  1188. } );
  1189. } );
  1190. };
  1191. GLTFParser.prototype.loadAnimations = function () {
  1192. var json = this.json;
  1193. return this._withDependencies( [
  1194. 'accessors',
  1195. 'nodes'
  1196. ] ).then( function ( dependencies ) {
  1197. return _each( json.animations, function ( animation, animationId ) {
  1198. var tracks = [];
  1199. for ( var channelId in animation.channels ) {
  1200. var channel = animation.channels[ channelId ];
  1201. var sampler = animation.samplers[ channel.sampler ];
  1202. if ( sampler ) {
  1203. var target = channel.target;
  1204. var name = target.node !== undefined ? target.node : target.id; // NOTE: target.id is deprecated.
  1205. var input = animation.parameters !== undefined ? animation.parameters[ sampler.input ] : sampler.input;
  1206. var output = animation.parameters !== undefined ? animation.parameters[ sampler.output ] : sampler.output;
  1207. var inputAccessor = dependencies.accessors[ input ];
  1208. var outputAccessor = dependencies.accessors[ output ];
  1209. var node = dependencies.nodes[ name ];
  1210. if ( node ) {
  1211. node.updateMatrix();
  1212. node.matrixAutoUpdate = true;
  1213. var TypedKeyframeTrack;
  1214. switch ( PATH_PROPERTIES[ target.path ] ) {
  1215. case PATH_PROPERTIES.weights:
  1216. TypedKeyframeTrack = THREE.NumberKeyframeTrack;
  1217. break;
  1218. case PATH_PROPERTIES.rotation:
  1219. TypedKeyframeTrack = THREE.QuaternionKeyframeTrack;
  1220. break;
  1221. case PATH_PROPERTIES.position:
  1222. case PATH_PROPERTIES.scale:
  1223. default:
  1224. TypedKeyframeTrack = THREE.VectorKeyframeTrack;
  1225. break;
  1226. }
  1227. var targetName = node.name ? node.name : node.uuid;
  1228. if ( sampler.interpolation === 'CATMULLROMSPLINE' ) {
  1229. console.warn( 'THREE.GLTF2Loader: CATMULLROMSPLINE interpolation is not supported. Using CUBICSPLINE instead.' );
  1230. }
  1231. var interpolation = sampler.interpolation !== undefined ? INTERPOLATION[ sampler.interpolation ] : THREE.InterpolateLinear;
  1232. var targetNames = [];
  1233. if ( PATH_PROPERTIES[ target.path ] === PATH_PROPERTIES.weights ) {
  1234. // node should be THREE.Group here but
  1235. // PATH_PROPERTIES.weights(morphTargetInfluences) should be
  1236. // the property of a mesh object under node.
  1237. // So finding targets here.
  1238. node.traverse( function ( object ) {
  1239. if ( object.isMesh === true && object.material.morphTargets === true ) {
  1240. targetNames.push( object.name ? object.name : object.uuid );
  1241. }
  1242. } );
  1243. } else {
  1244. targetNames.push( targetName );
  1245. }
  1246. // KeyframeTrack.optimize() will modify given 'times' and 'values'
  1247. // buffers before creating a truncated copy to keep. Because buffers may
  1248. // be reused by other tracks, make copies here.
  1249. for ( var i = 0, il = targetNames.length; i < il; i ++ ) {
  1250. tracks.push( new TypedKeyframeTrack(
  1251. targetNames[ i ] + '.' + PATH_PROPERTIES[ target.path ],
  1252. THREE.AnimationUtils.arraySlice( inputAccessor.array, 0 ),
  1253. THREE.AnimationUtils.arraySlice( outputAccessor.array, 0 ),
  1254. interpolation
  1255. ) );
  1256. }
  1257. }
  1258. }
  1259. }
  1260. var name = animation.name !== undefined ? animation.name : 'animation_' + animationId;
  1261. return new THREE.AnimationClip( name, undefined, tracks );
  1262. } );
  1263. } );
  1264. };
  1265. GLTFParser.prototype.loadNodes = function () {
  1266. var json = this.json;
  1267. var extensions = this.extensions;
  1268. var scope = this;
  1269. var nodes = json.nodes || [];
  1270. var skins = json.skins || [];
  1271. // Nothing in the node definition indicates whether it is a Bone or an
  1272. // Object3D. Use the skins' joint references to mark bones.
  1273. skins.forEach( function ( skin ) {
  1274. skin.joints.forEach( function ( id ) {
  1275. nodes[ id ].isBone = true;
  1276. } );
  1277. } );
  1278. return _each( json.nodes, function ( node ) {
  1279. var matrix = new THREE.Matrix4();
  1280. var _node = node.isBone === true ? new THREE.Bone() : new THREE.Object3D();
  1281. if ( node.name !== undefined ) {
  1282. _node.name = THREE.PropertyBinding.sanitizeNodeName( node.name );
  1283. }
  1284. if ( node.extras ) _node.userData = node.extras;
  1285. if ( node.matrix !== undefined ) {
  1286. matrix.fromArray( node.matrix );
  1287. _node.applyMatrix( matrix );
  1288. } else {
  1289. if ( node.translation !== undefined ) {
  1290. _node.position.fromArray( node.translation );
  1291. }
  1292. if ( node.rotation !== undefined ) {
  1293. _node.quaternion.fromArray( node.rotation );
  1294. }
  1295. if ( node.scale !== undefined ) {
  1296. _node.scale.fromArray( node.scale );
  1297. }
  1298. }
  1299. return _node;
  1300. } ).then( function ( __nodes ) {
  1301. return scope._withDependencies( [
  1302. 'meshes',
  1303. 'skins',
  1304. 'cameras'
  1305. ] ).then( function ( dependencies ) {
  1306. return _each( __nodes, function ( _node, nodeId ) {
  1307. var node = json.nodes[ nodeId ];
  1308. var meshes;
  1309. if ( node.mesh !== undefined) {
  1310. meshes = [ node.mesh ];
  1311. } else if ( node.meshes !== undefined ) {
  1312. console.warn( 'THREE.GLTF2Loader: Legacy glTF file detected. Nodes may have no more than one mesh.' );
  1313. meshes = node.meshes;
  1314. }
  1315. if ( meshes !== undefined ) {
  1316. for ( var meshId in meshes ) {
  1317. var mesh = meshes[ meshId ];
  1318. var group = dependencies.meshes[ mesh ];
  1319. if ( group === undefined ) {
  1320. console.warn( 'THREE.GLTF2Loader: Could not find node "' + mesh + '".' );
  1321. continue;
  1322. }
  1323. // do not clone children as they will be replaced anyway
  1324. var clonedgroup = group.clone( false );
  1325. for ( var childrenId in group.children ) {
  1326. var child = group.children[ childrenId ];
  1327. var originalChild = child;
  1328. // clone Mesh to add to _node
  1329. var originalMaterial = child.material;
  1330. var originalGeometry = child.geometry;
  1331. var originalInfluences = child.morphTargetInfluences;
  1332. var originalUserData = child.userData;
  1333. var originalName = child.name;
  1334. var material = originalMaterial;
  1335. switch ( child.type ) {
  1336. case 'LineSegments':
  1337. child = new THREE.LineSegments( originalGeometry, material );
  1338. break;
  1339. case 'LineLoop':
  1340. child = new THREE.LineLoop( originalGeometry, material );
  1341. break;
  1342. case 'Line':
  1343. child = new THREE.Line( originalGeometry, material );
  1344. break;
  1345. case 'Points':
  1346. child = new THREE.Points( originalGeometry, material );
  1347. break;
  1348. default:
  1349. child = new THREE.Mesh( originalGeometry, material );
  1350. child.drawMode = originalChild.drawMode;
  1351. }
  1352. child.castShadow = true;
  1353. child.morphTargetInfluences = originalInfluences;
  1354. child.userData = originalUserData;
  1355. child.name = originalName;
  1356. var skinEntry;
  1357. if ( node.skin !== undefined ) {
  1358. skinEntry = dependencies.skins[ node.skin ];
  1359. }
  1360. // Replace Mesh with SkinnedMesh in library
  1361. if ( skinEntry ) {
  1362. var geometry = originalGeometry;
  1363. material = originalMaterial;
  1364. material.skinning = true;
  1365. child = new THREE.SkinnedMesh( geometry, material );
  1366. child.castShadow = true;
  1367. child.userData = originalUserData;
  1368. child.name = originalName;
  1369. var bones = [];
  1370. var boneInverses = [];
  1371. for ( var i = 0, l = skinEntry.joints.length; i < l; i ++ ) {
  1372. var jointId = skinEntry.joints[ i ];
  1373. var jointNode = __nodes[ jointId ];
  1374. if ( jointNode ) {
  1375. bones.push( jointNode );
  1376. var m = skinEntry.inverseBindMatrices.array;
  1377. var mat = new THREE.Matrix4().fromArray( m, i * 16 );
  1378. boneInverses.push( mat );
  1379. } else {
  1380. console.warn( 'THREE.GLTF2Loader: Joint "%s" could not be found.', jointId );
  1381. }
  1382. }
  1383. child.bind( new THREE.Skeleton( bones, boneInverses ), child.matrixWorld );
  1384. }
  1385. clonedgroup.add( child );
  1386. }
  1387. _node.add( clonedgroup );
  1388. }
  1389. }
  1390. if ( node.camera !== undefined ) {
  1391. var camera = dependencies.cameras[ node.camera ];
  1392. _node.add( camera );
  1393. }
  1394. if ( node.extensions
  1395. && node.extensions[ EXTENSIONS.KHR_LIGHTS ]
  1396. && node.extensions[ EXTENSIONS.KHR_LIGHTS ].light !== undefined ) {
  1397. var lights = extensions[ EXTENSIONS.KHR_LIGHTS ].lights;
  1398. _node.add( lights[ node.extensions[ EXTENSIONS.KHR_LIGHTS ].light ] );
  1399. }
  1400. return _node;
  1401. } );
  1402. } );
  1403. } );
  1404. };
  1405. GLTFParser.prototype.loadScenes = function () {
  1406. var json = this.json;
  1407. var extensions = this.extensions;
  1408. // scene node hierachy builder
  1409. function buildNodeHierachy( nodeId, parentObject, allNodes ) {
  1410. var _node = allNodes[ nodeId ];
  1411. parentObject.add( _node );
  1412. var node = json.nodes[ nodeId ];
  1413. if ( node.children ) {
  1414. var children = node.children;
  1415. for ( var i = 0, l = children.length; i < l; i ++ ) {
  1416. var child = children[ i ];
  1417. buildNodeHierachy( child, _node, allNodes );
  1418. }
  1419. }
  1420. }
  1421. return this._withDependencies( [
  1422. 'nodes'
  1423. ] ).then( function ( dependencies ) {
  1424. return _each( json.scenes, function ( scene ) {
  1425. var _scene = new THREE.Scene();
  1426. if ( scene.name !== undefined ) _scene.name = scene.name;
  1427. if ( scene.extras ) _scene.userData = scene.extras;
  1428. var nodes = scene.nodes || [];
  1429. for ( var i = 0, l = nodes.length; i < l; i ++ ) {
  1430. var nodeId = nodes[ i ];
  1431. buildNodeHierachy( nodeId, _scene, dependencies.nodes );
  1432. }
  1433. _scene.traverse( function ( child ) {
  1434. // for Specular-Glossiness.
  1435. if ( child.material && child.material.isGLTFSpecularGlossinessMaterial ) {
  1436. child.onBeforeRender = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].refreshUniforms;
  1437. }
  1438. } );
  1439. // Ambient lighting, if present, is always attached to the scene root.
  1440. if ( scene.extensions
  1441. && scene.extensions[ EXTENSIONS.KHR_LIGHTS ]
  1442. && scene.extensions[ EXTENSIONS.KHR_LIGHTS ].light !== undefined ) {
  1443. var lights = extensions[ EXTENSIONS.KHR_LIGHTS ].lights;
  1444. _scene.add( lights[ scene.extensions[ EXTENSIONS.KHR_LIGHTS ].light ] );
  1445. }
  1446. return _scene;
  1447. } );
  1448. } );
  1449. };
  1450. return GLTF2Loader;
  1451. } )();