BufferGeometry.js 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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 positions = this.attributes.position.array;
  317. if ( positions !== undefined ) {
  318. this.boundingBox.setFromArray( positions );
  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 positions = this.attributes.position;
  334. if ( positions ) {
  335. var array = positions.array;
  336. var center = this.boundingSphere.center;
  337. box.setFromArray( array );
  338. box.getCenter( center );
  339. // hoping to find a boundingSphere with a radius smaller than the
  340. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  341. var maxRadiusSq = 0;
  342. for ( var i = 0, il = array.length; i < il; i += 3 ) {
  343. vector.fromArray( array, i );
  344. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  345. }
  346. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  347. if ( isNaN( this.boundingSphere.radius ) ) {
  348. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  349. }
  350. }
  351. };
  352. }(),
  353. computeFaceNormals: function () {
  354. // backwards compatibility
  355. },
  356. computeVertexNormals: function () {
  357. var index = this.index;
  358. var attributes = this.attributes;
  359. var groups = this.groups;
  360. if ( attributes.position ) {
  361. var positions = attributes.position.array;
  362. if ( attributes.normal === undefined ) {
  363. this.addAttribute( 'normal', new BufferAttribute( new Float32Array( positions.length ), 3 ) );
  364. } else {
  365. // reset existing normals to zero
  366. var array = attributes.normal.array;
  367. for ( var i = 0, il = array.length; i < il; i ++ ) {
  368. array[ i ] = 0;
  369. }
  370. }
  371. var normals = attributes.normal.array;
  372. var vA, vB, vC,
  373. pA = new Vector3(),
  374. pB = new Vector3(),
  375. pC = new Vector3(),
  376. cb = new Vector3(),
  377. ab = new Vector3();
  378. // indexed elements
  379. if ( index ) {
  380. var indices = index.array;
  381. if ( groups.length === 0 ) {
  382. this.addGroup( 0, indices.length );
  383. }
  384. for ( var j = 0, jl = groups.length; j < jl; ++ j ) {
  385. var group = groups[ j ];
  386. var start = group.start;
  387. var count = group.count;
  388. for ( var i = start, il = start + count; i < il; i += 3 ) {
  389. vA = indices[ i + 0 ] * 3;
  390. vB = indices[ i + 1 ] * 3;
  391. vC = indices[ i + 2 ] * 3;
  392. pA.fromArray( positions, vA );
  393. pB.fromArray( positions, vB );
  394. pC.fromArray( positions, vC );
  395. cb.subVectors( pC, pB );
  396. ab.subVectors( pA, pB );
  397. cb.cross( ab );
  398. normals[ vA ] += cb.x;
  399. normals[ vA + 1 ] += cb.y;
  400. normals[ vA + 2 ] += cb.z;
  401. normals[ vB ] += cb.x;
  402. normals[ vB + 1 ] += cb.y;
  403. normals[ vB + 2 ] += cb.z;
  404. normals[ vC ] += cb.x;
  405. normals[ vC + 1 ] += cb.y;
  406. normals[ vC + 2 ] += cb.z;
  407. }
  408. }
  409. } else {
  410. // non-indexed elements (unconnected triangle soup)
  411. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  412. pA.fromArray( positions, i );
  413. pB.fromArray( positions, i + 3 );
  414. pC.fromArray( positions, i + 6 );
  415. cb.subVectors( pC, pB );
  416. ab.subVectors( pA, pB );
  417. cb.cross( ab );
  418. normals[ i ] = cb.x;
  419. normals[ i + 1 ] = cb.y;
  420. normals[ i + 2 ] = cb.z;
  421. normals[ i + 3 ] = cb.x;
  422. normals[ i + 4 ] = cb.y;
  423. normals[ i + 5 ] = cb.z;
  424. normals[ i + 6 ] = cb.x;
  425. normals[ i + 7 ] = cb.y;
  426. normals[ i + 8 ] = cb.z;
  427. }
  428. }
  429. this.normalizeNormals();
  430. attributes.normal.needsUpdate = true;
  431. }
  432. },
  433. merge: function ( geometry, offset ) {
  434. if ( (geometry && geometry.isBufferGeometry) === false ) {
  435. console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
  436. return;
  437. }
  438. if ( offset === undefined ) offset = 0;
  439. var attributes = this.attributes;
  440. for ( var key in attributes ) {
  441. if ( geometry.attributes[ key ] === undefined ) continue;
  442. var attribute1 = attributes[ key ];
  443. var attributeArray1 = attribute1.array;
  444. var attribute2 = geometry.attributes[ key ];
  445. var attributeArray2 = attribute2.array;
  446. var attributeSize = attribute2.itemSize;
  447. for ( var i = 0, j = attributeSize * offset; i < attributeArray2.length; i ++, j ++ ) {
  448. attributeArray1[ j ] = attributeArray2[ i ];
  449. }
  450. }
  451. return this;
  452. },
  453. normalizeNormals: function () {
  454. var normals = this.attributes.normal.array;
  455. var x, y, z, n;
  456. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  457. x = normals[ i ];
  458. y = normals[ i + 1 ];
  459. z = normals[ i + 2 ];
  460. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  461. normals[ i ] *= n;
  462. normals[ i + 1 ] *= n;
  463. normals[ i + 2 ] *= n;
  464. }
  465. },
  466. toNonIndexed: function () {
  467. if ( this.index === null ) {
  468. console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
  469. return this;
  470. }
  471. var geometry2 = new BufferGeometry();
  472. var indices = this.index.array;
  473. var attributes = this.attributes;
  474. for ( var name in attributes ) {
  475. var attribute = attributes[ name ];
  476. var array = attribute.array;
  477. var itemSize = attribute.itemSize;
  478. var array2 = new array.constructor( indices.length * itemSize );
  479. var index = 0, index2 = 0;
  480. for ( var i = 0, l = indices.length; i < l; i ++ ) {
  481. index = indices[ i ] * itemSize;
  482. for ( var j = 0; j < itemSize; j ++ ) {
  483. array2[ index2 ++ ] = array[ index ++ ];
  484. }
  485. }
  486. geometry2.addAttribute( name, new BufferAttribute( array2, itemSize ) );
  487. }
  488. return geometry2;
  489. },
  490. toJSON: function () {
  491. var data = {
  492. metadata: {
  493. version: 4.4,
  494. type: 'BufferGeometry',
  495. generator: 'BufferGeometry.toJSON'
  496. }
  497. };
  498. // standard BufferGeometry serialization
  499. data.uuid = this.uuid;
  500. data.type = this.type;
  501. if ( this.name !== '' ) data.name = this.name;
  502. if ( this.parameters !== undefined ) {
  503. var parameters = this.parameters;
  504. for ( var key in parameters ) {
  505. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  506. }
  507. return data;
  508. }
  509. data.data = { attributes: {} };
  510. var index = this.index;
  511. if ( index !== null ) {
  512. var array = Array.prototype.slice.call( index.array );
  513. data.data.index = {
  514. type: index.array.constructor.name,
  515. array: array
  516. };
  517. }
  518. var attributes = this.attributes;
  519. for ( var key in attributes ) {
  520. var attribute = attributes[ key ];
  521. var array = Array.prototype.slice.call( attribute.array );
  522. data.data.attributes[ key ] = {
  523. itemSize: attribute.itemSize,
  524. type: attribute.array.constructor.name,
  525. array: array,
  526. normalized: attribute.normalized
  527. };
  528. }
  529. var groups = this.groups;
  530. if ( groups.length > 0 ) {
  531. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  532. }
  533. var boundingSphere = this.boundingSphere;
  534. if ( boundingSphere !== null ) {
  535. data.data.boundingSphere = {
  536. center: boundingSphere.center.toArray(),
  537. radius: boundingSphere.radius
  538. };
  539. }
  540. return data;
  541. },
  542. clone: function () {
  543. /*
  544. // Handle primitives
  545. var parameters = this.parameters;
  546. if ( parameters !== undefined ) {
  547. var values = [];
  548. for ( var key in parameters ) {
  549. values.push( parameters[ key ] );
  550. }
  551. var geometry = Object.create( this.constructor.prototype );
  552. this.constructor.apply( geometry, values );
  553. return geometry;
  554. }
  555. return new this.constructor().copy( this );
  556. */
  557. return new BufferGeometry().copy( this );
  558. },
  559. copy: function ( source ) {
  560. var index = source.index;
  561. if ( index !== null ) {
  562. this.setIndex( index.clone() );
  563. }
  564. var attributes = source.attributes;
  565. for ( var name in attributes ) {
  566. var attribute = attributes[ name ];
  567. this.addAttribute( name, attribute.clone() );
  568. }
  569. var groups = source.groups;
  570. for ( var i = 0, l = groups.length; i < l; i ++ ) {
  571. var group = groups[ i ];
  572. this.addGroup( group.start, group.count, group.materialIndex );
  573. }
  574. return this;
  575. },
  576. dispose: function () {
  577. this.dispatchEvent( { type: 'dispose' } );
  578. }
  579. } );
  580. BufferGeometry.MaxIndex = 65535;
  581. export { BufferGeometry };
粤ICP备19079148号