BufferGeometry.js 22 KB

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