Geometry.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. ( function () {
  2. const _m1 = new THREE.Matrix4();
  3. const _obj = new THREE.Object3D();
  4. const _offset = new THREE.Vector3();
  5. class Geometry extends THREE.EventDispatcher {
  6. constructor() {
  7. super();
  8. this.uuid = THREE.MathUtils.generateUUID();
  9. this.name = '';
  10. this.type = 'Geometry';
  11. this.vertices = [];
  12. this.colors = [];
  13. this.faces = [];
  14. this.faceVertexUvs = [[]];
  15. this.morphTargets = [];
  16. this.morphNormals = [];
  17. this.skinWeights = [];
  18. this.skinIndices = [];
  19. this.lineDistances = [];
  20. this.boundingBox = null;
  21. this.boundingSphere = null; // update flags
  22. this.elementsNeedUpdate = false;
  23. this.verticesNeedUpdate = false;
  24. this.uvsNeedUpdate = false;
  25. this.normalsNeedUpdate = false;
  26. this.colorsNeedUpdate = false;
  27. this.lineDistancesNeedUpdate = false;
  28. this.groupsNeedUpdate = false;
  29. }
  30. applyMatrix4( matrix ) {
  31. const normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  32. for ( let i = 0, il = this.vertices.length; i < il; i ++ ) {
  33. const vertex = this.vertices[ i ];
  34. vertex.applyMatrix4( matrix );
  35. }
  36. for ( let i = 0, il = this.faces.length; i < il; i ++ ) {
  37. const face = this.faces[ i ];
  38. face.normal.applyMatrix3( normalMatrix ).normalize();
  39. for ( let j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  40. face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
  41. }
  42. }
  43. if ( this.boundingBox !== null ) {
  44. this.computeBoundingBox();
  45. }
  46. if ( this.boundingSphere !== null ) {
  47. this.computeBoundingSphere();
  48. }
  49. this.verticesNeedUpdate = true;
  50. this.normalsNeedUpdate = true;
  51. return this;
  52. }
  53. rotateX( angle ) {
  54. // rotate geometry around world x-axis
  55. _m1.makeRotationX( angle );
  56. this.applyMatrix4( _m1 );
  57. return this;
  58. }
  59. rotateY( angle ) {
  60. // rotate geometry around world y-axis
  61. _m1.makeRotationY( angle );
  62. this.applyMatrix4( _m1 );
  63. return this;
  64. }
  65. rotateZ( angle ) {
  66. // rotate geometry around world z-axis
  67. _m1.makeRotationZ( angle );
  68. this.applyMatrix4( _m1 );
  69. return this;
  70. }
  71. translate( x, y, z ) {
  72. // translate geometry
  73. _m1.makeTranslation( x, y, z );
  74. this.applyMatrix4( _m1 );
  75. return this;
  76. }
  77. scale( x, y, z ) {
  78. // scale geometry
  79. _m1.makeScale( x, y, z );
  80. this.applyMatrix4( _m1 );
  81. return this;
  82. }
  83. lookAt( vector ) {
  84. _obj.lookAt( vector );
  85. _obj.updateMatrix();
  86. this.applyMatrix4( _obj.matrix );
  87. return this;
  88. }
  89. fromBufferGeometry( geometry ) {
  90. const scope = this;
  91. const index = geometry.index !== null ? geometry.index : undefined;
  92. const attributes = geometry.attributes;
  93. if ( attributes.position === undefined ) {
  94. console.error( 'THREE.Geometry.fromBufferGeometry(): Position attribute required for conversion.' );
  95. return this;
  96. }
  97. const position = attributes.position;
  98. const normal = attributes.normal;
  99. const color = attributes.color;
  100. const uv = attributes.uv;
  101. const uv2 = attributes.uv2;
  102. if ( uv2 !== undefined ) this.faceVertexUvs[ 1 ] = [];
  103. for ( let i = 0; i < position.count; i ++ ) {
  104. scope.vertices.push( new THREE.Vector3().fromBufferAttribute( position, i ) );
  105. if ( color !== undefined ) {
  106. scope.colors.push( new THREE.Color().fromBufferAttribute( color, i ) );
  107. }
  108. }
  109. function addFace( a, b, c, materialIndex ) {
  110. const vertexColors = color === undefined ? [] : [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ];
  111. const vertexNormals = normal === undefined ? [] : [ new THREE.Vector3().fromBufferAttribute( normal, a ), new THREE.Vector3().fromBufferAttribute( normal, b ), new THREE.Vector3().fromBufferAttribute( normal, c ) ];
  112. const face = new Face3( a, b, c, vertexNormals, vertexColors, materialIndex );
  113. scope.faces.push( face );
  114. if ( uv !== undefined ) {
  115. scope.faceVertexUvs[ 0 ].push( [ new THREE.Vector2().fromBufferAttribute( uv, a ), new THREE.Vector2().fromBufferAttribute( uv, b ), new THREE.Vector2().fromBufferAttribute( uv, c ) ] );
  116. }
  117. if ( uv2 !== undefined ) {
  118. scope.faceVertexUvs[ 1 ].push( [ new THREE.Vector2().fromBufferAttribute( uv2, a ), new THREE.Vector2().fromBufferAttribute( uv2, b ), new THREE.Vector2().fromBufferAttribute( uv2, c ) ] );
  119. }
  120. }
  121. const groups = geometry.groups;
  122. if ( groups.length > 0 ) {
  123. for ( let i = 0; i < groups.length; i ++ ) {
  124. const group = groups[ i ];
  125. const start = group.start;
  126. const count = group.count;
  127. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  128. if ( index !== undefined ) {
  129. addFace( index.getX( j ), index.getX( j + 1 ), index.getX( j + 2 ), group.materialIndex );
  130. } else {
  131. addFace( j, j + 1, j + 2, group.materialIndex );
  132. }
  133. }
  134. }
  135. } else {
  136. if ( index !== undefined ) {
  137. for ( let i = 0; i < index.count; i += 3 ) {
  138. addFace( index.getX( i ), index.getX( i + 1 ), index.getX( i + 2 ) );
  139. }
  140. } else {
  141. for ( let i = 0; i < position.count; i += 3 ) {
  142. addFace( i, i + 1, i + 2 );
  143. }
  144. }
  145. }
  146. this.computeFaceNormals();
  147. if ( geometry.boundingBox !== null ) {
  148. this.boundingBox = geometry.boundingBox.clone();
  149. }
  150. if ( geometry.boundingSphere !== null ) {
  151. this.boundingSphere = geometry.boundingSphere.clone();
  152. }
  153. return this;
  154. }
  155. center() {
  156. this.computeBoundingBox();
  157. this.boundingBox.getCenter( _offset ).negate();
  158. this.translate( _offset.x, _offset.y, _offset.z );
  159. return this;
  160. }
  161. normalize() {
  162. this.computeBoundingSphere();
  163. const center = this.boundingSphere.center;
  164. const radius = this.boundingSphere.radius;
  165. const s = radius === 0 ? 1 : 1.0 / radius;
  166. const matrix = new THREE.Matrix4();
  167. matrix.set( s, 0, 0, - s * center.x, 0, s, 0, - s * center.y, 0, 0, s, - s * center.z, 0, 0, 0, 1 );
  168. this.applyMatrix4( matrix );
  169. return this;
  170. }
  171. computeFaceNormals() {
  172. const cb = new THREE.Vector3(),
  173. ab = new THREE.Vector3();
  174. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  175. const face = this.faces[ f ];
  176. const vA = this.vertices[ face.a ];
  177. const vB = this.vertices[ face.b ];
  178. const vC = this.vertices[ face.c ];
  179. cb.subVectors( vC, vB );
  180. ab.subVectors( vA, vB );
  181. cb.cross( ab );
  182. cb.normalize();
  183. face.normal.copy( cb );
  184. }
  185. }
  186. computeVertexNormals( areaWeighted = true ) {
  187. const vertices = new Array( this.vertices.length );
  188. for ( let v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  189. vertices[ v ] = new THREE.Vector3();
  190. }
  191. if ( areaWeighted ) {
  192. // vertex normals weighted by triangle areas
  193. // http://www.iquilezles.org/www/articles/normals/normals.htm
  194. const cb = new THREE.Vector3(),
  195. ab = new THREE.Vector3();
  196. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  197. const face = this.faces[ f ];
  198. const vA = this.vertices[ face.a ];
  199. const vB = this.vertices[ face.b ];
  200. const vC = this.vertices[ face.c ];
  201. cb.subVectors( vC, vB );
  202. ab.subVectors( vA, vB );
  203. cb.cross( ab );
  204. vertices[ face.a ].add( cb );
  205. vertices[ face.b ].add( cb );
  206. vertices[ face.c ].add( cb );
  207. }
  208. } else {
  209. this.computeFaceNormals();
  210. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  211. const face = this.faces[ f ];
  212. vertices[ face.a ].add( face.normal );
  213. vertices[ face.b ].add( face.normal );
  214. vertices[ face.c ].add( face.normal );
  215. }
  216. }
  217. for ( let v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  218. vertices[ v ].normalize();
  219. }
  220. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  221. const face = this.faces[ f ];
  222. const vertexNormals = face.vertexNormals;
  223. if ( vertexNormals.length === 3 ) {
  224. vertexNormals[ 0 ].copy( vertices[ face.a ] );
  225. vertexNormals[ 1 ].copy( vertices[ face.b ] );
  226. vertexNormals[ 2 ].copy( vertices[ face.c ] );
  227. } else {
  228. vertexNormals[ 0 ] = vertices[ face.a ].clone();
  229. vertexNormals[ 1 ] = vertices[ face.b ].clone();
  230. vertexNormals[ 2 ] = vertices[ face.c ].clone();
  231. }
  232. }
  233. if ( this.faces.length > 0 ) {
  234. this.normalsNeedUpdate = true;
  235. }
  236. }
  237. computeFlatVertexNormals() {
  238. this.computeFaceNormals();
  239. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  240. const face = this.faces[ f ];
  241. const vertexNormals = face.vertexNormals;
  242. if ( vertexNormals.length === 3 ) {
  243. vertexNormals[ 0 ].copy( face.normal );
  244. vertexNormals[ 1 ].copy( face.normal );
  245. vertexNormals[ 2 ].copy( face.normal );
  246. } else {
  247. vertexNormals[ 0 ] = face.normal.clone();
  248. vertexNormals[ 1 ] = face.normal.clone();
  249. vertexNormals[ 2 ] = face.normal.clone();
  250. }
  251. }
  252. if ( this.faces.length > 0 ) {
  253. this.normalsNeedUpdate = true;
  254. }
  255. }
  256. computeMorphNormals() {
  257. // save original normals
  258. // - create temp variables on first access
  259. // otherwise just copy (for faster repeated calls)
  260. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  261. const face = this.faces[ f ];
  262. if ( ! face.__originalFaceNormal ) {
  263. face.__originalFaceNormal = face.normal.clone();
  264. } else {
  265. face.__originalFaceNormal.copy( face.normal );
  266. }
  267. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  268. for ( let i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  269. if ( ! face.__originalVertexNormals[ i ] ) {
  270. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  271. } else {
  272. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  273. }
  274. }
  275. } // use temp geometry to compute face and vertex normals for each morph
  276. const tmpGeo = new Geometry();
  277. tmpGeo.faces = this.faces;
  278. for ( let i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  279. // create on first access
  280. if ( ! this.morphNormals[ i ] ) {
  281. this.morphNormals[ i ] = {};
  282. this.morphNormals[ i ].faceNormals = [];
  283. this.morphNormals[ i ].vertexNormals = [];
  284. const dstNormalsFace = this.morphNormals[ i ].faceNormals;
  285. const dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  286. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  287. const faceNormal = new THREE.Vector3();
  288. const vertexNormals = {
  289. a: new THREE.Vector3(),
  290. b: new THREE.Vector3(),
  291. c: new THREE.Vector3()
  292. };
  293. dstNormalsFace.push( faceNormal );
  294. dstNormalsVertex.push( vertexNormals );
  295. }
  296. }
  297. const morphNormals = this.morphNormals[ i ]; // set vertices to morph target
  298. tmpGeo.vertices = this.morphTargets[ i ].vertices; // compute morph normals
  299. tmpGeo.computeFaceNormals();
  300. tmpGeo.computeVertexNormals(); // store morph normals
  301. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  302. const face = this.faces[ f ];
  303. const faceNormal = morphNormals.faceNormals[ f ];
  304. const vertexNormals = morphNormals.vertexNormals[ f ];
  305. faceNormal.copy( face.normal );
  306. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  307. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  308. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  309. }
  310. } // restore original normals
  311. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  312. const face = this.faces[ f ];
  313. face.normal = face.__originalFaceNormal;
  314. face.vertexNormals = face.__originalVertexNormals;
  315. }
  316. }
  317. computeBoundingBox() {
  318. if ( this.boundingBox === null ) {
  319. this.boundingBox = new THREE.Box3();
  320. }
  321. this.boundingBox.setFromPoints( this.vertices );
  322. }
  323. computeBoundingSphere() {
  324. if ( this.boundingSphere === null ) {
  325. this.boundingSphere = new THREE.Sphere();
  326. }
  327. this.boundingSphere.setFromPoints( this.vertices );
  328. }
  329. merge( geometry, matrix, materialIndexOffset = 0 ) {
  330. if ( ! ( geometry && geometry.isGeometry ) ) {
  331. console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
  332. return;
  333. }
  334. let normalMatrix;
  335. const vertexOffset = this.vertices.length,
  336. vertices1 = this.vertices,
  337. vertices2 = geometry.vertices,
  338. faces1 = this.faces,
  339. faces2 = geometry.faces,
  340. colors1 = this.colors,
  341. colors2 = geometry.colors;
  342. if ( matrix !== undefined ) {
  343. normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  344. } // vertices
  345. for ( let i = 0, il = vertices2.length; i < il; i ++ ) {
  346. const vertex = vertices2[ i ];
  347. const vertexCopy = vertex.clone();
  348. if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
  349. vertices1.push( vertexCopy );
  350. } // colors
  351. for ( let i = 0, il = colors2.length; i < il; i ++ ) {
  352. colors1.push( colors2[ i ].clone() );
  353. } // faces
  354. for ( let i = 0, il = faces2.length; i < il; i ++ ) {
  355. const face = faces2[ i ];
  356. let normal, color;
  357. const faceVertexNormals = face.vertexNormals,
  358. faceVertexColors = face.vertexColors;
  359. const faceCopy = new Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  360. faceCopy.normal.copy( face.normal );
  361. if ( normalMatrix !== undefined ) {
  362. faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
  363. }
  364. for ( let j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  365. normal = faceVertexNormals[ j ].clone();
  366. if ( normalMatrix !== undefined ) {
  367. normal.applyMatrix3( normalMatrix ).normalize();
  368. }
  369. faceCopy.vertexNormals.push( normal );
  370. }
  371. faceCopy.color.copy( face.color );
  372. for ( let j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  373. color = faceVertexColors[ j ];
  374. faceCopy.vertexColors.push( color.clone() );
  375. }
  376. faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
  377. faces1.push( faceCopy );
  378. } // uvs
  379. for ( let i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) {
  380. const faceVertexUvs2 = geometry.faceVertexUvs[ i ];
  381. if ( this.faceVertexUvs[ i ] === undefined ) this.faceVertexUvs[ i ] = [];
  382. for ( let j = 0, jl = faceVertexUvs2.length; j < jl; j ++ ) {
  383. const uvs2 = faceVertexUvs2[ j ],
  384. uvsCopy = [];
  385. for ( let k = 0, kl = uvs2.length; k < kl; k ++ ) {
  386. uvsCopy.push( uvs2[ k ].clone() );
  387. }
  388. this.faceVertexUvs[ i ].push( uvsCopy );
  389. }
  390. }
  391. }
  392. mergeMesh( mesh ) {
  393. if ( ! ( mesh && mesh.isMesh ) ) {
  394. console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
  395. return;
  396. }
  397. if ( mesh.matrixAutoUpdate ) mesh.updateMatrix();
  398. this.merge( mesh.geometry, mesh.matrix );
  399. }
  400. /*
  401. * Checks for duplicate vertices with hashmap.
  402. * Duplicated vertices are removed
  403. * and faces' vertices are updated.
  404. */
  405. mergeVertices( precisionPoints = 4 ) {
  406. const verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique)
  407. const unique = [],
  408. changes = [];
  409. const precision = Math.pow( 10, precisionPoints );
  410. for ( let i = 0, il = this.vertices.length; i < il; i ++ ) {
  411. const v = this.vertices[ i ];
  412. const key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
  413. if ( verticesMap[ key ] === undefined ) {
  414. verticesMap[ key ] = i;
  415. unique.push( this.vertices[ i ] );
  416. changes[ i ] = unique.length - 1;
  417. } else {
  418. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  419. changes[ i ] = changes[ verticesMap[ key ] ];
  420. }
  421. } // if faces are completely degenerate after merging vertices, we
  422. // have to remove them from the geometry.
  423. const faceIndicesToRemove = [];
  424. for ( let i = 0, il = this.faces.length; i < il; i ++ ) {
  425. const face = this.faces[ i ];
  426. face.a = changes[ face.a ];
  427. face.b = changes[ face.b ];
  428. face.c = changes[ face.c ];
  429. const indices = [ face.a, face.b, face.c ]; // if any duplicate vertices are found in a Face3
  430. // we have to remove the face as nothing can be saved
  431. for ( let n = 0; n < 3; n ++ ) {
  432. if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
  433. faceIndicesToRemove.push( i );
  434. break;
  435. }
  436. }
  437. }
  438. for ( let i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
  439. const idx = faceIndicesToRemove[ i ];
  440. this.faces.splice( idx, 1 );
  441. for ( let j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
  442. this.faceVertexUvs[ j ].splice( idx, 1 );
  443. }
  444. } // Use unique set of vertices
  445. const diff = this.vertices.length - unique.length;
  446. this.vertices = unique;
  447. return diff;
  448. }
  449. setFromPoints( points ) {
  450. this.vertices = [];
  451. for ( let i = 0, l = points.length; i < l; i ++ ) {
  452. const point = points[ i ];
  453. this.vertices.push( new THREE.Vector3( point.x, point.y, point.z || 0 ) );
  454. }
  455. return this;
  456. }
  457. sortFacesByMaterialIndex() {
  458. const faces = this.faces;
  459. const length = faces.length; // tag faces
  460. for ( let i = 0; i < length; i ++ ) {
  461. faces[ i ]._id = i;
  462. } // sort faces
  463. function materialIndexSort( a, b ) {
  464. return a.materialIndex - b.materialIndex;
  465. }
  466. faces.sort( materialIndexSort ); // sort uvs
  467. const uvs1 = this.faceVertexUvs[ 0 ];
  468. const uvs2 = this.faceVertexUvs[ 1 ];
  469. let newUvs1, newUvs2;
  470. if ( uvs1 && uvs1.length === length ) newUvs1 = [];
  471. if ( uvs2 && uvs2.length === length ) newUvs2 = [];
  472. for ( let i = 0; i < length; i ++ ) {
  473. const id = faces[ i ]._id;
  474. if ( newUvs1 ) newUvs1.push( uvs1[ id ] );
  475. if ( newUvs2 ) newUvs2.push( uvs2[ id ] );
  476. }
  477. if ( newUvs1 ) this.faceVertexUvs[ 0 ] = newUvs1;
  478. if ( newUvs2 ) this.faceVertexUvs[ 1 ] = newUvs2;
  479. }
  480. toJSON() {
  481. const data = {
  482. metadata: {
  483. version: 4.5,
  484. type: 'Geometry',
  485. generator: 'Geometry.toJSON'
  486. }
  487. }; // standard Geometry serialization
  488. data.uuid = this.uuid;
  489. data.type = this.type;
  490. if ( this.name !== '' ) data.name = this.name;
  491. if ( this.parameters !== undefined ) {
  492. const parameters = this.parameters;
  493. for ( const key in parameters ) {
  494. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  495. }
  496. return data;
  497. }
  498. const vertices = [];
  499. for ( let i = 0; i < this.vertices.length; i ++ ) {
  500. const vertex = this.vertices[ i ];
  501. vertices.push( vertex.x, vertex.y, vertex.z );
  502. }
  503. const faces = [];
  504. const normals = [];
  505. const normalsHash = {};
  506. const colors = [];
  507. const colorsHash = {};
  508. const uvs = [];
  509. const uvsHash = {};
  510. for ( let i = 0; i < this.faces.length; i ++ ) {
  511. const face = this.faces[ i ];
  512. const hasMaterial = true;
  513. const hasFaceUv = false; // deprecated
  514. const hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
  515. const hasFaceNormal = face.normal.length() > 0;
  516. const hasFaceVertexNormal = face.vertexNormals.length > 0;
  517. const hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  518. const hasFaceVertexColor = face.vertexColors.length > 0;
  519. let faceType = 0;
  520. faceType = setBit( faceType, 0, 0 ); // isQuad
  521. faceType = setBit( faceType, 1, hasMaterial );
  522. faceType = setBit( faceType, 2, hasFaceUv );
  523. faceType = setBit( faceType, 3, hasFaceVertexUv );
  524. faceType = setBit( faceType, 4, hasFaceNormal );
  525. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  526. faceType = setBit( faceType, 6, hasFaceColor );
  527. faceType = setBit( faceType, 7, hasFaceVertexColor );
  528. faces.push( faceType );
  529. faces.push( face.a, face.b, face.c );
  530. faces.push( face.materialIndex );
  531. if ( hasFaceVertexUv ) {
  532. const faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
  533. faces.push( getUvIndex( faceVertexUvs[ 0 ] ), getUvIndex( faceVertexUvs[ 1 ] ), getUvIndex( faceVertexUvs[ 2 ] ) );
  534. }
  535. if ( hasFaceNormal ) {
  536. faces.push( getNormalIndex( face.normal ) );
  537. }
  538. if ( hasFaceVertexNormal ) {
  539. const vertexNormals = face.vertexNormals;
  540. faces.push( getNormalIndex( vertexNormals[ 0 ] ), getNormalIndex( vertexNormals[ 1 ] ), getNormalIndex( vertexNormals[ 2 ] ) );
  541. }
  542. if ( hasFaceColor ) {
  543. faces.push( getColorIndex( face.color ) );
  544. }
  545. if ( hasFaceVertexColor ) {
  546. const vertexColors = face.vertexColors;
  547. faces.push( getColorIndex( vertexColors[ 0 ] ), getColorIndex( vertexColors[ 1 ] ), getColorIndex( vertexColors[ 2 ] ) );
  548. }
  549. }
  550. function setBit( value, position, enabled ) {
  551. return enabled ? value | 1 << position : value & ~ ( 1 << position );
  552. }
  553. function getNormalIndex( normal ) {
  554. const hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  555. if ( normalsHash[ hash ] !== undefined ) {
  556. return normalsHash[ hash ];
  557. }
  558. normalsHash[ hash ] = normals.length / 3;
  559. normals.push( normal.x, normal.y, normal.z );
  560. return normalsHash[ hash ];
  561. }
  562. function getColorIndex( color ) {
  563. const hash = color.r.toString() + color.g.toString() + color.b.toString();
  564. if ( colorsHash[ hash ] !== undefined ) {
  565. return colorsHash[ hash ];
  566. }
  567. colorsHash[ hash ] = colors.length;
  568. colors.push( color.getHex() );
  569. return colorsHash[ hash ];
  570. }
  571. function getUvIndex( uv ) {
  572. const hash = uv.x.toString() + uv.y.toString();
  573. if ( uvsHash[ hash ] !== undefined ) {
  574. return uvsHash[ hash ];
  575. }
  576. uvsHash[ hash ] = uvs.length / 2;
  577. uvs.push( uv.x, uv.y );
  578. return uvsHash[ hash ];
  579. }
  580. data.data = {};
  581. data.data.vertices = vertices;
  582. data.data.normals = normals;
  583. if ( colors.length > 0 ) data.data.colors = colors;
  584. if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility
  585. data.data.faces = faces;
  586. return data;
  587. }
  588. clone() {
  589. /*
  590. // Handle primitives
  591. const parameters = this.parameters;
  592. if ( parameters !== undefined ) {
  593. const values = [];
  594. for ( const key in parameters ) {
  595. values.push( parameters[ key ] );
  596. }
  597. const geometry = Object.create( this.constructor.prototype );
  598. this.constructor.apply( geometry, values );
  599. return geometry;
  600. }
  601. return new this.constructor().copy( this );
  602. */
  603. return new Geometry().copy( this );
  604. }
  605. copy( source ) {
  606. // reset
  607. this.vertices = [];
  608. this.colors = [];
  609. this.faces = [];
  610. this.faceVertexUvs = [[]];
  611. this.morphTargets = [];
  612. this.morphNormals = [];
  613. this.skinWeights = [];
  614. this.skinIndices = [];
  615. this.lineDistances = [];
  616. this.boundingBox = null;
  617. this.boundingSphere = null; // name
  618. this.name = source.name; // vertices
  619. const vertices = source.vertices;
  620. for ( let i = 0, il = vertices.length; i < il; i ++ ) {
  621. this.vertices.push( vertices[ i ].clone() );
  622. } // colors
  623. const colors = source.colors;
  624. for ( let i = 0, il = colors.length; i < il; i ++ ) {
  625. this.colors.push( colors[ i ].clone() );
  626. } // faces
  627. const faces = source.faces;
  628. for ( let i = 0, il = faces.length; i < il; i ++ ) {
  629. this.faces.push( faces[ i ].clone() );
  630. } // face vertex uvs
  631. for ( let i = 0, il = source.faceVertexUvs.length; i < il; i ++ ) {
  632. const faceVertexUvs = source.faceVertexUvs[ i ];
  633. if ( this.faceVertexUvs[ i ] === undefined ) {
  634. this.faceVertexUvs[ i ] = [];
  635. }
  636. for ( let j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {
  637. const uvs = faceVertexUvs[ j ],
  638. uvsCopy = [];
  639. for ( let k = 0, kl = uvs.length; k < kl; k ++ ) {
  640. const uv = uvs[ k ];
  641. uvsCopy.push( uv.clone() );
  642. }
  643. this.faceVertexUvs[ i ].push( uvsCopy );
  644. }
  645. } // morph targets
  646. const morphTargets = source.morphTargets;
  647. for ( let i = 0, il = morphTargets.length; i < il; i ++ ) {
  648. const morphTarget = {};
  649. morphTarget.name = morphTargets[ i ].name; // vertices
  650. if ( morphTargets[ i ].vertices !== undefined ) {
  651. morphTarget.vertices = [];
  652. for ( let j = 0, jl = morphTargets[ i ].vertices.length; j < jl; j ++ ) {
  653. morphTarget.vertices.push( morphTargets[ i ].vertices[ j ].clone() );
  654. }
  655. } // normals
  656. if ( morphTargets[ i ].normals !== undefined ) {
  657. morphTarget.normals = [];
  658. for ( let j = 0, jl = morphTargets[ i ].normals.length; j < jl; j ++ ) {
  659. morphTarget.normals.push( morphTargets[ i ].normals[ j ].clone() );
  660. }
  661. }
  662. this.morphTargets.push( morphTarget );
  663. } // morph normals
  664. const morphNormals = source.morphNormals;
  665. for ( let i = 0, il = morphNormals.length; i < il; i ++ ) {
  666. const morphNormal = {}; // vertex normals
  667. if ( morphNormals[ i ].vertexNormals !== undefined ) {
  668. morphNormal.vertexNormals = [];
  669. for ( let j = 0, jl = morphNormals[ i ].vertexNormals.length; j < jl; j ++ ) {
  670. const srcVertexNormal = morphNormals[ i ].vertexNormals[ j ];
  671. const destVertexNormal = {};
  672. destVertexNormal.a = srcVertexNormal.a.clone();
  673. destVertexNormal.b = srcVertexNormal.b.clone();
  674. destVertexNormal.c = srcVertexNormal.c.clone();
  675. morphNormal.vertexNormals.push( destVertexNormal );
  676. }
  677. } // face normals
  678. if ( morphNormals[ i ].faceNormals !== undefined ) {
  679. morphNormal.faceNormals = [];
  680. for ( let j = 0, jl = morphNormals[ i ].faceNormals.length; j < jl; j ++ ) {
  681. morphNormal.faceNormals.push( morphNormals[ i ].faceNormals[ j ].clone() );
  682. }
  683. }
  684. this.morphNormals.push( morphNormal );
  685. } // skin weights
  686. const skinWeights = source.skinWeights;
  687. for ( let i = 0, il = skinWeights.length; i < il; i ++ ) {
  688. this.skinWeights.push( skinWeights[ i ].clone() );
  689. } // skin indices
  690. const skinIndices = source.skinIndices;
  691. for ( let i = 0, il = skinIndices.length; i < il; i ++ ) {
  692. this.skinIndices.push( skinIndices[ i ].clone() );
  693. } // line distances
  694. const lineDistances = source.lineDistances;
  695. for ( let i = 0, il = lineDistances.length; i < il; i ++ ) {
  696. this.lineDistances.push( lineDistances[ i ] );
  697. } // bounding box
  698. const boundingBox = source.boundingBox;
  699. if ( boundingBox !== null ) {
  700. this.boundingBox = boundingBox.clone();
  701. } // bounding sphere
  702. const boundingSphere = source.boundingSphere;
  703. if ( boundingSphere !== null ) {
  704. this.boundingSphere = boundingSphere.clone();
  705. } // update flags
  706. this.elementsNeedUpdate = source.elementsNeedUpdate;
  707. this.verticesNeedUpdate = source.verticesNeedUpdate;
  708. this.uvsNeedUpdate = source.uvsNeedUpdate;
  709. this.normalsNeedUpdate = source.normalsNeedUpdate;
  710. this.colorsNeedUpdate = source.colorsNeedUpdate;
  711. this.lineDistancesNeedUpdate = source.lineDistancesNeedUpdate;
  712. this.groupsNeedUpdate = source.groupsNeedUpdate;
  713. return this;
  714. }
  715. toBufferGeometry() {
  716. const geometry = new DirectGeometry().fromGeometry( this );
  717. const buffergeometry = new THREE.BufferGeometry();
  718. const positions = new Float32Array( geometry.vertices.length * 3 );
  719. buffergeometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
  720. if ( geometry.normals.length > 0 ) {
  721. const normals = new Float32Array( geometry.normals.length * 3 );
  722. buffergeometry.setAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
  723. }
  724. if ( geometry.colors.length > 0 ) {
  725. const colors = new Float32Array( geometry.colors.length * 3 );
  726. buffergeometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
  727. }
  728. if ( geometry.uvs.length > 0 ) {
  729. const uvs = new Float32Array( geometry.uvs.length * 2 );
  730. buffergeometry.setAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
  731. }
  732. if ( geometry.uvs2.length > 0 ) {
  733. const uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  734. buffergeometry.setAttribute( 'uv2', new THREE.BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
  735. } // groups
  736. buffergeometry.groups = geometry.groups; // morphs
  737. for ( const name in geometry.morphTargets ) {
  738. const array = [];
  739. const morphTargets = geometry.morphTargets[ name ];
  740. for ( let i = 0, l = morphTargets.length; i < l; i ++ ) {
  741. const morphTarget = morphTargets[ i ];
  742. const attribute = new THREE.Float32BufferAttribute( morphTarget.data.length * 3, 3 );
  743. attribute.name = morphTarget.name;
  744. array.push( attribute.copyVector3sArray( morphTarget.data ) );
  745. }
  746. buffergeometry.morphAttributes[ name ] = array;
  747. } // skinning
  748. if ( geometry.skinIndices.length > 0 ) {
  749. const skinIndices = new THREE.Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
  750. buffergeometry.setAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
  751. }
  752. if ( geometry.skinWeights.length > 0 ) {
  753. const skinWeights = new THREE.Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
  754. buffergeometry.setAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
  755. } //
  756. if ( geometry.boundingSphere !== null ) {
  757. buffergeometry.boundingSphere = geometry.boundingSphere.clone();
  758. }
  759. if ( geometry.boundingBox !== null ) {
  760. buffergeometry.boundingBox = geometry.boundingBox.clone();
  761. }
  762. return buffergeometry;
  763. }
  764. computeTangents() {
  765. console.error( 'THREE.Geometry: .computeTangents() has been removed.' );
  766. }
  767. computeLineDistances() {
  768. console.error( 'THREE.Geometry: .computeLineDistances() has been removed. Use THREE.Line.computeLineDistances() instead.' );
  769. }
  770. applyMatrix( matrix ) {
  771. console.warn( 'THREE.Geometry: .applyMatrix() has been renamed to .applyMatrix4().' );
  772. return this.applyMatrix4( matrix );
  773. }
  774. dispose() {
  775. this.dispatchEvent( {
  776. type: 'dispose'
  777. } );
  778. }
  779. static createBufferGeometryFromObject( object ) {
  780. let buffergeometry = new THREE.BufferGeometry();
  781. const geometry = object.geometry;
  782. if ( object.isPoints || object.isLine ) {
  783. const positions = new THREE.Float32BufferAttribute( geometry.vertices.length * 3, 3 );
  784. const colors = new THREE.Float32BufferAttribute( geometry.colors.length * 3, 3 );
  785. buffergeometry.setAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
  786. buffergeometry.setAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
  787. if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
  788. const lineDistances = new THREE.Float32BufferAttribute( geometry.lineDistances.length, 1 );
  789. buffergeometry.setAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
  790. }
  791. if ( geometry.boundingSphere !== null ) {
  792. buffergeometry.boundingSphere = geometry.boundingSphere.clone();
  793. }
  794. if ( geometry.boundingBox !== null ) {
  795. buffergeometry.boundingBox = geometry.boundingBox.clone();
  796. }
  797. } else if ( object.isMesh ) {
  798. buffergeometry = geometry.toBufferGeometry();
  799. }
  800. return buffergeometry;
  801. }
  802. }
  803. Geometry.prototype.isGeometry = true;
  804. class DirectGeometry {
  805. constructor() {
  806. this.vertices = [];
  807. this.normals = [];
  808. this.colors = [];
  809. this.uvs = [];
  810. this.uvs2 = [];
  811. this.groups = [];
  812. this.morphTargets = {};
  813. this.skinWeights = [];
  814. this.skinIndices = []; // this.lineDistances = [];
  815. this.boundingBox = null;
  816. this.boundingSphere = null; // update flags
  817. this.verticesNeedUpdate = false;
  818. this.normalsNeedUpdate = false;
  819. this.colorsNeedUpdate = false;
  820. this.uvsNeedUpdate = false;
  821. this.groupsNeedUpdate = false;
  822. }
  823. computeGroups( geometry ) {
  824. const groups = [];
  825. let group, i;
  826. let materialIndex = undefined;
  827. const faces = geometry.faces;
  828. for ( i = 0; i < faces.length; i ++ ) {
  829. const face = faces[ i ]; // materials
  830. if ( face.materialIndex !== materialIndex ) {
  831. materialIndex = face.materialIndex;
  832. if ( group !== undefined ) {
  833. group.count = i * 3 - group.start;
  834. groups.push( group );
  835. }
  836. group = {
  837. start: i * 3,
  838. materialIndex: materialIndex
  839. };
  840. }
  841. }
  842. if ( group !== undefined ) {
  843. group.count = i * 3 - group.start;
  844. groups.push( group );
  845. }
  846. this.groups = groups;
  847. }
  848. fromGeometry( geometry ) {
  849. const faces = geometry.faces;
  850. const vertices = geometry.vertices;
  851. const faceVertexUvs = geometry.faceVertexUvs;
  852. const hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
  853. const hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0; // morphs
  854. const morphTargets = geometry.morphTargets;
  855. const morphTargetsLength = morphTargets.length;
  856. let morphTargetsPosition;
  857. if ( morphTargetsLength > 0 ) {
  858. morphTargetsPosition = [];
  859. for ( let i = 0; i < morphTargetsLength; i ++ ) {
  860. morphTargetsPosition[ i ] = {
  861. name: morphTargets[ i ].name,
  862. data: []
  863. };
  864. }
  865. this.morphTargets.position = morphTargetsPosition;
  866. }
  867. const morphNormals = geometry.morphNormals;
  868. const morphNormalsLength = morphNormals.length;
  869. let morphTargetsNormal;
  870. if ( morphNormalsLength > 0 ) {
  871. morphTargetsNormal = [];
  872. for ( let i = 0; i < morphNormalsLength; i ++ ) {
  873. morphTargetsNormal[ i ] = {
  874. name: morphNormals[ i ].name,
  875. data: []
  876. };
  877. }
  878. this.morphTargets.normal = morphTargetsNormal;
  879. } // skins
  880. const skinIndices = geometry.skinIndices;
  881. const skinWeights = geometry.skinWeights;
  882. const hasSkinIndices = skinIndices.length === vertices.length;
  883. const hasSkinWeights = skinWeights.length === vertices.length; //
  884. if ( vertices.length > 0 && faces.length === 0 ) {
  885. console.error( 'THREE.DirectGeometry: Faceless geometries are not supported.' );
  886. }
  887. for ( let i = 0; i < faces.length; i ++ ) {
  888. const face = faces[ i ];
  889. this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
  890. const vertexNormals = face.vertexNormals;
  891. if ( vertexNormals.length === 3 ) {
  892. this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
  893. } else {
  894. const normal = face.normal;
  895. this.normals.push( normal, normal, normal );
  896. }
  897. const vertexColors = face.vertexColors;
  898. if ( vertexColors.length === 3 ) {
  899. this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
  900. } else {
  901. const color = face.color;
  902. this.colors.push( color, color, color );
  903. }
  904. if ( hasFaceVertexUv === true ) {
  905. const vertexUvs = faceVertexUvs[ 0 ][ i ];
  906. if ( vertexUvs !== undefined ) {
  907. this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  908. } else {
  909. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
  910. this.uvs.push( new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() );
  911. }
  912. }
  913. if ( hasFaceVertexUv2 === true ) {
  914. const vertexUvs = faceVertexUvs[ 1 ][ i ];
  915. if ( vertexUvs !== undefined ) {
  916. this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  917. } else {
  918. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
  919. this.uvs2.push( new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() );
  920. }
  921. } // morphs
  922. for ( let j = 0; j < morphTargetsLength; j ++ ) {
  923. const morphTarget = morphTargets[ j ].vertices;
  924. morphTargetsPosition[ j ].data.push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
  925. }
  926. for ( let j = 0; j < morphNormalsLength; j ++ ) {
  927. const morphNormal = morphNormals[ j ].vertexNormals[ i ];
  928. morphTargetsNormal[ j ].data.push( morphNormal.a, morphNormal.b, morphNormal.c );
  929. } // skins
  930. if ( hasSkinIndices ) {
  931. this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
  932. }
  933. if ( hasSkinWeights ) {
  934. this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
  935. }
  936. }
  937. this.computeGroups( geometry );
  938. this.verticesNeedUpdate = geometry.verticesNeedUpdate;
  939. this.normalsNeedUpdate = geometry.normalsNeedUpdate;
  940. this.colorsNeedUpdate = geometry.colorsNeedUpdate;
  941. this.uvsNeedUpdate = geometry.uvsNeedUpdate;
  942. this.groupsNeedUpdate = geometry.groupsNeedUpdate;
  943. if ( geometry.boundingSphere !== null ) {
  944. this.boundingSphere = geometry.boundingSphere.clone();
  945. }
  946. if ( geometry.boundingBox !== null ) {
  947. this.boundingBox = geometry.boundingBox.clone();
  948. }
  949. return this;
  950. }
  951. }
  952. class Face3 {
  953. constructor( a, b, c, normal, color, materialIndex = 0 ) {
  954. this.a = a;
  955. this.b = b;
  956. this.c = c;
  957. this.normal = normal && normal.isVector3 ? normal : new THREE.Vector3();
  958. this.vertexNormals = Array.isArray( normal ) ? normal : [];
  959. this.color = color && color.isColor ? color : new THREE.Color();
  960. this.vertexColors = Array.isArray( color ) ? color : [];
  961. this.materialIndex = materialIndex;
  962. }
  963. clone() {
  964. return new this.constructor().copy( this );
  965. }
  966. copy( source ) {
  967. this.a = source.a;
  968. this.b = source.b;
  969. this.c = source.c;
  970. this.normal.copy( source.normal );
  971. this.color.copy( source.color );
  972. this.materialIndex = source.materialIndex;
  973. for ( let i = 0, il = source.vertexNormals.length; i < il; i ++ ) {
  974. this.vertexNormals[ i ] = source.vertexNormals[ i ].clone();
  975. }
  976. for ( let i = 0, il = source.vertexColors.length; i < il; i ++ ) {
  977. this.vertexColors[ i ] = source.vertexColors[ i ].clone();
  978. }
  979. return this;
  980. }
  981. }
  982. THREE.Face3 = Face3;
  983. THREE.Geometry = Geometry;
  984. } )();