BufferGeometry.js 21 KB

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