Geometry.js 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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, materialIndexOffset ) {
  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 ( materialIndexOffset === undefined ) materialIndexOffset = 0;
  394. if ( matrix !== undefined ) {
  395. normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  396. }
  397. // vertices
  398. for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
  399. var vertex = vertices2[ i ];
  400. var vertexCopy = vertex.clone();
  401. if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
  402. vertices1.push( vertexCopy );
  403. }
  404. // faces
  405. for ( i = 0, il = faces2.length; i < il; i ++ ) {
  406. var face = faces2[ i ], faceCopy, normal, color,
  407. faceVertexNormals = face.vertexNormals,
  408. faceVertexColors = face.vertexColors;
  409. faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  410. faceCopy.normal.copy( face.normal );
  411. if ( normalMatrix !== undefined ) {
  412. faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
  413. }
  414. for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  415. normal = faceVertexNormals[ j ].clone();
  416. if ( normalMatrix !== undefined ) {
  417. normal.applyMatrix3( normalMatrix ).normalize();
  418. }
  419. faceCopy.vertexNormals.push( normal );
  420. }
  421. faceCopy.color.copy( face.color );
  422. for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  423. color = faceVertexColors[ j ];
  424. faceCopy.vertexColors.push( color.clone() );
  425. }
  426. faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
  427. faces1.push( faceCopy );
  428. }
  429. // uvs
  430. for ( i = 0, il = uvs2.length; i < il; i ++ ) {
  431. var uv = uvs2[ i ], uvCopy = [];
  432. if ( uv === undefined ) {
  433. continue;
  434. }
  435. for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
  436. uvCopy.push( uv[ j ].clone() );
  437. }
  438. uvs1.push( uvCopy );
  439. }
  440. },
  441. mergeMesh: function ( mesh ) {
  442. if ( mesh instanceof THREE.Mesh === false ) {
  443. console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
  444. return;
  445. }
  446. mesh.matrixAutoUpdate && mesh.updateMatrix();
  447. this.merge( mesh.geometry, mesh.matrix );
  448. },
  449. /*
  450. * Checks for duplicate vertices with hashmap.
  451. * Duplicated vertices are removed
  452. * and faces' vertices are updated.
  453. */
  454. mergeVertices: function () {
  455. var verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique)
  456. var unique = [], changes = [];
  457. var v, key;
  458. var precisionPoints = 4; // number of decimal points, e.g. 4 for epsilon of 0.0001
  459. var precision = Math.pow( 10, precisionPoints );
  460. var i, il, face;
  461. var indices, j, jl;
  462. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  463. v = this.vertices[ i ];
  464. key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
  465. if ( verticesMap[ key ] === undefined ) {
  466. verticesMap[ key ] = i;
  467. unique.push( this.vertices[ i ] );
  468. changes[ i ] = unique.length - 1;
  469. } else {
  470. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  471. changes[ i ] = changes[ verticesMap[ key ] ];
  472. }
  473. }
  474. // if faces are completely degenerate after merging vertices, we
  475. // have to remove them from the geometry.
  476. var faceIndicesToRemove = [];
  477. for ( i = 0, il = this.faces.length; i < il; i ++ ) {
  478. face = this.faces[ i ];
  479. face.a = changes[ face.a ];
  480. face.b = changes[ face.b ];
  481. face.c = changes[ face.c ];
  482. indices = [ face.a, face.b, face.c ];
  483. var dupIndex = - 1;
  484. // if any duplicate vertices are found in a Face3
  485. // we have to remove the face as nothing can be saved
  486. for ( var n = 0; n < 3; n ++ ) {
  487. if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
  488. dupIndex = n;
  489. faceIndicesToRemove.push( i );
  490. break;
  491. }
  492. }
  493. }
  494. for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
  495. var idx = faceIndicesToRemove[ i ];
  496. this.faces.splice( idx, 1 );
  497. for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
  498. this.faceVertexUvs[ j ].splice( idx, 1 );
  499. }
  500. }
  501. // Use unique set of vertices
  502. var diff = this.vertices.length - unique.length;
  503. this.vertices = unique;
  504. return diff;
  505. },
  506. toJSON: function () {
  507. var data = {
  508. metadata: {
  509. version: 4.4,
  510. type: 'Geometry',
  511. generator: 'Geometry.toJSON'
  512. }
  513. };
  514. // standard Geometry serialization
  515. data.uuid = this.uuid;
  516. data.type = this.type;
  517. if ( this.name !== '' ) data.name = this.name;
  518. if ( this.parameters !== undefined ) {
  519. var parameters = this.parameters;
  520. for ( var key in parameters ) {
  521. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  522. }
  523. return data;
  524. }
  525. var vertices = [];
  526. for ( var i = 0; i < this.vertices.length; i ++ ) {
  527. var vertex = this.vertices[ i ];
  528. vertices.push( vertex.x, vertex.y, vertex.z );
  529. }
  530. var faces = [];
  531. var normals = [];
  532. var normalsHash = {};
  533. var colors = [];
  534. var colorsHash = {};
  535. var uvs = [];
  536. var uvsHash = {};
  537. for ( var i = 0; i < this.faces.length; i ++ ) {
  538. var face = this.faces[ i ];
  539. var hasMaterial = false; // face.materialIndex !== undefined;
  540. var hasFaceUv = false; // deprecated
  541. var hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
  542. var hasFaceNormal = face.normal.length() > 0;
  543. var hasFaceVertexNormal = face.vertexNormals.length > 0;
  544. var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  545. var hasFaceVertexColor = face.vertexColors.length > 0;
  546. var faceType = 0;
  547. faceType = setBit( faceType, 0, 0 );
  548. faceType = setBit( faceType, 1, hasMaterial );
  549. faceType = setBit( faceType, 2, hasFaceUv );
  550. faceType = setBit( faceType, 3, hasFaceVertexUv );
  551. faceType = setBit( faceType, 4, hasFaceNormal );
  552. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  553. faceType = setBit( faceType, 6, hasFaceColor );
  554. faceType = setBit( faceType, 7, hasFaceVertexColor );
  555. faces.push( faceType );
  556. faces.push( face.a, face.b, face.c );
  557. if ( hasFaceVertexUv ) {
  558. var faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
  559. faces.push(
  560. getUvIndex( faceVertexUvs[ 0 ] ),
  561. getUvIndex( faceVertexUvs[ 1 ] ),
  562. getUvIndex( faceVertexUvs[ 2 ] )
  563. );
  564. }
  565. if ( hasFaceNormal ) {
  566. faces.push( getNormalIndex( face.normal ) );
  567. }
  568. if ( hasFaceVertexNormal ) {
  569. var vertexNormals = face.vertexNormals;
  570. faces.push(
  571. getNormalIndex( vertexNormals[ 0 ] ),
  572. getNormalIndex( vertexNormals[ 1 ] ),
  573. getNormalIndex( vertexNormals[ 2 ] )
  574. );
  575. }
  576. if ( hasFaceColor ) {
  577. faces.push( getColorIndex( face.color ) );
  578. }
  579. if ( hasFaceVertexColor ) {
  580. var vertexColors = face.vertexColors;
  581. faces.push(
  582. getColorIndex( vertexColors[ 0 ] ),
  583. getColorIndex( vertexColors[ 1 ] ),
  584. getColorIndex( vertexColors[ 2 ] )
  585. );
  586. }
  587. }
  588. function setBit( value, position, enabled ) {
  589. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position ) );
  590. }
  591. function getNormalIndex( normal ) {
  592. var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  593. if ( normalsHash[ hash ] !== undefined ) {
  594. return normalsHash[ hash ];
  595. }
  596. normalsHash[ hash ] = normals.length / 3;
  597. normals.push( normal.x, normal.y, normal.z );
  598. return normalsHash[ hash ];
  599. }
  600. function getColorIndex( color ) {
  601. var hash = color.r.toString() + color.g.toString() + color.b.toString();
  602. if ( colorsHash[ hash ] !== undefined ) {
  603. return colorsHash[ hash ];
  604. }
  605. colorsHash[ hash ] = colors.length;
  606. colors.push( color.getHex() );
  607. return colorsHash[ hash ];
  608. }
  609. function getUvIndex( uv ) {
  610. var hash = uv.x.toString() + uv.y.toString();
  611. if ( uvsHash[ hash ] !== undefined ) {
  612. return uvsHash[ hash ];
  613. }
  614. uvsHash[ hash ] = uvs.length / 2;
  615. uvs.push( uv.x, uv.y );
  616. return uvsHash[ hash ];
  617. }
  618. data.data = {};
  619. data.data.vertices = vertices;
  620. data.data.normals = normals;
  621. if ( colors.length > 0 ) data.data.colors = colors;
  622. if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility
  623. data.data.faces = faces;
  624. return data;
  625. },
  626. clone: function () {
  627. return new THREE.Geometry().copy( this );
  628. },
  629. copy: function ( source ) {
  630. this.vertices = [];
  631. this.faces = [];
  632. this.faceVertexUvs = [ [] ];
  633. var vertices = source.vertices;
  634. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  635. this.vertices.push( vertices[ i ].clone() );
  636. }
  637. var faces = source.faces;
  638. for ( var i = 0, il = faces.length; i < il; i ++ ) {
  639. this.faces.push( faces[ i ].clone() );
  640. }
  641. for ( var i = 0, il = source.faceVertexUvs.length; i < il; i ++ ) {
  642. var faceVertexUvs = source.faceVertexUvs[ i ];
  643. if ( this.faceVertexUvs[ i ] === undefined ) {
  644. this.faceVertexUvs[ i ] = [];
  645. }
  646. for ( var j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {
  647. var uvs = faceVertexUvs[ j ], uvsCopy = [];
  648. for ( var k = 0, kl = uvs.length; k < kl; k ++ ) {
  649. var uv = uvs[ k ];
  650. uvsCopy.push( uv.clone() );
  651. }
  652. this.faceVertexUvs[ i ].push( uvsCopy );
  653. }
  654. }
  655. return this;
  656. },
  657. dispose: function () {
  658. this.dispatchEvent( { type: 'dispose' } );
  659. },
  660. // Backwards compatibility
  661. set groupsNeedUpdate ( value ) {
  662. if ( value === true ) this.dispose();
  663. }
  664. };
  665. THREE.EventDispatcher.prototype.apply( THREE.Geometry.prototype );
  666. THREE.GeometryIdCount = 0;
粤ICP备19079148号