BufferGeometryUtils.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. console.warn( "THREE.BufferGeometryUtils: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.BufferGeometryUtils = {
  6. computeTangents: function ( geometry ) {
  7. var index = geometry.index;
  8. var attributes = geometry.attributes;
  9. // based on http://www.terathon.com/code/tangent.html
  10. // (per vertex tangents)
  11. if ( index === null ||
  12. attributes.position === undefined ||
  13. attributes.normal === undefined ||
  14. attributes.uv === undefined ) {
  15. console.error( 'THREE.BufferGeometryUtils: .computeTangents() failed. Missing required attributes (index, position, normal or uv)' );
  16. return;
  17. }
  18. var indices = index.array;
  19. var positions = attributes.position.array;
  20. var normals = attributes.normal.array;
  21. var uvs = attributes.uv.array;
  22. var nVertices = positions.length / 3;
  23. if ( attributes.tangent === undefined ) {
  24. geometry.setAttribute( 'tangent', new THREE.BufferAttribute( new Float32Array( 4 * nVertices ), 4 ) );
  25. }
  26. var tangents = attributes.tangent.array;
  27. var tan1 = [], tan2 = [];
  28. for ( var i = 0; i < nVertices; i ++ ) {
  29. tan1[ i ] = new THREE.Vector3();
  30. tan2[ i ] = new THREE.Vector3();
  31. }
  32. var vA = new THREE.Vector3(),
  33. vB = new THREE.Vector3(),
  34. vC = new THREE.Vector3(),
  35. uvA = new THREE.Vector2(),
  36. uvB = new THREE.Vector2(),
  37. uvC = new THREE.Vector2(),
  38. sdir = new THREE.Vector3(),
  39. tdir = new THREE.Vector3();
  40. function handleTriangle( a, b, c ) {
  41. vA.fromArray( positions, a * 3 );
  42. vB.fromArray( positions, b * 3 );
  43. vC.fromArray( positions, c * 3 );
  44. uvA.fromArray( uvs, a * 2 );
  45. uvB.fromArray( uvs, b * 2 );
  46. uvC.fromArray( uvs, c * 2 );
  47. vB.sub( vA );
  48. vC.sub( vA );
  49. uvB.sub( uvA );
  50. uvC.sub( uvA );
  51. var r = 1.0 / ( uvB.x * uvC.y - uvC.x * uvB.y );
  52. // silently ignore degenerate uv triangles having coincident or colinear vertices
  53. if ( ! isFinite( r ) ) return;
  54. sdir.copy( vB ).multiplyScalar( uvC.y ).addScaledVector( vC, - uvB.y ).multiplyScalar( r );
  55. tdir.copy( vC ).multiplyScalar( uvB.x ).addScaledVector( vB, - uvC.x ).multiplyScalar( r );
  56. tan1[ a ].add( sdir );
  57. tan1[ b ].add( sdir );
  58. tan1[ c ].add( sdir );
  59. tan2[ a ].add( tdir );
  60. tan2[ b ].add( tdir );
  61. tan2[ c ].add( tdir );
  62. }
  63. var groups = geometry.groups;
  64. if ( groups.length === 0 ) {
  65. groups = [ {
  66. start: 0,
  67. count: indices.length
  68. } ];
  69. }
  70. for ( var i = 0, il = groups.length; i < il; ++ i ) {
  71. var group = groups[ i ];
  72. var start = group.start;
  73. var count = group.count;
  74. for ( var j = start, jl = start + count; j < jl; j += 3 ) {
  75. handleTriangle(
  76. indices[ j + 0 ],
  77. indices[ j + 1 ],
  78. indices[ j + 2 ]
  79. );
  80. }
  81. }
  82. var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
  83. var n = new THREE.Vector3(), n2 = new THREE.Vector3();
  84. var w, t, test;
  85. function handleVertex( v ) {
  86. n.fromArray( normals, v * 3 );
  87. n2.copy( n );
  88. t = tan1[ v ];
  89. // Gram-Schmidt orthogonalize
  90. tmp.copy( t );
  91. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  92. // Calculate handedness
  93. tmp2.crossVectors( n2, t );
  94. test = tmp2.dot( tan2[ v ] );
  95. w = ( test < 0.0 ) ? - 1.0 : 1.0;
  96. tangents[ v * 4 ] = tmp.x;
  97. tangents[ v * 4 + 1 ] = tmp.y;
  98. tangents[ v * 4 + 2 ] = tmp.z;
  99. tangents[ v * 4 + 3 ] = w;
  100. }
  101. for ( var i = 0, il = groups.length; i < il; ++ i ) {
  102. var group = groups[ i ];
  103. var start = group.start;
  104. var count = group.count;
  105. for ( var j = start, jl = start + count; j < jl; j += 3 ) {
  106. handleVertex( indices[ j + 0 ] );
  107. handleVertex( indices[ j + 1 ] );
  108. handleVertex( indices[ j + 2 ] );
  109. }
  110. }
  111. },
  112. /**
  113. * @param {Array<THREE.BufferGeometry>} geometries
  114. * @param {Boolean} useGroups
  115. * @return {THREE.BufferGeometry}
  116. */
  117. mergeBufferGeometries: function ( geometries, useGroups ) {
  118. var isIndexed = geometries[ 0 ].index !== null;
  119. var attributesUsed = new Set( Object.keys( geometries[ 0 ].attributes ) );
  120. var morphAttributesUsed = new Set( Object.keys( geometries[ 0 ].morphAttributes ) );
  121. var attributes = {};
  122. var morphAttributes = {};
  123. var morphTargetsRelative = geometries[ 0 ].morphTargetsRelative;
  124. var mergedGeometry = new THREE.BufferGeometry();
  125. var offset = 0;
  126. for ( var i = 0; i < geometries.length; ++ i ) {
  127. var geometry = geometries[ i ];
  128. var attributesCount = 0;
  129. // ensure that all geometries are indexed, or none
  130. if ( isIndexed !== ( geometry.index !== null ) ) {
  131. 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.' );
  132. return null;
  133. }
  134. // gather attributes, exit early if they're different
  135. for ( var name in geometry.attributes ) {
  136. if ( ! attributesUsed.has( name ) ) {
  137. 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.' );
  138. return null;
  139. }
  140. if ( attributes[ name ] === undefined ) attributes[ name ] = [];
  141. attributes[ name ].push( geometry.attributes[ name ] );
  142. attributesCount ++;
  143. }
  144. // ensure geometries have the same number of attributes
  145. if ( attributesCount !== attributesUsed.size ) {
  146. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. Make sure all geometries have the same number of attributes.' );
  147. return null;
  148. }
  149. // gather morph attributes, exit early if they're different
  150. if ( morphTargetsRelative !== geometry.morphTargetsRelative ) {
  151. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. .morphTargetsRelative must be consistent throughout all geometries.' );
  152. return null;
  153. }
  154. for ( var name in geometry.morphAttributes ) {
  155. if ( ! morphAttributesUsed.has( name ) ) {
  156. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. .morphAttributes must be consistent throughout all geometries.' );
  157. return null;
  158. }
  159. if ( morphAttributes[ name ] === undefined ) morphAttributes[ name ] = [];
  160. morphAttributes[ name ].push( geometry.morphAttributes[ name ] );
  161. }
  162. // gather .userData
  163. mergedGeometry.userData.mergedUserData = mergedGeometry.userData.mergedUserData || [];
  164. mergedGeometry.userData.mergedUserData.push( geometry.userData );
  165. if ( useGroups ) {
  166. var count;
  167. if ( isIndexed ) {
  168. count = geometry.index.count;
  169. } else if ( geometry.attributes.position !== undefined ) {
  170. count = geometry.attributes.position.count;
  171. } else {
  172. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed with geometry at index ' + i + '. The geometry must have either an index or a position attribute' );
  173. return null;
  174. }
  175. mergedGeometry.addGroup( offset, count, i );
  176. offset += count;
  177. }
  178. }
  179. // merge indices
  180. if ( isIndexed ) {
  181. var indexOffset = 0;
  182. var mergedIndex = [];
  183. for ( var i = 0; i < geometries.length; ++ i ) {
  184. var index = geometries[ i ].index;
  185. for ( var j = 0; j < index.count; ++ j ) {
  186. mergedIndex.push( index.getX( j ) + indexOffset );
  187. }
  188. indexOffset += geometries[ i ].attributes.position.count;
  189. }
  190. mergedGeometry.setIndex( mergedIndex );
  191. }
  192. // merge attributes
  193. for ( var name in attributes ) {
  194. var mergedAttribute = this.mergeBufferAttributes( attributes[ name ] );
  195. if ( ! mergedAttribute ) {
  196. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed while trying to merge the ' + name + ' attribute.' );
  197. return null;
  198. }
  199. mergedGeometry.setAttribute( name, mergedAttribute );
  200. }
  201. // merge morph attributes
  202. for ( var name in morphAttributes ) {
  203. var numMorphTargets = morphAttributes[ name ][ 0 ].length;
  204. if ( numMorphTargets === 0 ) break;
  205. mergedGeometry.morphAttributes = mergedGeometry.morphAttributes || {};
  206. mergedGeometry.morphAttributes[ name ] = [];
  207. for ( var i = 0; i < numMorphTargets; ++ i ) {
  208. var morphAttributesToMerge = [];
  209. for ( var j = 0; j < morphAttributes[ name ].length; ++ j ) {
  210. morphAttributesToMerge.push( morphAttributes[ name ][ j ][ i ] );
  211. }
  212. var mergedMorphAttribute = this.mergeBufferAttributes( morphAttributesToMerge );
  213. if ( ! mergedMorphAttribute ) {
  214. console.error( 'THREE.BufferGeometryUtils: .mergeBufferGeometries() failed while trying to merge the ' + name + ' morphAttribute.' );
  215. return null;
  216. }
  217. mergedGeometry.morphAttributes[ name ].push( mergedMorphAttribute );
  218. }
  219. }
  220. return mergedGeometry;
  221. },
  222. /**
  223. * @param {Array<THREE.BufferAttribute>} attributes
  224. * @return {THREE.BufferAttribute}
  225. */
  226. mergeBufferAttributes: function ( attributes ) {
  227. var TypedArray;
  228. var itemSize;
  229. var normalized;
  230. var arrayLength = 0;
  231. for ( var i = 0; i < attributes.length; ++ i ) {
  232. var attribute = attributes[ i ];
  233. if ( attribute.isInterleavedBufferAttribute ) {
  234. console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. InterleavedBufferAttributes are not supported.' );
  235. return null;
  236. }
  237. if ( TypedArray === undefined ) TypedArray = attribute.array.constructor;
  238. if ( TypedArray !== attribute.array.constructor ) {
  239. console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.array must be of consistent array types across matching attributes.' );
  240. return null;
  241. }
  242. if ( itemSize === undefined ) itemSize = attribute.itemSize;
  243. if ( itemSize !== attribute.itemSize ) {
  244. console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.itemSize must be consistent across matching attributes.' );
  245. return null;
  246. }
  247. if ( normalized === undefined ) normalized = attribute.normalized;
  248. if ( normalized !== attribute.normalized ) {
  249. console.error( 'THREE.BufferGeometryUtils: .mergeBufferAttributes() failed. BufferAttribute.normalized must be consistent across matching attributes.' );
  250. return null;
  251. }
  252. arrayLength += attribute.array.length;
  253. }
  254. var array = new TypedArray( arrayLength );
  255. var offset = 0;
  256. for ( var i = 0; i < attributes.length; ++ i ) {
  257. array.set( attributes[ i ].array, offset );
  258. offset += attributes[ i ].array.length;
  259. }
  260. return new THREE.BufferAttribute( array, itemSize, normalized );
  261. },
  262. /**
  263. * @param {Array<THREE.BufferAttribute>} attributes
  264. * @return {Array<THREE.InterleavedBufferAttribute>}
  265. */
  266. interleaveAttributes: function ( attributes ) {
  267. // Interleaves the provided attributes into an InterleavedBuffer and returns
  268. // a set of InterleavedBufferAttributes for each attribute
  269. var TypedArray;
  270. var arrayLength = 0;
  271. var stride = 0;
  272. // calculate the the length and type of the interleavedBuffer
  273. for ( var i = 0, l = attributes.length; i < l; ++ i ) {
  274. var attribute = attributes[ i ];
  275. if ( TypedArray === undefined ) TypedArray = attribute.array.constructor;
  276. if ( TypedArray !== attribute.array.constructor ) {
  277. console.error( 'AttributeBuffers of different types cannot be interleaved' );
  278. return null;
  279. }
  280. arrayLength += attribute.array.length;
  281. stride += attribute.itemSize;
  282. }
  283. // Create the set of buffer attributes
  284. var interleavedBuffer = new THREE.InterleavedBuffer( new TypedArray( arrayLength ), stride );
  285. var offset = 0;
  286. var res = [];
  287. var getters = [ 'getX', 'getY', 'getZ', 'getW' ];
  288. var setters = [ 'setX', 'setY', 'setZ', 'setW' ];
  289. for ( var j = 0, l = attributes.length; j < l; j ++ ) {
  290. var attribute = attributes[ j ];
  291. var itemSize = attribute.itemSize;
  292. var count = attribute.count;
  293. var iba = new THREE.InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, attribute.normalized );
  294. res.push( iba );
  295. offset += itemSize;
  296. // Move the data for each attribute into the new interleavedBuffer
  297. // at the appropriate offset
  298. for ( var c = 0; c < count; c ++ ) {
  299. for ( var k = 0; k < itemSize; k ++ ) {
  300. iba[ setters[ k ] ]( c, attribute[ getters[ k ] ]( c ) );
  301. }
  302. }
  303. }
  304. return res;
  305. },
  306. /**
  307. * @param {Array<THREE.BufferGeometry>} geometry
  308. * @return {number}
  309. */
  310. estimateBytesUsed: function ( geometry ) {
  311. // Return the estimated memory used by this geometry in bytes
  312. // Calculate using itemSize, count, and BYTES_PER_ELEMENT to account
  313. // for InterleavedBufferAttributes.
  314. var mem = 0;
  315. for ( var name in geometry.attributes ) {
  316. var attr = geometry.getAttribute( name );
  317. mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
  318. }
  319. var indices = geometry.getIndex();
  320. mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;
  321. return mem;
  322. },
  323. /**
  324. * @param {THREE.BufferGeometry} geometry
  325. * @param {number} tolerance
  326. * @return {THREE.BufferGeometry>}
  327. */
  328. mergeVertices: function ( geometry, tolerance = 1e-4 ) {
  329. tolerance = Math.max( tolerance, Number.EPSILON );
  330. // Generate an index buffer if the geometry doesn't have one, or optimize it
  331. // if it's already available.
  332. var hashToIndex = {};
  333. var indices = geometry.getIndex();
  334. var positions = geometry.getAttribute( 'position' );
  335. var vertexCount = indices ? indices.count : positions.count;
  336. // next value for triangle indices
  337. var nextIndex = 0;
  338. // attributes and new attribute arrays
  339. var attributeNames = Object.keys( geometry.attributes );
  340. var attrArrays = {};
  341. var morphAttrsArrays = {};
  342. var newIndices = [];
  343. var getters = [ 'getX', 'getY', 'getZ', 'getW' ];
  344. // initialize the arrays
  345. for ( var i = 0, l = attributeNames.length; i < l; i ++ ) {
  346. var name = attributeNames[ i ];
  347. attrArrays[ name ] = [];
  348. var morphAttr = geometry.morphAttributes[ name ];
  349. if ( morphAttr ) {
  350. morphAttrsArrays[ name ] = new Array( morphAttr.length ).fill().map( () => [] );
  351. }
  352. }
  353. // convert the error tolerance to an amount of decimal places to truncate to
  354. var decimalShift = Math.log10( 1 / tolerance );
  355. var shiftMultiplier = Math.pow( 10, decimalShift );
  356. for ( var i = 0; i < vertexCount; i ++ ) {
  357. var index = indices ? indices.getX( i ) : i;
  358. // Generate a hash for the vertex attributes at the current index 'i'
  359. var hash = '';
  360. for ( var j = 0, l = attributeNames.length; j < l; j ++ ) {
  361. var name = attributeNames[ j ];
  362. var attribute = geometry.getAttribute( name );
  363. var itemSize = attribute.itemSize;
  364. for ( var k = 0; k < itemSize; k ++ ) {
  365. // double tilde truncates the decimal value
  366. hash += `${ ~ ~ ( attribute[ getters[ k ] ]( index ) * shiftMultiplier ) },`;
  367. }
  368. }
  369. // Add another reference to the vertex if it's already
  370. // used by another index
  371. if ( hash in hashToIndex ) {
  372. newIndices.push( hashToIndex[ hash ] );
  373. } else {
  374. // copy data to the new index in the attribute arrays
  375. for ( var j = 0, l = attributeNames.length; j < l; j ++ ) {
  376. var name = attributeNames[ j ];
  377. var attribute = geometry.getAttribute( name );
  378. var morphAttr = geometry.morphAttributes[ name ];
  379. var itemSize = attribute.itemSize;
  380. var newarray = attrArrays[ name ];
  381. var newMorphArrays = morphAttrsArrays[ name ];
  382. for ( var k = 0; k < itemSize; k ++ ) {
  383. var getterFunc = getters[ k ];
  384. newarray.push( attribute[ getterFunc ]( index ) );
  385. if ( morphAttr ) {
  386. for ( var m = 0, ml = morphAttr.length; m < ml; m ++ ) {
  387. newMorphArrays[ m ].push( morphAttr[ m ][ getterFunc ]( index ) );
  388. }
  389. }
  390. }
  391. }
  392. hashToIndex[ hash ] = nextIndex;
  393. newIndices.push( nextIndex );
  394. nextIndex ++;
  395. }
  396. }
  397. // Generate typed arrays from new attribute arrays and update
  398. // the attributeBuffers
  399. const result = geometry.clone();
  400. for ( var i = 0, l = attributeNames.length; i < l; i ++ ) {
  401. var name = attributeNames[ i ];
  402. var oldAttribute = geometry.getAttribute( name );
  403. var buffer = new oldAttribute.array.constructor( attrArrays[ name ] );
  404. var attribute = new THREE.BufferAttribute( buffer, oldAttribute.itemSize, oldAttribute.normalized );
  405. result.setAttribute( name, attribute );
  406. // Update the attribute arrays
  407. if ( name in morphAttrsArrays ) {
  408. for ( var j = 0; j < morphAttrsArrays[ name ].length; j ++ ) {
  409. var oldMorphAttribute = geometry.morphAttributes[ name ][ j ];
  410. var buffer = new oldMorphAttribute.array.constructor( morphAttrsArrays[ name ][ j ] );
  411. var morphAttribute = new THREE.BufferAttribute( buffer, oldMorphAttribute.itemSize, oldMorphAttribute.normalized );
  412. result.morphAttributes[ name ][ j ] = morphAttribute;
  413. }
  414. }
  415. }
  416. // indices
  417. result.setIndex( newIndices );
  418. return result;
  419. },
  420. /**
  421. * @param {THREE.BufferGeometry} geometry
  422. * @param {number} drawMode
  423. * @return {THREE.BufferGeometry>}
  424. */
  425. toTrianglesDrawMode: function ( geometry, drawMode ) {
  426. if ( drawMode === THREE.TrianglesDrawMode ) {
  427. console.warn( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Geometry already defined as triangles.' );
  428. return geometry;
  429. }
  430. if ( drawMode === THREE.TriangleFanDrawMode || drawMode === THREE.TriangleStripDrawMode ) {
  431. var index = geometry.getIndex();
  432. // generate index if not present
  433. if ( index === null ) {
  434. var indices = [];
  435. var position = geometry.getAttribute( 'position' );
  436. if ( position !== undefined ) {
  437. for ( var i = 0; i < position.count; i ++ ) {
  438. indices.push( i );
  439. }
  440. geometry.setIndex( indices );
  441. index = geometry.getIndex();
  442. } else {
  443. console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.' );
  444. return geometry;
  445. }
  446. }
  447. //
  448. var numberOfTriangles = index.count - 2;
  449. var newIndices = [];
  450. if ( drawMode === THREE.TriangleFanDrawMode ) {
  451. // gl.TRIANGLE_FAN
  452. for ( var i = 1; i <= numberOfTriangles; i ++ ) {
  453. newIndices.push( index.getX( 0 ) );
  454. newIndices.push( index.getX( i ) );
  455. newIndices.push( index.getX( i + 1 ) );
  456. }
  457. } else {
  458. // gl.TRIANGLE_STRIP
  459. for ( var i = 0; i < numberOfTriangles; i ++ ) {
  460. if ( i % 2 === 0 ) {
  461. newIndices.push( index.getX( i ) );
  462. newIndices.push( index.getX( i + 1 ) );
  463. newIndices.push( index.getX( i + 2 ) );
  464. } else {
  465. newIndices.push( index.getX( i + 2 ) );
  466. newIndices.push( index.getX( i + 1 ) );
  467. newIndices.push( index.getX( i ) );
  468. }
  469. }
  470. }
  471. if ( ( newIndices.length / 3 ) !== numberOfTriangles ) {
  472. console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unable to generate correct amount of triangles.' );
  473. }
  474. // build final geometry
  475. var newGeometry = geometry.clone();
  476. newGeometry.setIndex( newIndices );
  477. newGeometry.clearGroups();
  478. return newGeometry;
  479. } else {
  480. console.error( 'THREE.BufferGeometryUtils.toTrianglesDrawMode(): Unknown draw mode:', drawMode );
  481. return geometry;
  482. }
  483. }
  484. };