ObjectLoader.js 24 KB

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