Geometry.js 27 KB

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