BufferGeometry.js 21 KB

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