BufferGeometry.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Vector2 } from '../math/Vector2.js';
  3. import { Box3 } from '../math/Box3.js';
  4. import { EventDispatcher } from './EventDispatcher.js';
  5. import { BufferAttribute, Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from './BufferAttribute.js';
  6. import { Sphere } from '../math/Sphere.js';
  7. import { Object3D } from './Object3D.js';
  8. import { Matrix4 } from '../math/Matrix4.js';
  9. import { Matrix3 } from '../math/Matrix3.js';
  10. import * as MathUtils from '../math/MathUtils.js';
  11. import { arrayNeedsUint32 } from '../utils.js';
  12. let _id = 0;
  13. const _m1 = /*@__PURE__*/ new Matrix4();
  14. const _obj = /*@__PURE__*/ new Object3D();
  15. const _offset = /*@__PURE__*/ new Vector3();
  16. const _box = /*@__PURE__*/ new Box3();
  17. const _boxMorphTargets = /*@__PURE__*/ new Box3();
  18. const _vector = /*@__PURE__*/ new Vector3();
  19. class BufferGeometry extends EventDispatcher {
  20. constructor() {
  21. super();
  22. this.isBufferGeometry = true;
  23. Object.defineProperty( this, 'id', { value: _id ++ } );
  24. this.uuid = MathUtils.generateUUID();
  25. this.name = '';
  26. this.type = 'BufferGeometry';
  27. this.index = null;
  28. this.attributes = {};
  29. this.morphAttributes = {};
  30. this.morphTargetsRelative = false;
  31. this.groups = [];
  32. this.boundingBox = null;
  33. this.boundingSphere = null;
  34. this.drawRange = { start: 0, count: Infinity };
  35. this.userData = {};
  36. }
  37. getIndex() {
  38. return this.index;
  39. }
  40. setIndex( index ) {
  41. if ( Array.isArray( index ) ) {
  42. this.index = new ( arrayNeedsUint32( index ) ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
  43. } else {
  44. this.index = index;
  45. }
  46. return this;
  47. }
  48. getAttribute( name ) {
  49. return this.attributes[ name ];
  50. }
  51. setAttribute( name, attribute ) {
  52. this.attributes[ name ] = attribute;
  53. return this;
  54. }
  55. deleteAttribute( name ) {
  56. delete this.attributes[ name ];
  57. return this;
  58. }
  59. hasAttribute( name ) {
  60. return this.attributes[ name ] !== undefined;
  61. }
  62. addGroup( start, count, materialIndex = 0 ) {
  63. this.groups.push( {
  64. start: start,
  65. count: count,
  66. materialIndex: materialIndex
  67. } );
  68. }
  69. clearGroups() {
  70. this.groups = [];
  71. }
  72. setDrawRange( start, count ) {
  73. this.drawRange.start = start;
  74. this.drawRange.count = count;
  75. }
  76. applyMatrix4( matrix ) {
  77. const position = this.attributes.position;
  78. if ( position !== undefined ) {
  79. position.applyMatrix4( matrix );
  80. position.needsUpdate = true;
  81. }
  82. const normal = this.attributes.normal;
  83. if ( normal !== undefined ) {
  84. const normalMatrix = new Matrix3().getNormalMatrix( matrix );
  85. normal.applyNormalMatrix( normalMatrix );
  86. normal.needsUpdate = true;
  87. }
  88. const tangent = this.attributes.tangent;
  89. if ( tangent !== undefined ) {
  90. tangent.transformDirection( matrix );
  91. tangent.needsUpdate = true;
  92. }
  93. if ( this.boundingBox !== null ) {
  94. this.computeBoundingBox();
  95. }
  96. if ( this.boundingSphere !== null ) {
  97. this.computeBoundingSphere();
  98. }
  99. return this;
  100. }
  101. applyQuaternion( q ) {
  102. _m1.makeRotationFromQuaternion( q );
  103. this.applyMatrix4( _m1 );
  104. return this;
  105. }
  106. rotateX( angle ) {
  107. // rotate geometry around world x-axis
  108. _m1.makeRotationX( angle );
  109. this.applyMatrix4( _m1 );
  110. return this;
  111. }
  112. rotateY( angle ) {
  113. // rotate geometry around world y-axis
  114. _m1.makeRotationY( angle );
  115. this.applyMatrix4( _m1 );
  116. return this;
  117. }
  118. rotateZ( angle ) {
  119. // rotate geometry around world z-axis
  120. _m1.makeRotationZ( angle );
  121. this.applyMatrix4( _m1 );
  122. return this;
  123. }
  124. translate( x, y, z ) {
  125. // translate geometry
  126. _m1.makeTranslation( x, y, z );
  127. this.applyMatrix4( _m1 );
  128. return this;
  129. }
  130. scale( x, y, z ) {
  131. // scale geometry
  132. _m1.makeScale( x, y, z );
  133. this.applyMatrix4( _m1 );
  134. return this;
  135. }
  136. lookAt( vector ) {
  137. _obj.lookAt( vector );
  138. _obj.updateMatrix();
  139. this.applyMatrix4( _obj.matrix );
  140. return this;
  141. }
  142. center() {
  143. this.computeBoundingBox();
  144. this.boundingBox.getCenter( _offset ).negate();
  145. this.translate( _offset.x, _offset.y, _offset.z );
  146. return this;
  147. }
  148. setFromPoints( points ) {
  149. const position = [];
  150. for ( let i = 0, l = points.length; i < l; i ++ ) {
  151. const point = points[ i ];
  152. position.push( point.x, point.y, point.z || 0 );
  153. }
  154. this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  155. return this;
  156. }
  157. computeBoundingBox() {
  158. if ( this.boundingBox === null ) {
  159. this.boundingBox = new Box3();
  160. }
  161. const position = this.attributes.position;
  162. const morphAttributesPosition = this.morphAttributes.position;
  163. if ( position && position.isGLBufferAttribute ) {
  164. console.error( 'THREE.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box. Alternatively set "mesh.frustumCulled" to "false".', this );
  165. this.boundingBox.set(
  166. new Vector3( - Infinity, - Infinity, - Infinity ),
  167. new Vector3( + Infinity, + Infinity, + Infinity )
  168. );
  169. return;
  170. }
  171. if ( position !== undefined ) {
  172. this.boundingBox.setFromBufferAttribute( position );
  173. // process morph attributes if present
  174. if ( morphAttributesPosition ) {
  175. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  176. const morphAttribute = morphAttributesPosition[ i ];
  177. _box.setFromBufferAttribute( morphAttribute );
  178. if ( this.morphTargetsRelative ) {
  179. _vector.addVectors( this.boundingBox.min, _box.min );
  180. this.boundingBox.expandByPoint( _vector );
  181. _vector.addVectors( this.boundingBox.max, _box.max );
  182. this.boundingBox.expandByPoint( _vector );
  183. } else {
  184. this.boundingBox.expandByPoint( _box.min );
  185. this.boundingBox.expandByPoint( _box.max );
  186. }
  187. }
  188. }
  189. } else {
  190. this.boundingBox.makeEmpty();
  191. }
  192. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  193. console.error( 'THREE.BufferGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  194. }
  195. }
  196. computeBoundingSphere() {
  197. if ( this.boundingSphere === null ) {
  198. this.boundingSphere = new Sphere();
  199. }
  200. const position = this.attributes.position;
  201. const morphAttributesPosition = this.morphAttributes.position;
  202. if ( position && position.isGLBufferAttribute ) {
  203. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere. Alternatively set "mesh.frustumCulled" to "false".', this );
  204. this.boundingSphere.set( new Vector3(), Infinity );
  205. return;
  206. }
  207. if ( position ) {
  208. // first, find the center of the bounding sphere
  209. const center = this.boundingSphere.center;
  210. _box.setFromBufferAttribute( position );
  211. // process morph attributes if present
  212. if ( morphAttributesPosition ) {
  213. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  214. const morphAttribute = morphAttributesPosition[ i ];
  215. _boxMorphTargets.setFromBufferAttribute( morphAttribute );
  216. if ( this.morphTargetsRelative ) {
  217. _vector.addVectors( _box.min, _boxMorphTargets.min );
  218. _box.expandByPoint( _vector );
  219. _vector.addVectors( _box.max, _boxMorphTargets.max );
  220. _box.expandByPoint( _vector );
  221. } else {
  222. _box.expandByPoint( _boxMorphTargets.min );
  223. _box.expandByPoint( _boxMorphTargets.max );
  224. }
  225. }
  226. }
  227. _box.getCenter( center );
  228. // second, try to find a boundingSphere with a radius smaller than the
  229. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  230. let maxRadiusSq = 0;
  231. for ( let i = 0, il = position.count; i < il; i ++ ) {
  232. _vector.fromBufferAttribute( position, i );
  233. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  234. }
  235. // process morph attributes if present
  236. if ( morphAttributesPosition ) {
  237. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  238. const morphAttribute = morphAttributesPosition[ i ];
  239. const morphTargetsRelative = this.morphTargetsRelative;
  240. for ( let j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
  241. _vector.fromBufferAttribute( morphAttribute, j );
  242. if ( morphTargetsRelative ) {
  243. _offset.fromBufferAttribute( position, j );
  244. _vector.add( _offset );
  245. }
  246. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  247. }
  248. }
  249. }
  250. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  251. if ( isNaN( this.boundingSphere.radius ) ) {
  252. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  253. }
  254. }
  255. }
  256. computeTangents() {
  257. const index = this.index;
  258. const attributes = this.attributes;
  259. // based on http://www.terathon.com/code/tangent.html
  260. // (per vertex tangents)
  261. if ( index === null ||
  262. attributes.position === undefined ||
  263. attributes.normal === undefined ||
  264. attributes.uv === undefined ) {
  265. console.error( 'THREE.BufferGeometry: .computeTangents() failed. Missing required attributes (index, position, normal or uv)' );
  266. return;
  267. }
  268. const indices = index.array;
  269. const positions = attributes.position.array;
  270. const normals = attributes.normal.array;
  271. const uvs = attributes.uv.array;
  272. const nVertices = positions.length / 3;
  273. if ( this.hasAttribute( 'tangent' ) === false ) {
  274. this.setAttribute( 'tangent', new BufferAttribute( new Float32Array( 4 * nVertices ), 4 ) );
  275. }
  276. const tangents = this.getAttribute( 'tangent' ).array;
  277. const tan1 = [], tan2 = [];
  278. for ( let i = 0; i < nVertices; i ++ ) {
  279. tan1[ i ] = new Vector3();
  280. tan2[ i ] = new Vector3();
  281. }
  282. const vA = new Vector3(),
  283. vB = new Vector3(),
  284. vC = new Vector3(),
  285. uvA = new Vector2(),
  286. uvB = new Vector2(),
  287. uvC = new Vector2(),
  288. sdir = new Vector3(),
  289. tdir = new Vector3();
  290. function handleTriangle( a, b, c ) {
  291. vA.fromArray( positions, a * 3 );
  292. vB.fromArray( positions, b * 3 );
  293. vC.fromArray( positions, c * 3 );
  294. uvA.fromArray( uvs, a * 2 );
  295. uvB.fromArray( uvs, b * 2 );
  296. uvC.fromArray( uvs, c * 2 );
  297. vB.sub( vA );
  298. vC.sub( vA );
  299. uvB.sub( uvA );
  300. uvC.sub( uvA );
  301. const r = 1.0 / ( uvB.x * uvC.y - uvC.x * uvB.y );
  302. // silently ignore degenerate uv triangles having coincident or colinear vertices
  303. if ( ! isFinite( r ) ) return;
  304. sdir.copy( vB ).multiplyScalar( uvC.y ).addScaledVector( vC, - uvB.y ).multiplyScalar( r );
  305. tdir.copy( vC ).multiplyScalar( uvB.x ).addScaledVector( vB, - uvC.x ).multiplyScalar( r );
  306. tan1[ a ].add( sdir );
  307. tan1[ b ].add( sdir );
  308. tan1[ c ].add( sdir );
  309. tan2[ a ].add( tdir );
  310. tan2[ b ].add( tdir );
  311. tan2[ c ].add( tdir );
  312. }
  313. let groups = this.groups;
  314. if ( groups.length === 0 ) {
  315. groups = [ {
  316. start: 0,
  317. count: indices.length
  318. } ];
  319. }
  320. for ( let i = 0, il = groups.length; i < il; ++ i ) {
  321. const group = groups[ i ];
  322. const start = group.start;
  323. const count = group.count;
  324. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  325. handleTriangle(
  326. indices[ j + 0 ],
  327. indices[ j + 1 ],
  328. indices[ j + 2 ]
  329. );
  330. }
  331. }
  332. const tmp = new Vector3(), tmp2 = new Vector3();
  333. const n = new Vector3(), n2 = new Vector3();
  334. function handleVertex( v ) {
  335. n.fromArray( normals, v * 3 );
  336. n2.copy( n );
  337. const t = tan1[ v ];
  338. // Gram-Schmidt orthogonalize
  339. tmp.copy( t );
  340. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  341. // Calculate handedness
  342. tmp2.crossVectors( n2, t );
  343. const test = tmp2.dot( tan2[ v ] );
  344. const w = ( test < 0.0 ) ? - 1.0 : 1.0;
  345. tangents[ v * 4 ] = tmp.x;
  346. tangents[ v * 4 + 1 ] = tmp.y;
  347. tangents[ v * 4 + 2 ] = tmp.z;
  348. tangents[ v * 4 + 3 ] = w;
  349. }
  350. for ( let i = 0, il = groups.length; i < il; ++ i ) {
  351. const group = groups[ i ];
  352. const start = group.start;
  353. const count = group.count;
  354. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  355. handleVertex( indices[ j + 0 ] );
  356. handleVertex( indices[ j + 1 ] );
  357. handleVertex( indices[ j + 2 ] );
  358. }
  359. }
  360. }
  361. computeVertexNormals() {
  362. const index = this.index;
  363. const positionAttribute = this.getAttribute( 'position' );
  364. if ( positionAttribute !== undefined ) {
  365. let normalAttribute = this.getAttribute( 'normal' );
  366. if ( normalAttribute === undefined ) {
  367. normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 );
  368. this.setAttribute( 'normal', normalAttribute );
  369. } else {
  370. // reset existing normals to zero
  371. for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) {
  372. normalAttribute.setXYZ( i, 0, 0, 0 );
  373. }
  374. }
  375. const pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
  376. const nA = new Vector3(), nB = new Vector3(), nC = new Vector3();
  377. const cb = new Vector3(), ab = new Vector3();
  378. // indexed elements
  379. if ( index ) {
  380. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  381. const vA = index.getX( i + 0 );
  382. const vB = index.getX( i + 1 );
  383. const vC = index.getX( i + 2 );
  384. pA.fromBufferAttribute( positionAttribute, vA );
  385. pB.fromBufferAttribute( positionAttribute, vB );
  386. pC.fromBufferAttribute( positionAttribute, vC );
  387. cb.subVectors( pC, pB );
  388. ab.subVectors( pA, pB );
  389. cb.cross( ab );
  390. nA.fromBufferAttribute( normalAttribute, vA );
  391. nB.fromBufferAttribute( normalAttribute, vB );
  392. nC.fromBufferAttribute( normalAttribute, vC );
  393. nA.add( cb );
  394. nB.add( cb );
  395. nC.add( cb );
  396. normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z );
  397. normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z );
  398. normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z );
  399. }
  400. } else {
  401. // non-indexed elements (unconnected triangle soup)
  402. for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) {
  403. pA.fromBufferAttribute( positionAttribute, i + 0 );
  404. pB.fromBufferAttribute( positionAttribute, i + 1 );
  405. pC.fromBufferAttribute( positionAttribute, i + 2 );
  406. cb.subVectors( pC, pB );
  407. ab.subVectors( pA, pB );
  408. cb.cross( ab );
  409. normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z );
  410. normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z );
  411. normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z );
  412. }
  413. }
  414. this.normalizeNormals();
  415. normalAttribute.needsUpdate = true;
  416. }
  417. }
  418. // @deprecated since r144
  419. merge() {
  420. console.error( 'THREE.BufferGeometry.merge() has been removed. Use THREE.BufferGeometryUtils.mergeBufferGeometries() instead.' );
  421. return this;
  422. }
  423. normalizeNormals() {
  424. const normals = this.attributes.normal;
  425. for ( let i = 0, il = normals.count; i < il; i ++ ) {
  426. _vector.fromBufferAttribute( normals, i );
  427. _vector.normalize();
  428. normals.setXYZ( i, _vector.x, _vector.y, _vector.z );
  429. }
  430. }
  431. toNonIndexed() {
  432. function convertBufferAttribute( attribute, indices ) {
  433. const array = attribute.array;
  434. const itemSize = attribute.itemSize;
  435. const normalized = attribute.normalized;
  436. const array2 = new array.constructor( indices.length * itemSize );
  437. let index = 0, index2 = 0;
  438. for ( let i = 0, l = indices.length; i < l; i ++ ) {
  439. if ( attribute.isInterleavedBufferAttribute ) {
  440. index = indices[ i ] * attribute.data.stride + attribute.offset;
  441. } else {
  442. index = indices[ i ] * itemSize;
  443. }
  444. for ( let j = 0; j < itemSize; j ++ ) {
  445. array2[ index2 ++ ] = array[ index ++ ];
  446. }
  447. }
  448. return new BufferAttribute( array2, itemSize, normalized );
  449. }
  450. //
  451. if ( this.index === null ) {
  452. console.warn( 'THREE.BufferGeometry.toNonIndexed(): BufferGeometry is already non-indexed.' );
  453. return this;
  454. }
  455. const geometry2 = new BufferGeometry();
  456. const indices = this.index.array;
  457. const attributes = this.attributes;
  458. // attributes
  459. for ( const name in attributes ) {
  460. const attribute = attributes[ name ];
  461. const newAttribute = convertBufferAttribute( attribute, indices );
  462. geometry2.setAttribute( name, newAttribute );
  463. }
  464. // morph attributes
  465. const morphAttributes = this.morphAttributes;
  466. for ( const name in morphAttributes ) {
  467. const morphArray = [];
  468. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  469. for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
  470. const attribute = morphAttribute[ i ];
  471. const newAttribute = convertBufferAttribute( attribute, indices );
  472. morphArray.push( newAttribute );
  473. }
  474. geometry2.morphAttributes[ name ] = morphArray;
  475. }
  476. geometry2.morphTargetsRelative = this.morphTargetsRelative;
  477. // groups
  478. const groups = this.groups;
  479. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  480. const group = groups[ i ];
  481. geometry2.addGroup( group.start, group.count, group.materialIndex );
  482. }
  483. return geometry2;
  484. }
  485. toJSON() {
  486. const data = {
  487. metadata: {
  488. version: 4.5,
  489. type: 'BufferGeometry',
  490. generator: 'BufferGeometry.toJSON'
  491. }
  492. };
  493. // standard BufferGeometry serialization
  494. data.uuid = this.uuid;
  495. data.type = this.type;
  496. if ( this.name !== '' ) data.name = this.name;
  497. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  498. if ( this.parameters !== undefined ) {
  499. const parameters = this.parameters;
  500. for ( const key in parameters ) {
  501. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  502. }
  503. return data;
  504. }
  505. // for simplicity the code assumes attributes are not shared across geometries, see #15811
  506. data.data = { attributes: {} };
  507. const index = this.index;
  508. if ( index !== null ) {
  509. data.data.index = {
  510. type: index.array.constructor.name,
  511. array: Array.prototype.slice.call( index.array )
  512. };
  513. }
  514. const attributes = this.attributes;
  515. for ( const key in attributes ) {
  516. const attribute = attributes[ key ];
  517. data.data.attributes[ key ] = attribute.toJSON( data.data );
  518. }
  519. const morphAttributes = {};
  520. let hasMorphAttributes = false;
  521. for ( const key in this.morphAttributes ) {
  522. const attributeArray = this.morphAttributes[ key ];
  523. const array = [];
  524. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  525. const attribute = attributeArray[ i ];
  526. array.push( attribute.toJSON( data.data ) );
  527. }
  528. if ( array.length > 0 ) {
  529. morphAttributes[ key ] = array;
  530. hasMorphAttributes = true;
  531. }
  532. }
  533. if ( hasMorphAttributes ) {
  534. data.data.morphAttributes = morphAttributes;
  535. data.data.morphTargetsRelative = this.morphTargetsRelative;
  536. }
  537. const groups = this.groups;
  538. if ( groups.length > 0 ) {
  539. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  540. }
  541. const boundingSphere = this.boundingSphere;
  542. if ( boundingSphere !== null ) {
  543. data.data.boundingSphere = {
  544. center: boundingSphere.center.toArray(),
  545. radius: boundingSphere.radius
  546. };
  547. }
  548. return data;
  549. }
  550. clone() {
  551. return new this.constructor().copy( this );
  552. }
  553. copy( source ) {
  554. // reset
  555. this.index = null;
  556. this.attributes = {};
  557. this.morphAttributes = {};
  558. this.groups = [];
  559. this.boundingBox = null;
  560. this.boundingSphere = null;
  561. // used for storing cloned, shared data
  562. const data = {};
  563. // name
  564. this.name = source.name;
  565. // index
  566. const index = source.index;
  567. if ( index !== null ) {
  568. this.setIndex( index.clone( data ) );
  569. }
  570. // attributes
  571. const attributes = source.attributes;
  572. for ( const name in attributes ) {
  573. const attribute = attributes[ name ];
  574. this.setAttribute( name, attribute.clone( data ) );
  575. }
  576. // morph attributes
  577. const morphAttributes = source.morphAttributes;
  578. for ( const name in morphAttributes ) {
  579. const array = [];
  580. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  581. for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) {
  582. array.push( morphAttribute[ i ].clone( data ) );
  583. }
  584. this.morphAttributes[ name ] = array;
  585. }
  586. this.morphTargetsRelative = source.morphTargetsRelative;
  587. // groups
  588. const groups = source.groups;
  589. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  590. const group = groups[ i ];
  591. this.addGroup( group.start, group.count, group.materialIndex );
  592. }
  593. // bounding box
  594. const boundingBox = source.boundingBox;
  595. if ( boundingBox !== null ) {
  596. this.boundingBox = boundingBox.clone();
  597. }
  598. // bounding sphere
  599. const boundingSphere = source.boundingSphere;
  600. if ( boundingSphere !== null ) {
  601. this.boundingSphere = boundingSphere.clone();
  602. }
  603. // draw range
  604. this.drawRange.start = source.drawRange.start;
  605. this.drawRange.count = source.drawRange.count;
  606. // user data
  607. this.userData = source.userData;
  608. // geometry generator parameters
  609. if ( source.parameters !== undefined ) this.parameters = Object.assign( {}, source.parameters );
  610. return this;
  611. }
  612. dispose() {
  613. this.dispatchEvent( { type: 'dispose' } );
  614. }
  615. }
  616. export { BufferGeometry };
粤ICP备19079148号