BufferGeometry.js 20 KB

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