BufferGeometry.js 25 KB

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