BufferGeometryUtils.js 23 KB

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