Geometry.js 23 KB

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