BufferGeometry.js 25 KB

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