BufferGeometryUtils.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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 {Mesh | Line | Points} object An instance of Mesh, Line or Points.
  393. * @return {Object} An Object with original position/normal attributes and morphed ones.
  394. */
  395. computeMorphedAttributes: function ( object ) {
  396. if ( object.geometry.isBufferGeometry !== true ) {
  397. console.error( 'THREE.BufferGeometryUtils: Geometry is not of type BufferGeometry.' );
  398. return null;
  399. }
  400. var _vA = new Vector3();
  401. var _vB = new Vector3();
  402. var _vC = new Vector3();
  403. var _tempA = new Vector3();
  404. var _tempB = new Vector3();
  405. var _tempC = new Vector3();
  406. var _morphA = new Vector3();
  407. var _morphB = new Vector3();
  408. var _morphC = new Vector3();
  409. function _calculateMorphedAttributeData(
  410. object,
  411. material,
  412. attribute,
  413. morphAttribute,
  414. morphTargetsRelative,
  415. a,
  416. b,
  417. c,
  418. modifiedAttributeArray
  419. ) {
  420. _vA.fromBufferAttribute( attribute, a );
  421. _vB.fromBufferAttribute( attribute, b );
  422. _vC.fromBufferAttribute( attribute, c );
  423. var morphInfluences = object.morphTargetInfluences;
  424. if ( material.morphTargets && morphAttribute && morphInfluences ) {
  425. _morphA.set( 0, 0, 0 );
  426. _morphB.set( 0, 0, 0 );
  427. _morphC.set( 0, 0, 0 );
  428. for ( var i = 0, il = morphAttribute.length; i < il; i ++ ) {
  429. var influence = morphInfluences[ i ];
  430. var morph = morphAttribute[ i ];
  431. if ( influence === 0 ) continue;
  432. _tempA.fromBufferAttribute( morph, a );
  433. _tempB.fromBufferAttribute( morph, b );
  434. _tempC.fromBufferAttribute( morph, c );
  435. if ( morphTargetsRelative ) {
  436. _morphA.addScaledVector( _tempA, influence );
  437. _morphB.addScaledVector( _tempB, influence );
  438. _morphC.addScaledVector( _tempC, influence );
  439. } else {
  440. _morphA.addScaledVector( _tempA.sub( _vA ), influence );
  441. _morphB.addScaledVector( _tempB.sub( _vB ), influence );
  442. _morphC.addScaledVector( _tempC.sub( _vC ), influence );
  443. }
  444. }
  445. _vA.add( _morphA );
  446. _vB.add( _morphB );
  447. _vC.add( _morphC );
  448. }
  449. if ( object.isSkinnedMesh ) {
  450. object.boneTransform( a, _vA );
  451. object.boneTransform( b, _vB );
  452. object.boneTransform( c, _vC );
  453. }
  454. modifiedAttributeArray[ a * 3 + 0 ] = _vA.x;
  455. modifiedAttributeArray[ a * 3 + 1 ] = _vA.y;
  456. modifiedAttributeArray[ a * 3 + 2 ] = _vA.z;
  457. modifiedAttributeArray[ b * 3 + 0 ] = _vB.x;
  458. modifiedAttributeArray[ b * 3 + 1 ] = _vB.y;
  459. modifiedAttributeArray[ b * 3 + 2 ] = _vB.z;
  460. modifiedAttributeArray[ c * 3 + 0 ] = _vC.x;
  461. modifiedAttributeArray[ c * 3 + 1 ] = _vC.y;
  462. modifiedAttributeArray[ c * 3 + 2 ] = _vC.z;
  463. }
  464. var geometry = object.geometry;
  465. var material = object.material;
  466. var a, b, c;
  467. var index = geometry.index;
  468. var positionAttribute = geometry.attributes.position;
  469. var morphPosition = geometry.morphAttributes.position;
  470. var morphTargetsRelative = geometry.morphTargetsRelative;
  471. var normalAttribute = geometry.attributes.normal;
  472. var morphNormal = geometry.morphAttributes.position;
  473. var groups = geometry.groups;
  474. var drawRange = geometry.drawRange;
  475. var i, j, il, jl;
  476. var group, groupMaterial;
  477. var start, end;
  478. var modifiedPosition = new Float32Array( positionAttribute.count * positionAttribute.itemSize );
  479. var modifiedNormal = new Float32Array( normalAttribute.count * normalAttribute.itemSize );
  480. if ( index !== null ) {
  481. // indexed buffer geometry
  482. if ( Array.isArray( material ) ) {
  483. for ( i = 0, il = groups.length; i < il; i ++ ) {
  484. group = groups[ i ];
  485. groupMaterial = material[ group.materialIndex ];
  486. start = Math.max( group.start, drawRange.start );
  487. end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
  488. for ( j = start, jl = end; j < jl; j += 3 ) {
  489. a = index.getX( j );
  490. b = index.getX( j + 1 );
  491. c = index.getX( j + 2 );
  492. _calculateMorphedAttributeData(
  493. object,
  494. groupMaterial,
  495. positionAttribute,
  496. morphPosition,
  497. morphTargetsRelative,
  498. a, b, c,
  499. modifiedPosition
  500. );
  501. _calculateMorphedAttributeData(
  502. object,
  503. groupMaterial,
  504. normalAttribute,
  505. morphNormal,
  506. morphTargetsRelative,
  507. a, b, c,
  508. modifiedNormal
  509. );
  510. }
  511. }
  512. } else {
  513. start = Math.max( 0, drawRange.start );
  514. end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  515. for ( i = start, il = end; i < il; i += 3 ) {
  516. a = index.getX( i );
  517. b = index.getX( i + 1 );
  518. c = index.getX( i + 2 );
  519. _calculateMorphedAttributeData(
  520. object,
  521. material,
  522. positionAttribute,
  523. morphPosition,
  524. morphTargetsRelative,
  525. a, b, c,
  526. modifiedPosition
  527. );
  528. _calculateMorphedAttributeData(
  529. object,
  530. material,
  531. normalAttribute,
  532. morphNormal,
  533. morphTargetsRelative,
  534. a, b, c,
  535. modifiedNormal
  536. );
  537. }
  538. }
  539. } else if ( positionAttribute !== undefined ) {
  540. // non-indexed buffer geometry
  541. if ( Array.isArray( material ) ) {
  542. for ( i = 0, il = groups.length; i < il; i ++ ) {
  543. group = groups[ i ];
  544. groupMaterial = material[ group.materialIndex ];
  545. start = Math.max( group.start, drawRange.start );
  546. end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
  547. for ( j = start, jl = end; j < jl; j += 3 ) {
  548. a = j;
  549. b = j + 1;
  550. c = j + 2;
  551. _calculateMorphedAttributeData(
  552. object,
  553. groupMaterial,
  554. positionAttribute,
  555. morphPosition,
  556. morphTargetsRelative,
  557. a, b, c,
  558. modifiedPosition
  559. );
  560. _calculateMorphedAttributeData(
  561. object,
  562. groupMaterial,
  563. normalAttribute,
  564. morphNormal,
  565. morphTargetsRelative,
  566. a, b, c,
  567. modifiedNormal
  568. );
  569. }
  570. }
  571. } else {
  572. start = Math.max( 0, drawRange.start );
  573. end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  574. for ( i = start, il = end; i < il; i += 3 ) {
  575. a = i;
  576. b = i + 1;
  577. c = i + 2;
  578. _calculateMorphedAttributeData(
  579. object,
  580. material,
  581. positionAttribute,
  582. morphPosition,
  583. morphTargetsRelative,
  584. a, b, c,
  585. modifiedPosition
  586. );
  587. _calculateMorphedAttributeData(
  588. object,
  589. material,
  590. normalAttribute,
  591. morphNormal,
  592. morphTargetsRelative,
  593. a, b, c,
  594. modifiedNormal
  595. );
  596. }
  597. }
  598. }
  599. var morphedPositionAttribute = new Float32BufferAttribute( modifiedPosition, 3 );
  600. var morphedNormalAttribute = new Float32BufferAttribute( modifiedNormal, 3 );
  601. return {
  602. positionAttribute: positionAttribute,
  603. normalAttribute: normalAttribute,
  604. morphedPositionAttribute: morphedPositionAttribute,
  605. morphedNormalAttribute: morphedNormalAttribute
  606. };
  607. }
  608. };
  609. export { BufferGeometryUtils };