BufferGeometry.js 21 KB

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