BufferGeometryUtils.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. ( function () {
  2. function computeTangents( geometry, negateSign = true ) {
  3. function getAttributeArray( attribute ) {
  4. if ( attribute.normalized || attribute.isInterleavedBufferAttribute ) {
  5. const srcArray = attribute.isInterleavedBufferAttribute ? attribute.data.array : attribute.array;
  6. const dstArray = new Float32Array( attribute.getCount() * attribute.itemSize );
  7. for ( let i = 0, j = 0; i < attribute.getCount(); i ++ ) {
  8. dstArray[ j ++ ] = THREE.MathUtils.denormalize( attribute.getX( i ), srcArray );
  9. dstArray[ j ++ ] = THREE.MathUtils.denormalize( attribute.getY( i ), srcArray );
  10. if ( attribute.itemSize > 2 ) {
  11. dstArray[ j ++ ] = THREE.MathUtils.denormalize( attribute.getZ( i ), srcArray );
  12. }
  13. }
  14. return dstArray;
  15. }
  16. if ( attribute.array instanceof Float32Array ) {
  17. return attribute.array;
  18. }
  19. return new Float32Array( attribute.array );
  20. } // MikkTSpace algorithm requires non-indexed input.
  21. const _geometry = geometry.index ? geometry.toNonIndexed() : geometry; // Compute vertex tangents.
  22. const tangents = generateTangents( getAttributeArray( _geometry.attributes.position ), getAttributeArray( _geometry.attributes.normal ), getAttributeArray( _geometry.attributes.uv ) ); // Texture coordinate convention of glTF differs from the apparent
  23. // default of the MikkTSpace library; .w component must be flipped.
  24. if ( negateSign ) {
  25. for ( let i = 3; i < tangents.length; i += 4 ) {
  26. tangents[ i ] *= - 1;
  27. }
  28. } //
  29. _geometry.setAttribute( 'tangent', new THREE.BufferAttribute( tangents, 4 ) );
  30. return geometry.copy( _geometry );
  31. }
  32. /**
  33. * @param {Array<BufferGeometry>} geometries
  34. * @param {Boolean} useGroups
  35. * @return {BufferGeometry}
  36. */
  37. function mergeBufferGeometries( geometries, useGroups = false ) {
  38. const isIndexed = geometries[ 0 ].index !== null;
  39. const attributesUsed = new Set( Object.keys( geometries[ 0 ].attributes ) );
  40. const morphAttributesUsed = new Set( Object.keys( geometries[ 0 ].morphAttributes ) );
  41. const attributes = {};
  42. const morphAttributes = {};
  43. const morphTargetsRelative = geometries[ 0 ].morphTargetsRelative;
  44. const mergedGeometry = new THREE.BufferGeometry();
  45. let offset = 0;
  46. for ( let i = 0; i < geometries.length; ++ i ) {
  47. const geometry = geometries[ i ];
  48. let attributesCount = 0; // ensure that all geometries are indexed, or none
  49. if ( isIndexed !== ( geometry.index !== null ) ) {
  50. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure index attribute exists among all geometries, or in none of them.' );
  51. return null;
  52. } // gather attributes, exit early if they're different
  53. for ( const name in geometry.attributes ) {
  54. if ( ! attributesUsed.has( name ) ) {
  55. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. All geometries must have compatible attributes; make sure "' + name + '" attribute exists among all geometries, or in none of them.' );
  56. return null;
  57. }
  58. if ( attributes[ name ] === undefined ) attributes[ name ] = [];
  59. attributes[ name ].push( geometry.attributes[ name ] );
  60. attributesCount ++;
  61. } // ensure geometries have the same number of attributes
  62. if ( attributesCount !== attributesUsed.size ) {
  63. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. Make sure all geometries have the same number of attributes.' );
  64. return null;
  65. } // gather morph attributes, exit early if they're different
  66. if ( morphTargetsRelative !== geometry.morphTargetsRelative ) {
  67. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. .morphTargetsRelative must be consistent throughout all geometries.' );
  68. return null;
  69. }
  70. for ( const name in geometry.morphAttributes ) {
  71. if ( ! morphAttributesUsed.has( name ) ) {
  72. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. .morphAttributes must be consistent throughout all geometries.' );
  73. return null;
  74. }
  75. if ( morphAttributes[ name ] === undefined ) morphAttributes[ name ] = [];
  76. morphAttributes[ name ].push( geometry.morphAttributes[ name ] );
  77. } // gather .userData
  78. mergedGeometry.userData.mergedUserData = mergedGeometry.userData.mergedUserData || [];
  79. mergedGeometry.userData.mergedUserData.push( geometry.userData );
  80. if ( useGroups ) {
  81. let count;
  82. if ( isIndexed ) {
  83. count = geometry.index.count;
  84. } else if ( geometry.attributes.position !== undefined ) {
  85. count = geometry.attributes.position.count;
  86. } else {
  87. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. The geometry must have either an index or a position attribute' );
  88. return null;
  89. }
  90. mergedGeometry.addGroup( offset, count, i );
  91. offset += count;
  92. }
  93. } // merge indices
  94. if ( isIndexed ) {
  95. let indexOffset = 0;
  96. const mergedIndex = [];
  97. for ( let i = 0; i < geometries.length; ++ i ) {
  98. const index = geometries[ i ].index;
  99. for ( let j = 0; j < index.count; ++ j ) {
  100. mergedIndex.push( index.getX( j ) + indexOffset );
  101. }
  102. indexOffset += geometries[ i ].attributes.position.count;
  103. }
  104. mergedGeometry.setIndex( mergedIndex );
  105. } // merge attributes
  106. for ( const name in attributes ) {
  107. const mergedAttribute = mergeBufferAttributes( attributes[ name ] );
  108. if ( ! mergedAttribute ) {
  109. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed while trying to merge the ' + name + ' attribute.' );
  110. return null;
  111. }
  112. mergedGeometry.setAttribute( name, mergedAttribute );
  113. } // merge morph attributes
  114. for ( const name in morphAttributes ) {
  115. const numMorphTargets = morphAttributes[ name ][ 0 ].length;
  116. if ( numMorphTargets === 0 ) break;
  117. mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};
  118. mergedGeometry.morphAttributes[ name ] = [];
  119. for ( let i = 0; i < numMorphTargets; ++ i ) {
  120. const morphAttributesToMerge = [];
  121. for ( let j = 0; j < morphAttributes[ name ].length; ++ j ) {
  122. morphAttributesToMerge.push( morphAttributes[ name ][ j ][ i ] );
  123. }
  124. const mergedMorphAttribute = mergeBufferAttributes( morphAttributesToMerge );
  125. if ( ! mergedMorphAttribute ) {
  126. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed while trying to merge the ' + name + ' morphAttribute.' );
  127. return null;
  128. }
  129. mergedGeometry.morphAttributes[ name ].push( mergedMorphAttribute );
  130. }
  131. }
  132. return mergedGeometry;
  133. }
  134. /**
  135. * @param {Array<BufferAttribute>} attributes
  136. * @return {BufferAttribute}
  137. */
  138. function mergeBufferAttributes( attributes ) {
  139. let TypedArray;
  140. let itemSize;
  141. let normalized;
  142. let arrayLength = 0;
  143. for ( let i = 0; i < attributes.length; ++ i ) {
  144. const attribute = attributes[ i ];
  145. if ( attribute.isInterleavedBufferAttribute ) {
  146. console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. InterleavedBufferAttributes are not supported.' );
  147. return null;
  148. }
  149. if ( TypedArray === undefined ) TypedArray = attribute.array.constructor;
  150. if ( TypedArray !== attribute.array.constructor ) {
  151. console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. THREE.BufferAttribute.array must be of consistent array types across matching attributes.' );
  152. return null;
  153. }
  154. if ( itemSize === undefined ) itemSize = attribute.itemSize;
  155. if ( itemSize !== attribute.itemSize ) {
  156. console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. THREE.BufferAttribute.itemSize must be consistent across matching attributes.' );
  157. return null;
  158. }
  159. if ( normalized === undefined ) normalized = attribute.normalized;
  160. if ( normalized !== attribute.normalized ) {
  161. console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. THREE.BufferAttribute.normalized must be consistent across matching attributes.' );
  162. return null;
  163. }
  164. arrayLength += attribute.array.length;
  165. }
  166. const array = new TypedArray( arrayLength );
  167. let offset = 0;
  168. for ( let i = 0; i < attributes.length; ++ i ) {
  169. array.set( attributes[ i ].array, offset );
  170. offset += attributes[ i ].array.length;
  171. }
  172. return new THREE.BufferAttribute( array, itemSize, normalized );
  173. }
  174. /**
  175. * @param {Array<BufferAttribute>} attributes
  176. * @return {Array<InterleavedBufferAttribute>}
  177. */
  178. function interleaveAttributes( attributes ) {
  179. // Interleaves the provided attributes into an THREE.InterleavedBuffer and returns
  180. // a set of InterleavedBufferAttributes for each attribute
  181. let TypedArray;
  182. let arrayLength = 0;
  183. let stride = 0; // calculate the the length and type of the interleavedBuffer
  184. for ( let i = 0, l = attributes.length; i < l; ++ i ) {
  185. const attribute = attributes[ i ];
  186. if ( TypedArray === undefined ) TypedArray = attribute.array.constructor;
  187. if ( TypedArray !== attribute.array.constructor ) {
  188. console.error( 'AttributeBuffers of different types cannot be interleaved' );
  189. return null;
  190. }
  191. arrayLength += attribute.array.length;
  192. stride += attribute.itemSize;
  193. } // Create the set of buffer attributes
  194. const interleavedBuffer = new THREE.InterleavedBuffer( new TypedArray( arrayLength ), stride );
  195. let offset = 0;
  196. const res = [];
  197. const getters = [ 'getX', 'getY', 'getZ', 'getW' ];
  198. const setters = [ 'setX', 'setY', 'setZ', 'setW' ];
  199. for ( let j = 0, l = attributes.length; j < l; j ++ ) {
  200. const attribute = attributes[ j ];
  201. const itemSize = attribute.itemSize;
  202. const count = attribute.count;
  203. const iba = new THREE.InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, attribute.normalized );
  204. res.push( iba );
  205. offset += itemSize; // Move the data for each attribute into the new interleavedBuffer
  206. // at the appropriate offset
  207. for ( let c = 0; c < count; c ++ ) {
  208. for ( let k = 0; k < itemSize; k ++ ) {
  209. iba[ setters[ k ] ]( c, attribute[ getters[ k ] ]( c ) );
  210. }
  211. }
  212. }
  213. return res;
  214. }
  215. /**
  216. * @param {Array<BufferGeometry>} geometry
  217. * @return {number}
  218. */
  219. function estimateBytesUsed( geometry ) {
  220. // Return the estimated memory used by this geometry in bytes
  221. // Calculate using itemSize, count, and BYTES_PER_ELEMENT to account
  222. // for InterleavedBufferAttributes.
  223. let mem = 0;
  224. for ( const name in geometry.attributes ) {
  225. const attr = geometry.getAttribute( name );
  226. mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
  227. }
  228. const indices = geometry.getIndex();
  229. mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;
  230. return mem;
  231. }
  232. /**
  233. * @param {BufferGeometry} geometry
  234. * @param {number} tolerance
  235. * @return {BufferGeometry>}
  236. */
  237. function mergeVertices( geometry, tolerance = 1e-4 ) {
  238. tolerance = Math.max( tolerance, Number.EPSILON ); // Generate an index buffer if the geometry doesn't have one, or optimize it
  239. // if it's already available.
  240. const hashToIndex = {};
  241. const indices = geometry.getIndex();
  242. const positions = geometry.getAttribute( 'position' );
  243. const vertexCount = indices ? indices.count : positions.count; // next value for triangle indices
  244. let nextIndex = 0; // attributes and new attribute arrays
  245. const attributeNames = Object.keys( geometry.attributes );
  246. const attrArrays = {};
  247. const morphAttrsArrays = {};
  248. const newIndices = [];
  249. const getters = [ 'getX', 'getY', 'getZ', 'getW' ]; // initialize the arrays
  250. for ( let i = 0, l = attributeNames.length; i < l; i ++ ) {
  251. const name = attributeNames[ i ];
  252. attrArrays[ name ] = [];
  253. const morphAttr = geometry.morphAttributes[ name ];
  254. if ( morphAttr ) {
  255. morphAttrsArrays[ name ] = new Array( morphAttr.length ).fill().map( () => [] );
  256. }
  257. } // convert the error tolerance to an amount of decimal places to truncate to
  258. const decimalShift = Math.log10( 1 / tolerance );
  259. const shiftMultiplier = Math.pow( 10, decimalShift );
  260. for ( let i = 0; i < vertexCount; i ++ ) {
  261. const index = indices ? indices.getX( i ) : i; // Generate a hash for the vertex attributes at the current index 'i'
  262. let hash = '';
  263. for ( let j = 0, l = attributeNames.length; j < l; j ++ ) {
  264. const name = attributeNames[ j ];
  265. const attribute = geometry.getAttribute( name );
  266. const itemSize = attribute.itemSize;
  267. for ( let k = 0; k < itemSize; k ++ ) {
  268. // double tilde truncates the decimal value
  269. hash += `${~ ~ ( attribute[ getters[ k ] ]( index ) * shiftMultiplier )},`;
  270. }
  271. } // Add another reference to the vertex if it's already
  272. // used by another index
  273. if ( hash in hashToIndex ) {
  274. newIndices.push( hashToIndex[ hash ] );
  275. } else {
  276. // copy data to the new index in the attribute arrays
  277. for ( let j = 0, l = attributeNames.length; j < l; j ++ ) {
  278. const name = attributeNames[ j ];
  279. const attribute = geometry.getAttribute( name );
  280. const morphAttr = geometry.morphAttributes[ name ];
  281. const itemSize = attribute.itemSize;
  282. const newarray = attrArrays[ name ];
  283. const newMorphArrays = morphAttrsArrays[ name ];
  284. for ( let k = 0; k < itemSize; k ++ ) {
  285. const getterFunc = getters[ k ];
  286. newarray.push( attribute[ getterFunc ]( index ) );
  287. if ( morphAttr ) {
  288. for ( let m = 0, ml = morphAttr.length; m < ml; m ++ ) {
  289. newMorphArrays[ m ].push( morphAttr[ m ][ getterFunc ]( index ) );
  290. }
  291. }
  292. }
  293. }
  294. hashToIndex[ hash ] = nextIndex;
  295. newIndices.push( nextIndex );
  296. nextIndex ++;
  297. }
  298. } // Generate typed arrays from new attribute arrays and update
  299. // the attributeBuffers
  300. const result = geometry.clone();
  301. for ( let i = 0, l = attributeNames.length; i < l; i ++ ) {
  302. const name = attributeNames[ i ];
  303. const oldAttribute = geometry.getAttribute( name );
  304. const buffer = new oldAttribute.array.constructor( attrArrays[ name ] );
  305. const attribute = new THREE.BufferAttribute( buffer, oldAttribute.itemSize, oldAttribute.normalized );
  306. result.setAttribute( name, attribute ); // Update the attribute arrays
  307. if ( name in morphAttrsArrays ) {
  308. for ( let j = 0; j < morphAttrsArrays[ name ].length; j ++ ) {
  309. const oldMorphAttribute = geometry.morphAttributes[ name ][ j ];
  310. const buffer = new oldMorphAttribute.array.constructor( morphAttrsArrays[ name ][ j ] );
  311. const morphAttribute = new THREE.BufferAttribute( buffer, oldMorphAttribute.itemSize, oldMorphAttribute.normalized );
  312. result.morphAttributes[ name ][ j ] = morphAttribute;
  313. }
  314. }
  315. } // indices
  316. result.setIndex( newIndices );
  317. return result;
  318. }
  319. /**
  320. * @param {BufferGeometry} geometry
  321. * @param {number} drawMode
  322. * @return {BufferGeometry>}
  323. */
  324. function toTrianglesDrawMode( geometry, drawMode ) {
  325. if ( drawMode === THREE.TrianglesDrawMode ) {
  326. console.warn( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Geometry already defined as triangles.' );
  327. return geometry;
  328. }
  329. if ( drawMode === THREE.TriangleFanDrawMode || drawMode === THREE.TriangleStripDrawMode ) {
  330. let index = geometry.getIndex(); // generate index if not present
  331. if ( index === null ) {
  332. const indices = [];
  333. const position = geometry.getAttribute( 'position' );
  334. if ( position !== undefined ) {
  335. for ( let i = 0; i < position.count; i ++ ) {
  336. indices.push( i );
  337. }
  338. geometry.setIndex( indices );
  339. index = geometry.getIndex();
  340. } else {
  341. console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.' );
  342. return geometry;
  343. }
  344. } //
  345. const numberOfTriangles = index.count - 2;
  346. const newIndices = [];
  347. if ( drawMode === THREE.TriangleFanDrawMode ) {
  348. // gl.TRIANGLE_FAN
  349. for ( let i = 1; i <= numberOfTriangles; i ++ ) {
  350. newIndices.push( index.getX( 0 ) );
  351. newIndices.push( index.getX( i ) );
  352. newIndices.push( index.getX( i + 1 ) );
  353. }
  354. } else {
  355. // gl.TRIANGLE_STRIP
  356. for ( let i = 0; i < numberOfTriangles; i ++ ) {
  357. if ( i % 2 === 0 ) {
  358. newIndices.push( index.getX( i ) );
  359. newIndices.push( index.getX( i + 1 ) );
  360. newIndices.push( index.getX( i + 2 ) );
  361. } else {
  362. newIndices.push( index.getX( i + 2 ) );
  363. newIndices.push( index.getX( i + 1 ) );
  364. newIndices.push( index.getX( i ) );
  365. }
  366. }
  367. }
  368. if ( newIndices.length / 3 !== numberOfTriangles ) {
  369. console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unable to generate correct amount of triangles.' );
  370. } // build final geometry
  371. const newGeometry = geometry.clone();
  372. newGeometry.setIndex( newIndices );
  373. newGeometry.clearGroups();
  374. return newGeometry;
  375. } else {
  376. console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unknown draw mode:', drawMode );
  377. return geometry;
  378. }
  379. }
  380. /**
  381. * Calculates the morphed attributes of a morphed/skinned THREE.BufferGeometry.
  382. * Helpful for Raytracing or Decals.
  383. * @param {Mesh | Line | Points} object An instance of Mesh, Line or Points.
  384. * @return {Object} An Object with original position/normal attributes and morphed ones.
  385. */
  386. function computeMorphedAttributes( object ) {
  387. if ( object.geometry.isBufferGeometry !== true ) {
  388. console.error( 'THREE.BufferGeometryUtils: Geometry is not of type THREE.BufferGeometry.' );
  389. return null;
  390. }
  391. const _vA = new THREE.Vector3();
  392. const _vB = new THREE.Vector3();
  393. const _vC = new THREE.Vector3();
  394. const _tempA = new THREE.Vector3();
  395. const _tempB = new THREE.Vector3();
  396. const _tempC = new THREE.Vector3();
  397. const _morphA = new THREE.Vector3();
  398. const _morphB = new THREE.Vector3();
  399. const _morphC = new THREE.Vector3();
  400. function _calculateMorphedAttributeData( object, material, attribute, morphAttribute, morphTargetsRelative, a, b, c, modifiedAttributeArray ) {
  401. _vA.fromBufferAttribute( attribute, a );
  402. _vB.fromBufferAttribute( attribute, b );
  403. _vC.fromBufferAttribute( attribute, c );
  404. const morphInfluences = object.morphTargetInfluences;
  405. if ( material.morphTargets && morphAttribute && morphInfluences ) {
  406. _morphA.set( 0, 0, 0 );
  407. _morphB.set( 0, 0, 0 );
  408. _morphC.set( 0, 0, 0 );
  409. for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
  410. const influence = morphInfluences[ i ];
  411. const morph = morphAttribute[ i ];
  412. if ( influence === 0 ) continue;
  413. _tempA.fromBufferAttribute( morph, a );
  414. _tempB.fromBufferAttribute( morph, b );
  415. _tempC.fromBufferAttribute( morph, c );
  416. if ( morphTargetsRelative ) {
  417. _morphA.addScaledVector( _tempA, influence );
  418. _morphB.addScaledVector( _tempB, influence );
  419. _morphC.addScaledVector( _tempC, influence );
  420. } else {
  421. _morphA.addScaledVector( _tempA.sub( _vA ), influence );
  422. _morphB.addScaledVector( _tempB.sub( _vB ), influence );
  423. _morphC.addScaledVector( _tempC.sub( _vC ), influence );
  424. }
  425. }
  426. _vA.add( _morphA );
  427. _vB.add( _morphB );
  428. _vC.add( _morphC );
  429. }
  430. if ( object.isSkinnedMesh ) {
  431. object.boneTransform( a, _vA );
  432. object.boneTransform( b, _vB );
  433. object.boneTransform( c, _vC );
  434. }
  435. modifiedAttributeArray[ a * 3 + 0 ] = _vA.x;
  436. modifiedAttributeArray[ a * 3 + 1 ] = _vA.y;
  437. modifiedAttributeArray[ a * 3 + 2 ] = _vA.z;
  438. modifiedAttributeArray[ b * 3 + 0 ] = _vB.x;
  439. modifiedAttributeArray[ b * 3 + 1 ] = _vB.y;
  440. modifiedAttributeArray[ b * 3 + 2 ] = _vB.z;
  441. modifiedAttributeArray[ c * 3 + 0 ] = _vC.x;
  442. modifiedAttributeArray[ c * 3 + 1 ] = _vC.y;
  443. modifiedAttributeArray[ c * 3 + 2 ] = _vC.z;
  444. }
  445. const geometry = object.geometry;
  446. const material = object.material;
  447. let a, b, c;
  448. const index = geometry.index;
  449. const positionAttribute = geometry.attributes.position;
  450. const morphPosition = geometry.morphAttributes.position;
  451. const morphTargetsRelative = geometry.morphTargetsRelative;
  452. const normalAttribute = geometry.attributes.normal;
  453. const morphNormal = geometry.morphAttributes.position;
  454. const groups = geometry.groups;
  455. const drawRange = geometry.drawRange;
  456. let i, j, il, jl;
  457. let group, groupMaterial;
  458. let start, end;
  459. const modifiedPosition = new Float32Array( positionAttribute.count * positionAttribute.itemSize );
  460. const modifiedNormal = new Float32Array( normalAttribute.count * normalAttribute.itemSize );
  461. if ( index !== null ) {
  462. // indexed buffer geometry
  463. if ( Array.isArray( material ) ) {
  464. for ( i = 0, il = groups.length; i < il; i ++ ) {
  465. group = groups[ i ];
  466. groupMaterial = material[ group.materialIndex ];
  467. start = Math.max( group.start, drawRange.start );
  468. end = Math.min( group.start + group.count, drawRange.start + drawRange.count );
  469. for ( j = start, jl = end; j < jl; j += 3 ) {
  470. a = index.getX( j );
  471. b = index.getX( j + 1 );
  472. c = index.getX( j + 2 );
  473. _calculateMorphedAttributeData( object, groupMaterial, positionAttribute, morphPosition, morphTargetsRelative, a, b, c, modifiedPosition );
  474. _calculateMorphedAttributeData( object, groupMaterial, normalAttribute, morphNormal, morphTargetsRelative, a, b, c, modifiedNormal );
  475. }
  476. }
  477. } else {
  478. start = Math.max( 0, drawRange.start );
  479. end = Math.min( index.count, drawRange.start + drawRange.count );
  480. for ( i = start, il = end; i < il; i += 3 ) {
  481. a = index.getX( i );
  482. b = index.getX( i + 1 );
  483. c = index.getX( i + 2 );
  484. _calculateMorphedAttributeData( object, material, positionAttribute, morphPosition, morphTargetsRelative, a, b, c, modifiedPosition );
  485. _calculateMorphedAttributeData( object, material, normalAttribute, morphNormal, morphTargetsRelative, a, b, c, modifiedNormal );
  486. }
  487. }
  488. } else {
  489. // non-indexed buffer geometry
  490. if ( Array.isArray( material ) ) {
  491. for ( i = 0, il = groups.length; i < il; i ++ ) {
  492. group = groups[ i ];
  493. groupMaterial = material[ group.materialIndex ];
  494. start = Math.max( group.start, drawRange.start );
  495. end = Math.min( group.start + group.count, drawRange.start + drawRange.count );
  496. for ( j = start, jl = end; j < jl; j += 3 ) {
  497. a = j;
  498. b = j + 1;
  499. c = j + 2;
  500. _calculateMorphedAttributeData( object, groupMaterial, positionAttribute, morphPosition, morphTargetsRelative, a, b, c, modifiedPosition );
  501. _calculateMorphedAttributeData( object, groupMaterial, normalAttribute, morphNormal, morphTargetsRelative, a, b, c, modifiedNormal );
  502. }
  503. }
  504. } else {
  505. start = Math.max( 0, drawRange.start );
  506. end = Math.min( positionAttribute.count, drawRange.start + drawRange.count );
  507. for ( i = start, il = end; i < il; i += 3 ) {
  508. a = i;
  509. b = i + 1;
  510. c = i + 2;
  511. _calculateMorphedAttributeData( object, material, positionAttribute, morphPosition, morphTargetsRelative, a, b, c, modifiedPosition );
  512. _calculateMorphedAttributeData( object, material, normalAttribute, morphNormal, morphTargetsRelative, a, b, c, modifiedNormal );
  513. }
  514. }
  515. }
  516. const morphedPositionAttribute = new THREE.Float32BufferAttribute( modifiedPosition, 3 );
  517. const morphedNormalAttribute = new THREE.Float32BufferAttribute( modifiedNormal, 3 );
  518. return {
  519. positionAttribute: positionAttribute,
  520. normalAttribute: normalAttribute,
  521. morphedPositionAttribute: morphedPositionAttribute,
  522. morphedNormalAttribute: morphedNormalAttribute
  523. };
  524. }
  525. function mergeGroups( geometry ) {
  526. if ( geometry.groups.length === 0 ) {
  527. console.warn( 'THREE.BufferGeometryUtils.mergeGroups(): No groups are defined. Nothing to merge.' );
  528. return geometry;
  529. }
  530. let groups = geometry.groups; // sort groups by material index
  531. groups = groups.sort( ( a, b ) => {
  532. if ( a.materialIndex !== b.materialIndex ) return a.materialIndex - b.materialIndex;
  533. return a.start - b.start;
  534. } ); // create index for non-indexed geometries
  535. if ( geometry.getIndex() === null ) {
  536. const positionAttribute = geometry.getAttribute( 'position' );
  537. const indices = [];
  538. for ( let i = 0; i < positionAttribute.count; i += 3 ) {
  539. indices.push( i, i + 1, i + 2 );
  540. }
  541. geometry.setIndex( indices );
  542. } // sort index
  543. const index = geometry.getIndex();
  544. const newIndices = [];
  545. for ( let i = 0; i < groups.length; i ++ ) {
  546. const group = groups[ i ];
  547. const groupStart = group.start;
  548. const groupLength = groupStart + group.count;
  549. for ( let j = groupStart; j < groupLength; j ++ ) {
  550. newIndices.push( index.getX( j ) );
  551. }
  552. }
  553. geometry.dispose(); // Required to force buffer recreation
  554. geometry.setIndex( newIndices ); // update groups indices
  555. let start = 0;
  556. for ( let i = 0; i < groups.length; i ++ ) {
  557. const group = groups[ i ];
  558. group.start = start;
  559. start += group.count;
  560. } // merge groups
  561. let currentGroup = groups[ 0 ];
  562. geometry.groups = [ currentGroup ];
  563. for ( let i = 1; i < groups.length; i ++ ) {
  564. const group = groups[ i ];
  565. if ( currentGroup.materialIndex === group.materialIndex ) {
  566. currentGroup.count += group.count;
  567. } else {
  568. currentGroup = group;
  569. geometry.groups.push( currentGroup );
  570. }
  571. }
  572. return geometry;
  573. }
  574. THREE.BufferGeometryUtils = {};
  575. THREE.BufferGeometryUtils.computeMorphedAttributes = computeMorphedAttributes;
  576. THREE.BufferGeometryUtils.computeTangents = computeTangents;
  577. THREE.BufferGeometryUtils.estimateBytesUsed = estimateBytesUsed;
  578. THREE.BufferGeometryUtils.interleaveAttributes = interleaveAttributes;
  579. THREE.BufferGeometryUtils.mergeBufferAttributes = mergeBufferAttributes;
  580. THREE.BufferGeometryUtils.mergeBufferGeometries = mergeBufferGeometries;
  581. THREE.BufferGeometryUtils.mergeGroups = mergeGroups;
  582. THREE.BufferGeometryUtils.mergeVertices = mergeVertices;
  583. THREE.BufferGeometryUtils.toTrianglesDrawMode = toTrianglesDrawMode;
  584. } )();