ObjectLoader.js 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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.encoding !== undefined ) texture.encoding = data.encoding; // @deprecated, r152
  388. if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER );
  389. if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER );
  390. if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy;
  391. if ( data.flipY !== undefined ) texture.flipY = data.flipY;
  392. if ( data.generateMipmaps !== undefined ) texture.generateMipmaps = data.generateMipmaps;
  393. if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha;
  394. if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment;
  395. if ( data.compareFunction !== undefined ) texture.compareFunction = data.compareFunction;
  396. if ( data.userData !== undefined ) texture.userData = data.userData;
  397. textures[ data.uuid ] = texture;
  398. }
  399. }
  400. return textures;
  401. }
  402. parseObject( data, geometries, materials, textures, animations ) {
  403. let object;
  404. function getGeometry( name ) {
  405. if ( geometries[ name ] === undefined ) {
  406. console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
  407. }
  408. return geometries[ name ];
  409. }
  410. function getMaterial( name ) {
  411. if ( name === undefined ) return undefined;
  412. if ( Array.isArray( name ) ) {
  413. const array = [];
  414. for ( let i = 0, l = name.length; i < l; i ++ ) {
  415. const uuid = name[ i ];
  416. if ( materials[ uuid ] === undefined ) {
  417. console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
  418. }
  419. array.push( materials[ uuid ] );
  420. }
  421. return array;
  422. }
  423. if ( materials[ name ] === undefined ) {
  424. console.warn( 'THREE.ObjectLoader: Undefined material', name );
  425. }
  426. return materials[ name ];
  427. }
  428. function getTexture( uuid ) {
  429. if ( textures[ uuid ] === undefined ) {
  430. console.warn( 'THREE.ObjectLoader: Undefined texture', uuid );
  431. }
  432. return textures[ uuid ];
  433. }
  434. let geometry, material;
  435. switch ( data.type ) {
  436. case 'Scene':
  437. object = new Scene();
  438. if ( data.background !== undefined ) {
  439. if ( Number.isInteger( data.background ) ) {
  440. object.background = new Color( data.background );
  441. } else {
  442. object.background = getTexture( data.background );
  443. }
  444. }
  445. if ( data.environment !== undefined ) {
  446. object.environment = getTexture( data.environment );
  447. }
  448. if ( data.fog !== undefined ) {
  449. if ( data.fog.type === 'Fog' ) {
  450. object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
  451. } else if ( data.fog.type === 'FogExp2' ) {
  452. object.fog = new FogExp2( data.fog.color, data.fog.density );
  453. }
  454. if ( data.fog.name !== '' ) {
  455. object.fog.name = data.fog.name;
  456. }
  457. }
  458. if ( data.backgroundBlurriness !== undefined ) object.backgroundBlurriness = data.backgroundBlurriness;
  459. if ( data.backgroundIntensity !== undefined ) object.backgroundIntensity = data.backgroundIntensity;
  460. break;
  461. case 'PerspectiveCamera':
  462. object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
  463. if ( data.focus !== undefined ) object.focus = data.focus;
  464. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  465. if ( data.filmGauge !== undefined ) object.filmGauge = data.filmGauge;
  466. if ( data.filmOffset !== undefined ) object.filmOffset = data.filmOffset;
  467. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  468. break;
  469. case 'OrthographicCamera':
  470. object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
  471. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  472. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  473. break;
  474. case 'AmbientLight':
  475. object = new AmbientLight( data.color, data.intensity );
  476. break;
  477. case 'DirectionalLight':
  478. object = new DirectionalLight( data.color, data.intensity );
  479. break;
  480. case 'PointLight':
  481. object = new PointLight( data.color, data.intensity, data.distance, data.decay );
  482. break;
  483. case 'RectAreaLight':
  484. object = new RectAreaLight( data.color, data.intensity, data.width, data.height );
  485. break;
  486. case 'SpotLight':
  487. object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
  488. break;
  489. case 'HemisphereLight':
  490. object = new HemisphereLight( data.color, data.groundColor, data.intensity );
  491. break;
  492. case 'LightProbe':
  493. object = new LightProbe().fromJSON( data );
  494. break;
  495. case 'SkinnedMesh':
  496. geometry = getGeometry( data.geometry );
  497. material = getMaterial( data.material );
  498. object = new SkinnedMesh( geometry, material );
  499. if ( data.bindMode !== undefined ) object.bindMode = data.bindMode;
  500. if ( data.bindMatrix !== undefined ) object.bindMatrix.fromArray( data.bindMatrix );
  501. if ( data.skeleton !== undefined ) object.skeleton = data.skeleton;
  502. break;
  503. case 'Mesh':
  504. geometry = getGeometry( data.geometry );
  505. material = getMaterial( data.material );
  506. object = new Mesh( geometry, material );
  507. break;
  508. case 'InstancedMesh':
  509. geometry = getGeometry( data.geometry );
  510. material = getMaterial( data.material );
  511. const count = data.count;
  512. const instanceMatrix = data.instanceMatrix;
  513. const instanceColor = data.instanceColor;
  514. object = new InstancedMesh( geometry, material, count );
  515. object.instanceMatrix = new InstancedBufferAttribute( new Float32Array( instanceMatrix.array ), 16 );
  516. if ( instanceColor !== undefined ) object.instanceColor = new InstancedBufferAttribute( new Float32Array( instanceColor.array ), instanceColor.itemSize );
  517. break;
  518. case 'BatchedMesh':
  519. geometry = getGeometry( data.geometry );
  520. material = getMaterial( data.material );
  521. object = new BatchedMesh( data.maxGeometryCount, data.maxVertexCount, data.maxIndexCount, material );
  522. object.geometry = geometry;
  523. object.perObjectFrustumCulled = data.perObjectFrustumCulled;
  524. object.sortObjects = data.sortObjects;
  525. object._drawRanges = data.drawRanges;
  526. object._reservedRanges = data.reservedRanges;
  527. object._visibility = data.visibility;
  528. object._active = data.active;
  529. object._bounds = data.bounds.map( bound => {
  530. const box = new Box3();
  531. box.min.fromArray( bound.boxMin );
  532. box.max.fromArray( bound.boxMax );
  533. const sphere = new Sphere();
  534. sphere.radius = bound.sphereRadius;
  535. sphere.center.fromArray( bound.sphereCenter );
  536. return {
  537. boxInitialized: bound.boxInitialized,
  538. box: box,
  539. sphereInitialized: bound.sphereInitialized,
  540. sphere: sphere
  541. };
  542. } );
  543. object._maxGeometryCount = data.maxGeometryCount;
  544. object._maxVertexCount = data.maxVertexCount;
  545. object._maxIndexCount = data.maxIndexCount;
  546. object._geometryInitialized = data.geometryInitialized;
  547. object._geometryCount = data.geometryCount;
  548. object._matricesTexture = getTexture( data.matricesTexture.uuid );
  549. break;
  550. case 'LOD':
  551. object = new LOD();
  552. break;
  553. case 'Line':
  554. object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) );
  555. break;
  556. case 'LineLoop':
  557. object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
  558. break;
  559. case 'LineSegments':
  560. object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
  561. break;
  562. case 'PointCloud':
  563. case 'Points':
  564. object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
  565. break;
  566. case 'Sprite':
  567. object = new Sprite( getMaterial( data.material ) );
  568. break;
  569. case 'Group':
  570. object = new Group();
  571. break;
  572. case 'Bone':
  573. object = new Bone();
  574. break;
  575. default:
  576. object = new Object3D();
  577. }
  578. object.uuid = data.uuid;
  579. if ( data.name !== undefined ) object.name = data.name;
  580. if ( data.matrix !== undefined ) {
  581. object.matrix.fromArray( data.matrix );
  582. if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate;
  583. if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale );
  584. } else {
  585. if ( data.position !== undefined ) object.position.fromArray( data.position );
  586. if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation );
  587. if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion );
  588. if ( data.scale !== undefined ) object.scale.fromArray( data.scale );
  589. }
  590. if ( data.up !== undefined ) object.up.fromArray( data.up );
  591. if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
  592. if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
  593. if ( data.shadow ) {
  594. if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
  595. if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias;
  596. if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
  597. if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
  598. if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
  599. }
  600. if ( data.visible !== undefined ) object.visible = data.visible;
  601. if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled;
  602. if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder;
  603. if ( data.userData !== undefined ) object.userData = data.userData;
  604. if ( data.layers !== undefined ) object.layers.mask = data.layers;
  605. if ( data.children !== undefined ) {
  606. const children = data.children;
  607. for ( let i = 0; i < children.length; i ++ ) {
  608. object.add( this.parseObject( children[ i ], geometries, materials, textures, animations ) );
  609. }
  610. }
  611. if ( data.animations !== undefined ) {
  612. const objectAnimations = data.animations;
  613. for ( let i = 0; i < objectAnimations.length; i ++ ) {
  614. const uuid = objectAnimations[ i ];
  615. object.animations.push( animations[ uuid ] );
  616. }
  617. }
  618. if ( data.type === 'LOD' ) {
  619. if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate;
  620. const levels = data.levels;
  621. for ( let l = 0; l < levels.length; l ++ ) {
  622. const level = levels[ l ];
  623. const child = object.getObjectByProperty( 'uuid', level.object );
  624. if ( child !== undefined ) {
  625. object.addLevel( child, level.distance, level.hysteresis );
  626. }
  627. }
  628. }
  629. return object;
  630. }
  631. bindSkeletons( object, skeletons ) {
  632. if ( Object.keys( skeletons ).length === 0 ) return;
  633. object.traverse( function ( child ) {
  634. if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) {
  635. const skeleton = skeletons[ child.skeleton ];
  636. if ( skeleton === undefined ) {
  637. console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton );
  638. } else {
  639. child.bind( skeleton, child.bindMatrix );
  640. }
  641. }
  642. } );
  643. }
  644. }
  645. const TEXTURE_MAPPING = {
  646. UVMapping: UVMapping,
  647. CubeReflectionMapping: CubeReflectionMapping,
  648. CubeRefractionMapping: CubeRefractionMapping,
  649. EquirectangularReflectionMapping: EquirectangularReflectionMapping,
  650. EquirectangularRefractionMapping: EquirectangularRefractionMapping,
  651. CubeUVReflectionMapping: CubeUVReflectionMapping
  652. };
  653. const TEXTURE_WRAPPING = {
  654. RepeatWrapping: RepeatWrapping,
  655. ClampToEdgeWrapping: ClampToEdgeWrapping,
  656. MirroredRepeatWrapping: MirroredRepeatWrapping
  657. };
  658. const TEXTURE_FILTER = {
  659. NearestFilter: NearestFilter,
  660. NearestMipmapNearestFilter: NearestMipmapNearestFilter,
  661. NearestMipmapLinearFilter: NearestMipmapLinearFilter,
  662. LinearFilter: LinearFilter,
  663. LinearMipmapNearestFilter: LinearMipmapNearestFilter,
  664. LinearMipmapLinearFilter: LinearMipmapLinearFilter
  665. };
  666. export { ObjectLoader };
粤ICP备19079148号