ObjectLoader.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 { BufferAttribute } from '../core/BufferAttribute.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 * as Curves from '../extras/curves/Curves.js';
  60. import { getTypedArray } from '../utils.js';
  61. class ObjectLoader extends Loader {
  62. constructor( manager ) {
  63. super( manager );
  64. }
  65. load( url, onLoad, onProgress, onError ) {
  66. const scope = this;
  67. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  68. this.resourcePath = this.resourcePath || path;
  69. const loader = new FileLoader( this.manager );
  70. loader.setPath( this.path );
  71. loader.setRequestHeader( this.requestHeader );
  72. loader.setWithCredentials( this.withCredentials );
  73. loader.load( url, function ( text ) {
  74. let json = null;
  75. try {
  76. json = JSON.parse( text );
  77. } catch ( error ) {
  78. if ( onError !== undefined ) onError( error );
  79. console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message );
  80. return;
  81. }
  82. const metadata = json.metadata;
  83. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  84. console.error( 'THREE.ObjectLoader: Can\'t load ' + url );
  85. return;
  86. }
  87. scope.parse( json, onLoad );
  88. }, onProgress, onError );
  89. }
  90. parse( json, onLoad ) {
  91. const animations = this.parseAnimations( json.animations );
  92. const shapes = this.parseShapes( json.shapes );
  93. const geometries = this.parseGeometries( json.geometries, shapes );
  94. const images = this.parseImages( json.images, function () {
  95. if ( onLoad !== undefined ) onLoad( object );
  96. } );
  97. const textures = this.parseTextures( json.textures, images );
  98. const materials = this.parseMaterials( json.materials, textures );
  99. const object = this.parseObject( json.object, geometries, materials, animations );
  100. const skeletons = this.parseSkeletons( json.skeletons, object );
  101. this.bindSkeletons( object, skeletons );
  102. //
  103. if ( onLoad !== undefined ) {
  104. let hasImages = false;
  105. for ( const uuid in images ) {
  106. if ( images[ uuid ] instanceof HTMLImageElement ) {
  107. hasImages = true;
  108. break;
  109. }
  110. }
  111. if ( hasImages === false ) onLoad( object );
  112. }
  113. return object;
  114. }
  115. parseShapes( json ) {
  116. const shapes = {};
  117. if ( json !== undefined ) {
  118. for ( let i = 0, l = json.length; i < l; i ++ ) {
  119. const shape = new Shape().fromJSON( json[ i ] );
  120. shapes[ shape.uuid ] = shape;
  121. }
  122. }
  123. return shapes;
  124. }
  125. parseSkeletons( json, object ) {
  126. const skeletons = {};
  127. const bones = {};
  128. // generate bone lookup table
  129. object.traverse( function ( child ) {
  130. if ( child.isBone ) bones[ child.uuid ] = child;
  131. } );
  132. // create skeletons
  133. if ( json !== undefined ) {
  134. for ( let i = 0, l = json.length; i < l; i ++ ) {
  135. const skeleton = new Skeleton().fromJSON( json[ i ], bones );
  136. skeletons[ skeleton.uuid ] = skeleton;
  137. }
  138. }
  139. return skeletons;
  140. }
  141. parseGeometries( json, shapes ) {
  142. const geometries = {};
  143. let geometryShapes;
  144. if ( json !== undefined ) {
  145. const bufferGeometryLoader = new BufferGeometryLoader();
  146. for ( let i = 0, l = json.length; i < l; i ++ ) {
  147. let geometry;
  148. const data = json[ i ];
  149. switch ( data.type ) {
  150. case 'PlaneGeometry':
  151. case 'PlaneBufferGeometry':
  152. geometry = new Geometries[ data.type ](
  153. data.width,
  154. data.height,
  155. data.widthSegments,
  156. data.heightSegments
  157. );
  158. break;
  159. case 'BoxGeometry':
  160. case 'BoxBufferGeometry':
  161. geometry = new Geometries[ data.type ](
  162. data.width,
  163. data.height,
  164. data.depth,
  165. data.widthSegments,
  166. data.heightSegments,
  167. data.depthSegments
  168. );
  169. break;
  170. case 'CircleGeometry':
  171. case 'CircleBufferGeometry':
  172. geometry = new Geometries[ data.type ](
  173. data.radius,
  174. data.segments,
  175. data.thetaStart,
  176. data.thetaLength
  177. );
  178. break;
  179. case 'CylinderGeometry':
  180. case 'CylinderBufferGeometry':
  181. geometry = new Geometries[ data.type ](
  182. data.radiusTop,
  183. data.radiusBottom,
  184. data.height,
  185. data.radialSegments,
  186. data.heightSegments,
  187. data.openEnded,
  188. data.thetaStart,
  189. data.thetaLength
  190. );
  191. break;
  192. case 'ConeGeometry':
  193. case 'ConeBufferGeometry':
  194. geometry = new Geometries[ data.type ](
  195. data.radius,
  196. data.height,
  197. data.radialSegments,
  198. data.heightSegments,
  199. data.openEnded,
  200. data.thetaStart,
  201. data.thetaLength
  202. );
  203. break;
  204. case 'SphereGeometry':
  205. case 'SphereBufferGeometry':
  206. geometry = new Geometries[ data.type ](
  207. data.radius,
  208. data.widthSegments,
  209. data.heightSegments,
  210. data.phiStart,
  211. data.phiLength,
  212. data.thetaStart,
  213. data.thetaLength
  214. );
  215. break;
  216. case 'DodecahedronGeometry':
  217. case 'DodecahedronBufferGeometry':
  218. case 'IcosahedronGeometry':
  219. case 'IcosahedronBufferGeometry':
  220. case 'OctahedronGeometry':
  221. case 'OctahedronBufferGeometry':
  222. case 'TetrahedronGeometry':
  223. case 'TetrahedronBufferGeometry':
  224. geometry = new Geometries[ data.type ](
  225. data.radius,
  226. data.detail
  227. );
  228. break;
  229. case 'RingGeometry':
  230. case 'RingBufferGeometry':
  231. geometry = new Geometries[ data.type ](
  232. data.innerRadius,
  233. data.outerRadius,
  234. data.thetaSegments,
  235. data.phiSegments,
  236. data.thetaStart,
  237. data.thetaLength
  238. );
  239. break;
  240. case 'TorusGeometry':
  241. case 'TorusBufferGeometry':
  242. geometry = new Geometries[ data.type ](
  243. data.radius,
  244. data.tube,
  245. data.radialSegments,
  246. data.tubularSegments,
  247. data.arc
  248. );
  249. break;
  250. case 'TorusKnotGeometry':
  251. case 'TorusKnotBufferGeometry':
  252. geometry = new Geometries[ data.type ](
  253. data.radius,
  254. data.tube,
  255. data.tubularSegments,
  256. data.radialSegments,
  257. data.p,
  258. data.q
  259. );
  260. break;
  261. case 'TubeGeometry':
  262. case 'TubeBufferGeometry':
  263. // This only works for built-in curves (e.g. CatmullRomCurve3).
  264. // User defined curves or instances of CurvePath will not be deserialized.
  265. geometry = new Geometries[ data.type ](
  266. new Curves[ data.path.type ]().fromJSON( data.path ),
  267. data.tubularSegments,
  268. data.radius,
  269. data.radialSegments,
  270. data.closed
  271. );
  272. break;
  273. case 'LatheGeometry':
  274. case 'LatheBufferGeometry':
  275. geometry = new Geometries[ data.type ](
  276. data.points,
  277. data.segments,
  278. data.phiStart,
  279. data.phiLength
  280. );
  281. break;
  282. case 'PolyhedronGeometry':
  283. case 'PolyhedronBufferGeometry':
  284. geometry = new Geometries[ data.type ](
  285. data.vertices,
  286. data.indices,
  287. data.radius,
  288. data.details
  289. );
  290. break;
  291. case 'ShapeGeometry':
  292. case 'ShapeBufferGeometry':
  293. geometryShapes = [];
  294. for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) {
  295. const shape = shapes[ data.shapes[ j ] ];
  296. geometryShapes.push( shape );
  297. }
  298. geometry = new Geometries[ data.type ](
  299. geometryShapes,
  300. data.curveSegments
  301. );
  302. break;
  303. case 'ExtrudeGeometry':
  304. case 'ExtrudeBufferGeometry':
  305. geometryShapes = [];
  306. for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) {
  307. const shape = shapes[ data.shapes[ j ] ];
  308. geometryShapes.push( shape );
  309. }
  310. const extrudePath = data.options.extrudePath;
  311. if ( extrudePath !== undefined ) {
  312. data.options.extrudePath = new Curves[ extrudePath.type ]().fromJSON( extrudePath );
  313. }
  314. geometry = new Geometries[ data.type ](
  315. geometryShapes,
  316. data.options
  317. );
  318. break;
  319. case 'BufferGeometry':
  320. case 'InstancedBufferGeometry':
  321. geometry = bufferGeometryLoader.parse( data );
  322. break;
  323. case 'Geometry':
  324. console.error( 'THREE.ObjectLoader: Loading "Geometry" is not supported anymore.' );
  325. break;
  326. default:
  327. console.warn( 'THREE.ObjectLoader: Unsupported geometry type "' + data.type + '"' );
  328. continue;
  329. }
  330. geometry.uuid = data.uuid;
  331. if ( data.name !== undefined ) geometry.name = data.name;
  332. if ( geometry.isBufferGeometry === true && data.userData !== undefined ) geometry.userData = data.userData;
  333. geometries[ data.uuid ] = geometry;
  334. }
  335. }
  336. return geometries;
  337. }
  338. parseMaterials( json, textures ) {
  339. const cache = {}; // MultiMaterial
  340. const materials = {};
  341. if ( json !== undefined ) {
  342. const loader = new MaterialLoader();
  343. loader.setTextures( textures );
  344. for ( let i = 0, l = json.length; i < l; i ++ ) {
  345. const data = json[ i ];
  346. if ( data.type === 'MultiMaterial' ) {
  347. // Deprecated
  348. const array = [];
  349. for ( let j = 0; j < data.materials.length; j ++ ) {
  350. const material = data.materials[ j ];
  351. if ( cache[ material.uuid ] === undefined ) {
  352. cache[ material.uuid ] = loader.parse( material );
  353. }
  354. array.push( cache[ material.uuid ] );
  355. }
  356. materials[ data.uuid ] = array;
  357. } else {
  358. if ( cache[ data.uuid ] === undefined ) {
  359. cache[ data.uuid ] = loader.parse( data );
  360. }
  361. materials[ data.uuid ] = cache[ data.uuid ];
  362. }
  363. }
  364. }
  365. return materials;
  366. }
  367. parseAnimations( json ) {
  368. const animations = {};
  369. if ( json !== undefined ) {
  370. for ( let i = 0; i < json.length; i ++ ) {
  371. const data = json[ i ];
  372. const clip = AnimationClip.parse( data );
  373. animations[ clip.uuid ] = clip;
  374. }
  375. }
  376. return animations;
  377. }
  378. parseImages( json, onLoad ) {
  379. const scope = this;
  380. const images = {};
  381. let loader;
  382. function loadImage( url ) {
  383. scope.manager.itemStart( url );
  384. return loader.load( url, function () {
  385. scope.manager.itemEnd( url );
  386. }, undefined, function () {
  387. scope.manager.itemError( url );
  388. scope.manager.itemEnd( url );
  389. } );
  390. }
  391. function deserializeImage( image ) {
  392. if ( typeof image === 'string' ) {
  393. const url = image;
  394. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  395. return loadImage( path );
  396. } else {
  397. if ( image.data ) {
  398. return {
  399. data: getTypedArray( image.type, image.data ),
  400. width: image.width,
  401. height: image.height
  402. };
  403. } else {
  404. return null;
  405. }
  406. }
  407. }
  408. if ( json !== undefined && json.length > 0 ) {
  409. const manager = new LoadingManager( onLoad );
  410. loader = new ImageLoader( manager );
  411. loader.setCrossOrigin( this.crossOrigin );
  412. for ( let i = 0, il = json.length; i < il; i ++ ) {
  413. const image = json[ i ];
  414. const url = image.url;
  415. if ( Array.isArray( url ) ) {
  416. // load array of images e.g CubeTexture
  417. images[ image.uuid ] = [];
  418. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  419. const currentUrl = url[ j ];
  420. const deserializedImage = deserializeImage( currentUrl );
  421. if ( deserializedImage !== null ) {
  422. if ( deserializedImage instanceof HTMLImageElement ) {
  423. images[ image.uuid ].push( deserializedImage );
  424. } else {
  425. // special case: handle array of data textures for cube textures
  426. images[ image.uuid ].push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  427. }
  428. }
  429. }
  430. } else {
  431. // load single image
  432. const deserializedImage = deserializeImage( image.url );
  433. if ( deserializedImage !== null ) {
  434. images[ image.uuid ] = deserializedImage;
  435. }
  436. }
  437. }
  438. }
  439. return images;
  440. }
  441. parseTextures( json, images ) {
  442. function parseConstant( value, type ) {
  443. if ( typeof value === 'number' ) return value;
  444. console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value );
  445. return type[ value ];
  446. }
  447. const textures = {};
  448. if ( json !== undefined ) {
  449. for ( let i = 0, l = json.length; i < l; i ++ ) {
  450. const data = json[ i ];
  451. if ( data.image === undefined ) {
  452. console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid );
  453. }
  454. if ( images[ data.image ] === undefined ) {
  455. console.warn( 'THREE.ObjectLoader: Undefined image', data.image );
  456. }
  457. let texture;
  458. const image = images[ data.image ];
  459. if ( Array.isArray( image ) ) {
  460. texture = new CubeTexture( image );
  461. if ( image.length === 6 ) texture.needsUpdate = true;
  462. } else {
  463. if ( image && image.data ) {
  464. texture = new DataTexture( image.data, image.width, image.height );
  465. } else {
  466. texture = new Texture( image );
  467. }
  468. if ( image ) texture.needsUpdate = true; // textures can have undefined image data
  469. }
  470. texture.uuid = data.uuid;
  471. if ( data.name !== undefined ) texture.name = data.name;
  472. if ( data.mapping !== undefined ) texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING );
  473. if ( data.offset !== undefined ) texture.offset.fromArray( data.offset );
  474. if ( data.repeat !== undefined ) texture.repeat.fromArray( data.repeat );
  475. if ( data.center !== undefined ) texture.center.fromArray( data.center );
  476. if ( data.rotation !== undefined ) texture.rotation = data.rotation;
  477. if ( data.wrap !== undefined ) {
  478. texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING );
  479. texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING );
  480. }
  481. if ( data.format !== undefined ) texture.format = data.format;
  482. if ( data.type !== undefined ) texture.type = data.type;
  483. if ( data.encoding !== undefined ) texture.encoding = data.encoding;
  484. if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER );
  485. if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER );
  486. if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy;
  487. if ( data.flipY !== undefined ) texture.flipY = data.flipY;
  488. if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha;
  489. if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment;
  490. textures[ data.uuid ] = texture;
  491. }
  492. }
  493. return textures;
  494. }
  495. parseObject( data, geometries, materials, animations ) {
  496. let object;
  497. function getGeometry( name ) {
  498. if ( geometries[ name ] === undefined ) {
  499. console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
  500. }
  501. return geometries[ name ];
  502. }
  503. function getMaterial( name ) {
  504. if ( name === undefined ) return undefined;
  505. if ( Array.isArray( name ) ) {
  506. const array = [];
  507. for ( let i = 0, l = name.length; i < l; i ++ ) {
  508. const uuid = name[ i ];
  509. if ( materials[ uuid ] === undefined ) {
  510. console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
  511. }
  512. array.push( materials[ uuid ] );
  513. }
  514. return array;
  515. }
  516. if ( materials[ name ] === undefined ) {
  517. console.warn( 'THREE.ObjectLoader: Undefined material', name );
  518. }
  519. return materials[ name ];
  520. }
  521. let geometry, material;
  522. switch ( data.type ) {
  523. case 'Scene':
  524. object = new Scene();
  525. if ( data.background !== undefined ) {
  526. if ( Number.isInteger( data.background ) ) {
  527. object.background = new Color( data.background );
  528. }
  529. }
  530. if ( data.fog !== undefined ) {
  531. if ( data.fog.type === 'Fog' ) {
  532. object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
  533. } else if ( data.fog.type === 'FogExp2' ) {
  534. object.fog = new FogExp2( data.fog.color, data.fog.density );
  535. }
  536. }
  537. break;
  538. case 'PerspectiveCamera':
  539. object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
  540. if ( data.focus !== undefined ) object.focus = data.focus;
  541. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  542. if ( data.filmGauge !== undefined ) object.filmGauge = data.filmGauge;
  543. if ( data.filmOffset !== undefined ) object.filmOffset = data.filmOffset;
  544. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  545. break;
  546. case 'OrthographicCamera':
  547. object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
  548. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  549. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  550. break;
  551. case 'AmbientLight':
  552. object = new AmbientLight( data.color, data.intensity );
  553. break;
  554. case 'DirectionalLight':
  555. object = new DirectionalLight( data.color, data.intensity );
  556. break;
  557. case 'PointLight':
  558. object = new PointLight( data.color, data.intensity, data.distance, data.decay );
  559. break;
  560. case 'RectAreaLight':
  561. object = new RectAreaLight( data.color, data.intensity, data.width, data.height );
  562. break;
  563. case 'SpotLight':
  564. object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
  565. break;
  566. case 'HemisphereLight':
  567. object = new HemisphereLight( data.color, data.groundColor, data.intensity );
  568. break;
  569. case 'LightProbe':
  570. object = new LightProbe().fromJSON( data );
  571. break;
  572. case 'SkinnedMesh':
  573. geometry = getGeometry( data.geometry );
  574. material = getMaterial( data.material );
  575. object = new SkinnedMesh( geometry, material );
  576. if ( data.bindMode !== undefined ) object.bindMode = data.bindMode;
  577. if ( data.bindMatrix !== undefined ) object.bindMatrix.fromArray( data.bindMatrix );
  578. if ( data.skeleton !== undefined ) object.skeleton = data.skeleton;
  579. break;
  580. case 'Mesh':
  581. geometry = getGeometry( data.geometry );
  582. material = getMaterial( data.material );
  583. object = new Mesh( geometry, material );
  584. break;
  585. case 'InstancedMesh':
  586. geometry = getGeometry( data.geometry );
  587. material = getMaterial( data.material );
  588. const count = data.count;
  589. const instanceMatrix = data.instanceMatrix;
  590. object = new InstancedMesh( geometry, material, count );
  591. object.instanceMatrix = new BufferAttribute( new Float32Array( instanceMatrix.array ), 16 );
  592. break;
  593. case 'LOD':
  594. object = new LOD();
  595. break;
  596. case 'Line':
  597. object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) );
  598. break;
  599. case 'LineLoop':
  600. object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
  601. break;
  602. case 'LineSegments':
  603. object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
  604. break;
  605. case 'PointCloud':
  606. case 'Points':
  607. object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
  608. break;
  609. case 'Sprite':
  610. object = new Sprite( getMaterial( data.material ) );
  611. break;
  612. case 'Group':
  613. object = new Group();
  614. break;
  615. case 'Bone':
  616. object = new Bone();
  617. break;
  618. default:
  619. object = new Object3D();
  620. }
  621. object.uuid = data.uuid;
  622. if ( data.name !== undefined ) object.name = data.name;
  623. if ( data.matrix !== undefined ) {
  624. object.matrix.fromArray( data.matrix );
  625. if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate;
  626. if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale );
  627. } else {
  628. if ( data.position !== undefined ) object.position.fromArray( data.position );
  629. if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation );
  630. if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion );
  631. if ( data.scale !== undefined ) object.scale.fromArray( data.scale );
  632. }
  633. if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
  634. if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
  635. if ( data.shadow ) {
  636. if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
  637. if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias;
  638. if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
  639. if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
  640. if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
  641. }
  642. if ( data.visible !== undefined ) object.visible = data.visible;
  643. if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled;
  644. if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder;
  645. if ( data.userData !== undefined ) object.userData = data.userData;
  646. if ( data.layers !== undefined ) object.layers.mask = data.layers;
  647. if ( data.children !== undefined ) {
  648. const children = data.children;
  649. for ( let i = 0; i < children.length; i ++ ) {
  650. object.add( this.parseObject( children[ i ], geometries, materials, animations ) );
  651. }
  652. }
  653. if ( data.animations !== undefined ) {
  654. const objectAnimations = data.animations;
  655. for ( let i = 0; i < objectAnimations.length; i ++ ) {
  656. const uuid = objectAnimations[ i ];
  657. object.animations.push( animations[ uuid ] );
  658. }
  659. }
  660. if ( data.type === 'LOD' ) {
  661. if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate;
  662. const levels = data.levels;
  663. for ( let l = 0; l < levels.length; l ++ ) {
  664. const level = levels[ l ];
  665. const child = object.getObjectByProperty( 'uuid', level.object );
  666. if ( child !== undefined ) {
  667. object.addLevel( child, level.distance );
  668. }
  669. }
  670. }
  671. return object;
  672. }
  673. bindSkeletons( object, skeletons ) {
  674. if ( Object.keys( skeletons ).length === 0 ) return;
  675. object.traverse( function ( child ) {
  676. if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) {
  677. const skeleton = skeletons[ child.skeleton ];
  678. if ( skeleton === undefined ) {
  679. console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton );
  680. } else {
  681. child.bind( skeleton, child.bindMatrix );
  682. }
  683. }
  684. } );
  685. }
  686. /* DEPRECATED */
  687. setTexturePath( value ) {
  688. console.warn( 'THREE.ObjectLoader: .setTexturePath() has been renamed to .setResourcePath().' );
  689. return this.setResourcePath( value );
  690. }
  691. }
  692. const TEXTURE_MAPPING = {
  693. UVMapping: UVMapping,
  694. CubeReflectionMapping: CubeReflectionMapping,
  695. CubeRefractionMapping: CubeRefractionMapping,
  696. EquirectangularReflectionMapping: EquirectangularReflectionMapping,
  697. EquirectangularRefractionMapping: EquirectangularRefractionMapping,
  698. CubeUVReflectionMapping: CubeUVReflectionMapping,
  699. CubeUVRefractionMapping: CubeUVRefractionMapping
  700. };
  701. const TEXTURE_WRAPPING = {
  702. RepeatWrapping: RepeatWrapping,
  703. ClampToEdgeWrapping: ClampToEdgeWrapping,
  704. MirroredRepeatWrapping: MirroredRepeatWrapping
  705. };
  706. const TEXTURE_FILTER = {
  707. NearestFilter: NearestFilter,
  708. NearestMipmapNearestFilter: NearestMipmapNearestFilter,
  709. NearestMipmapLinearFilter: NearestMipmapLinearFilter,
  710. LinearFilter: LinearFilter,
  711. LinearMipmapNearestFilter: LinearMipmapNearestFilter,
  712. LinearMipmapLinearFilter: LinearMipmapLinearFilter
  713. };
  714. export { ObjectLoader };
粤ICP备19079148号