BufferGeometryUtils.js 23 KB

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