BufferGeometryUtils.js 25 KB

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