ObjectLoader.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. import {
  2. UVMapping,
  3. CubeReflectionMapping,
  4. CubeRefractionMapping,
  5. EquirectangularReflectionMapping,
  6. EquirectangularRefractionMapping,
  7. CubeUVReflectionMapping,
  8. RepeatWrapping,
  9. ClampToEdgeWrapping,
  10. MirroredRepeatWrapping,
  11. NearestFilter,
  12. NearestMipmapNearestFilter,
  13. NearestMipmapLinearFilter,
  14. LinearFilter,
  15. LinearMipmapNearestFilter,
  16. LinearMipmapLinearFilter
  17. } from '../constants.js';
  18. import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
  19. import { Color } from '../math/Color.js';
  20. import { Object3D } from '../core/Object3D.js';
  21. import { Group } from '../objects/Group.js';
  22. import { InstancedMesh } from '../objects/InstancedMesh.js';
  23. import { Sprite } from '../objects/Sprite.js';
  24. import { Points } from '../objects/Points.js';
  25. import { Line } from '../objects/Line.js';
  26. import { LineLoop } from '../objects/LineLoop.js';
  27. import { LineSegments } from '../objects/LineSegments.js';
  28. import { LOD } from '../objects/LOD.js';
  29. import { Mesh } from '../objects/Mesh.js';
  30. import { SkinnedMesh } from '../objects/SkinnedMesh.js';
  31. import { Bone } from '../objects/Bone.js';
  32. import { Skeleton } from '../objects/Skeleton.js';
  33. import { Shape } from '../extras/core/Shape.js';
  34. import { Fog } from '../scenes/Fog.js';
  35. import { FogExp2 } from '../scenes/FogExp2.js';
  36. import { HemisphereLight } from '../lights/HemisphereLight.js';
  37. import { SpotLight } from '../lights/SpotLight.js';
  38. import { PointLight } from '../lights/PointLight.js';
  39. import { DirectionalLight } from '../lights/DirectionalLight.js';
  40. import { AmbientLight } from '../lights/AmbientLight.js';
  41. import { RectAreaLight } from '../lights/RectAreaLight.js';
  42. import { LightProbe } from '../lights/LightProbe.js';
  43. import { OrthographicCamera } from '../cameras/OrthographicCamera.js';
  44. import { PerspectiveCamera } from '../cameras/PerspectiveCamera.js';
  45. import { Scene } from '../scenes/Scene.js';
  46. import { CubeTexture } from '../textures/CubeTexture.js';
  47. import { Texture } from '../textures/Texture.js';
  48. import { Source } from '../textures/Source.js';
  49. import { DataTexture } from '../textures/DataTexture.js';
  50. import { ImageLoader } from './ImageLoader.js';
  51. import { LoadingManager } from './LoadingManager.js';
  52. import { AnimationClip } from '../animation/AnimationClip.js';
  53. import { MaterialLoader } from './MaterialLoader.js';
  54. import { LoaderUtils } from './LoaderUtils.js';
  55. import { BufferGeometryLoader } from './BufferGeometryLoader.js';
  56. import { Loader } from './Loader.js';
  57. import { FileLoader } from './FileLoader.js';
  58. import * as Geometries from '../geometries/Geometries.js';
  59. import { getTypedArray } from '../utils.js';
  60. class ObjectLoader extends Loader {
  61. constructor( manager ) {
  62. super( manager );
  63. }
  64. load( url, onLoad, onProgress, onError ) {
  65. const scope = this;
  66. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  67. this.resourcePath = this.resourcePath || path;
  68. const loader = new FileLoader( this.manager );
  69. loader.setPath( this.path );
  70. loader.setRequestHeader( this.requestHeader );
  71. loader.setWithCredentials( this.withCredentials );
  72. loader.load( url, function ( text ) {
  73. let json = null;
  74. try {
  75. json = JSON.parse( text );
  76. } catch ( error ) {
  77. if ( onError !== undefined ) onError( error );
  78. console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message );
  79. return;
  80. }
  81. const metadata = json.metadata;
  82. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  83. if ( onError !== undefined ) onError( new Error( 'THREE.ObjectLoader: Can\'t load ' + url ) );
  84. console.error( 'THREE.ObjectLoader: Can\'t load ' + url );
  85. return;
  86. }
  87. scope.parse( json, onLoad );
  88. }, onProgress, onError );
  89. }
  90. async loadAsync( url, onProgress ) {
  91. const scope = this;
  92. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  93. this.resourcePath = this.resourcePath || path;
  94. const loader = new FileLoader( this.manager );
  95. loader.setPath( this.path );
  96. loader.setRequestHeader( this.requestHeader );
  97. loader.setWithCredentials( this.withCredentials );
  98. const text = await loader.loadAsync( url, onProgress );
  99. const json = JSON.parse( text );
  100. const metadata = json.metadata;
  101. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  102. throw new Error( 'THREE.ObjectLoader: Can\'t load ' + url );
  103. }
  104. return await scope.parseAsync( json );
  105. }
  106. parse( json, onLoad ) {
  107. const animations = this.parseAnimations( json.animations );
  108. const shapes = this.parseShapes( json.shapes );
  109. const geometries = this.parseGeometries( json.geometries, shapes );
  110. const images = this.parseImages( json.images, function () {
  111. if ( onLoad !== undefined ) onLoad( object );
  112. } );
  113. const textures = this.parseTextures( json.textures, images );
  114. const materials = this.parseMaterials( json.materials, textures );
  115. const object = this.parseObject( json.object, geometries, materials, textures, animations );
  116. const skeletons = this.parseSkeletons( json.skeletons, object );
  117. this.bindSkeletons( object, skeletons );
  118. //
  119. if ( onLoad !== undefined ) {
  120. let hasImages = false;
  121. for ( const uuid in images ) {
  122. if ( images[ uuid ].data instanceof HTMLImageElement ) {
  123. hasImages = true;
  124. break;
  125. }
  126. }
  127. if ( hasImages === false ) onLoad( object );
  128. }
  129. return object;
  130. }
  131. async parseAsync( json ) {
  132. const animations = this.parseAnimations( json.animations );
  133. const shapes = this.parseShapes( json.shapes );
  134. const geometries = this.parseGeometries( json.geometries, shapes );
  135. const images = await this.parseImagesAsync( json.images );
  136. const textures = this.parseTextures( json.textures, images );
  137. const materials = this.parseMaterials( json.materials, textures );
  138. const object = this.parseObject( json.object, geometries, materials, textures, animations );
  139. const skeletons = this.parseSkeletons( json.skeletons, object );
  140. this.bindSkeletons( object, skeletons );
  141. return object;
  142. }
  143. parseShapes( json ) {
  144. const shapes = {};
  145. if ( json !== undefined ) {
  146. for ( let i = 0, l = json.length; i < l; i ++ ) {
  147. const shape = new Shape().fromJSON( json[ i ] );
  148. shapes[ shape.uuid ] = shape;
  149. }
  150. }
  151. return shapes;
  152. }
  153. parseSkeletons( json, object ) {
  154. const skeletons = {};
  155. const bones = {};
  156. // generate bone lookup table
  157. object.traverse( function ( child ) {
  158. if ( child.isBone ) bones[ child.uuid ] = child;
  159. } );
  160. // create skeletons
  161. if ( json !== undefined ) {
  162. for ( let i = 0, l = json.length; i < l; i ++ ) {
  163. const skeleton = new Skeleton().fromJSON( json[ i ], bones );
  164. skeletons[ skeleton.uuid ] = skeleton;
  165. }
  166. }
  167. return skeletons;
  168. }
  169. parseGeometries( json, shapes ) {
  170. const geometries = {};
  171. if ( json !== undefined ) {
  172. const bufferGeometryLoader = new BufferGeometryLoader();
  173. for ( let i = 0, l = json.length; i < l; i ++ ) {
  174. let geometry;
  175. const data = json[ i ];
  176. switch ( data.type ) {
  177. case 'BufferGeometry':
  178. case 'InstancedBufferGeometry':
  179. geometry = bufferGeometryLoader.parse( data );
  180. break;
  181. default:
  182. if ( data.type in Geometries ) {
  183. geometry = Geometries[ data.type ].fromJSON( data, shapes );
  184. } else {
  185. console.warn( `THREE.ObjectLoader: Unsupported geometry type "${ data.type }"` );
  186. }
  187. }
  188. geometry.uuid = data.uuid;
  189. if ( data.name !== undefined ) geometry.name = data.name;
  190. if ( data.userData !== undefined ) geometry.userData = data.userData;
  191. geometries[ data.uuid ] = geometry;
  192. }
  193. }
  194. return geometries;
  195. }
  196. parseMaterials( json, textures ) {
  197. const cache = {}; // MultiMaterial
  198. const materials = {};
  199. if ( json !== undefined ) {
  200. const loader = new MaterialLoader();
  201. loader.setTextures( textures );
  202. for ( let i = 0, l = json.length; i < l; i ++ ) {
  203. const data = json[ i ];
  204. if ( cache[ data.uuid ] === undefined ) {
  205. cache[ data.uuid ] = loader.parse( data );
  206. }
  207. materials[ data.uuid ] = cache[ data.uuid ];
  208. }
  209. }
  210. return materials;
  211. }
  212. parseAnimations( json ) {
  213. const animations = {};
  214. if ( json !== undefined ) {
  215. for ( let i = 0; i < json.length; i ++ ) {
  216. const data = json[ i ];
  217. const clip = AnimationClip.parse( data );
  218. animations[ clip.uuid ] = clip;
  219. }
  220. }
  221. return animations;
  222. }
  223. parseImages( json, onLoad ) {
  224. const scope = this;
  225. const images = {};
  226. let loader;
  227. function loadImage( url ) {
  228. scope.manager.itemStart( url );
  229. return loader.load( url, function () {
  230. scope.manager.itemEnd( url );
  231. }, undefined, function () {
  232. scope.manager.itemError( url );
  233. scope.manager.itemEnd( url );
  234. } );
  235. }
  236. function deserializeImage( image ) {
  237. if ( typeof image === 'string' ) {
  238. const url = image;
  239. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  240. return loadImage( path );
  241. } else {
  242. if ( image.data ) {
  243. return {
  244. data: getTypedArray( image.type, image.data ),
  245. width: image.width,
  246. height: image.height
  247. };
  248. } else {
  249. return null;
  250. }
  251. }
  252. }
  253. if ( json !== undefined && json.length > 0 ) {
  254. const manager = new LoadingManager( onLoad );
  255. loader = new ImageLoader( manager );
  256. loader.setCrossOrigin( this.crossOrigin );
  257. for ( let i = 0, il = json.length; i < il; i ++ ) {
  258. const image = json[ i ];
  259. const url = image.url;
  260. if ( Array.isArray( url ) ) {
  261. // load array of images e.g CubeTexture
  262. const imageArray = [];
  263. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  264. const currentUrl = url[ j ];
  265. const deserializedImage = deserializeImage( currentUrl );
  266. if ( deserializedImage !== null ) {
  267. if ( deserializedImage instanceof HTMLImageElement ) {
  268. imageArray.push( deserializedImage );
  269. } else {
  270. // special case: handle array of data textures for cube textures
  271. imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  272. }
  273. }
  274. }
  275. images[ image.uuid ] = new Source( imageArray );
  276. } else {
  277. // load single image
  278. const deserializedImage = deserializeImage( image.url );
  279. images[ image.uuid ] = new Source( deserializedImage );
  280. }
  281. }
  282. }
  283. return images;
  284. }
  285. async parseImagesAsync( json ) {
  286. const scope = this;
  287. const images = {};
  288. let loader;
  289. async function deserializeImage( image ) {
  290. if ( typeof image === 'string' ) {
  291. const url = image;
  292. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  293. return await loader.loadAsync( path );
  294. } else {
  295. if ( image.data ) {
  296. return {
  297. data: getTypedArray( image.type, image.data ),
  298. width: image.width,
  299. height: image.height
  300. };
  301. } else {
  302. return null;
  303. }
  304. }
  305. }
  306. if ( json !== undefined && json.length > 0 ) {
  307. loader = new ImageLoader( this.manager );
  308. loader.setCrossOrigin( this.crossOrigin );
  309. for ( let i = 0, il = json.length; i < il; i ++ ) {
  310. const image = json[ i ];
  311. const url = image.url;
  312. if ( Array.isArray( url ) ) {
  313. // load array of images e.g CubeTexture
  314. const imageArray = [];
  315. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  316. const currentUrl = url[ j ];
  317. const deserializedImage = await deserializeImage( currentUrl );
  318. if ( deserializedImage !== null ) {
  319. if ( deserializedImage instanceof HTMLImageElement ) {
  320. imageArray.push( deserializedImage );
  321. } else {
  322. // special case: handle array of data textures for cube textures
  323. imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  324. }
  325. }
  326. }
  327. images[ image.uuid ] = new Source( imageArray );
  328. } else {
  329. // load single image
  330. const deserializedImage = await deserializeImage( image.url );
  331. images[ image.uuid ] = new Source( deserializedImage );
  332. }
  333. }
  334. }
  335. return images;
  336. }
  337. parseTextures( json, images ) {
  338. function parseConstant( value, type ) {
  339. if ( typeof value === 'number' ) return value;
  340. console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value );
  341. return type[ value ];
  342. }
  343. const textures = {};
  344. if ( json !== undefined ) {
  345. for ( let i = 0, l = json.length; i < l; i ++ ) {
  346. const data = json[ i ];
  347. if ( data.image === undefined ) {
  348. console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid );
  349. }
  350. if ( images[ data.image ] === undefined ) {
  351. console.warn( 'THREE.ObjectLoader: Undefined image', data.image );
  352. }
  353. const source = images[ data.image ];
  354. const image = source.data;
  355. let texture;
  356. if ( Array.isArray( image ) ) {
  357. texture = new CubeTexture();
  358. if ( image.length === 6 ) texture.needsUpdate = true;
  359. } else {
  360. if ( image && image.data ) {
  361. texture = new DataTexture();
  362. } else {
  363. texture = new Texture();
  364. }
  365. if ( image ) texture.needsUpdate = true; // textures can have undefined image data
  366. }
  367. texture.source = source;
  368. texture.uuid = data.uuid;
  369. if ( data.name !== undefined ) texture.name = data.name;
  370. if ( data.mapping !== undefined ) texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING );
  371. if ( data.channel !== undefined ) texture.channel = data.channel;
  372. if ( data.offset !== undefined ) texture.offset.fromArray( data.offset );
  373. if ( data.repeat !== undefined ) texture.repeat.fromArray( data.repeat );
  374. if ( data.center !== undefined ) texture.center.fromArray( data.center );
  375. if ( data.rotation !== undefined ) texture.rotation = data.rotation;
  376. if ( data.wrap !== undefined ) {
  377. texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING );
  378. texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING );
  379. }
  380. if ( data.format !== undefined ) texture.format = data.format;
  381. if ( data.internalFormat !== undefined ) texture.internalFormat = data.internalFormat;
  382. if ( data.type !== undefined ) texture.type = data.type;
  383. if ( data.colorSpace !== undefined ) texture.colorSpace = data.colorSpace;
  384. if ( data.encoding !== undefined ) texture.encoding = data.encoding; // @deprecated, r152
  385. if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER );
  386. if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER );
  387. if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy;
  388. if ( data.flipY !== undefined ) texture.flipY = data.flipY;
  389. if ( data.generateMipmaps !== undefined ) texture.generateMipmaps = data.generateMipmaps;
  390. if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha;
  391. if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment;
  392. if ( data.compareFunction !== undefined ) texture.compareFunction = data.compareFunction;
  393. if ( data.userData !== undefined ) texture.userData = data.userData;
  394. textures[ data.uuid ] = texture;
  395. }
  396. }
  397. return textures;
  398. }
  399. parseObject( data, geometries, materials, textures, animations ) {
  400. let object;
  401. function getGeometry( name ) {
  402. if ( geometries[ name ] === undefined ) {
  403. console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
  404. }
  405. return geometries[ name ];
  406. }
  407. function getMaterial( name ) {
  408. if ( name === undefined ) return undefined;
  409. if ( Array.isArray( name ) ) {
  410. const array = [];
  411. for ( let i = 0, l = name.length; i < l; i ++ ) {
  412. const uuid = name[ i ];
  413. if ( materials[ uuid ] === undefined ) {
  414. console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
  415. }
  416. array.push( materials[ uuid ] );
  417. }
  418. return array;
  419. }
  420. if ( materials[ name ] === undefined ) {
  421. console.warn( 'THREE.ObjectLoader: Undefined material', name );
  422. }
  423. return materials[ name ];
  424. }
  425. function getTexture( uuid ) {
  426. if ( textures[ uuid ] === undefined ) {
  427. console.warn( 'THREE.ObjectLoader: Undefined texture', uuid );
  428. }
  429. return textures[ uuid ];
  430. }
  431. let geometry, material;
  432. switch ( data.type ) {
  433. case 'Scene':
  434. object = new Scene();
  435. if ( data.background !== undefined ) {
  436. if ( Number.isInteger( data.background ) ) {
  437. object.background = new Color( data.background );
  438. } else {
  439. object.background = getTexture( data.background );
  440. }
  441. }
  442. if ( data.environment !== undefined ) {
  443. object.environment = getTexture( data.environment );
  444. }
  445. if ( data.fog !== undefined ) {
  446. if ( data.fog.type === 'Fog' ) {
  447. object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
  448. } else if ( data.fog.type === 'FogExp2' ) {
  449. object.fog = new FogExp2( data.fog.color, data.fog.density );
  450. }
  451. if ( data.fog.name !== '' ) {
  452. object.fog.name = data.fog.name;
  453. }
  454. }
  455. if ( data.backgroundBlurriness !== undefined ) object.backgroundBlurriness = data.backgroundBlurriness;
  456. if ( data.backgroundIntensity !== undefined ) object.backgroundIntensity = data.backgroundIntensity;
  457. break;
  458. case 'PerspectiveCamera':
  459. object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
  460. if ( data.focus !== undefined ) object.focus = data.focus;
  461. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  462. if ( data.filmGauge !== undefined ) object.filmGauge = data.filmGauge;
  463. if ( data.filmOffset !== undefined ) object.filmOffset = data.filmOffset;
  464. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  465. break;
  466. case 'OrthographicCamera':
  467. object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
  468. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  469. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  470. break;
  471. case 'AmbientLight':
  472. object = new AmbientLight( data.color, data.intensity );
  473. break;
  474. case 'DirectionalLight':
  475. object = new DirectionalLight( data.color, data.intensity );
  476. break;
  477. case 'PointLight':
  478. object = new PointLight( data.color, data.intensity, data.distance, data.decay );
  479. break;
  480. case 'RectAreaLight':
  481. object = new RectAreaLight( data.color, data.intensity, data.width, data.height );
  482. break;
  483. case 'SpotLight':
  484. object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
  485. break;
  486. case 'HemisphereLight':
  487. object = new HemisphereLight( data.color, data.groundColor, data.intensity );
  488. break;
  489. case 'LightProbe':
  490. object = new LightProbe().fromJSON( data );
  491. break;
  492. case 'SkinnedMesh':
  493. geometry = getGeometry( data.geometry );
  494. material = getMaterial( data.material );
  495. object = new SkinnedMesh( geometry, material );
  496. if ( data.bindMode !== undefined ) object.bindMode = data.bindMode;
  497. if ( data.bindMatrix !== undefined ) object.bindMatrix.fromArray( data.bindMatrix );
  498. if ( data.skeleton !== undefined ) object.skeleton = data.skeleton;
  499. break;
  500. case 'Mesh':
  501. geometry = getGeometry( data.geometry );
  502. material = getMaterial( data.material );
  503. object = new Mesh( geometry, material );
  504. break;
  505. case 'InstancedMesh':
  506. geometry = getGeometry( data.geometry );
  507. material = getMaterial( data.material );
  508. const count = data.count;
  509. const instanceMatrix = data.instanceMatrix;
  510. const instanceColor = data.instanceColor;
  511. object = new InstancedMesh( geometry, material, count );
  512. object.instanceMatrix = new InstancedBufferAttribute( new Float32Array( instanceMatrix.array ), 16 );
  513. if ( instanceColor !== undefined ) object.instanceColor = new InstancedBufferAttribute( new Float32Array( instanceColor.array ), instanceColor.itemSize );
  514. break;
  515. case 'LOD':
  516. object = new LOD();
  517. break;
  518. case 'Line':
  519. object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) );
  520. break;
  521. case 'LineLoop':
  522. object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
  523. break;
  524. case 'LineSegments':
  525. object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
  526. break;
  527. case 'PointCloud':
  528. case 'Points':
  529. object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
  530. break;
  531. case 'Sprite':
  532. object = new Sprite( getMaterial( data.material ) );
  533. break;
  534. case 'Group':
  535. object = new Group();
  536. break;
  537. case 'Bone':
  538. object = new Bone();
  539. break;
  540. default:
  541. object = new Object3D();
  542. }
  543. object.uuid = data.uuid;
  544. if ( data.name !== undefined ) object.name = data.name;
  545. if ( data.matrix !== undefined ) {
  546. object.matrix.fromArray( data.matrix );
  547. if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate;
  548. if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale );
  549. } else {
  550. if ( data.position !== undefined ) object.position.fromArray( data.position );
  551. if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation );
  552. if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion );
  553. if ( data.scale !== undefined ) object.scale.fromArray( data.scale );
  554. }
  555. if ( data.up !== undefined ) object.up.fromArray( data.up );
  556. if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
  557. if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
  558. if ( data.shadow ) {
  559. if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
  560. if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias;
  561. if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
  562. if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
  563. if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
  564. }
  565. if ( data.visible !== undefined ) object.visible = data.visible;
  566. if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled;
  567. if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder;
  568. if ( data.userData !== undefined ) object.userData = data.userData;
  569. if ( data.layers !== undefined ) object.layers.mask = data.layers;
  570. if ( data.children !== undefined ) {
  571. const children = data.children;
  572. for ( let i = 0; i < children.length; i ++ ) {
  573. object.add( this.parseObject( children[ i ], geometries, materials, textures, animations ) );
  574. }
  575. }
  576. if ( data.animations !== undefined ) {
  577. const objectAnimations = data.animations;
  578. for ( let i = 0; i < objectAnimations.length; i ++ ) {
  579. const uuid = objectAnimations[ i ];
  580. object.animations.push( animations[ uuid ] );
  581. }
  582. }
  583. if ( data.type === 'LOD' ) {
  584. if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate;
  585. const levels = data.levels;
  586. for ( let l = 0; l < levels.length; l ++ ) {
  587. const level = levels[ l ];
  588. const child = object.getObjectByProperty( 'uuid', level.object );
  589. if ( child !== undefined ) {
  590. object.addLevel( child, level.distance, level.hysteresis );
  591. }
  592. }
  593. }
  594. return object;
  595. }
  596. bindSkeletons( object, skeletons ) {
  597. if ( Object.keys( skeletons ).length === 0 ) return;
  598. object.traverse( function ( child ) {
  599. if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) {
  600. const skeleton = skeletons[ child.skeleton ];
  601. if ( skeleton === undefined ) {
  602. console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton );
  603. } else {
  604. child.bind( skeleton, child.bindMatrix );
  605. }
  606. }
  607. } );
  608. }
  609. }
  610. const TEXTURE_MAPPING = {
  611. UVMapping: UVMapping,
  612. CubeReflectionMapping: CubeReflectionMapping,
  613. CubeRefractionMapping: CubeRefractionMapping,
  614. EquirectangularReflectionMapping: EquirectangularReflectionMapping,
  615. EquirectangularRefractionMapping: EquirectangularRefractionMapping,
  616. CubeUVReflectionMapping: CubeUVReflectionMapping
  617. };
  618. const TEXTURE_WRAPPING = {
  619. RepeatWrapping: RepeatWrapping,
  620. ClampToEdgeWrapping: ClampToEdgeWrapping,
  621. MirroredRepeatWrapping: MirroredRepeatWrapping
  622. };
  623. const TEXTURE_FILTER = {
  624. NearestFilter: NearestFilter,
  625. NearestMipmapNearestFilter: NearestMipmapNearestFilter,
  626. NearestMipmapLinearFilter: NearestMipmapLinearFilter,
  627. LinearFilter: LinearFilter,
  628. LinearMipmapNearestFilter: LinearMipmapNearestFilter,
  629. LinearMipmapLinearFilter: LinearMipmapLinearFilter
  630. };
  631. export { ObjectLoader };