ObjectLoader.js 24 KB

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