BufferGeometry.js 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. import { GeometryIdCount } from './Geometry.js';
  13. /**
  14. * @author alteredq / http://alteredqualia.com/
  15. * @author mrdoob / http://mrdoob.com/
  16. */
  17. function BufferGeometry() {
  18. Object.defineProperty( this, 'id', { value: GeometryIdCount() } );
  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. updateFromObject: function ( object ) {
  182. var geometry = object.geometry;
  183. if ( object.isMesh ) {
  184. var direct = geometry.__directGeometry;
  185. if ( geometry.elementsNeedUpdate === true ) {
  186. direct = undefined;
  187. geometry.elementsNeedUpdate = false;
  188. }
  189. if ( direct === undefined ) {
  190. return this.fromGeometry( geometry );
  191. }
  192. direct.verticesNeedUpdate = geometry.verticesNeedUpdate;
  193. direct.normalsNeedUpdate = geometry.normalsNeedUpdate;
  194. direct.colorsNeedUpdate = geometry.colorsNeedUpdate;
  195. direct.uvsNeedUpdate = geometry.uvsNeedUpdate;
  196. direct.groupsNeedUpdate = geometry.groupsNeedUpdate;
  197. geometry.verticesNeedUpdate = false;
  198. geometry.normalsNeedUpdate = false;
  199. geometry.colorsNeedUpdate = false;
  200. geometry.uvsNeedUpdate = false;
  201. geometry.groupsNeedUpdate = false;
  202. geometry = direct;
  203. }
  204. var attribute;
  205. if ( geometry.verticesNeedUpdate === true ) {
  206. attribute = this.attributes.position;
  207. if ( attribute !== undefined ) {
  208. attribute.copyVector3sArray( geometry.vertices );
  209. attribute.needsUpdate = true;
  210. }
  211. geometry.verticesNeedUpdate = false;
  212. }
  213. if ( geometry.normalsNeedUpdate === true ) {
  214. attribute = this.attributes.normal;
  215. if ( attribute !== undefined ) {
  216. attribute.copyVector3sArray( geometry.normals );
  217. attribute.needsUpdate = true;
  218. }
  219. geometry.normalsNeedUpdate = false;
  220. }
  221. if ( geometry.colorsNeedUpdate === true ) {
  222. attribute = this.attributes.color;
  223. if ( attribute !== undefined ) {
  224. attribute.copyColorsArray( geometry.colors );
  225. attribute.needsUpdate = true;
  226. }
  227. geometry.colorsNeedUpdate = false;
  228. }
  229. if ( geometry.uvsNeedUpdate ) {
  230. attribute = this.attributes.uv;
  231. if ( attribute !== undefined ) {
  232. attribute.copyVector2sArray( geometry.uvs );
  233. attribute.needsUpdate = true;
  234. }
  235. geometry.uvsNeedUpdate = false;
  236. }
  237. if ( geometry.lineDistancesNeedUpdate ) {
  238. attribute = this.attributes.lineDistance;
  239. if ( attribute !== undefined ) {
  240. attribute.copyArray( geometry.lineDistances );
  241. attribute.needsUpdate = true;
  242. }
  243. geometry.lineDistancesNeedUpdate = false;
  244. }
  245. if ( geometry.groupsNeedUpdate ) {
  246. geometry.computeGroups( object.geometry );
  247. this.groups = geometry.groups;
  248. geometry.groupsNeedUpdate = false;
  249. }
  250. return this;
  251. },
  252. fromGeometry: function ( geometry ) {
  253. geometry.__directGeometry = new DirectGeometry().fromGeometry( geometry );
  254. return this.fromDirectGeometry( geometry.__directGeometry );
  255. },
  256. fromDirectGeometry: function ( geometry ) {
  257. var positions = new Float32Array( geometry.vertices.length * 3 );
  258. this.addAttribute( 'position', new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
  259. if ( geometry.normals.length > 0 ) {
  260. var normals = new Float32Array( geometry.normals.length * 3 );
  261. this.addAttribute( 'normal', new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
  262. }
  263. if ( geometry.colors.length > 0 ) {
  264. var colors = new Float32Array( geometry.colors.length * 3 );
  265. this.addAttribute( 'color', new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
  266. }
  267. if ( geometry.uvs.length > 0 ) {
  268. var uvs = new Float32Array( geometry.uvs.length * 2 );
  269. this.addAttribute( 'uv', new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
  270. }
  271. if ( geometry.uvs2.length > 0 ) {
  272. var uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  273. this.addAttribute( 'uv2', new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
  274. }
  275. if ( geometry.indices.length > 0 ) {
  276. var TypeArray = arrayMax( geometry.indices ) > 65535 ? Uint32Array : Uint16Array;
  277. var indices = new TypeArray( geometry.indices.length * 3 );
  278. this.setIndex( new BufferAttribute( indices, 1 ).copyIndicesArray( geometry.indices ) );
  279. }
  280. // groups
  281. this.groups = geometry.groups;
  282. // morphs
  283. for ( var name in geometry.morphTargets ) {
  284. var array = [];
  285. var morphTargets = geometry.morphTargets[ name ];
  286. for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {
  287. var morphTarget = morphTargets[ i ];
  288. var attribute = new Float32BufferAttribute( morphTarget.length * 3, 3 );
  289. array.push( attribute.copyVector3sArray( morphTarget ) );
  290. }
  291. this.morphAttributes[ name ] = array;
  292. }
  293. // skinning
  294. if ( geometry.skinIndices.length > 0 ) {
  295. var skinIndices = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
  296. this.addAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
  297. }
  298. if ( geometry.skinWeights.length > 0 ) {
  299. var skinWeights = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
  300. this.addAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
  301. }
  302. //
  303. if ( geometry.boundingSphere !== null ) {
  304. this.boundingSphere = geometry.boundingSphere.clone();
  305. }
  306. if ( geometry.boundingBox !== null ) {
  307. this.boundingBox = geometry.boundingBox.clone();
  308. }
  309. return this;
  310. },
  311. computeBoundingBox: function () {
  312. if ( this.boundingBox === null ) {
  313. this.boundingBox = new Box3();
  314. }
  315. var position = this.attributes.position;
  316. if ( position !== undefined ) {
  317. this.boundingBox.setFromBufferAttribute( position );
  318. } else {
  319. this.boundingBox.makeEmpty();
  320. }
  321. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  322. console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  323. }
  324. },
  325. computeBoundingSphere: function () {
  326. var box = new Box3();
  327. var vector = new Vector3();
  328. return function computeBoundingSphere() {
  329. if ( this.boundingSphere === null ) {
  330. this.boundingSphere = new Sphere();
  331. }
  332. var position = this.attributes.position;
  333. if ( position ) {
  334. var center = this.boundingSphere.center;
  335. box.setFromBufferAttribute( position );
  336. box.getCenter( center );
  337. // hoping to find a boundingSphere with a radius smaller than the
  338. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  339. var maxRadiusSq = 0;
  340. for ( var i = 0, il = position.count; i < il; i ++ ) {
  341. vector.x = position.getX( i );
  342. vector.y = position.getY( i );
  343. vector.z = position.getZ( 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. var pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
  374. var cb = new Vector3(), ab = new Vector3();
  375. // indexed elements
  376. if ( index ) {
  377. var indices = index.array;
  378. if ( groups.length === 0 ) {
  379. this.addGroup( 0, indices.length );
  380. }
  381. for ( var j = 0, jl = groups.length; j < jl; ++ j ) {
  382. var group = groups[ j ];
  383. var start = group.start;
  384. var count = group.count;
  385. for ( var i = start, il = start + count; i < il; i += 3 ) {
  386. vA = indices[ i + 0 ] * 3;
  387. vB = indices[ i + 1 ] * 3;
  388. vC = indices[ i + 2 ] * 3;
  389. pA.fromArray( positions, vA );
  390. pB.fromArray( positions, vB );
  391. pC.fromArray( positions, vC );
  392. cb.subVectors( pC, pB );
  393. ab.subVectors( pA, pB );
  394. cb.cross( ab );
  395. normals[ vA ] += cb.x;
  396. normals[ vA + 1 ] += cb.y;
  397. normals[ vA + 2 ] += cb.z;
  398. normals[ vB ] += cb.x;
  399. normals[ vB + 1 ] += cb.y;
  400. normals[ vB + 2 ] += cb.z;
  401. normals[ vC ] += cb.x;
  402. normals[ vC + 1 ] += cb.y;
  403. normals[ vC + 2 ] += cb.z;
  404. }
  405. }
  406. } else {
  407. // non-indexed elements (unconnected triangle soup)
  408. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  409. pA.fromArray( positions, i );
  410. pB.fromArray( positions, i + 3 );
  411. pC.fromArray( positions, i + 6 );
  412. cb.subVectors( pC, pB );
  413. ab.subVectors( pA, pB );
  414. cb.cross( ab );
  415. normals[ i ] = cb.x;
  416. normals[ i + 1 ] = cb.y;
  417. normals[ i + 2 ] = cb.z;
  418. normals[ i + 3 ] = cb.x;
  419. normals[ i + 4 ] = cb.y;
  420. normals[ i + 5 ] = cb.z;
  421. normals[ i + 6 ] = cb.x;
  422. normals[ i + 7 ] = cb.y;
  423. normals[ i + 8 ] = cb.z;
  424. }
  425. }
  426. this.normalizeNormals();
  427. attributes.normal.needsUpdate = true;
  428. }
  429. },
  430. merge: function ( geometry, offset ) {
  431. if ( ! ( geometry && geometry.isBufferGeometry ) ) {
  432. console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
  433. return;
  434. }
  435. if ( offset === undefined ) offset = 0;
  436. var attributes = this.attributes;
  437. for ( var key in attributes ) {
  438. if ( geometry.attributes[ key ] === undefined ) continue;
  439. var attribute1 = attributes[ key ];
  440. var attributeArray1 = attribute1.array;
  441. var attribute2 = geometry.attributes[ key ];
  442. var attributeArray2 = attribute2.array;
  443. var attributeSize = attribute2.itemSize;
  444. for ( var i = 0, j = attributeSize * offset; i < attributeArray2.length; i ++, j ++ ) {
  445. attributeArray1[ j ] = attributeArray2[ i ];
  446. }
  447. }
  448. return this;
  449. },
  450. normalizeNormals: function () {
  451. var vector = new Vector3();
  452. return function normalizeNormals() {
  453. var normals = this.attributes.normal;
  454. for ( var i = 0, il = normals.count; i < il; i ++ ) {
  455. vector.x = normals.getX( i );
  456. vector.y = normals.getY( i );
  457. vector.z = normals.getZ( i );
  458. vector.normalize();
  459. normals.setXYZ( i, vector.x, vector.y, vector.z );
  460. }
  461. };
  462. }(),
  463. toNonIndexed: function () {
  464. if ( this.index === null ) {
  465. console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
  466. return this;
  467. }
  468. var geometry2 = new BufferGeometry();
  469. var indices = this.index.array;
  470. var attributes = this.attributes;
  471. for ( var name in attributes ) {
  472. var attribute = attributes[ name ];
  473. var array = attribute.array;
  474. var itemSize = attribute.itemSize;
  475. var array2 = new array.constructor( indices.length * itemSize );
  476. var index = 0, index2 = 0;
  477. for ( var i = 0, l = indices.length; i < l; i ++ ) {
  478. index = indices[ i ] * itemSize;
  479. for ( var j = 0; j < itemSize; j ++ ) {
  480. array2[ index2 ++ ] = array[ index ++ ];
  481. }
  482. }
  483. geometry2.addAttribute( name, new BufferAttribute( array2, itemSize ) );
  484. }
  485. return geometry2;
  486. },
  487. toJSON: function () {
  488. var data = {
  489. metadata: {
  490. version: 4.5,
  491. type: 'BufferGeometry',
  492. generator: 'BufferGeometry.toJSON'
  493. }
  494. };
  495. // standard BufferGeometry serialization
  496. data.uuid = this.uuid;
  497. data.type = this.type;
  498. if ( this.name !== '' ) data.name = this.name;
  499. if ( this.parameters !== undefined ) {
  500. var parameters = this.parameters;
  501. for ( var key in parameters ) {
  502. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  503. }
  504. return data;
  505. }
  506. data.data = { attributes: {} };
  507. var index = this.index;
  508. if ( index !== null ) {
  509. var array = Array.prototype.slice.call( index.array );
  510. data.data.index = {
  511. type: index.array.constructor.name,
  512. array: array
  513. };
  514. }
  515. var attributes = this.attributes;
  516. for ( var key in attributes ) {
  517. var attribute = attributes[ key ];
  518. var array = Array.prototype.slice.call( attribute.array );
  519. data.data.attributes[ key ] = {
  520. itemSize: attribute.itemSize,
  521. type: attribute.array.constructor.name,
  522. array: array,
  523. normalized: attribute.normalized
  524. };
  525. }
  526. var groups = this.groups;
  527. if ( groups.length > 0 ) {
  528. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  529. }
  530. var boundingSphere = this.boundingSphere;
  531. if ( boundingSphere !== null ) {
  532. data.data.boundingSphere = {
  533. center: boundingSphere.center.toArray(),
  534. radius: boundingSphere.radius
  535. };
  536. }
  537. return data;
  538. },
  539. clone: function () {
  540. /*
  541. // Handle primitives
  542. var parameters = this.parameters;
  543. if ( parameters !== undefined ) {
  544. var values = [];
  545. for ( var key in parameters ) {
  546. values.push( parameters[ key ] );
  547. }
  548. var geometry = Object.create( this.constructor.prototype );
  549. this.constructor.apply( geometry, values );
  550. return geometry;
  551. }
  552. return new this.constructor().copy( this );
  553. */
  554. return new BufferGeometry().copy( this );
  555. },
  556. copy: function ( source ) {
  557. var name, i, l;
  558. // reset
  559. this.index = null;
  560. this.attributes = {};
  561. this.morphAttributes = {};
  562. this.groups = [];
  563. this.boundingBox = null;
  564. this.boundingSphere = null;
  565. // name
  566. this.name = source.name;
  567. // index
  568. var index = source.index;
  569. if ( index !== null ) {
  570. this.setIndex( index.clone() );
  571. }
  572. // attributes
  573. var attributes = source.attributes;
  574. for ( name in attributes ) {
  575. var attribute = attributes[ name ];
  576. this.addAttribute( name, attribute.clone() );
  577. }
  578. // morph attributes
  579. var morphAttributes = source.morphAttributes;
  580. for ( name in morphAttributes ) {
  581. var array = [];
  582. var morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  583. for ( i = 0, l = morphAttribute.length; i < l; i ++ ) {
  584. array.push( morphAttribute[ i ].clone() );
  585. }
  586. this.morphAttributes[ name ] = array;
  587. }
  588. // groups
  589. var groups = source.groups;
  590. for ( i = 0, l = groups.length; i < l; i ++ ) {
  591. var group = groups[ i ];
  592. this.addGroup( group.start, group.count, group.materialIndex );
  593. }
  594. // bounding box
  595. var boundingBox = source.boundingBox;
  596. if ( boundingBox !== null ) {
  597. this.boundingBox = boundingBox.clone();
  598. }
  599. // bounding sphere
  600. var boundingSphere = source.boundingSphere;
  601. if ( boundingSphere !== null ) {
  602. this.boundingSphere = boundingSphere.clone();
  603. }
  604. // draw range
  605. this.drawRange.start = source.drawRange.start;
  606. this.drawRange.count = source.drawRange.count;
  607. return this;
  608. },
  609. dispose: function () {
  610. this.dispatchEvent( { type: 'dispose' } );
  611. }
  612. } );
  613. export { BufferGeometry };
粤ICP备19079148号