BufferGeometry.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Vector2 } from '../math/Vector2.js';
  3. import { Box3 } from '../math/Box3.js';
  4. import { EventDispatcher } from './EventDispatcher.js';
  5. import { BufferAttribute, Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from './BufferAttribute.js';
  6. import { Sphere } from '../math/Sphere.js';
  7. import { Object3D } from './Object3D.js';
  8. import { Matrix4 } from '../math/Matrix4.js';
  9. import { Matrix3 } from '../math/Matrix3.js';
  10. import * as MathUtils from '../math/MathUtils.js';
  11. import { arrayMax } from '../utils.js';
  12. let _id = 0;
  13. const _m1 = new /*@__PURE__*/ Matrix4();
  14. const _obj = new /*@__PURE__*/ Object3D();
  15. const _offset = new /*@__PURE__*/ Vector3();
  16. const _box = new /*@__PURE__*/ Box3();
  17. const _boxMorphTargets = new /*@__PURE__*/ Box3();
  18. const _vector = new /*@__PURE__*/ Vector3();
  19. class BufferGeometry extends EventDispatcher {
  20. constructor() {
  21. super();
  22. Object.defineProperty( this, 'id', { value: _id ++ } );
  23. this.uuid = MathUtils.generateUUID();
  24. this.name = '';
  25. this.type = 'BufferGeometry';
  26. this.index = null;
  27. this.attributes = {};
  28. this.morphAttributes = {};
  29. this.morphTargetsRelative = false;
  30. this.groups = [];
  31. this.boundingBox = null;
  32. this.boundingSphere = null;
  33. this.drawRange = { start: 0, count: Infinity };
  34. this.userData = {};
  35. }
  36. getIndex() {
  37. return this.index;
  38. }
  39. setIndex( index ) {
  40. if ( Array.isArray( index ) ) {
  41. this.index = new ( arrayMax( index ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
  42. } else {
  43. this.index = index;
  44. }
  45. return this;
  46. }
  47. getAttribute( name ) {
  48. return this.attributes[ name ];
  49. }
  50. setAttribute( name, attribute ) {
  51. this.attributes[ name ] = attribute;
  52. return this;
  53. }
  54. deleteAttribute( name ) {
  55. delete this.attributes[ name ];
  56. return this;
  57. }
  58. hasAttribute( name ) {
  59. return this.attributes[ name ] !== undefined;
  60. }
  61. addGroup( start, count, materialIndex = 0 ) {
  62. this.groups.push( {
  63. start: start,
  64. count: count,
  65. materialIndex: materialIndex
  66. } );
  67. }
  68. clearGroups() {
  69. this.groups = [];
  70. }
  71. setDrawRange( start, count ) {
  72. this.drawRange.start = start;
  73. this.drawRange.count = count;
  74. }
  75. applyMatrix4( matrix ) {
  76. const position = this.attributes.position;
  77. if ( position !== undefined ) {
  78. position.applyMatrix4( matrix );
  79. position.needsUpdate = true;
  80. }
  81. const normal = this.attributes.normal;
  82. if ( normal !== undefined ) {
  83. const normalMatrix = new Matrix3().getNormalMatrix( matrix );
  84. normal.applyNormalMatrix( normalMatrix );
  85. normal.needsUpdate = true;
  86. }
  87. const tangent = this.attributes.tangent;
  88. if ( tangent !== undefined ) {
  89. tangent.transformDirection( matrix );
  90. tangent.needsUpdate = true;
  91. }
  92. if ( this.boundingBox !== null ) {
  93. this.computeBoundingBox();
  94. }
  95. if ( this.boundingSphere !== null ) {
  96. this.computeBoundingSphere();
  97. }
  98. return this;
  99. }
  100. rotateX( angle ) {
  101. // rotate geometry around world x-axis
  102. _m1.makeRotationX( angle );
  103. this.applyMatrix4( _m1 );
  104. return this;
  105. }
  106. rotateY( angle ) {
  107. // rotate geometry around world y-axis
  108. _m1.makeRotationY( angle );
  109. this.applyMatrix4( _m1 );
  110. return this;
  111. }
  112. rotateZ( angle ) {
  113. // rotate geometry around world z-axis
  114. _m1.makeRotationZ( angle );
  115. this.applyMatrix4( _m1 );
  116. return this;
  117. }
  118. translate( x, y, z ) {
  119. // translate geometry
  120. _m1.makeTranslation( x, y, z );
  121. this.applyMatrix4( _m1 );
  122. return this;
  123. }
  124. scale( x, y, z ) {
  125. // scale geometry
  126. _m1.makeScale( x, y, z );
  127. this.applyMatrix4( _m1 );
  128. return this;
  129. }
  130. lookAt( vector ) {
  131. _obj.lookAt( vector );
  132. _obj.updateMatrix();
  133. this.applyMatrix4( _obj.matrix );
  134. return this;
  135. }
  136. center() {
  137. this.computeBoundingBox();
  138. this.boundingBox.getCenter( _offset ).negate();
  139. this.translate( _offset.x, _offset.y, _offset.z );
  140. return this;
  141. }
  142. setFromPoints( points ) {
  143. const position = [];
  144. for ( let i = 0, l = points.length; i < l; i ++ ) {
  145. const point = points[ i ];
  146. position.push( point.x, point.y, point.z || 0 );
  147. }
  148. this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  149. return this;
  150. }
  151. computeBoundingBox() {
  152. if ( this.boundingBox === null ) {
  153. this.boundingBox = new Box3();
  154. }
  155. const position = this.attributes.position;
  156. const morphAttributesPosition = this.morphAttributes.position;
  157. if ( position && position.isGLBufferAttribute ) {
  158. console.error( 'THREE.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box. Alternatively set "mesh.frustumCulled" to "false".', this );
  159. this.boundingBox.set(
  160. new Vector3( - Infinity, - Infinity, - Infinity ),
  161. new Vector3( + Infinity, + Infinity, + Infinity )
  162. );
  163. return;
  164. }
  165. if ( position !== undefined ) {
  166. this.boundingBox.setFromBufferAttribute( position );
  167. // process morph attributes if present
  168. if ( morphAttributesPosition ) {
  169. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  170. const morphAttribute = morphAttributesPosition[ i ];
  171. _box.setFromBufferAttribute( morphAttribute );
  172. if ( this.morphTargetsRelative ) {
  173. _vector.addVectors( this.boundingBox.min, _box.min );
  174. this.boundingBox.expandByPoint( _vector );
  175. _vector.addVectors( this.boundingBox.max, _box.max );
  176. this.boundingBox.expandByPoint( _vector );
  177. } else {
  178. this.boundingBox.expandByPoint( _box.min );
  179. this.boundingBox.expandByPoint( _box.max );
  180. }
  181. }
  182. }
  183. } else {
  184. this.boundingBox.makeEmpty();
  185. }
  186. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  187. console.error( 'THREE.BufferGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  188. }
  189. }
  190. computeBoundingSphere() {
  191. if ( this.boundingSphere === null ) {
  192. this.boundingSphere = new Sphere();
  193. }
  194. const position = this.attributes.position;
  195. const morphAttributesPosition = this.morphAttributes.position;
  196. if ( position && position.isGLBufferAttribute ) {
  197. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere. Alternatively set "mesh.frustumCulled" to "false".', this );
  198. this.boundingSphere.set( new Vector3(), Infinity );
  199. return;
  200. }
  201. if ( position ) {
  202. // first, find the center of the bounding sphere
  203. const center = this.boundingSphere.center;
  204. _box.setFromBufferAttribute( position );
  205. // process morph attributes if present
  206. if ( morphAttributesPosition ) {
  207. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  208. const morphAttribute = morphAttributesPosition[ i ];
  209. _boxMorphTargets.setFromBufferAttribute( morphAttribute );
  210. if ( this.morphTargetsRelative ) {
  211. _vector.addVectors( _box.min, _boxMorphTargets.min );
  212. _box.expandByPoint( _vector );
  213. _vector.addVectors( _box.max, _boxMorphTargets.max );
  214. _box.expandByPoint( _vector );
  215. } else {
  216. _box.expandByPoint( _boxMorphTargets.min );
  217. _box.expandByPoint( _boxMorphTargets.max );
  218. }
  219. }
  220. }
  221. _box.getCenter( center );
  222. // second, try to find a boundingSphere with a radius smaller than the
  223. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  224. let maxRadiusSq = 0;
  225. for ( let i = 0, il = position.count; i < il; i ++ ) {
  226. _vector.fromBufferAttribute( position, i );
  227. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  228. }
  229. // process morph attributes if present
  230. if ( morphAttributesPosition ) {
  231. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  232. const morphAttribute = morphAttributesPosition[ i ];
  233. const morphTargetsRelative = this.morphTargetsRelative;
  234. for ( let j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
  235. _vector.fromBufferAttribute( morphAttribute, j );
  236. if ( morphTargetsRelative ) {
  237. _offset.fromBufferAttribute( position, j );
  238. _vector.add( _offset );
  239. }
  240. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
  241. }
  242. }
  243. }
  244. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  245. if ( isNaN( this.boundingSphere.radius ) ) {
  246. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  247. }
  248. }
  249. }
  250. computeFaceNormals() {
  251. // backwards compatibility
  252. }
  253. computeTangents() {
  254. const index = this.index;
  255. const attributes = this.attributes;
  256. // based on http://www.terathon.com/code/tangent.html
  257. // (per vertex tangents)
  258. if ( index === null ||
  259. attributes.position === undefined ||
  260. attributes.normal === undefined ||
  261. attributes.uv === undefined ) {
  262. console.error( 'THREE.BufferGeometry: .computeTangents() failed. Missing required attributes (index, position, normal or uv)' );
  263. return;
  264. }
  265. const indices = index.array;
  266. const positions = attributes.position.array;
  267. const normals = attributes.normal.array;
  268. const uvs = attributes.uv.array;
  269. const nVertices = positions.length / 3;
  270. if ( attributes.tangent === undefined ) {
  271. this.setAttribute( 'tangent', new BufferAttribute( new Float32Array( 4 * nVertices ), 4 ) );
  272. }
  273. const tangents = attributes.tangent.array;
  274. const tan1 = [], tan2 = [];
  275. for ( let i = 0; i < nVertices; i ++ ) {
  276. tan1[ i ] = new Vector3();
  277. tan2[ i ] = new Vector3();
  278. }
  279. const vA = new Vector3(),
  280. vB = new Vector3(),
  281. vC = new Vector3(),
  282. uvA = new Vector2(),
  283. uvB = new Vector2(),
  284. uvC = new Vector2(),
  285. sdir = new Vector3(),
  286. tdir = new Vector3();
  287. function handleTriangle( a, b, c ) {
  288. vA.fromArray( positions, a * 3 );
  289. vB.fromArray( positions, b * 3 );
  290. vC.fromArray( positions, c * 3 );
  291. uvA.fromArray( uvs, a * 2 );
  292. uvB.fromArray( uvs, b * 2 );
  293. uvC.fromArray( uvs, c * 2 );
  294. vB.sub( vA );
  295. vC.sub( vA );
  296. uvB.sub( uvA );
  297. uvC.sub( uvA );
  298. const r = 1.0 / ( uvB.x * uvC.y - uvC.x * uvB.y );
  299. // silently ignore degenerate uv triangles having coincident or colinear vertices
  300. if ( ! isFinite( r ) ) return;
  301. sdir.copy( vB ).multiplyScalar( uvC.y ).addScaledVector( vC, - uvB.y ).multiplyScalar( r );
  302. tdir.copy( vC ).multiplyScalar( uvB.x ).addScaledVector( vB, - uvC.x ).multiplyScalar( r );
  303. tan1[ a ].add( sdir );
  304. tan1[ b ].add( sdir );
  305. tan1[ c ].add( sdir );
  306. tan2[ a ].add( tdir );
  307. tan2[ b ].add( tdir );
  308. tan2[ c ].add( tdir );
  309. }
  310. let groups = this.groups;
  311. if ( groups.length === 0 ) {
  312. groups = [ {
  313. start: 0,
  314. count: indices.length
  315. } ];
  316. }
  317. for ( let i = 0, il = groups.length; i < il; ++ i ) {
  318. const group = groups[ i ];
  319. const start = group.start;
  320. const count = group.count;
  321. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  322. handleTriangle(
  323. indices[ j + 0 ],
  324. indices[ j + 1 ],
  325. indices[ j + 2 ]
  326. );
  327. }
  328. }
  329. const tmp = new Vector3(), tmp2 = new Vector3();
  330. const n = new Vector3(), n2 = new Vector3();
  331. function handleVertex( v ) {
  332. n.fromArray( normals, v * 3 );
  333. n2.copy( n );
  334. const t = tan1[ v ];
  335. // Gram-Schmidt orthogonalize
  336. tmp.copy( t );
  337. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  338. // Calculate handedness
  339. tmp2.crossVectors( n2, t );
  340. const test = tmp2.dot( tan2[ v ] );
  341. const w = ( test < 0.0 ) ? - 1.0 : 1.0;
  342. tangents[ v * 4 ] = tmp.x;
  343. tangents[ v * 4 + 1 ] = tmp.y;
  344. tangents[ v * 4 + 2 ] = tmp.z;
  345. tangents[ v * 4 + 3 ] = w;
  346. }
  347. for ( let i = 0, il = groups.length; i < il; ++ i ) {
  348. const group = groups[ i ];
  349. const start = group.start;
  350. const count = group.count;
  351. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  352. handleVertex( indices[ j + 0 ] );
  353. handleVertex( indices[ j + 1 ] );
  354. handleVertex( indices[ j + 2 ] );
  355. }
  356. }
  357. }
  358. computeVertexNormals() {
  359. const index = this.index;
  360. const positionAttribute = this.getAttribute( 'position' );
  361. if ( positionAttribute !== undefined ) {
  362. let normalAttribute = this.getAttribute( 'normal' );
  363. if ( normalAttribute === undefined ) {
  364. normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 );
  365. this.setAttribute( 'normal', normalAttribute );
  366. } else {
  367. // reset existing normals to zero
  368. for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) {
  369. normalAttribute.setXYZ( i, 0, 0, 0 );
  370. }
  371. }
  372. const pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
  373. const nA = new Vector3(), nB = new Vector3(), nC = new Vector3();
  374. const cb = new Vector3(), ab = new Vector3();
  375. // indexed elements
  376. if ( index ) {
  377. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  378. const vA = index.getX( i + 0 );
  379. const vB = index.getX( i + 1 );
  380. const vC = index.getX( i + 2 );
  381. pA.fromBufferAttribute( positionAttribute, vA );
  382. pB.fromBufferAttribute( positionAttribute, vB );
  383. pC.fromBufferAttribute( positionAttribute, vC );
  384. cb.subVectors( pC, pB );
  385. ab.subVectors( pA, pB );
  386. cb.cross( ab );
  387. nA.fromBufferAttribute( normalAttribute, vA );
  388. nB.fromBufferAttribute( normalAttribute, vB );
  389. nC.fromBufferAttribute( normalAttribute, vC );
  390. nA.add( cb );
  391. nB.add( cb );
  392. nC.add( cb );
  393. normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z );
  394. normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z );
  395. normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z );
  396. }
  397. } else {
  398. // non-indexed elements (unconnected triangle soup)
  399. for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) {
  400. pA.fromBufferAttribute( positionAttribute, i + 0 );
  401. pB.fromBufferAttribute( positionAttribute, i + 1 );
  402. pC.fromBufferAttribute( positionAttribute, i + 2 );
  403. cb.subVectors( pC, pB );
  404. ab.subVectors( pA, pB );
  405. cb.cross( ab );
  406. normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z );
  407. normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z );
  408. normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z );
  409. }
  410. }
  411. this.normalizeNormals();
  412. normalAttribute.needsUpdate = true;
  413. }
  414. }
  415. merge( geometry, offset ) {
  416. if ( ! ( geometry && geometry.isBufferGeometry ) ) {
  417. console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
  418. return;
  419. }
  420. if ( offset === undefined ) {
  421. offset = 0;
  422. console.warn(
  423. 'THREE.BufferGeometry.merge(): Overwriting original geometry, starting at offset=0. '
  424. + 'Use BufferGeometryUtils.mergeBufferGeometries() for lossless merge.'
  425. );
  426. }
  427. const attributes = this.attributes;
  428. for ( const key in attributes ) {
  429. if ( geometry.attributes[ key ] === undefined ) continue;
  430. const attribute1 = attributes[ key ];
  431. const attributeArray1 = attribute1.array;
  432. const attribute2 = geometry.attributes[ key ];
  433. const attributeArray2 = attribute2.array;
  434. const attributeOffset = attribute2.itemSize * offset;
  435. const length = Math.min( attributeArray2.length, attributeArray1.length - attributeOffset );
  436. for ( let i = 0, j = attributeOffset; i < length; i ++, j ++ ) {
  437. attributeArray1[ j ] = attributeArray2[ i ];
  438. }
  439. }
  440. return this;
  441. }
  442. normalizeNormals() {
  443. const normals = this.attributes.normal;
  444. for ( let i = 0, il = normals.count; i < il; i ++ ) {
  445. _vector.fromBufferAttribute( normals, i );
  446. _vector.normalize();
  447. normals.setXYZ( i, _vector.x, _vector.y, _vector.z );
  448. }
  449. }
  450. toNonIndexed() {
  451. function convertBufferAttribute( attribute, indices ) {
  452. const array = attribute.array;
  453. const itemSize = attribute.itemSize;
  454. const normalized = attribute.normalized;
  455. const array2 = new array.constructor( indices.length * itemSize );
  456. let index = 0, index2 = 0;
  457. for ( let i = 0, l = indices.length; i < l; i ++ ) {
  458. index = indices[ i ] * itemSize;
  459. for ( let j = 0; j < itemSize; j ++ ) {
  460. array2[ index2 ++ ] = array[ index ++ ];
  461. }
  462. }
  463. return new BufferAttribute( array2, itemSize, normalized );
  464. }
  465. //
  466. if ( this.index === null ) {
  467. console.warn( 'THREE.BufferGeometry.toNonIndexed(): BufferGeometry is already non-indexed.' );
  468. return this;
  469. }
  470. const geometry2 = new BufferGeometry();
  471. const indices = this.index.array;
  472. const attributes = this.attributes;
  473. // attributes
  474. for ( const name in attributes ) {
  475. const attribute = attributes[ name ];
  476. const newAttribute = convertBufferAttribute( attribute, indices );
  477. geometry2.setAttribute( name, newAttribute );
  478. }
  479. // morph attributes
  480. const morphAttributes = this.morphAttributes;
  481. for ( const name in morphAttributes ) {
  482. const morphArray = [];
  483. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  484. for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
  485. const attribute = morphAttribute[ i ];
  486. const newAttribute = convertBufferAttribute( attribute, indices );
  487. morphArray.push( newAttribute );
  488. }
  489. geometry2.morphAttributes[ name ] = morphArray;
  490. }
  491. geometry2.morphTargetsRelative = this.morphTargetsRelative;
  492. // groups
  493. const groups = this.groups;
  494. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  495. const group = groups[ i ];
  496. geometry2.addGroup( group.start, group.count, group.materialIndex );
  497. }
  498. return geometry2;
  499. }
  500. toJSON() {
  501. const data = {
  502. metadata: {
  503. version: 4.5,
  504. type: 'BufferGeometry',
  505. generator: 'BufferGeometry.toJSON'
  506. }
  507. };
  508. // standard BufferGeometry serialization
  509. data.uuid = this.uuid;
  510. data.type = this.type;
  511. if ( this.name !== '' ) data.name = this.name;
  512. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  513. if ( this.parameters !== undefined ) {
  514. const parameters = this.parameters;
  515. for ( const key in parameters ) {
  516. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  517. }
  518. return data;
  519. }
  520. // for simplicity the code assumes attributes are not shared across geometries, see #15811
  521. data.data = { attributes: {} };
  522. const index = this.index;
  523. if ( index !== null ) {
  524. data.data.index = {
  525. type: index.array.constructor.name,
  526. array: Array.prototype.slice.call( index.array )
  527. };
  528. }
  529. const attributes = this.attributes;
  530. for ( const key in attributes ) {
  531. const attribute = attributes[ key ];
  532. data.data.attributes[ key ] = attribute.toJSON( data.data );
  533. }
  534. const morphAttributes = {};
  535. let hasMorphAttributes = false;
  536. for ( const key in this.morphAttributes ) {
  537. const attributeArray = this.morphAttributes[ key ];
  538. const array = [];
  539. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  540. const attribute = attributeArray[ i ];
  541. array.push( attribute.toJSON( data.data ) );
  542. }
  543. if ( array.length > 0 ) {
  544. morphAttributes[ key ] = array;
  545. hasMorphAttributes = true;
  546. }
  547. }
  548. if ( hasMorphAttributes ) {
  549. data.data.morphAttributes = morphAttributes;
  550. data.data.morphTargetsRelative = this.morphTargetsRelative;
  551. }
  552. const groups = this.groups;
  553. if ( groups.length > 0 ) {
  554. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  555. }
  556. const boundingSphere = this.boundingSphere;
  557. if ( boundingSphere !== null ) {
  558. data.data.boundingSphere = {
  559. center: boundingSphere.center.toArray(),
  560. radius: boundingSphere.radius
  561. };
  562. }
  563. return data;
  564. }
  565. clone() {
  566. /*
  567. // Handle primitives
  568. const parameters = this.parameters;
  569. if ( parameters !== undefined ) {
  570. const values = [];
  571. for ( const key in parameters ) {
  572. values.push( parameters[ key ] );
  573. }
  574. const geometry = Object.create( this.constructor.prototype );
  575. this.constructor.apply( geometry, values );
  576. return geometry;
  577. }
  578. return new this.constructor().copy( this );
  579. */
  580. return new BufferGeometry().copy( this );
  581. }
  582. copy( source ) {
  583. // reset
  584. this.index = null;
  585. this.attributes = {};
  586. this.morphAttributes = {};
  587. this.groups = [];
  588. this.boundingBox = null;
  589. this.boundingSphere = null;
  590. // used for storing cloned, shared data
  591. const data = {};
  592. // name
  593. this.name = source.name;
  594. // index
  595. const index = source.index;
  596. if ( index !== null ) {
  597. this.setIndex( index.clone( data ) );
  598. }
  599. // attributes
  600. const attributes = source.attributes;
  601. for ( const name in attributes ) {
  602. const attribute = attributes[ name ];
  603. this.setAttribute( name, attribute.clone( data ) );
  604. }
  605. // morph attributes
  606. const morphAttributes = source.morphAttributes;
  607. for ( const name in morphAttributes ) {
  608. const array = [];
  609. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  610. for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) {
  611. array.push( morphAttribute[ i ].clone( data ) );
  612. }
  613. this.morphAttributes[ name ] = array;
  614. }
  615. this.morphTargetsRelative = source.morphTargetsRelative;
  616. // groups
  617. const groups = source.groups;
  618. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  619. const group = groups[ i ];
  620. this.addGroup( group.start, group.count, group.materialIndex );
  621. }
  622. // bounding box
  623. const boundingBox = source.boundingBox;
  624. if ( boundingBox !== null ) {
  625. this.boundingBox = boundingBox.clone();
  626. }
  627. // bounding sphere
  628. const boundingSphere = source.boundingSphere;
  629. if ( boundingSphere !== null ) {
  630. this.boundingSphere = boundingSphere.clone();
  631. }
  632. // draw range
  633. this.drawRange.start = source.drawRange.start;
  634. this.drawRange.count = source.drawRange.count;
  635. // user data
  636. this.userData = source.userData;
  637. return this;
  638. }
  639. dispose() {
  640. this.dispatchEvent( { type: 'dispose' } );
  641. }
  642. }
  643. BufferGeometry.prototype.isBufferGeometry = true;
  644. export { BufferGeometry };
粤ICP备19079148号