Geometry.js 27 KB

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