2
0

BufferGeometryUtils.js 26 KB

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