BufferGeometry.js 22 KB

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