ObjectLoader.js 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 { BatchedMesh } from '../objects/BatchedMesh.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 { Source } from '../textures/Source.js';
  50. import { DataTexture } from '../textures/DataTexture.js';
  51. import { ImageLoader } from './ImageLoader.js';
  52. import { LoadingManager } from './LoadingManager.js';
  53. import { AnimationClip } from '../animation/AnimationClip.js';
  54. import { MaterialLoader } from './MaterialLoader.js';
  55. import { LoaderUtils } from './LoaderUtils.js';
  56. import { BufferGeometryLoader } from './BufferGeometryLoader.js';
  57. import { Loader } from './Loader.js';
  58. import { FileLoader } from './FileLoader.js';
  59. import * as Geometries from '../geometries/Geometries.js';
  60. import { getTypedArray } from '../utils.js';
  61. import { Box3 } from '../math/Box3.js';
  62. import { Sphere } from '../math/Sphere.js';
  63. class ObjectLoader extends Loader {
  64. constructor( manager ) {
  65. super( manager );
  66. }
  67. load( url, onLoad, onProgress, onError ) {
  68. const scope = this;
  69. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  70. this.resourcePath = this.resourcePath || path;
  71. const loader = new FileLoader( this.manager );
  72. loader.setPath( this.path );
  73. loader.setRequestHeader( this.requestHeader );
  74. loader.setWithCredentials( this.withCredentials );
  75. loader.load( url, function ( text ) {
  76. let json = null;
  77. try {
  78. json = JSON.parse( text );
  79. } catch ( error ) {
  80. if ( onError !== undefined ) onError( error );
  81. console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message );
  82. return;
  83. }
  84. const metadata = json.metadata;
  85. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  86. if ( onError !== undefined ) onError( new Error( 'THREE.ObjectLoader: Can\'t load ' + url ) );
  87. console.error( 'THREE.ObjectLoader: Can\'t load ' + url );
  88. return;
  89. }
  90. scope.parse( json, onLoad );
  91. }, onProgress, onError );
  92. }
  93. async loadAsync( url, onProgress ) {
  94. const scope = this;
  95. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  96. this.resourcePath = this.resourcePath || path;
  97. const loader = new FileLoader( this.manager );
  98. loader.setPath( this.path );
  99. loader.setRequestHeader( this.requestHeader );
  100. loader.setWithCredentials( this.withCredentials );
  101. const text = await loader.loadAsync( url, onProgress );
  102. const json = JSON.parse( text );
  103. const metadata = json.metadata;
  104. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  105. throw new Error( 'THREE.ObjectLoader: Can\'t load ' + url );
  106. }
  107. return await scope.parseAsync( json );
  108. }
  109. parse( json, onLoad ) {
  110. const animations = this.parseAnimations( json.animations );
  111. const shapes = this.parseShapes( json.shapes );
  112. const geometries = this.parseGeometries( json.geometries, shapes );
  113. const images = this.parseImages( json.images, function () {
  114. if ( onLoad !== undefined ) onLoad( object );
  115. } );
  116. const textures = this.parseTextures( json.textures, images );
  117. const materials = this.parseMaterials( json.materials, textures );
  118. const object = this.parseObject( json.object, geometries, materials, textures, animations );
  119. const skeletons = this.parseSkeletons( json.skeletons, object );
  120. this.bindSkeletons( object, skeletons );
  121. //
  122. if ( onLoad !== undefined ) {
  123. let hasImages = false;
  124. for ( const uuid in images ) {
  125. if ( images[ uuid ].data instanceof HTMLImageElement ) {
  126. hasImages = true;
  127. break;
  128. }
  129. }
  130. if ( hasImages === false ) onLoad( object );
  131. }
  132. return object;
  133. }
  134. async parseAsync( json ) {
  135. const animations = this.parseAnimations( json.animations );
  136. const shapes = this.parseShapes( json.shapes );
  137. const geometries = this.parseGeometries( json.geometries, shapes );
  138. const images = await this.parseImagesAsync( json.images );
  139. const textures = this.parseTextures( json.textures, images );
  140. const materials = this.parseMaterials( json.materials, textures );
  141. const object = this.parseObject( json.object, geometries, materials, textures, animations );
  142. const skeletons = this.parseSkeletons( json.skeletons, object );
  143. this.bindSkeletons( object, skeletons );
  144. return object;
  145. }
  146. parseShapes( json ) {
  147. const shapes = {};
  148. if ( json !== undefined ) {
  149. for ( let i = 0, l = json.length; i < l; i ++ ) {
  150. const shape = new Shape().fromJSON( json[ i ] );
  151. shapes[ shape.uuid ] = shape;
  152. }
  153. }
  154. return shapes;
  155. }
  156. parseSkeletons( json, object ) {
  157. const skeletons = {};
  158. const bones = {};
  159. // generate bone lookup table
  160. object.traverse( function ( child ) {
  161. if ( child.isBone ) bones[ child.uuid ] = child;
  162. } );
  163. // create skeletons
  164. if ( json !== undefined ) {
  165. for ( let i = 0, l = json.length; i < l; i ++ ) {
  166. const skeleton = new Skeleton().fromJSON( json[ i ], bones );
  167. skeletons[ skeleton.uuid ] = skeleton;
  168. }
  169. }
  170. return skeletons;
  171. }
  172. parseGeometries( json, shapes ) {
  173. const geometries = {};
  174. if ( json !== undefined ) {
  175. const bufferGeometryLoader = new BufferGeometryLoader();
  176. for ( let i = 0, l = json.length; i < l; i ++ ) {
  177. let geometry;
  178. const data = json[ i ];
  179. switch ( data.type ) {
  180. case 'BufferGeometry':
  181. case 'InstancedBufferGeometry':
  182. geometry = bufferGeometryLoader.parse( data );
  183. break;
  184. default:
  185. if ( data.type in Geometries ) {
  186. geometry = Geometries[ data.type ].fromJSON( data, shapes );
  187. } else {
  188. console.warn( `THREE.ObjectLoader: Unsupported geometry type "${ data.type }"` );
  189. }
  190. }
  191. geometry.uuid = data.uuid;
  192. if ( data.name !== undefined ) geometry.name = data.name;
  193. if ( data.userData !== undefined ) geometry.userData = data.userData;
  194. geometries[ data.uuid ] = geometry;
  195. }
  196. }
  197. return geometries;
  198. }
  199. parseMaterials( json, textures ) {
  200. const cache = {}; // MultiMaterial
  201. const materials = {};
  202. if ( json !== undefined ) {
  203. const loader = new MaterialLoader();
  204. loader.setTextures( textures );
  205. for ( let i = 0, l = json.length; i < l; i ++ ) {
  206. const data = json[ i ];
  207. if ( cache[ data.uuid ] === undefined ) {
  208. cache[ data.uuid ] = loader.parse( data );
  209. }
  210. materials[ data.uuid ] = cache[ data.uuid ];
  211. }
  212. }
  213. return materials;
  214. }
  215. parseAnimations( json ) {
  216. const animations = {};
  217. if ( json !== undefined ) {
  218. for ( let i = 0; i < json.length; i ++ ) {
  219. const data = json[ i ];
  220. const clip = AnimationClip.parse( data );
  221. animations[ clip.uuid ] = clip;
  222. }
  223. }
  224. return animations;
  225. }
  226. parseImages( json, onLoad ) {
  227. const scope = this;
  228. const images = {};
  229. let loader;
  230. function loadImage( url ) {
  231. scope.manager.itemStart( url );
  232. return loader.load( url, function () {
  233. scope.manager.itemEnd( url );
  234. }, undefined, function () {
  235. scope.manager.itemError( url );
  236. scope.manager.itemEnd( url );
  237. } );
  238. }
  239. function deserializeImage( image ) {
  240. if ( typeof image === 'string' ) {
  241. const url = image;
  242. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  243. return loadImage( path );
  244. } else {
  245. if ( image.data ) {
  246. return {
  247. data: getTypedArray( image.type, image.data ),
  248. width: image.width,
  249. height: image.height
  250. };
  251. } else {
  252. return null;
  253. }
  254. }
  255. }
  256. if ( json !== undefined && json.length > 0 ) {
  257. const manager = new LoadingManager( onLoad );
  258. loader = new ImageLoader( manager );
  259. loader.setCrossOrigin( this.crossOrigin );
  260. for ( let i = 0, il = json.length; i < il; i ++ ) {
  261. const image = json[ i ];
  262. const url = image.url;
  263. if ( Array.isArray( url ) ) {
  264. // load array of images e.g CubeTexture
  265. const imageArray = [];
  266. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  267. const currentUrl = url[ j ];
  268. const deserializedImage = deserializeImage( currentUrl );
  269. if ( deserializedImage !== null ) {
  270. if ( deserializedImage instanceof HTMLImageElement ) {
  271. imageArray.push( deserializedImage );
  272. } else {
  273. // special case: handle array of data textures for cube textures
  274. imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  275. }
  276. }
  277. }
  278. images[ image.uuid ] = new Source( imageArray );
  279. } else {
  280. // load single image
  281. const deserializedImage = deserializeImage( image.url );
  282. images[ image.uuid ] = new Source( deserializedImage );
  283. }
  284. }
  285. }
  286. return images;
  287. }
  288. async parseImagesAsync( json ) {
  289. const scope = this;
  290. const images = {};
  291. let loader;
  292. async function deserializeImage( image ) {
  293. if ( typeof image === 'string' ) {
  294. const url = image;
  295. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  296. return await loader.loadAsync( path );
  297. } else {
  298. if ( image.data ) {
  299. return {
  300. data: getTypedArray( image.type, image.data ),
  301. width: image.width,
  302. height: image.height
  303. };
  304. } else {
  305. return null;
  306. }
  307. }
  308. }
  309. if ( json !== undefined && json.length > 0 ) {
  310. loader = new ImageLoader( this.manager );
  311. loader.setCrossOrigin( this.crossOrigin );
  312. for ( let i = 0, il = json.length; i < il; i ++ ) {
  313. const image = json[ i ];
  314. const url = image.url;
  315. if ( Array.isArray( url ) ) {
  316. // load array of images e.g CubeTexture
  317. const imageArray = [];
  318. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  319. const currentUrl = url[ j ];
  320. const deserializedImage = await deserializeImage( currentUrl );
  321. if ( deserializedImage !== null ) {
  322. if ( deserializedImage instanceof HTMLImageElement ) {
  323. imageArray.push( deserializedImage );
  324. } else {
  325. // special case: handle array of data textures for cube textures
  326. imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  327. }
  328. }
  329. }
  330. images[ image.uuid ] = new Source( imageArray );
  331. } else {
  332. // load single image
  333. const deserializedImage = await deserializeImage( image.url );
  334. images[ image.uuid ] = new Source( deserializedImage );
  335. }
  336. }
  337. }
  338. return images;
  339. }
  340. parseTextures( json, images ) {
  341. function parseConstant( value, type ) {
  342. if ( typeof value === 'number' ) return value;
  343. console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value );
  344. return type[ value ];
  345. }
  346. const textures = {};
  347. if ( json !== undefined ) {
  348. for ( let i = 0, l = json.length; i < l; i ++ ) {
  349. const data = json[ i ];
  350. if ( data.image === undefined ) {
  351. console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid );
  352. }
  353. if ( images[ data.image ] === undefined ) {
  354. console.warn( 'THREE.ObjectLoader: Undefined image', data.image );
  355. }
  356. const source = images[ data.image ];
  357. const image = source.data;
  358. let texture;
  359. if ( Array.isArray( image ) ) {
  360. texture = new CubeTexture();
  361. if ( image.length === 6 ) texture.needsUpdate = true;
  362. } else {
  363. if ( image && image.data ) {
  364. texture = new DataTexture();
  365. } else {
  366. texture = new Texture();
  367. }
  368. if ( image ) texture.needsUpdate = true; // textures can have undefined image data
  369. }
  370. texture.source = source;
  371. texture.uuid = data.uuid;
  372. if ( data.name !== undefined ) texture.name = data.name;
  373. if ( data.mapping !== undefined ) texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING );
  374. if ( data.channel !== undefined ) texture.channel = data.channel;
  375. if ( data.offset !== undefined ) texture.offset.fromArray( data.offset );
  376. if ( data.repeat !== undefined ) texture.repeat.fromArray( data.repeat );
  377. if ( data.center !== undefined ) texture.center.fromArray( data.center );
  378. if ( data.rotation !== undefined ) texture.rotation = data.rotation;
  379. if ( data.wrap !== undefined ) {
  380. texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING );
  381. texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING );
  382. }
  383. if ( data.format !== undefined ) texture.format = data.format;
  384. if ( data.internalFormat !== undefined ) texture.internalFormat = data.internalFormat;
  385. if ( data.type !== undefined ) texture.type = data.type;
  386. if ( data.colorSpace !== undefined ) texture.colorSpace = data.colorSpace;
  387. if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER );
  388. if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER );
  389. if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy;
  390. if ( data.flipY !== undefined ) texture.flipY = data.flipY;
  391. if ( data.generateMipmaps !== undefined ) texture.generateMipmaps = data.generateMipmaps;
  392. if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha;
  393. if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment;
  394. if ( data.compareFunction !== undefined ) texture.compareFunction = data.compareFunction;
  395. if ( data.userData !== undefined ) texture.userData = data.userData;
  396. textures[ data.uuid ] = texture;
  397. }
  398. }
  399. return textures;
  400. }
  401. parseObject( data, geometries, materials, textures, animations ) {
  402. let object;
  403. function getGeometry( name ) {
  404. if ( geometries[ name ] === undefined ) {
  405. console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
  406. }
  407. return geometries[ name ];
  408. }
  409. function getMaterial( name ) {
  410. if ( name === undefined ) return undefined;
  411. if ( Array.isArray( name ) ) {
  412. const array = [];
  413. for ( let i = 0, l = name.length; i < l; i ++ ) {
  414. const uuid = name[ i ];
  415. if ( materials[ uuid ] === undefined ) {
  416. console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
  417. }
  418. array.push( materials[ uuid ] );
  419. }
  420. return array;
  421. }
  422. if ( materials[ name ] === undefined ) {
  423. console.warn( 'THREE.ObjectLoader: Undefined material', name );
  424. }
  425. return materials[ name ];
  426. }
  427. function getTexture( uuid ) {
  428. if ( textures[ uuid ] === undefined ) {
  429. console.warn( 'THREE.ObjectLoader: Undefined texture', uuid );
  430. }
  431. return textures[ uuid ];
  432. }
  433. let geometry, material;
  434. switch ( data.type ) {
  435. case 'Scene':
  436. object = new Scene();
  437. if ( data.background !== undefined ) {
  438. if ( Number.isInteger( data.background ) ) {
  439. object.background = new Color( data.background );
  440. } else {
  441. object.background = getTexture( data.background );
  442. }
  443. }
  444. if ( data.environment !== undefined ) {
  445. object.environment = getTexture( data.environment );
  446. }
  447. if ( data.fog !== undefined ) {
  448. if ( data.fog.type === 'Fog' ) {
  449. object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
  450. } else if ( data.fog.type === 'FogExp2' ) {
  451. object.fog = new FogExp2( data.fog.color, data.fog.density );
  452. }
  453. if ( data.fog.name !== '' ) {
  454. object.fog.name = data.fog.name;
  455. }
  456. }
  457. if ( data.backgroundBlurriness !== undefined ) object.backgroundBlurriness = data.backgroundBlurriness;
  458. if ( data.backgroundIntensity !== undefined ) object.backgroundIntensity = data.backgroundIntensity;
  459. if ( data.backgroundRotation !== undefined ) object.backgroundRotation.fromArray( data.backgroundRotation );
  460. if ( data.environmentIntensity !== undefined ) object.environmentIntensity = data.environmentIntensity;
  461. if ( data.environmentRotation !== undefined ) object.environmentRotation.fromArray( data.environmentRotation );
  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 'BatchedMesh':
  521. geometry = getGeometry( data.geometry );
  522. material = getMaterial( data.material );
  523. object = new BatchedMesh( data.maxGeometryCount, data.maxVertexCount, data.maxIndexCount, material );
  524. object.geometry = geometry;
  525. object.perObjectFrustumCulled = data.perObjectFrustumCulled;
  526. object.sortObjects = data.sortObjects;
  527. object._drawRanges = data.drawRanges;
  528. object._reservedRanges = data.reservedRanges;
  529. object._visibility = data.visibility;
  530. object._active = data.active;
  531. object._bounds = data.bounds.map( bound => {
  532. const box = new Box3();
  533. box.min.fromArray( bound.boxMin );
  534. box.max.fromArray( bound.boxMax );
  535. const sphere = new Sphere();
  536. sphere.radius = bound.sphereRadius;
  537. sphere.center.fromArray( bound.sphereCenter );
  538. return {
  539. boxInitialized: bound.boxInitialized,
  540. box: box,
  541. sphereInitialized: bound.sphereInitialized,
  542. sphere: sphere
  543. };
  544. } );
  545. object._maxGeometryCount = data.maxGeometryCount;
  546. object._maxVertexCount = data.maxVertexCount;
  547. object._maxIndexCount = data.maxIndexCount;
  548. object._geometryInitialized = data.geometryInitialized;
  549. object._geometryCount = data.geometryCount;
  550. object._matricesTexture = getTexture( data.matricesTexture.uuid );
  551. if ( data.colorsTexture !== undefined ) object._colorsTexture = getTexture( data.colorsTexture.uuid );
  552. break;
  553. case 'LOD':
  554. object = new LOD();
  555. break;
  556. case 'Line':
  557. object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) );
  558. break;
  559. case 'LineLoop':
  560. object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
  561. break;
  562. case 'LineSegments':
  563. object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
  564. break;
  565. case 'PointCloud':
  566. case 'Points':
  567. object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
  568. break;
  569. case 'Sprite':
  570. object = new Sprite( getMaterial( data.material ) );
  571. break;
  572. case 'Group':
  573. object = new Group();
  574. break;
  575. case 'Bone':
  576. object = new Bone();
  577. break;
  578. default:
  579. object = new Object3D();
  580. }
  581. object.uuid = data.uuid;
  582. if ( data.name !== undefined ) object.name = data.name;
  583. if ( data.matrix !== undefined ) {
  584. object.matrix.fromArray( data.matrix );
  585. if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate;
  586. if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale );
  587. } else {
  588. if ( data.position !== undefined ) object.position.fromArray( data.position );
  589. if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation );
  590. if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion );
  591. if ( data.scale !== undefined ) object.scale.fromArray( data.scale );
  592. }
  593. if ( data.up !== undefined ) object.up.fromArray( data.up );
  594. if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
  595. if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
  596. if ( data.shadow ) {
  597. if ( data.shadow.intensity !== undefined ) object.shadow.intensity = data.shadow.intensity;
  598. if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
  599. if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias;
  600. if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
  601. if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
  602. if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
  603. }
  604. if ( data.visible !== undefined ) object.visible = data.visible;
  605. if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled;
  606. if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder;
  607. if ( data.userData !== undefined ) object.userData = data.userData;
  608. if ( data.layers !== undefined ) object.layers.mask = data.layers;
  609. if ( data.children !== undefined ) {
  610. const children = data.children;
  611. for ( let i = 0; i < children.length; i ++ ) {
  612. object.add( this.parseObject( children[ i ], geometries, materials, textures, animations ) );
  613. }
  614. }
  615. if ( data.animations !== undefined ) {
  616. const objectAnimations = data.animations;
  617. for ( let i = 0; i < objectAnimations.length; i ++ ) {
  618. const uuid = objectAnimations[ i ];
  619. object.animations.push( animations[ uuid ] );
  620. }
  621. }
  622. if ( data.type === 'LOD' ) {
  623. if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate;
  624. const levels = data.levels;
  625. for ( let l = 0; l < levels.length; l ++ ) {
  626. const level = levels[ l ];
  627. const child = object.getObjectByProperty( 'uuid', level.object );
  628. if ( child !== undefined ) {
  629. object.addLevel( child, level.distance, level.hysteresis );
  630. }
  631. }
  632. }
  633. return object;
  634. }
  635. bindSkeletons( object, skeletons ) {
  636. if ( Object.keys( skeletons ).length === 0 ) return;
  637. object.traverse( function ( child ) {
  638. if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) {
  639. const skeleton = skeletons[ child.skeleton ];
  640. if ( skeleton === undefined ) {
  641. console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton );
  642. } else {
  643. child.bind( skeleton, child.bindMatrix );
  644. }
  645. }
  646. } );
  647. }
  648. }
  649. const TEXTURE_MAPPING = {
  650. UVMapping: UVMapping,
  651. CubeReflectionMapping: CubeReflectionMapping,
  652. CubeRefractionMapping: CubeRefractionMapping,
  653. EquirectangularReflectionMapping: EquirectangularReflectionMapping,
  654. EquirectangularRefractionMapping: EquirectangularRefractionMapping,
  655. CubeUVReflectionMapping: CubeUVReflectionMapping
  656. };
  657. const TEXTURE_WRAPPING = {
  658. RepeatWrapping: RepeatWrapping,
  659. ClampToEdgeWrapping: ClampToEdgeWrapping,
  660. MirroredRepeatWrapping: MirroredRepeatWrapping
  661. };
  662. const TEXTURE_FILTER = {
  663. NearestFilter: NearestFilter,
  664. NearestMipmapNearestFilter: NearestMipmapNearestFilter,
  665. NearestMipmapLinearFilter: NearestMipmapLinearFilter,
  666. LinearFilter: LinearFilter,
  667. LinearMipmapNearestFilter: LinearMipmapNearestFilter,
  668. LinearMipmapLinearFilter: LinearMipmapLinearFilter
  669. };
  670. export { ObjectLoader };
粤ICP备19079148号