BufferGeometry.js 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. normalizeNormals() {
  419. const normals = this.attributes.normal;
  420. for ( let i = 0, il = normals.count; i < il; i ++ ) {
  421. _vector.fromBufferAttribute( normals, i );
  422. _vector.normalize();
  423. normals.setXYZ( i, _vector.x, _vector.y, _vector.z );
  424. }
  425. }
  426. toNonIndexed() {
  427. function convertBufferAttribute( attribute, indices ) {
  428. const array = attribute.array;
  429. const itemSize = attribute.itemSize;
  430. const normalized = attribute.normalized;
  431. const array2 = new array.constructor( indices.length * itemSize );
  432. let index = 0, index2 = 0;
  433. for ( let i = 0, l = indices.length; i < l; i ++ ) {
  434. if ( attribute.isInterleavedBufferAttribute ) {
  435. index = indices[ i ] * attribute.data.stride + attribute.offset;
  436. } else {
  437. index = indices[ i ] * itemSize;
  438. }
  439. for ( let j = 0; j < itemSize; j ++ ) {
  440. array2[ index2 ++ ] = array[ index ++ ];
  441. }
  442. }
  443. return new BufferAttribute( array2, itemSize, normalized );
  444. }
  445. //
  446. if ( this.index === null ) {
  447. console.warn( 'THREE.BufferGeometry.toNonIndexed(): BufferGeometry is already non-indexed.' );
  448. return this;
  449. }
  450. const geometry2 = new BufferGeometry();
  451. const indices = this.index.array;
  452. const attributes = this.attributes;
  453. // attributes
  454. for ( const name in attributes ) {
  455. const attribute = attributes[ name ];
  456. const newAttribute = convertBufferAttribute( attribute, indices );
  457. geometry2.setAttribute( name, newAttribute );
  458. }
  459. // morph attributes
  460. const morphAttributes = this.morphAttributes;
  461. for ( const name in morphAttributes ) {
  462. const morphArray = [];
  463. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  464. for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
  465. const attribute = morphAttribute[ i ];
  466. const newAttribute = convertBufferAttribute( attribute, indices );
  467. morphArray.push( newAttribute );
  468. }
  469. geometry2.morphAttributes[ name ] = morphArray;
  470. }
  471. geometry2.morphTargetsRelative = this.morphTargetsRelative;
  472. // groups
  473. const groups = this.groups;
  474. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  475. const group = groups[ i ];
  476. geometry2.addGroup( group.start, group.count, group.materialIndex );
  477. }
  478. return geometry2;
  479. }
  480. toJSON() {
  481. const data = {
  482. metadata: {
  483. version: 4.6,
  484. type: 'BufferGeometry',
  485. generator: 'BufferGeometry.toJSON'
  486. }
  487. };
  488. // standard BufferGeometry serialization
  489. data.uuid = this.uuid;
  490. data.type = this.type;
  491. if ( this.name !== '' ) data.name = this.name;
  492. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  493. if ( this.parameters !== undefined ) {
  494. const parameters = this.parameters;
  495. for ( const key in parameters ) {
  496. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  497. }
  498. return data;
  499. }
  500. // for simplicity the code assumes attributes are not shared across geometries, see #15811
  501. data.data = { attributes: {} };
  502. const index = this.index;
  503. if ( index !== null ) {
  504. data.data.index = {
  505. type: index.array.constructor.name,
  506. array: Array.prototype.slice.call( index.array )
  507. };
  508. }
  509. const attributes = this.attributes;
  510. for ( const key in attributes ) {
  511. const attribute = attributes[ key ];
  512. data.data.attributes[ key ] = attribute.toJSON( data.data );
  513. }
  514. const morphAttributes = {};
  515. let hasMorphAttributes = false;
  516. for ( const key in this.morphAttributes ) {
  517. const attributeArray = this.morphAttributes[ key ];
  518. const array = [];
  519. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  520. const attribute = attributeArray[ i ];
  521. array.push( attribute.toJSON( data.data ) );
  522. }
  523. if ( array.length > 0 ) {
  524. morphAttributes[ key ] = array;
  525. hasMorphAttributes = true;
  526. }
  527. }
  528. if ( hasMorphAttributes ) {
  529. data.data.morphAttributes = morphAttributes;
  530. data.data.morphTargetsRelative = this.morphTargetsRelative;
  531. }
  532. const groups = this.groups;
  533. if ( groups.length > 0 ) {
  534. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  535. }
  536. const boundingSphere = this.boundingSphere;
  537. if ( boundingSphere !== null ) {
  538. data.data.boundingSphere = {
  539. center: boundingSphere.center.toArray(),
  540. radius: boundingSphere.radius
  541. };
  542. }
  543. return data;
  544. }
  545. clone() {
  546. return new this.constructor().copy( this );
  547. }
  548. copy( source ) {
  549. // reset
  550. this.index = null;
  551. this.attributes = {};
  552. this.morphAttributes = {};
  553. this.groups = [];
  554. this.boundingBox = null;
  555. this.boundingSphere = null;
  556. // used for storing cloned, shared data
  557. const data = {};
  558. // name
  559. this.name = source.name;
  560. // index
  561. const index = source.index;
  562. if ( index !== null ) {
  563. this.setIndex( index.clone( data ) );
  564. }
  565. // attributes
  566. const attributes = source.attributes;
  567. for ( const name in attributes ) {
  568. const attribute = attributes[ name ];
  569. this.setAttribute( name, attribute.clone( data ) );
  570. }
  571. // morph attributes
  572. const morphAttributes = source.morphAttributes;
  573. for ( const name in morphAttributes ) {
  574. const array = [];
  575. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  576. for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) {
  577. array.push( morphAttribute[ i ].clone( data ) );
  578. }
  579. this.morphAttributes[ name ] = array;
  580. }
  581. this.morphTargetsRelative = source.morphTargetsRelative;
  582. // groups
  583. const groups = source.groups;
  584. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  585. const group = groups[ i ];
  586. this.addGroup( group.start, group.count, group.materialIndex );
  587. }
  588. // bounding box
  589. const boundingBox = source.boundingBox;
  590. if ( boundingBox !== null ) {
  591. this.boundingBox = boundingBox.clone();
  592. }
  593. // bounding sphere
  594. const boundingSphere = source.boundingSphere;
  595. if ( boundingSphere !== null ) {
  596. this.boundingSphere = boundingSphere.clone();
  597. }
  598. // draw range
  599. this.drawRange.start = source.drawRange.start;
  600. this.drawRange.count = source.drawRange.count;
  601. // user data
  602. this.userData = source.userData;
  603. return this;
  604. }
  605. dispose() {
  606. this.dispatchEvent( { type: 'dispose' } );
  607. }
  608. }
  609. export { BufferGeometry };
粤ICP备19079148号