BufferGeometry.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. import { Vector3 } from '../math/Vector3';
  2. import { Box3 } from '../math/Box3';
  3. import { EventDispatcher } from './EventDispatcher';
  4. import { BufferAttribute, Float32BufferAttribute } from './BufferAttribute';
  5. import { Sphere } from '../math/Sphere';
  6. import { DirectGeometry } from './DirectGeometry';
  7. import { Object3D } from './Object3D';
  8. import { Matrix4 } from '../math/Matrix4';
  9. import { Matrix3 } from '../math/Matrix3';
  10. import { _Math } from '../math/Math';
  11. import { GeometryIdCount } from './Geometry';
  12. /**
  13. * @author alteredq / http://alteredqualia.com/
  14. * @author mrdoob / http://mrdoob.com/
  15. */
  16. function BufferGeometry() {
  17. Object.defineProperty( this, 'id', { value: GeometryIdCount() } );
  18. this.uuid = _Math.generateUUID();
  19. this.name = '';
  20. this.type = 'BufferGeometry';
  21. this.index = null;
  22. this.attributes = {};
  23. this.morphAttributes = {};
  24. this.groups = [];
  25. this.boundingBox = null;
  26. this.boundingSphere = null;
  27. this.drawRange = { start: 0, count: Infinity };
  28. }
  29. Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
  30. isBufferGeometry: true,
  31. getIndex: function () {
  32. return this.index;
  33. },
  34. setIndex: function ( index ) {
  35. this.index = index;
  36. },
  37. addAttribute: function ( name, attribute ) {
  38. if ( ( attribute && attribute.isBufferAttribute ) === false && ( attribute && attribute.isInterleavedBufferAttribute ) === false ) {
  39. console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' );
  40. this.addAttribute( name, new BufferAttribute( arguments[ 1 ], arguments[ 2 ] ) );
  41. return;
  42. }
  43. if ( name === 'index' ) {
  44. console.warn( 'THREE.BufferGeometry.addAttribute: Use .setIndex() for index attribute.' );
  45. this.setIndex( attribute );
  46. return;
  47. }
  48. this.attributes[ name ] = attribute;
  49. return this;
  50. },
  51. getAttribute: function ( name ) {
  52. return this.attributes[ name ];
  53. },
  54. removeAttribute: function ( name ) {
  55. delete this.attributes[ name ];
  56. return this;
  57. },
  58. addGroup: function ( start, count, materialIndex ) {
  59. this.groups.push( {
  60. start: start,
  61. count: count,
  62. materialIndex: materialIndex !== undefined ? materialIndex : 0
  63. } );
  64. },
  65. clearGroups: function () {
  66. this.groups = [];
  67. },
  68. setDrawRange: function ( start, count ) {
  69. this.drawRange.start = start;
  70. this.drawRange.count = count;
  71. },
  72. applyMatrix: function ( matrix ) {
  73. var position = this.attributes.position;
  74. if ( position !== undefined ) {
  75. matrix.applyToVector3Array( position.array );
  76. position.needsUpdate = true;
  77. }
  78. var normal = this.attributes.normal;
  79. if ( normal !== undefined ) {
  80. var normalMatrix = new Matrix3().getNormalMatrix( matrix );
  81. normalMatrix.applyToVector3Array( normal.array );
  82. normal.needsUpdate = true;
  83. }
  84. if ( this.boundingBox !== null ) {
  85. this.computeBoundingBox();
  86. }
  87. if ( this.boundingSphere !== null ) {
  88. this.computeBoundingSphere();
  89. }
  90. return this;
  91. },
  92. rotateX: function () {
  93. // rotate geometry around world x-axis
  94. var m1;
  95. return function rotateX( angle ) {
  96. if ( m1 === undefined ) m1 = new Matrix4();
  97. m1.makeRotationX( angle );
  98. this.applyMatrix( m1 );
  99. return this;
  100. };
  101. }(),
  102. rotateY: function () {
  103. // rotate geometry around world y-axis
  104. var m1;
  105. return function rotateY( angle ) {
  106. if ( m1 === undefined ) m1 = new Matrix4();
  107. m1.makeRotationY( angle );
  108. this.applyMatrix( m1 );
  109. return this;
  110. };
  111. }(),
  112. rotateZ: function () {
  113. // rotate geometry around world z-axis
  114. var m1;
  115. return function rotateZ( angle ) {
  116. if ( m1 === undefined ) m1 = new Matrix4();
  117. m1.makeRotationZ( angle );
  118. this.applyMatrix( m1 );
  119. return this;
  120. };
  121. }(),
  122. translate: function () {
  123. // translate geometry
  124. var m1;
  125. return function translate( x, y, z ) {
  126. if ( m1 === undefined ) m1 = new Matrix4();
  127. m1.makeTranslation( x, y, z );
  128. this.applyMatrix( m1 );
  129. return this;
  130. };
  131. }(),
  132. scale: function () {
  133. // scale geometry
  134. var m1;
  135. return function scale( x, y, z ) {
  136. if ( m1 === undefined ) m1 = new Matrix4();
  137. m1.makeScale( x, y, z );
  138. this.applyMatrix( m1 );
  139. return this;
  140. };
  141. }(),
  142. lookAt: function () {
  143. var obj;
  144. return function lookAt( vector ) {
  145. if ( obj === undefined ) obj = new Object3D();
  146. obj.lookAt( vector );
  147. obj.updateMatrix();
  148. this.applyMatrix( obj.matrix );
  149. };
  150. }(),
  151. center: function () {
  152. this.computeBoundingBox();
  153. var offset = this.boundingBox.getCenter().negate();
  154. this.translate( offset.x, offset.y, offset.z );
  155. return offset;
  156. },
  157. setFromObject: function ( object ) {
  158. // console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this );
  159. var geometry = object.geometry;
  160. if ( object.isPoints || object.isLine ) {
  161. var positions = new Float32BufferAttribute( geometry.vertices.length * 3, 3 );
  162. var colors = new Float32BufferAttribute( geometry.colors.length * 3, 3 );
  163. this.addAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
  164. this.addAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
  165. if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
  166. var lineDistances = new Float32BufferAttribute( geometry.lineDistances.length, 1 );
  167. this.addAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
  168. }
  169. if ( geometry.boundingSphere !== null ) {
  170. this.boundingSphere = geometry.boundingSphere.clone();
  171. }
  172. if ( geometry.boundingBox !== null ) {
  173. this.boundingBox = geometry.boundingBox.clone();
  174. }
  175. } else if ( object.isMesh ) {
  176. if ( geometry && geometry.isGeometry ) {
  177. this.fromGeometry( geometry );
  178. }
  179. }
  180. return this;
  181. },
  182. updateFromObject: function ( object ) {
  183. var geometry = object.geometry;
  184. if ( object.isMesh ) {
  185. var direct = geometry.__directGeometry;
  186. if ( geometry.elementsNeedUpdate === true ) {
  187. direct = undefined;
  188. geometry.elementsNeedUpdate = false;
  189. }
  190. if ( direct === undefined ) {
  191. return this.fromGeometry( geometry );
  192. }
  193. direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
  194. direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
  195. direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
  196. direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
  197. direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
  198. geometry.verticesNeedUpdate = false;
  199. geometry.normalsNeedUpdate = false;
  200. geometry.colorsNeedUpdate = false;
  201. geometry.uvsNeedUpdate = false;
  202. geometry.groupsNeedUpdate = false;
  203. geometry = direct;
  204. }
  205. var attribute;
  206. if ( geometry.verticesNeedUpdate === true ) {
  207. attribute = this.attributes.position;
  208. if ( attribute !== undefined ) {
  209. attribute.copyVector3sArray( geometry.vertices );
  210. attribute.needsUpdate = true;
  211. }
  212. geometry.verticesNeedUpdate = false;
  213. }
  214. if ( geometry.normalsNeedUpdate === true ) {
  215. attribute = this.attributes.normal;
  216. if ( attribute !== undefined ) {
  217. attribute.copyVector3sArray( geometry.normals );
  218. attribute.needsUpdate = true;
  219. }
  220. geometry.normalsNeedUpdate = false;
  221. }
  222. if ( geometry.colorsNeedUpdate === true ) {
  223. attribute = this.attributes.color;
  224. if ( attribute !== undefined ) {
  225. attribute.copyColorsArray( geometry.colors );
  226. attribute.needsUpdate = true;
  227. }
  228. geometry.colorsNeedUpdate = false;
  229. }
  230. if ( geometry.uvsNeedUpdate ) {
  231. attribute = this.attributes.uv;
  232. if ( attribute !== undefined ) {
  233. attribute.copyVector2sArray( geometry.uvs );
  234. attribute.needsUpdate = true;
  235. }
  236. geometry.uvsNeedUpdate = false;
  237. }
  238. if ( geometry.lineDistancesNeedUpdate ) {
  239. attribute = this.attributes.lineDistance;
  240. if ( attribute !== undefined ) {
  241. attribute.copyArray( geometry.lineDistances );
  242. attribute.needsUpdate = true;
  243. }
  244. geometry.lineDistancesNeedUpdate = false;
  245. }
  246. if ( geometry.groupsNeedUpdate ) {
  247. geometry.computeGroups( object.geometry );
  248. this.groups = geometry.groups;
  249. geometry.groupsNeedUpdate = false;
  250. }
  251. return this;
  252. },
  253. fromGeometry: function ( geometry ) {
  254. geometry.__directGeometry = new DirectGeometry().fromGeometry( geometry );
  255. return this.fromDirectGeometry( geometry.__directGeometry );
  256. },
  257. fromDirectGeometry: function ( geometry ) {
  258. var positions = new Float32Array( geometry.vertices.length * 3 );
  259. this.addAttribute( 'position', new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
  260. if ( geometry.normals.length > 0 ) {
  261. var normals = new Float32Array( geometry.normals.length * 3 );
  262. this.addAttribute( 'normal', new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
  263. }
  264. if ( geometry.colors.length > 0 ) {
  265. var colors = new Float32Array( geometry.colors.length * 3 );
  266. this.addAttribute( 'color', new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
  267. }
  268. if ( geometry.uvs.length > 0 ) {
  269. var uvs = new Float32Array( geometry.uvs.length * 2 );
  270. this.addAttribute( 'uv', new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
  271. }
  272. if ( geometry.uvs2.length > 0 ) {
  273. var uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  274. this.addAttribute( 'uv2', new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
  275. }
  276. if ( geometry.indices.length > 0 ) {
  277. var TypeArray = geometry.vertices.length > 65535 ? Uint32Array : Uint16Array;
  278. var indices = new TypeArray( geometry.indices.length * 3 );
  279. this.setIndex( new BufferAttribute( indices, 1 ).copyIndicesArray( geometry.indices ) );
  280. }
  281. // groups
  282. this.groups = geometry.groups;
  283. // morphs
  284. for ( var name in geometry.morphTargets ) {
  285. var array = [];
  286. var morphTargets = geometry.morphTargets[ name ];
  287. for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {
  288. var morphTarget = morphTargets[ i ];
  289. var attribute = new Float32BufferAttribute( morphTarget.length * 3, 3 );
  290. array.push( attribute.copyVector3sArray( morphTarget ) );
  291. }
  292. this.morphAttributes[ name ] = array;
  293. }
  294. // skinning
  295. if ( geometry.skinIndices.length > 0 ) {
  296. var skinIndices = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
  297. this.addAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
  298. }
  299. if ( geometry.skinWeights.length > 0 ) {
  300. var skinWeights = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
  301. this.addAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
  302. }
  303. //
  304. if ( geometry.boundingSphere !== null ) {
  305. this.boundingSphere = geometry.boundingSphere.clone();
  306. }
  307. if ( geometry.boundingBox !== null ) {
  308. this.boundingBox = geometry.boundingBox.clone();
  309. }
  310. return this;
  311. },
  312. computeBoundingBox: function () {
  313. if ( this.boundingBox === null ) {
  314. this.boundingBox = new Box3();
  315. }
  316. var position = this.attributes.position;
  317. if ( position !== undefined ) {
  318. this.boundingBox.setFromBufferAttribute( position );
  319. } else {
  320. this.boundingBox.makeEmpty();
  321. }
  322. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  323. console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  324. }
  325. },
  326. computeBoundingSphere: function () {
  327. var box = new Box3();
  328. var vector = new Vector3();
  329. return function computeBoundingSphere() {
  330. if ( this.boundingSphere === null ) {
  331. this.boundingSphere = new Sphere();
  332. }
  333. var position = this.attributes.position;
  334. if ( position ) {
  335. var center = this.boundingSphere.center;
  336. box.setFromBufferAttribute( position );
  337. box.getCenter( center );
  338. // hoping to find a boundingSphere with a radius smaller than the
  339. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  340. var maxRadiusSq = 0;
  341. for ( var i = 0, il = position.count; i < il; i ++ ) {
  342. vector.x = position.getX( i );
  343. vector.y = position.getY( i );
  344. vector.z = position.getZ( i );
  345. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  346. }
  347. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  348. if ( isNaN( this.boundingSphere.radius ) ) {
  349. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  350. }
  351. }
  352. };
  353. }(),
  354. computeFaceNormals: function () {
  355. // backwards compatibility
  356. },
  357. computeVertexNormals: function () {
  358. var index = this.index;
  359. var attributes = this.attributes;
  360. var groups = this.groups;
  361. if ( attributes.position ) {
  362. var positions = attributes.position.array;
  363. if ( attributes.normal === undefined ) {
  364. this.addAttribute( 'normal', new BufferAttribute( new Float32Array( positions.length ), 3 ) );
  365. } else {
  366. // reset existing normals to zero
  367. var array = attributes.normal.array;
  368. for ( var i = 0, il = array.length; i < il; i ++ ) {
  369. array[ i ] = 0;
  370. }
  371. }
  372. var normals = attributes.normal.array;
  373. var vA, vB, vC;
  374. var pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
  375. var cb = new Vector3(), ab = new Vector3();
  376. // indexed elements
  377. if ( index ) {
  378. var indices = index.array;
  379. if ( groups.length === 0 ) {
  380. this.addGroup( 0, indices.length );
  381. }
  382. for ( var j = 0, jl = groups.length; j < jl; ++ j ) {
  383. var group = groups[ j ];
  384. var start = group.start;
  385. var count = group.count;
  386. for ( var i = start, il = start + count; i < il; i += 3 ) {
  387. vA = indices[ i + 0 ] * 3;
  388. vB = indices[ i + 1 ] * 3;
  389. vC = indices[ i + 2 ] * 3;
  390. pA.fromArray( positions, vA );
  391. pB.fromArray( positions, vB );
  392. pC.fromArray( positions, vC );
  393. cb.subVectors( pC, pB );
  394. ab.subVectors( pA, pB );
  395. cb.cross( ab );
  396. normals[ vA ] += cb.x;
  397. normals[ vA + 1 ] += cb.y;
  398. normals[ vA + 2 ] += cb.z;
  399. normals[ vB ] += cb.x;
  400. normals[ vB + 1 ] += cb.y;
  401. normals[ vB + 2 ] += cb.z;
  402. normals[ vC ] += cb.x;
  403. normals[ vC + 1 ] += cb.y;
  404. normals[ vC + 2 ] += cb.z;
  405. }
  406. }
  407. } else {
  408. // non-indexed elements (unconnected triangle soup)
  409. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  410. pA.fromArray( positions, i );
  411. pB.fromArray( positions, i + 3 );
  412. pC.fromArray( positions, i + 6 );
  413. cb.subVectors( pC, pB );
  414. ab.subVectors( pA, pB );
  415. cb.cross( ab );
  416. normals[ i ] = cb.x;
  417. normals[ i + 1 ] = cb.y;
  418. normals[ i + 2 ] = cb.z;
  419. normals[ i + 3 ] = cb.x;
  420. normals[ i + 4 ] = cb.y;
  421. normals[ i + 5 ] = cb.z;
  422. normals[ i + 6 ] = cb.x;
  423. normals[ i + 7 ] = cb.y;
  424. normals[ i + 8 ] = cb.z;
  425. }
  426. }
  427. this.normalizeNormals();
  428. attributes.normal.needsUpdate = true;
  429. }
  430. },
  431. merge: function ( geometry, offset ) {
  432. if ( ( geometry && geometry.isBufferGeometry ) === false ) {
  433. console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
  434. return;
  435. }
  436. if ( offset === undefined ) offset = 0;
  437. var attributes = this.attributes;
  438. for ( var key in attributes ) {
  439. if ( geometry.attributes[ key ] === undefined ) continue;
  440. var attribute1 = attributes[ key ];
  441. var attributeArray1 = attribute1.array;
  442. var attribute2 = geometry.attributes[ key ];
  443. var attributeArray2 = attribute2.array;
  444. var attributeSize = attribute2.itemSize;
  445. for ( var i = 0, j = attributeSize * offset; i < attributeArray2.length; i ++, j ++ ) {
  446. attributeArray1[ j ] = attributeArray2[ i ];
  447. }
  448. }
  449. return this;
  450. },
  451. normalizeNormals: function () {
  452. var normals = this.attributes.normal.array;
  453. var x, y, z, n;
  454. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  455. x = normals[ i ];
  456. y = normals[ i + 1 ];
  457. z = normals[ i + 2 ];
  458. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  459. normals[ i ] *= n;
  460. normals[ i + 1 ] *= n;
  461. normals[ i + 2 ] *= n;
  462. }
  463. },
  464. toNonIndexed: function () {
  465. if ( this.index === null ) {
  466. console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
  467. return this;
  468. }
  469. var geometry2 = new BufferGeometry();
  470. var indices = this.index.array;
  471. var attributes = this.attributes;
  472. for ( var name in attributes ) {
  473. var attribute = attributes[ name ];
  474. var array = attribute.array;
  475. var itemSize = attribute.itemSize;
  476. var array2 = new array.constructor( indices.length * itemSize );
  477. var index = 0, index2 = 0;
  478. for ( var i = 0, l = indices.length; i < l; i ++ ) {
  479. index = indices[ i ] * itemSize;
  480. for ( var j = 0; j < itemSize; j ++ ) {
  481. array2[ index2 ++ ] = array[ index ++ ];
  482. }
  483. }
  484. geometry2.addAttribute( name, new BufferAttribute( array2, itemSize ) );
  485. }
  486. return geometry2;
  487. },
  488. toJSON: function () {
  489. var data = {
  490. metadata: {
  491. version: 4.4,
  492. type: 'BufferGeometry',
  493. generator: 'BufferGeometry.toJSON'
  494. }
  495. };
  496. // standard BufferGeometry serialization
  497. data.uuid = this.uuid;
  498. data.type = this.type;
  499. if ( this.name !== '' ) data.name = this.name;
  500. if ( this.parameters !== undefined ) {
  501. var parameters = this.parameters;
  502. for ( var key in parameters ) {
  503. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  504. }
  505. return data;
  506. }
  507. data.data = { attributes: {} };
  508. var index = this.index;
  509. if ( index !== null ) {
  510. var array = Array.prototype.slice.call( index.array );
  511. data.data.index = {
  512. type: index.array.constructor.name,
  513. array: array
  514. };
  515. }
  516. var attributes = this.attributes;
  517. for ( var key in attributes ) {
  518. var attribute = attributes[ key ];
  519. var array = Array.prototype.slice.call( attribute.array );
  520. data.data.attributes[ key ] = {
  521. itemSize: attribute.itemSize,
  522. type: attribute.array.constructor.name,
  523. array: array,
  524. normalized: attribute.normalized
  525. };
  526. }
  527. var groups = this.groups;
  528. if ( groups.length > 0 ) {
  529. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  530. }
  531. var boundingSphere = this.boundingSphere;
  532. if ( boundingSphere !== null ) {
  533. data.data.boundingSphere = {
  534. center: boundingSphere.center.toArray(),
  535. radius: boundingSphere.radius
  536. };
  537. }
  538. return data;
  539. },
  540. clone: function () {
  541. /*
  542. // Handle primitives
  543. var parameters = this.parameters;
  544. if ( parameters !== undefined ) {
  545. var values = [];
  546. for ( var key in parameters ) {
  547. values.push( parameters[ key ] );
  548. }
  549. var geometry = Object.create( this.constructor.prototype );
  550. this.constructor.apply( geometry, values );
  551. return geometry;
  552. }
  553. return new this.constructor().copy( this );
  554. */
  555. return new BufferGeometry().copy( this );
  556. },
  557. copy: function ( source ) {
  558. var index = source.index;
  559. if ( index !== null ) {
  560. this.setIndex( index.clone() );
  561. }
  562. var attributes = source.attributes;
  563. for ( var name in attributes ) {
  564. var attribute = attributes[ name ];
  565. this.addAttribute( name, attribute.clone() );
  566. }
  567. var groups = source.groups;
  568. for ( var i = 0, l = groups.length; i < l; i ++ ) {
  569. var group = groups[ i ];
  570. this.addGroup( group.start, group.count, group.materialIndex );
  571. }
  572. return this;
  573. },
  574. dispose: function () {
  575. this.dispatchEvent( { type: 'dispose' } );
  576. }
  577. } );
  578. BufferGeometry.MaxIndex = 65535;
  579. export { BufferGeometry };