BufferGeometryUtils.js 26 KB

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