ObjectLoader.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. import {
  2. UVMapping,
  3. CubeReflectionMapping,
  4. CubeRefractionMapping,
  5. EquirectangularReflectionMapping,
  6. EquirectangularRefractionMapping,
  7. CubeUVReflectionMapping,
  8. CubeUVRefractionMapping,
  9. RepeatWrapping,
  10. ClampToEdgeWrapping,
  11. MirroredRepeatWrapping,
  12. NearestFilter,
  13. NearestMipmapNearestFilter,
  14. NearestMipmapLinearFilter,
  15. LinearFilter,
  16. LinearMipmapNearestFilter,
  17. LinearMipmapLinearFilter
  18. } from '../constants.js';
  19. import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
  20. import { Color } from '../math/Color.js';
  21. import { Object3D } from '../core/Object3D.js';
  22. import { Group } from '../objects/Group.js';
  23. import { InstancedMesh } from '../objects/InstancedMesh.js';
  24. import { Sprite } from '../objects/Sprite.js';
  25. import { Points } from '../objects/Points.js';
  26. import { Line } from '../objects/Line.js';
  27. import { LineLoop } from '../objects/LineLoop.js';
  28. import { LineSegments } from '../objects/LineSegments.js';
  29. import { LOD } from '../objects/LOD.js';
  30. import { Mesh } from '../objects/Mesh.js';
  31. import { SkinnedMesh } from '../objects/SkinnedMesh.js';
  32. import { Bone } from '../objects/Bone.js';
  33. import { Skeleton } from '../objects/Skeleton.js';
  34. import { Shape } from '../extras/core/Shape.js';
  35. import { Fog } from '../scenes/Fog.js';
  36. import { FogExp2 } from '../scenes/FogExp2.js';
  37. import { HemisphereLight } from '../lights/HemisphereLight.js';
  38. import { SpotLight } from '../lights/SpotLight.js';
  39. import { PointLight } from '../lights/PointLight.js';
  40. import { DirectionalLight } from '../lights/DirectionalLight.js';
  41. import { AmbientLight } from '../lights/AmbientLight.js';
  42. import { RectAreaLight } from '../lights/RectAreaLight.js';
  43. import { LightProbe } from '../lights/LightProbe.js';
  44. import { OrthographicCamera } from '../cameras/OrthographicCamera.js';
  45. import { PerspectiveCamera } from '../cameras/PerspectiveCamera.js';
  46. import { Scene } from '../scenes/Scene.js';
  47. import { CubeTexture } from '../textures/CubeTexture.js';
  48. import { Texture } from '../textures/Texture.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 ] 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. case 'Geometry':
  181. console.error( 'THREE.ObjectLoader: The legacy Geometry type is no longer supported.' );
  182. break;
  183. default:
  184. if ( data.type in Geometries ) {
  185. geometry = Geometries[ data.type ].fromJSON( data, shapes );
  186. } else {
  187. console.warn( `THREE.ObjectLoader: Unsupported geometry type "${ data.type }"` );
  188. }
  189. }
  190. geometry.uuid = data.uuid;
  191. if ( data.name !== undefined ) geometry.name = data.name;
  192. if ( geometry.isBufferGeometry === true && data.userData !== undefined ) geometry.userData = data.userData;
  193. geometries[ data.uuid ] = geometry;
  194. }
  195. }
  196. return geometries;
  197. }
  198. parseMaterials( json, textures ) {
  199. const cache = {}; // MultiMaterial
  200. const materials = {};
  201. if ( json !== undefined ) {
  202. const loader = new MaterialLoader();
  203. loader.setTextures( textures );
  204. for ( let i = 0, l = json.length; i < l; i ++ ) {
  205. const data = json[ i ];
  206. if ( data.type === 'MultiMaterial' ) {
  207. // Deprecated
  208. const array = [];
  209. for ( let j = 0; j < data.materials.length; j ++ ) {
  210. const material = data.materials[ j ];
  211. if ( cache[ material.uuid ] === undefined ) {
  212. cache[ material.uuid ] = loader.parse( material );
  213. }
  214. array.push( cache[ material.uuid ] );
  215. }
  216. materials[ data.uuid ] = array;
  217. } else {
  218. if ( cache[ data.uuid ] === undefined ) {
  219. cache[ data.uuid ] = loader.parse( data );
  220. }
  221. materials[ data.uuid ] = cache[ data.uuid ];
  222. }
  223. }
  224. }
  225. return materials;
  226. }
  227. parseAnimations( json ) {
  228. const animations = {};
  229. if ( json !== undefined ) {
  230. for ( let i = 0; i < json.length; i ++ ) {
  231. const data = json[ i ];
  232. const clip = AnimationClip.parse( data );
  233. animations[ clip.uuid ] = clip;
  234. }
  235. }
  236. return animations;
  237. }
  238. parseImages( json, onLoad ) {
  239. const scope = this;
  240. const images = {};
  241. let loader;
  242. function loadImage( url ) {
  243. scope.manager.itemStart( url );
  244. return loader.load( url, function () {
  245. scope.manager.itemEnd( url );
  246. }, undefined, function () {
  247. scope.manager.itemError( url );
  248. scope.manager.itemEnd( url );
  249. } );
  250. }
  251. function deserializeImage( image ) {
  252. if ( typeof image === 'string' ) {
  253. const url = image;
  254. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  255. return loadImage( path );
  256. } else {
  257. if ( image.data ) {
  258. return {
  259. data: getTypedArray( image.type, image.data ),
  260. width: image.width,
  261. height: image.height
  262. };
  263. } else {
  264. return null;
  265. }
  266. }
  267. }
  268. if ( json !== undefined && json.length > 0 ) {
  269. const manager = new LoadingManager( onLoad );
  270. loader = new ImageLoader( manager );
  271. loader.setCrossOrigin( this.crossOrigin );
  272. for ( let i = 0, il = json.length; i < il; i ++ ) {
  273. const image = json[ i ];
  274. const url = image.url;
  275. if ( Array.isArray( url ) ) {
  276. // load array of images e.g CubeTexture
  277. images[ image.uuid ] = [];
  278. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  279. const currentUrl = url[ j ];
  280. const deserializedImage = deserializeImage( currentUrl );
  281. if ( deserializedImage !== null ) {
  282. if ( deserializedImage instanceof HTMLImageElement ) {
  283. images[ image.uuid ].push( deserializedImage );
  284. } else {
  285. // special case: handle array of data textures for cube textures
  286. images[ image.uuid ].push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  287. }
  288. }
  289. }
  290. } else {
  291. // load single image
  292. const deserializedImage = deserializeImage( image.url );
  293. if ( deserializedImage !== null ) {
  294. images[ image.uuid ] = deserializedImage;
  295. }
  296. }
  297. }
  298. }
  299. return images;
  300. }
  301. async parseImagesAsync( json ) {
  302. const scope = this;
  303. const images = {};
  304. let loader;
  305. async function deserializeImage( image ) {
  306. if ( typeof image === 'string' ) {
  307. const url = image;
  308. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  309. return await loader.loadAsync( path );
  310. } else {
  311. if ( image.data ) {
  312. return {
  313. data: getTypedArray( image.type, image.data ),
  314. width: image.width,
  315. height: image.height
  316. };
  317. } else {
  318. return null;
  319. }
  320. }
  321. }
  322. if ( json !== undefined && json.length > 0 ) {
  323. loader = new ImageLoader( this.manager );
  324. loader.setCrossOrigin( this.crossOrigin );
  325. for ( let i = 0, il = json.length; i < il; i ++ ) {
  326. const image = json[ i ];
  327. const url = image.url;
  328. if ( Array.isArray( url ) ) {
  329. // load array of images e.g CubeTexture
  330. images[ image.uuid ] = [];
  331. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  332. const currentUrl = url[ j ];
  333. const deserializedImage = await deserializeImage( currentUrl );
  334. if ( deserializedImage !== null ) {
  335. if ( deserializedImage instanceof HTMLImageElement ) {
  336. images[ image.uuid ].push( deserializedImage );
  337. } else {
  338. // special case: handle array of data textures for cube textures
  339. images[ image.uuid ].push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  340. }
  341. }
  342. }
  343. } else {
  344. // load single image
  345. const deserializedImage = await deserializeImage( image.url );
  346. if ( deserializedImage !== null ) {
  347. images[ image.uuid ] = deserializedImage;
  348. }
  349. }
  350. }
  351. }
  352. return images;
  353. }
  354. parseTextures( json, images ) {
  355. function parseConstant( value, type ) {
  356. if ( typeof value === 'number' ) return value;
  357. console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value );
  358. return type[ value ];
  359. }
  360. const textures = {};
  361. if ( json !== undefined ) {
  362. for ( let i = 0, l = json.length; i < l; i ++ ) {
  363. const data = json[ i ];
  364. if ( data.image === undefined ) {
  365. console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid );
  366. }
  367. if ( images[ data.image ] === undefined ) {
  368. console.warn( 'THREE.ObjectLoader: Undefined image', data.image );
  369. }
  370. let texture;
  371. const image = images[ data.image ];
  372. if ( Array.isArray( image ) ) {
  373. texture = new CubeTexture( image );
  374. if ( image.length === 6 ) texture.needsUpdate = true;
  375. } else {
  376. if ( image && image.data ) {
  377. texture = new DataTexture( image.data, image.width, image.height );
  378. } else {
  379. texture = new Texture( image );
  380. }
  381. if ( image ) texture.needsUpdate = true; // textures can have undefined image data
  382. }
  383. texture.uuid = data.uuid;
  384. if ( data.name !== undefined ) texture.name = data.name;
  385. if ( data.mapping !== undefined ) texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING );
  386. if ( data.offset !== undefined ) texture.offset.fromArray( data.offset );
  387. if ( data.repeat !== undefined ) texture.repeat.fromArray( data.repeat );
  388. if ( data.center !== undefined ) texture.center.fromArray( data.center );
  389. if ( data.rotation !== undefined ) texture.rotation = data.rotation;
  390. if ( data.wrap !== undefined ) {
  391. texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING );
  392. texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING );
  393. }
  394. if ( data.format !== undefined ) texture.format = data.format;
  395. if ( data.type !== undefined ) texture.type = data.type;
  396. if ( data.encoding !== undefined ) texture.encoding = data.encoding;
  397. if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER );
  398. if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER );
  399. if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy;
  400. if ( data.flipY !== undefined ) texture.flipY = data.flipY;
  401. if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha;
  402. if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment;
  403. if ( data.userData !== undefined ) texture.userData = data.userData;
  404. textures[ data.uuid ] = texture;
  405. }
  406. }
  407. return textures;
  408. }
  409. parseObject( data, geometries, materials, textures, animations ) {
  410. let object;
  411. function getGeometry( name ) {
  412. if ( geometries[ name ] === undefined ) {
  413. console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
  414. }
  415. return geometries[ name ];
  416. }
  417. function getMaterial( name ) {
  418. if ( name === undefined ) return undefined;
  419. if ( Array.isArray( name ) ) {
  420. const array = [];
  421. for ( let i = 0, l = name.length; i < l; i ++ ) {
  422. const uuid = name[ i ];
  423. if ( materials[ uuid ] === undefined ) {
  424. console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
  425. }
  426. array.push( materials[ uuid ] );
  427. }
  428. return array;
  429. }
  430. if ( materials[ name ] === undefined ) {
  431. console.warn( 'THREE.ObjectLoader: Undefined material', name );
  432. }
  433. return materials[ name ];
  434. }
  435. function getTexture( uuid ) {
  436. if ( textures[ uuid ] === undefined ) {
  437. console.warn( 'THREE.ObjectLoader: Undefined texture', uuid );
  438. }
  439. return textures[ uuid ];
  440. }
  441. let geometry, material;
  442. switch ( data.type ) {
  443. case 'Scene':
  444. object = new Scene();
  445. if ( data.background !== undefined ) {
  446. if ( Number.isInteger( data.background ) ) {
  447. object.background = new Color( data.background );
  448. } else {
  449. object.background = getTexture( data.background );
  450. }
  451. }
  452. if ( data.environment !== undefined ) {
  453. object.environment = getTexture( data.environment );
  454. }
  455. if ( data.fog !== undefined ) {
  456. if ( data.fog.type === 'Fog' ) {
  457. object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
  458. } else if ( data.fog.type === 'FogExp2' ) {
  459. object.fog = new FogExp2( data.fog.color, data.fog.density );
  460. }
  461. }
  462. break;
  463. case 'PerspectiveCamera':
  464. object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
  465. if ( data.focus !== undefined ) object.focus = data.focus;
  466. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  467. if ( data.filmGauge !== undefined ) object.filmGauge = data.filmGauge;
  468. if ( data.filmOffset !== undefined ) object.filmOffset = data.filmOffset;
  469. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  470. break;
  471. case 'OrthographicCamera':
  472. object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
  473. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  474. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  475. break;
  476. case 'AmbientLight':
  477. object = new AmbientLight( data.color, data.intensity );
  478. break;
  479. case 'DirectionalLight':
  480. object = new DirectionalLight( data.color, data.intensity );
  481. break;
  482. case 'PointLight':
  483. object = new PointLight( data.color, data.intensity, data.distance, data.decay );
  484. break;
  485. case 'RectAreaLight':
  486. object = new RectAreaLight( data.color, data.intensity, data.width, data.height );
  487. break;
  488. case 'SpotLight':
  489. object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
  490. break;
  491. case 'HemisphereLight':
  492. object = new HemisphereLight( data.color, data.groundColor, data.intensity );
  493. break;
  494. case 'LightProbe':
  495. object = new LightProbe().fromJSON( data );
  496. break;
  497. case 'SkinnedMesh':
  498. geometry = getGeometry( data.geometry );
  499. material = getMaterial( data.material );
  500. object = new SkinnedMesh( geometry, material );
  501. if ( data.bindMode !== undefined ) object.bindMode = data.bindMode;
  502. if ( data.bindMatrix !== undefined ) object.bindMatrix.fromArray( data.bindMatrix );
  503. if ( data.skeleton !== undefined ) object.skeleton = data.skeleton;
  504. break;
  505. case 'Mesh':
  506. geometry = getGeometry( data.geometry );
  507. material = getMaterial( data.material );
  508. object = new Mesh( geometry, material );
  509. break;
  510. case 'InstancedMesh':
  511. geometry = getGeometry( data.geometry );
  512. material = getMaterial( data.material );
  513. const count = data.count;
  514. const instanceMatrix = data.instanceMatrix;
  515. const instanceColor = data.instanceColor;
  516. object = new InstancedMesh( geometry, material, count );
  517. object.instanceMatrix = new InstancedBufferAttribute( new Float32Array( instanceMatrix.array ), 16 );
  518. if ( instanceColor !== undefined ) object.instanceColor = new InstancedBufferAttribute( new Float32Array( instanceColor.array ), instanceColor.itemSize );
  519. break;
  520. case 'LOD':
  521. object = new LOD();
  522. break;
  523. case 'Line':
  524. object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) );
  525. break;
  526. case 'LineLoop':
  527. object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
  528. break;
  529. case 'LineSegments':
  530. object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
  531. break;
  532. case 'PointCloud':
  533. case 'Points':
  534. object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
  535. break;
  536. case 'Sprite':
  537. object = new Sprite( getMaterial( data.material ) );
  538. break;
  539. case 'Group':
  540. object = new Group();
  541. break;
  542. case 'Bone':
  543. object = new Bone();
  544. break;
  545. default:
  546. object = new Object3D();
  547. }
  548. object.uuid = data.uuid;
  549. if ( data.name !== undefined ) object.name = data.name;
  550. if ( data.matrix !== undefined ) {
  551. object.matrix.fromArray( data.matrix );
  552. if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate;
  553. if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale );
  554. } else {
  555. if ( data.position !== undefined ) object.position.fromArray( data.position );
  556. if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation );
  557. if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion );
  558. if ( data.scale !== undefined ) object.scale.fromArray( data.scale );
  559. }
  560. if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
  561. if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
  562. if ( data.shadow ) {
  563. if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
  564. if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias;
  565. if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
  566. if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
  567. if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
  568. }
  569. if ( data.visible !== undefined ) object.visible = data.visible;
  570. if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled;
  571. if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder;
  572. if ( data.userData !== undefined ) object.userData = data.userData;
  573. if ( data.layers !== undefined ) object.layers.mask = data.layers;
  574. if ( data.children !== undefined ) {
  575. const children = data.children;
  576. for ( let i = 0; i < children.length; i ++ ) {
  577. object.add( this.parseObject( children[ i ], geometries, materials, textures, animations ) );
  578. }
  579. }
  580. if ( data.animations !== undefined ) {
  581. const objectAnimations = data.animations;
  582. for ( let i = 0; i < objectAnimations.length; i ++ ) {
  583. const uuid = objectAnimations[ i ];
  584. object.animations.push( animations[ uuid ] );
  585. }
  586. }
  587. if ( data.type === 'LOD' ) {
  588. if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate;
  589. const levels = data.levels;
  590. for ( let l = 0; l < levels.length; l ++ ) {
  591. const level = levels[ l ];
  592. const child = object.getObjectByProperty( 'uuid', level.object );
  593. if ( child !== undefined ) {
  594. object.addLevel( child, level.distance );
  595. }
  596. }
  597. }
  598. return object;
  599. }
  600. bindSkeletons( object, skeletons ) {
  601. if ( Object.keys( skeletons ).length === 0 ) return;
  602. object.traverse( function ( child ) {
  603. if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) {
  604. const skeleton = skeletons[ child.skeleton ];
  605. if ( skeleton === undefined ) {
  606. console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton );
  607. } else {
  608. child.bind( skeleton, child.bindMatrix );
  609. }
  610. }
  611. } );
  612. }
  613. /* DEPRECATED */
  614. setTexturePath( value ) {
  615. console.warn( 'THREE.ObjectLoader: .setTexturePath() has been renamed to .setResourcePath().' );
  616. return this.setResourcePath( value );
  617. }
  618. }
  619. const TEXTURE_MAPPING = {
  620. UVMapping: UVMapping,
  621. CubeReflectionMapping: CubeReflectionMapping,
  622. CubeRefractionMapping: CubeRefractionMapping,
  623. EquirectangularReflectionMapping: EquirectangularReflectionMapping,
  624. EquirectangularRefractionMapping: EquirectangularRefractionMapping,
  625. CubeUVReflectionMapping: CubeUVReflectionMapping,
  626. CubeUVRefractionMapping: CubeUVRefractionMapping
  627. };
  628. const TEXTURE_WRAPPING = {
  629. RepeatWrapping: RepeatWrapping,
  630. ClampToEdgeWrapping: ClampToEdgeWrapping,
  631. MirroredRepeatWrapping: MirroredRepeatWrapping
  632. };
  633. const TEXTURE_FILTER = {
  634. NearestFilter: NearestFilter,
  635. NearestMipmapNearestFilter: NearestMipmapNearestFilter,
  636. NearestMipmapLinearFilter: NearestMipmapLinearFilter,
  637. LinearFilter: LinearFilter,
  638. LinearMipmapNearestFilter: LinearMipmapNearestFilter,
  639. LinearMipmapLinearFilter: LinearMipmapLinearFilter
  640. };
  641. export { ObjectLoader };
粤ICP备19079148号