ObjectLoader.js 22 KB

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