Geometry.js 23 KB

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