Geometry.js 27 KB

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