BufferGeometryUtils.js 19 KB

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