BufferGeometryUtils.js 22 KB

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