BufferGeometryUtils.js 22 KB

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