Geometry.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author kile / http://kile.stravaganza.org/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author mikael emtinger / http://gomo.se/
  6. * @author zz85 / http://www.lab4games.net/zz85/blog
  7. * @author bhouston / http://exocortex.com
  8. */
  9. THREE.Geometry = function () {
  10. Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } );
  11. this.uuid = THREE.Math.generateUUID();
  12. this.name = '';
  13. this.type = 'Geometry';
  14. this.vertices = [];
  15. this.colors = [];
  16. this.faces = [];
  17. this.faceVertexUvs = [ [] ];
  18. this.morphTargets = [];
  19. this.morphColors = [];
  20. this.morphNormals = [];
  21. this.skinWeights = [];
  22. this.skinIndices = [];
  23. this.lineDistances = [];
  24. this.boundingBox = null;
  25. this.boundingSphere = null;
  26. this.hasTangents = false;
  27. // update flags
  28. this.verticesNeedUpdate = false;
  29. this.elementsNeedUpdate = false;
  30. this.uvsNeedUpdate = false;
  31. this.normalsNeedUpdate = false;
  32. this.tangentsNeedUpdate = false;
  33. this.colorsNeedUpdate = false;
  34. this.lineDistancesNeedUpdate = false;
  35. };
  36. THREE.Geometry.prototype = {
  37. constructor: THREE.Geometry,
  38. applyMatrix: function ( matrix ) {
  39. var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  40. for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
  41. var vertex = this.vertices[ i ];
  42. vertex.applyMatrix4( matrix );
  43. }
  44. for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
  45. var face = this.faces[ i ];
  46. face.normal.applyMatrix3( normalMatrix ).normalize();
  47. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  48. face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
  49. }
  50. }
  51. if ( this.boundingBox !== null ) {
  52. this.computeBoundingBox();
  53. }
  54. if ( this.boundingSphere !== null ) {
  55. this.computeBoundingSphere();
  56. }
  57. this.verticesNeedUpdate = true;
  58. this.normalsNeedUpdate = true;
  59. },
  60. fromBufferGeometry: function ( geometry ) {
  61. var scope = this;
  62. var attributes = geometry.attributes;
  63. var vertices = attributes.position.array;
  64. var indices = attributes.index !== undefined ? attributes.index.array : undefined;
  65. var normals = attributes.normal !== undefined ? attributes.normal.array : undefined;
  66. var colors = attributes.color !== undefined ? attributes.color.array : undefined;
  67. var uvs = attributes.uv !== undefined ? attributes.uv.array : undefined;
  68. var uvs2 = attributes.uv2 !== undefined ? attributes.uv2.array : undefined;
  69. var tangents = attributes.tangent !== undefined ? attributes.tangent.array : undefined;
  70. if ( uvs2 !== undefined ) this.faceVertexUvs[ 1 ] = [];
  71. if ( tangents !== undefined ) this.hasTangents = true;
  72. var tempNormals = [];
  73. var tempUVs = [];
  74. var tempUVs2 = [];
  75. var tempTangents = [];
  76. for ( var i = 0, j = 0, k = 0; i < vertices.length; i += 3, j += 2, k += 4 ) {
  77. scope.vertices.push( new THREE.Vector3( vertices[ i ], vertices[ i + 1 ], vertices[ i + 2 ] ) );
  78. if ( normals !== undefined ) {
  79. tempNormals.push( new THREE.Vector3( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] ) );
  80. }
  81. if ( colors !== undefined ) {
  82. scope.colors.push( new THREE.Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );
  83. }
  84. if ( uvs !== undefined ) {
  85. tempUVs.push( new THREE.Vector2( uvs[ j ], uvs[ j + 1 ] ) );
  86. }
  87. if ( uvs2 !== undefined ) {
  88. tempUVs2.push( new THREE.Vector2( uvs2[ j ], uvs2[ j + 1 ] ) );
  89. }
  90. if ( tangents !== undefined ) {
  91. tempTangents.push( new THREE.Vector4( tangents[ k ], tangents[ k + 1 ], tangents[ k + 2 ], tangents[ k + 3 ] ) );
  92. }
  93. }
  94. var addFace = function ( a, b, c ) {
  95. var vertexNormals = normals !== undefined ? [ tempNormals[ a ].clone(), tempNormals[ b ].clone(), tempNormals[ c ].clone() ] : [];
  96. var vertexColors = colors !== undefined ? [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ] : [];
  97. var face = new THREE.Face3( a, b, c, vertexNormals, vertexColors );
  98. scope.faces.push( face );
  99. if ( uvs !== undefined ) {
  100. scope.faceVertexUvs[ 0 ].push( [ tempUVs[ a ].clone(), tempUVs[ b ].clone(), tempUVs[ c ].clone() ] );
  101. }
  102. if ( uvs2 !== undefined ) {
  103. scope.faceVertexUvs[ 1 ].push( [ tempUVs2[ a ].clone(), tempUVs2[ b ].clone(), tempUVs2[ c ].clone() ] );
  104. }
  105. if ( tangents !== undefined ) {
  106. face.vertexTangents.push( tempTangents[ a ].clone(), tempTangents[ b ].clone(), tempTangents[ c ].clone() );
  107. }
  108. };
  109. if ( indices !== undefined ) {
  110. var drawcalls = geometry.drawcalls;
  111. if ( drawcalls.length > 0 ) {
  112. for ( var i = 0; i < drawcalls.length; i ++ ) {
  113. var drawcall = drawcalls[ i ];
  114. var start = drawcall.start;
  115. var count = drawcall.count;
  116. var index = drawcall.index;
  117. for ( var j = start, jl = start + count; j < jl; j += 3 ) {
  118. addFace( index + indices[ j ], index + indices[ j + 1 ], index + indices[ j + 2 ] );
  119. }
  120. }
  121. } else {
  122. for ( var i = 0; i < indices.length; i += 3 ) {
  123. addFace( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
  124. }
  125. }
  126. } else {
  127. for ( var i = 0; i < vertices.length / 3; i += 3 ) {
  128. addFace( i, i + 1, i + 2 );
  129. }
  130. }
  131. this.computeFaceNormals();
  132. if ( geometry.boundingBox !== null ) {
  133. this.boundingBox = geometry.boundingBox.clone();
  134. }
  135. if ( geometry.boundingSphere !== null ) {
  136. this.boundingSphere = geometry.boundingSphere.clone();
  137. }
  138. return this;
  139. },
  140. center: function () {
  141. this.computeBoundingBox();
  142. var offset = this.boundingBox.center().negate();
  143. this.applyMatrix( new THREE.Matrix4().setPosition( offset ) );
  144. return offset;
  145. },
  146. normalize: function () {
  147. this.computeBoundingSphere();
  148. var center = this.boundingSphere.center;
  149. var radius = this.boundingSphere.radius;
  150. var s = radius === 0 ? 1 : 1.0 / radius;
  151. var matrix = new THREE.Matrix4();
  152. matrix.set(
  153. s, 0, 0, -s * center.x,
  154. 0, s, 0, -s * center.y,
  155. 0, 0, s, -s * center.z,
  156. 0, 0, 0, 1
  157. );
  158. this.applyMatrix( matrix );
  159. return this;
  160. },
  161. computeFaceNormals: function () {
  162. var cb = new THREE.Vector3(), ab = new THREE.Vector3();
  163. for ( var f = 0, fl = this.faces.length; f < fl; f ++ ) {
  164. var face = this.faces[ f ];
  165. var vA = this.vertices[ face.a ];
  166. var vB = this.vertices[ face.b ];
  167. var vC = this.vertices[ face.c ];
  168. cb.subVectors( vC, vB );
  169. ab.subVectors( vA, vB );
  170. cb.cross( ab );
  171. cb.normalize();
  172. face.normal.copy( cb );
  173. }
  174. },
  175. computeVertexNormals: function ( areaWeighted ) {
  176. var v, vl, f, fl, face, vertices;
  177. vertices = new Array( this.vertices.length );
  178. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  179. vertices[ v ] = new THREE.Vector3();
  180. }
  181. if ( areaWeighted ) {
  182. // vertex normals weighted by triangle areas
  183. // http://www.iquilezles.org/www/articles/normals/normals.htm
  184. var vA, vB, vC;
  185. var cb = new THREE.Vector3(), ab = new THREE.Vector3();
  186. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  187. face = this.faces[ f ];
  188. vA = this.vertices[ face.a ];
  189. vB = this.vertices[ face.b ];
  190. vC = this.vertices[ face.c ];
  191. cb.subVectors( vC, vB );
  192. ab.subVectors( vA, vB );
  193. cb.cross( ab );
  194. vertices[ face.a ].add( cb );
  195. vertices[ face.b ].add( cb );
  196. vertices[ face.c ].add( cb );
  197. }
  198. } else {
  199. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  200. face = this.faces[ f ];
  201. vertices[ face.a ].add( face.normal );
  202. vertices[ face.b ].add( face.normal );
  203. vertices[ face.c ].add( face.normal );
  204. }
  205. }
  206. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  207. vertices[ v ].normalize();
  208. }
  209. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  210. face = this.faces[ f ];
  211. var vertexNormals = face.vertexNormals;
  212. if ( vertexNormals.length === 3 ) {
  213. vertexNormals[ 0 ].copy( vertices[ face.a ] );
  214. vertexNormals[ 1 ].copy( vertices[ face.b ] );
  215. vertexNormals[ 2 ].copy( vertices[ face.c ] );
  216. } else {
  217. vertexNormals[ 0 ] = vertices[ face.a ].clone();
  218. vertexNormals[ 1 ] = vertices[ face.b ].clone();
  219. vertexNormals[ 2 ] = vertices[ face.c ].clone();
  220. }
  221. }
  222. },
  223. computeMorphNormals: function () {
  224. var i, il, f, fl, face;
  225. // save original normals
  226. // - create temp variables on first access
  227. // otherwise just copy (for faster repeated calls)
  228. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  229. face = this.faces[ f ];
  230. if ( ! face.__originalFaceNormal ) {
  231. face.__originalFaceNormal = face.normal.clone();
  232. } else {
  233. face.__originalFaceNormal.copy( face.normal );
  234. }
  235. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  236. for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  237. if ( ! face.__originalVertexNormals[ i ] ) {
  238. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  239. } else {
  240. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  241. }
  242. }
  243. }
  244. // use temp geometry to compute face and vertex normals for each morph
  245. var tmpGeo = new THREE.Geometry();
  246. tmpGeo.faces = this.faces;
  247. for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  248. // create on first access
  249. if ( ! this.morphNormals[ i ] ) {
  250. this.morphNormals[ i ] = {};
  251. this.morphNormals[ i ].faceNormals = [];
  252. this.morphNormals[ i ].vertexNormals = [];
  253. var dstNormalsFace = this.morphNormals[ i ].faceNormals;
  254. var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  255. var faceNormal, vertexNormals;
  256. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  257. faceNormal = new THREE.Vector3();
  258. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
  259. dstNormalsFace.push( faceNormal );
  260. dstNormalsVertex.push( vertexNormals );
  261. }
  262. }
  263. var morphNormals = this.morphNormals[ i ];
  264. // set vertices to morph target
  265. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  266. // compute morph normals
  267. tmpGeo.computeFaceNormals();
  268. tmpGeo.computeVertexNormals();
  269. // store morph normals
  270. var faceNormal, vertexNormals;
  271. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  272. face = this.faces[ f ];
  273. faceNormal = morphNormals.faceNormals[ f ];
  274. vertexNormals = morphNormals.vertexNormals[ f ];
  275. faceNormal.copy( face.normal );
  276. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  277. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  278. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  279. }
  280. }
  281. // restore original normals
  282. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  283. face = this.faces[ f ];
  284. face.normal = face.__originalFaceNormal;
  285. face.vertexNormals = face.__originalVertexNormals;
  286. }
  287. },
  288. computeTangents: function () {
  289. // based on http://www.terathon.com/code/tangent.html
  290. // tangents go to vertices
  291. var f, fl, v, vl, i, vertexIndex,
  292. face, uv, vA, vB, vC, uvA, uvB, uvC,
  293. x1, x2, y1, y2, z1, z2,
  294. s1, s2, t1, t2, r, t, test,
  295. tan1 = [], tan2 = [],
  296. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  297. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  298. n = new THREE.Vector3(), w;
  299. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  300. tan1[ v ] = new THREE.Vector3();
  301. tan2[ v ] = new THREE.Vector3();
  302. }
  303. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  304. vA = context.vertices[ a ];
  305. vB = context.vertices[ b ];
  306. vC = context.vertices[ c ];
  307. uvA = uv[ ua ];
  308. uvB = uv[ ub ];
  309. uvC = uv[ uc ];
  310. x1 = vB.x - vA.x;
  311. x2 = vC.x - vA.x;
  312. y1 = vB.y - vA.y;
  313. y2 = vC.y - vA.y;
  314. z1 = vB.z - vA.z;
  315. z2 = vC.z - vA.z;
  316. s1 = uvB.x - uvA.x;
  317. s2 = uvC.x - uvA.x;
  318. t1 = uvB.y - uvA.y;
  319. t2 = uvC.y - uvA.y;
  320. r = 1.0 / ( s1 * t2 - s2 * t1 );
  321. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  322. ( t2 * y1 - t1 * y2 ) * r,
  323. ( t2 * z1 - t1 * z2 ) * r );
  324. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  325. ( s1 * y2 - s2 * y1 ) * r,
  326. ( s1 * z2 - s2 * z1 ) * r );
  327. tan1[ a ].add( sdir );
  328. tan1[ b ].add( sdir );
  329. tan1[ c ].add( sdir );
  330. tan2[ a ].add( tdir );
  331. tan2[ b ].add( tdir );
  332. tan2[ c ].add( tdir );
  333. }
  334. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  335. face = this.faces[ f ];
  336. uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
  337. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  338. }
  339. var faceIndex = [ 'a', 'b', 'c', 'd' ];
  340. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  341. face = this.faces[ f ];
  342. for ( i = 0; i < Math.min( face.vertexNormals.length, 3 ); i ++ ) {
  343. n.copy( face.vertexNormals[ i ] );
  344. vertexIndex = face[ faceIndex[ i ] ];
  345. t = tan1[ vertexIndex ];
  346. // Gram-Schmidt orthogonalize
  347. tmp.copy( t );
  348. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  349. // Calculate handedness
  350. tmp2.crossVectors( face.vertexNormals[ i ], t );
  351. test = tmp2.dot( tan2[ vertexIndex ] );
  352. w = ( test < 0.0 ) ? - 1.0 : 1.0;
  353. face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
  354. }
  355. }
  356. this.hasTangents = true;
  357. },
  358. computeLineDistances: function () {
  359. var d = 0;
  360. var vertices = this.vertices;
  361. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  362. if ( i > 0 ) {
  363. d += vertices[ i ].distanceTo( vertices[ i - 1 ] );
  364. }
  365. this.lineDistances[ i ] = d;
  366. }
  367. },
  368. computeBoundingBox: function () {
  369. if ( this.boundingBox === null ) {
  370. this.boundingBox = new THREE.Box3();
  371. }
  372. this.boundingBox.setFromPoints( this.vertices );
  373. },
  374. computeBoundingSphere: function () {
  375. if ( this.boundingSphere === null ) {
  376. this.boundingSphere = new THREE.Sphere();
  377. }
  378. this.boundingSphere.setFromPoints( this.vertices );
  379. },
  380. merge: function ( geometry, matrix ) {
  381. if ( geometry instanceof THREE.Geometry === false ) {
  382. console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
  383. return;
  384. }
  385. var normalMatrix,
  386. vertexOffset = this.vertices.length,
  387. vertices1 = this.vertices,
  388. vertices2 = geometry.vertices,
  389. faces1 = this.faces,
  390. faces2 = geometry.faces,
  391. uvs1 = this.faceVertexUvs[ 0 ],
  392. uvs2 = geometry.faceVertexUvs[ 0 ];
  393. if ( matrix !== undefined ) {
  394. normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  395. }
  396. // vertices
  397. for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
  398. var vertex = vertices2[ i ];
  399. var vertexCopy = vertex.clone();
  400. if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
  401. vertices1.push( vertexCopy );
  402. }
  403. // faces
  404. for ( i = 0, il = faces2.length; i < il; i ++ ) {
  405. var face = faces2[ i ], faceCopy, normal, color,
  406. faceVertexNormals = face.vertexNormals,
  407. faceVertexColors = face.vertexColors;
  408. faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  409. faceCopy.normal.copy( face.normal );
  410. if ( normalMatrix !== undefined ) {
  411. faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
  412. }
  413. for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  414. normal = faceVertexNormals[ j ].clone();
  415. if ( normalMatrix !== undefined ) {
  416. normal.applyMatrix3( normalMatrix ).normalize();
  417. }
  418. faceCopy.vertexNormals.push( normal );
  419. }
  420. faceCopy.color.copy( face.color );
  421. for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  422. color = faceVertexColors[ j ];
  423. faceCopy.vertexColors.push( color.clone() );
  424. }
  425. faces1.push( faceCopy );
  426. }
  427. // uvs
  428. for ( i = 0, il = uvs2.length; i < il; i ++ ) {
  429. var uv = uvs2[ i ], uvCopy = [];
  430. if ( uv === undefined ) {
  431. continue;
  432. }
  433. for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
  434. uvCopy.push( uv[ j ].clone() );
  435. }
  436. uvs1.push( uvCopy );
  437. }
  438. },
  439. mergeMesh: function ( mesh ) {
  440. if ( mesh instanceof THREE.Mesh === false ) {
  441. console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
  442. return;
  443. }
  444. mesh.matrixAutoUpdate && mesh.updateMatrix();
  445. this.merge( mesh.geometry, mesh.matrix );
  446. },
  447. /*
  448. * Checks for duplicate vertices with hashmap.
  449. * Duplicated vertices are removed
  450. * and faces' vertices are updated.
  451. */
  452. mergeVertices: function () {
  453. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  454. var unique = [], changes = [];
  455. var v, key;
  456. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  457. var precision = Math.pow( 10, precisionPoints );
  458. var i, il, face;
  459. var indices, j, jl;
  460. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  461. v = this.vertices[ i ];
  462. key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
  463. if ( verticesMap[ key ] === undefined ) {
  464. verticesMap[ key ] = i;
  465. unique.push( this.vertices[ i ] );
  466. changes[ i ] = unique.length - 1;
  467. } else {
  468. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  469. changes[ i ] = changes[ verticesMap[ key ] ];
  470. }
  471. }
  472. // if faces are completely degenerate after merging vertices, we
  473. // have to remove them from the geometry.
  474. var faceIndicesToRemove = [];
  475. for ( i = 0, il = this.faces.length; i < il; i ++ ) {
  476. face = this.faces[ i ];
  477. face.a = changes[ face.a ];
  478. face.b = changes[ face.b ];
  479. face.c = changes[ face.c ];
  480. indices = [ face.a, face.b, face.c ];
  481. var dupIndex = - 1;
  482. // if any duplicate vertices are found in a Face3
  483. // we have to remove the face as nothing can be saved
  484. for ( var n = 0; n < 3; n ++ ) {
  485. if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
  486. dupIndex = n;
  487. faceIndicesToRemove.push( i );
  488. break;
  489. }
  490. }
  491. }
  492. for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
  493. var idx = faceIndicesToRemove[ i ];
  494. this.faces.splice( idx, 1 );
  495. for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
  496. this.faceVertexUvs[ j ].splice( idx, 1 );
  497. }
  498. }
  499. // Use unique set of vertices
  500. var diff = this.vertices.length - unique.length;
  501. this.vertices = unique;
  502. return diff;
  503. },
  504. toJSON: function () {
  505. var data = {
  506. metadata: {
  507. version: 4.4,
  508. type: 'Geometry',
  509. generator: 'Geometry.toJSON'
  510. }
  511. };
  512. // standard Geometry serialization
  513. data.uuid = this.uuid;
  514. data.type = this.type;
  515. if ( this.name !== '' ) data.name = this.name;
  516. if ( this.parameters !== undefined ) {
  517. var parameters = this.parameters;
  518. for ( var key in parameters ) {
  519. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  520. }
  521. return data;
  522. }
  523. var vertices = [];
  524. for ( var i = 0; i < this.vertices.length; i ++ ) {
  525. var vertex = this.vertices[ i ];
  526. vertices.push( vertex.x, vertex.y, vertex.z );
  527. }
  528. var faces = [];
  529. var normals = [];
  530. var normalsHash = {};
  531. var colors = [];
  532. var colorsHash = {};
  533. var uvs = [];
  534. var uvsHash = {};
  535. for ( var i = 0; i < this.faces.length; i ++ ) {
  536. var face = this.faces[ i ];
  537. var hasMaterial = false; // face.materialIndex !== undefined;
  538. var hasFaceUv = false; // deprecated
  539. var hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
  540. var hasFaceNormal = face.normal.length() > 0;
  541. var hasFaceVertexNormal = face.vertexNormals.length > 0;
  542. var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  543. var hasFaceVertexColor = face.vertexColors.length > 0;
  544. var faceType = 0;
  545. faceType = setBit( faceType, 0, 0 );
  546. faceType = setBit( faceType, 1, hasMaterial );
  547. faceType = setBit( faceType, 2, hasFaceUv );
  548. faceType = setBit( faceType, 3, hasFaceVertexUv );
  549. faceType = setBit( faceType, 4, hasFaceNormal );
  550. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  551. faceType = setBit( faceType, 6, hasFaceColor );
  552. faceType = setBit( faceType, 7, hasFaceVertexColor );
  553. faces.push( faceType );
  554. faces.push( face.a, face.b, face.c );
  555. if ( hasFaceVertexUv ) {
  556. var faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
  557. faces.push(
  558. getUvIndex( faceVertexUvs[ 0 ] ),
  559. getUvIndex( faceVertexUvs[ 1 ] ),
  560. getUvIndex( faceVertexUvs[ 2 ] )
  561. );
  562. }
  563. if ( hasFaceNormal ) {
  564. faces.push( getNormalIndex( face.normal ) );
  565. }
  566. if ( hasFaceVertexNormal ) {
  567. var vertexNormals = face.vertexNormals;
  568. faces.push(
  569. getNormalIndex( vertexNormals[ 0 ] ),
  570. getNormalIndex( vertexNormals[ 1 ] ),
  571. getNormalIndex( vertexNormals[ 2 ] )
  572. );
  573. }
  574. if ( hasFaceColor ) {
  575. faces.push( getColorIndex( face.color ) );
  576. }
  577. if ( hasFaceVertexColor ) {
  578. var vertexColors = face.vertexColors;
  579. faces.push(
  580. getColorIndex( vertexColors[ 0 ] ),
  581. getColorIndex( vertexColors[ 1 ] ),
  582. getColorIndex( vertexColors[ 2 ] )
  583. );
  584. }
  585. }
  586. function setBit( value, position, enabled ) {
  587. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
  588. }
  589. function getNormalIndex( normal ) {
  590. var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  591. if ( normalsHash[ hash ] !== undefined ) {
  592. return normalsHash[ hash ];
  593. }
  594. normalsHash[ hash ] = normals.length / 3;
  595. normals.push( normal.x, normal.y, normal.z );
  596. return normalsHash[ hash ];
  597. }
  598. function getColorIndex( color ) {
  599. var hash = color.r.toString() + color.g.toString() + color.b.toString();
  600. if ( colorsHash[ hash ] !== undefined ) {
  601. return colorsHash[ hash ];
  602. }
  603. colorsHash[ hash ] = colors.length;
  604. colors.push( color.getHex() );
  605. return colorsHash[ hash ];
  606. }
  607. function getUvIndex( uv ) {
  608. var hash = uv.x.toString() + uv.y.toString();
  609. if ( uvsHash[ hash ] !== undefined ) {
  610. return uvsHash[ hash ];
  611. }
  612. uvsHash[ hash ] = uvs.length / 2;
  613. uvs.push( uv.x, uv.y );
  614. return uvsHash[ hash ];
  615. }
  616. data.data = {};
  617. data.data.vertices = vertices;
  618. data.data.normals = normals;
  619. if ( colors.length > 0 ) data.data.colors = colors;
  620. if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility
  621. data.data.faces = faces;
  622. return data;
  623. },
  624. clone: function () {
  625. var geometry = new THREE.Geometry();
  626. var vertices = this.vertices;
  627. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  628. geometry.vertices.push( vertices[ i ].clone() );
  629. }
  630. var faces = this.faces;
  631. for ( var i = 0, il = faces.length; i < il; i ++ ) {
  632. geometry.faces.push( faces[ i ].clone() );
  633. }
  634. for ( var i = 0, il = this.faceVertexUvs.length; i < il; i ++ ) {
  635. var faceVertexUvs = this.faceVertexUvs[ i ];
  636. if ( geometry.faceVertexUvs[ i ] === undefined ) {
  637. geometry.faceVertexUvs[ i ] = [];
  638. }
  639. for ( var j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {
  640. var uvs = faceVertexUvs[ j ], uvsCopy = [];
  641. for ( var k = 0, kl = uvs.length; k < kl; k ++ ) {
  642. var uv = uvs[ k ];
  643. uvsCopy.push( uv.clone() );
  644. }
  645. geometry.faceVertexUvs[ i ].push( uvsCopy );
  646. }
  647. }
  648. return geometry;
  649. },
  650. dispose: function () {
  651. this.dispatchEvent( { type: 'dispose' } );
  652. },
  653. // Backwards compatibility
  654. set groupsNeedUpdate ( value ) {
  655. if ( value === true ) this.dispose();
  656. }
  657. };
  658. THREE.EventDispatcher.prototype.apply( THREE.Geometry.prototype );
  659. THREE.GeometryIdCount = 0;