Geometry.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. ( function () {
  2. const _m1 = new THREE.Matrix4();
  3. const _obj = new THREE.Object3D();
  4. const _offset = new THREE.Vector3();
  5. class Geometry extends THREE.EventDispatcher {
  6. constructor() {
  7. super();
  8. this.uuid = THREE.MathUtils.generateUUID();
  9. this.name = '';
  10. this.type = 'Geometry';
  11. this.vertices = [];
  12. this.colors = [];
  13. this.faces = [];
  14. this.faceVertexUvs = [[]];
  15. this.morphTargets = [];
  16. this.morphNormals = [];
  17. this.skinWeights = [];
  18. this.skinIndices = [];
  19. this.lineDistances = [];
  20. this.boundingBox = null;
  21. this.boundingSphere = null; // update flags
  22. this.elementsNeedUpdate = false;
  23. this.verticesNeedUpdate = false;
  24. this.uvsNeedUpdate = false;
  25. this.normalsNeedUpdate = false;
  26. this.colorsNeedUpdate = false;
  27. this.lineDistancesNeedUpdate = false;
  28. this.groupsNeedUpdate = false;
  29. }
  30. applyMatrix4( matrix ) {
  31. const normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  32. for ( let i = 0, il = this.vertices.length; i < il; i ++ ) {
  33. const vertex = this.vertices[ i ];
  34. vertex.applyMatrix4( matrix );
  35. }
  36. for ( let i = 0, il = this.faces.length; i < il; i ++ ) {
  37. const face = this.faces[ i ];
  38. face.normal.applyMatrix3( normalMatrix ).normalize();
  39. for ( let j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  40. face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
  41. }
  42. }
  43. if ( this.boundingBox !== null ) {
  44. this.computeBoundingBox();
  45. }
  46. if ( this.boundingSphere !== null ) {
  47. this.computeBoundingSphere();
  48. }
  49. this.verticesNeedUpdate = true;
  50. this.normalsNeedUpdate = true;
  51. return this;
  52. }
  53. rotateX( angle ) {
  54. // rotate geometry around world x-axis
  55. _m1.makeRotationX( angle );
  56. this.applyMatrix4( _m1 );
  57. return this;
  58. }
  59. rotateY( angle ) {
  60. // rotate geometry around world y-axis
  61. _m1.makeRotationY( angle );
  62. this.applyMatrix4( _m1 );
  63. return this;
  64. }
  65. rotateZ( angle ) {
  66. // rotate geometry around world z-axis
  67. _m1.makeRotationZ( angle );
  68. this.applyMatrix4( _m1 );
  69. return this;
  70. }
  71. translate( x, y, z ) {
  72. // translate geometry
  73. _m1.makeTranslation( x, y, z );
  74. this.applyMatrix4( _m1 );
  75. return this;
  76. }
  77. scale( x, y, z ) {
  78. // scale geometry
  79. _m1.makeScale( x, y, z );
  80. this.applyMatrix4( _m1 );
  81. return this;
  82. }
  83. lookAt( vector ) {
  84. _obj.lookAt( vector );
  85. _obj.updateMatrix();
  86. this.applyMatrix4( _obj.matrix );
  87. return this;
  88. }
  89. fromBufferGeometry( geometry ) {
  90. const scope = this;
  91. const index = geometry.index !== null ? geometry.index : undefined;
  92. const attributes = geometry.attributes;
  93. if ( attributes.position === undefined ) {
  94. console.error( 'THREE.Geometry.fromBufferGeometry(): Position attribute required for conversion.' );
  95. return this;
  96. }
  97. const position = attributes.position;
  98. const normal = attributes.normal;
  99. const color = attributes.color;
  100. const uv = attributes.uv;
  101. const uv2 = attributes.uv2;
  102. if ( uv2 !== undefined ) this.faceVertexUvs[ 1 ] = [];
  103. for ( let i = 0; i < position.count; i ++ ) {
  104. scope.vertices.push( new THREE.Vector3().fromBufferAttribute( position, i ) );
  105. if ( color !== undefined ) {
  106. scope.colors.push( new THREE.Color().fromBufferAttribute( color, i ) );
  107. }
  108. }
  109. function addFace( a, b, c, materialIndex ) {
  110. const vertexColors = color === undefined ? [] : [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ];
  111. const vertexNormals = normal === undefined ? [] : [ new THREE.Vector3().fromBufferAttribute( normal, a ), new THREE.Vector3().fromBufferAttribute( normal, b ), new THREE.Vector3().fromBufferAttribute( normal, c ) ];
  112. const face = new Face3( a, b, c, vertexNormals, vertexColors, materialIndex );
  113. scope.faces.push( face );
  114. if ( uv !== undefined ) {
  115. scope.faceVertexUvs[ 0 ].push( [ new THREE.Vector2().fromBufferAttribute( uv, a ), new THREE.Vector2().fromBufferAttribute( uv, b ), new THREE.Vector2().fromBufferAttribute( uv, c ) ] );
  116. }
  117. if ( uv2 !== undefined ) {
  118. scope.faceVertexUvs[ 1 ].push( [ new THREE.Vector2().fromBufferAttribute( uv2, a ), new THREE.Vector2().fromBufferAttribute( uv2, b ), new THREE.Vector2().fromBufferAttribute( uv2, c ) ] );
  119. }
  120. }
  121. const groups = geometry.groups;
  122. if ( groups.length > 0 ) {
  123. for ( let i = 0; i < groups.length; i ++ ) {
  124. const group = groups[ i ];
  125. const start = group.start;
  126. const count = group.count;
  127. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  128. if ( index !== undefined ) {
  129. addFace( index.getX( j ), index.getX( j + 1 ), index.getX( j + 2 ), group.materialIndex );
  130. } else {
  131. addFace( j, j + 1, j + 2, group.materialIndex );
  132. }
  133. }
  134. }
  135. } else {
  136. if ( index !== undefined ) {
  137. for ( let i = 0; i < index.count; i += 3 ) {
  138. addFace( index.getX( i ), index.getX( i + 1 ), index.getX( i + 2 ) );
  139. }
  140. } else {
  141. for ( let i = 0; i < position.count; i += 3 ) {
  142. addFace( i, i + 1, i + 2 );
  143. }
  144. }
  145. }
  146. this.computeFaceNormals();
  147. if ( geometry.boundingBox !== null ) {
  148. this.boundingBox = geometry.boundingBox.clone();
  149. }
  150. if ( geometry.boundingSphere !== null ) {
  151. this.boundingSphere = geometry.boundingSphere.clone();
  152. }
  153. return this;
  154. }
  155. center() {
  156. this.computeBoundingBox();
  157. this.boundingBox.getCenter( _offset ).negate();
  158. this.translate( _offset.x, _offset.y, _offset.z );
  159. return this;
  160. }
  161. normalize() {
  162. this.computeBoundingSphere();
  163. const center = this.boundingSphere.center;
  164. const radius = this.boundingSphere.radius;
  165. const s = radius === 0 ? 1 : 1.0 / radius;
  166. const matrix = new THREE.Matrix4();
  167. matrix.set( s, 0, 0, - s * center.x, 0, s, 0, - s * center.y, 0, 0, s, - s * center.z, 0, 0, 0, 1 );
  168. this.applyMatrix4( matrix );
  169. return this;
  170. }
  171. computeFaceNormals() {
  172. const cb = new THREE.Vector3(),
  173. ab = new THREE.Vector3();
  174. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  175. const face = this.faces[ f ];
  176. const vA = this.vertices[ face.a ];
  177. const vB = this.vertices[ face.b ];
  178. const vC = this.vertices[ face.c ];
  179. cb.subVectors( vC, vB );
  180. ab.subVectors( vA, vB );
  181. cb.cross( ab );
  182. cb.normalize();
  183. face.normal.copy( cb );
  184. }
  185. }
  186. computeVertexNormals( areaWeighted = true ) {
  187. const vertices = new Array( this.vertices.length );
  188. for ( let v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  189. vertices[ v ] = new THREE.Vector3();
  190. }
  191. if ( areaWeighted ) {
  192. // vertex normals weighted by triangle areas
  193. // http://www.iquilezles.org/www/articles/normals/normals.htm
  194. const cb = new THREE.Vector3(),
  195. ab = new THREE.Vector3();
  196. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  197. const face = this.faces[ f ];
  198. const vA = this.vertices[ face.a ];
  199. const vB = this.vertices[ face.b ];
  200. const vC = this.vertices[ face.c ];
  201. cb.subVectors( vC, vB );
  202. ab.subVectors( vA, vB );
  203. cb.cross( ab );
  204. vertices[ face.a ].add( cb );
  205. vertices[ face.b ].add( cb );
  206. vertices[ face.c ].add( cb );
  207. }
  208. } else {
  209. this.computeFaceNormals();
  210. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  211. const face = this.faces[ f ];
  212. vertices[ face.a ].add( face.normal );
  213. vertices[ face.b ].add( face.normal );
  214. vertices[ face.c ].add( face.normal );
  215. }
  216. }
  217. for ( let v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  218. vertices[ v ].normalize();
  219. }
  220. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  221. const face = this.faces[ f ];
  222. const vertexNormals = face.vertexNormals;
  223. if ( vertexNormals.length === 3 ) {
  224. vertexNormals[ 0 ].copy( vertices[ face.a ] );
  225. vertexNormals[ 1 ].copy( vertices[ face.b ] );
  226. vertexNormals[ 2 ].copy( vertices[ face.c ] );
  227. } else {
  228. vertexNormals[ 0 ] = vertices[ face.a ].clone();
  229. vertexNormals[ 1 ] = vertices[ face.b ].clone();
  230. vertexNormals[ 2 ] = vertices[ face.c ].clone();
  231. }
  232. }
  233. if ( this.faces.length > 0 ) {
  234. this.normalsNeedUpdate = true;
  235. }
  236. }
  237. computeFlatVertexNormals() {
  238. this.computeFaceNormals();
  239. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  240. const face = this.faces[ f ];
  241. const vertexNormals = face.vertexNormals;
  242. if ( vertexNormals.length === 3 ) {
  243. vertexNormals[ 0 ].copy( face.normal );
  244. vertexNormals[ 1 ].copy( face.normal );
  245. vertexNormals[ 2 ].copy( face.normal );
  246. } else {
  247. vertexNormals[ 0 ] = face.normal.clone();
  248. vertexNormals[ 1 ] = face.normal.clone();
  249. vertexNormals[ 2 ] = face.normal.clone();
  250. }
  251. }
  252. if ( this.faces.length > 0 ) {
  253. this.normalsNeedUpdate = true;
  254. }
  255. }
  256. computeMorphNormals() {
  257. // save original normals
  258. // - create temp variables on first access
  259. // otherwise just copy (for faster repeated calls)
  260. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  261. const face = this.faces[ f ];
  262. if ( ! face.__originalFaceNormal ) {
  263. face.__originalFaceNormal = face.normal.clone();
  264. } else {
  265. face.__originalFaceNormal.copy( face.normal );
  266. }
  267. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  268. for ( let i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  269. if ( ! face.__originalVertexNormals[ i ] ) {
  270. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  271. } else {
  272. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  273. }
  274. }
  275. } // use temp geometry to compute face and vertex normals for each morph
  276. const tmpGeo = new Geometry();
  277. tmpGeo.faces = this.faces;
  278. for ( let i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  279. // create on first access
  280. if ( ! this.morphNormals[ i ] ) {
  281. this.morphNormals[ i ] = {};
  282. this.morphNormals[ i ].faceNormals = [];
  283. this.morphNormals[ i ].vertexNormals = [];
  284. const dstNormalsFace = this.morphNormals[ i ].faceNormals;
  285. const dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  286. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  287. const faceNormal = new THREE.Vector3();
  288. const vertexNormals = {
  289. a: new THREE.Vector3(),
  290. b: new THREE.Vector3(),
  291. c: new THREE.Vector3()
  292. };
  293. dstNormalsFace.push( faceNormal );
  294. dstNormalsVertex.push( vertexNormals );
  295. }
  296. }
  297. const morphNormals = this.morphNormals[ i ]; // set vertices to morph target
  298. tmpGeo.vertices = this.morphTargets[ i ].vertices; // compute morph normals
  299. tmpGeo.computeFaceNormals();
  300. tmpGeo.computeVertexNormals(); // store morph normals
  301. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  302. const face = this.faces[ f ];
  303. const faceNormal = morphNormals.faceNormals[ f ];
  304. const vertexNormals = morphNormals.vertexNormals[ f ];
  305. faceNormal.copy( face.normal );
  306. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  307. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  308. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  309. }
  310. } // restore original normals
  311. for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) {
  312. const face = this.faces[ f ];
  313. face.normal = face.__originalFaceNormal;
  314. face.vertexNormals = face.__originalVertexNormals;
  315. }
  316. }
  317. computeBoundingBox() {
  318. if ( this.boundingBox === null ) {
  319. this.boundingBox = new THREE.Box3();
  320. }
  321. this.boundingBox.setFromPoints( this.vertices );
  322. }
  323. computeBoundingSphere() {
  324. if ( this.boundingSphere === null ) {
  325. this.boundingSphere = new THREE.Sphere();
  326. }
  327. this.boundingSphere.setFromPoints( this.vertices );
  328. }
  329. merge( geometry, matrix, materialIndexOffset = 0 ) {
  330. if ( ! ( geometry && geometry.isGeometry ) ) {
  331. console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
  332. return;
  333. }
  334. let normalMatrix;
  335. const vertexOffset = this.vertices.length,
  336. vertices1 = this.vertices,
  337. vertices2 = geometry.vertices,
  338. faces1 = this.faces,
  339. faces2 = geometry.faces,
  340. colors1 = this.colors,
  341. colors2 = geometry.colors;
  342. if ( matrix !== undefined ) {
  343. normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  344. } // vertices
  345. for ( let i = 0, il = vertices2.length; i < il; i ++ ) {
  346. const vertex = vertices2[ i ];
  347. const vertexCopy = vertex.clone();
  348. if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
  349. vertices1.push( vertexCopy );
  350. } // colors
  351. for ( let i = 0, il = colors2.length; i < il; i ++ ) {
  352. colors1.push( colors2[ i ].clone() );
  353. } // faces
  354. for ( let i = 0, il = faces2.length; i < il; i ++ ) {
  355. const face = faces2[ i ];
  356. let normal, color;
  357. const faceVertexNormals = face.vertexNormals,
  358. faceVertexColors = face.vertexColors;
  359. const faceCopy = new Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  360. faceCopy.normal.copy( face.normal );
  361. if ( normalMatrix !== undefined ) {
  362. faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
  363. }
  364. for ( let j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  365. normal = faceVertexNormals[ j ].clone();
  366. if ( normalMatrix !== undefined ) {
  367. normal.applyMatrix3( normalMatrix ).normalize();
  368. }
  369. faceCopy.vertexNormals.push( normal );
  370. }
  371. faceCopy.color.copy( face.color );
  372. for ( let j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  373. color = faceVertexColors[ j ];
  374. faceCopy.vertexColors.push( color.clone() );
  375. }
  376. faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
  377. faces1.push( faceCopy );
  378. } // uvs
  379. for ( let i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) {
  380. const faceVertexUvs2 = geometry.faceVertexUvs[ i ];
  381. if ( this.faceVertexUvs[ i ] === undefined ) this.faceVertexUvs[ i ] = [];
  382. for ( let j = 0, jl = faceVertexUvs2.length; j < jl; j ++ ) {
  383. const uvs2 = faceVertexUvs2[ j ],
  384. uvsCopy = [];
  385. for ( let k = 0, kl = uvs2.length; k < kl; k ++ ) {
  386. uvsCopy.push( uvs2[ k ].clone() );
  387. }
  388. this.faceVertexUvs[ i ].push( uvsCopy );
  389. }
  390. }
  391. }
  392. mergeMesh( mesh ) {
  393. if ( ! ( mesh && mesh.isMesh ) ) {
  394. console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
  395. return;
  396. }
  397. if ( mesh.matrixAutoUpdate ) mesh.updateMatrix();
  398. this.merge( mesh.geometry, mesh.matrix );
  399. }
  400. /*
  401. * Checks for duplicate vertices with hashmap.
  402. * Duplicated vertices are removed
  403. * and faces' vertices are updated.
  404. */
  405. mergeVertices( precisionPoints = 4 ) {
  406. const verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique)
  407. const unique = [],
  408. changes = [];
  409. const precision = Math.pow( 10, precisionPoints );
  410. for ( let i = 0, il = this.vertices.length; i < il; i ++ ) {
  411. const v = this.vertices[ i ];
  412. const key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
  413. if ( verticesMap[ key ] === undefined ) {
  414. verticesMap[ key ] = i;
  415. unique.push( this.vertices[ i ] );
  416. changes[ i ] = unique.length - 1;
  417. } else {
  418. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  419. changes[ i ] = changes[ verticesMap[ key ] ];
  420. }
  421. } // if faces are completely degenerate after merging vertices, we
  422. // have to remove them from the geometry.
  423. const faceIndicesToRemove = [];
  424. for ( let i = 0, il = this.faces.length; i < il; i ++ ) {
  425. const face = this.faces[ i ];
  426. face.a = changes[ face.a ];
  427. face.b = changes[ face.b ];
  428. face.c = changes[ face.c ];
  429. const indices = [ face.a, face.b, face.c ]; // if any duplicate vertices are found in a Face3
  430. // we have to remove the face as nothing can be saved
  431. for ( let n = 0; n < 3; n ++ ) {
  432. if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) {
  433. faceIndicesToRemove.push( i );
  434. break;
  435. }
  436. }
  437. }
  438. for ( let i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
  439. const idx = faceIndicesToRemove[ i ];
  440. this.faces.splice( idx, 1 );
  441. for ( let j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
  442. this.faceVertexUvs[ j ].splice( idx, 1 );
  443. }
  444. } // Use unique set of vertices
  445. const diff = this.vertices.length - unique.length;
  446. this.vertices = unique;
  447. return diff;
  448. }
  449. setFromPoints( points ) {
  450. this.vertices = [];
  451. for ( let i = 0, l = points.length; i < l; i ++ ) {
  452. const point = points[ i ];
  453. this.vertices.push( new THREE.Vector3( point.x, point.y, point.z || 0 ) );
  454. }
  455. return this;
  456. }
  457. sortFacesByMaterialIndex() {
  458. const faces = this.faces;
  459. const length = faces.length; // tag faces
  460. for ( let i = 0; i < length; i ++ ) {
  461. faces[ i ]._id = i;
  462. } // sort faces
  463. function materialIndexSort( a, b ) {
  464. return a.materialIndex - b.materialIndex;
  465. }
  466. faces.sort( materialIndexSort ); // sort uvs
  467. const uvs1 = this.faceVertexUvs[ 0 ];
  468. const uvs2 = this.faceVertexUvs[ 1 ];
  469. let newUvs1, newUvs2;
  470. if ( uvs1 && uvs1.length === length ) newUvs1 = [];
  471. if ( uvs2 && uvs2.length === length ) newUvs2 = [];
  472. for ( let i = 0; i < length; i ++ ) {
  473. const id = faces[ i ]._id;
  474. if ( newUvs1 ) newUvs1.push( uvs1[ id ] );
  475. if ( newUvs2 ) newUvs2.push( uvs2[ id ] );
  476. }
  477. if ( newUvs1 ) this.faceVertexUvs[ 0 ] = newUvs1;
  478. if ( newUvs2 ) this.faceVertexUvs[ 1 ] = newUvs2;
  479. }
  480. toJSON() {
  481. const data = {
  482. metadata: {
  483. version: 4.5,
  484. type: 'Geometry',
  485. generator: 'Geometry.toJSON'
  486. }
  487. }; // standard Geometry serialization
  488. data.uuid = this.uuid;
  489. data.type = this.type;
  490. if ( this.name !== '' ) data.name = this.name;
  491. if ( this.parameters !== undefined ) {
  492. const parameters = this.parameters;
  493. for ( const key in parameters ) {
  494. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  495. }
  496. return data;
  497. }
  498. const vertices = [];
  499. for ( let i = 0; i < this.vertices.length; i ++ ) {
  500. const vertex = this.vertices[ i ];
  501. vertices.push( vertex.x, vertex.y, vertex.z );
  502. }
  503. const faces = [];
  504. const normals = [];
  505. const normalsHash = {};
  506. const colors = [];
  507. const colorsHash = {};
  508. const uvs = [];
  509. const uvsHash = {};
  510. for ( let i = 0; i < this.faces.length; i ++ ) {
  511. const face = this.faces[ i ];
  512. const hasMaterial = true;
  513. const hasFaceUv = false; // deprecated
  514. const hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
  515. const hasFaceNormal = face.normal.length() > 0;
  516. const hasFaceVertexNormal = face.vertexNormals.length > 0;
  517. const hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  518. const hasFaceVertexColor = face.vertexColors.length > 0;
  519. let faceType = 0;
  520. faceType = setBit( faceType, 0, 0 ); // isQuad
  521. faceType = setBit( faceType, 1, hasMaterial );
  522. faceType = setBit( faceType, 2, hasFaceUv );
  523. faceType = setBit( faceType, 3, hasFaceVertexUv );
  524. faceType = setBit( faceType, 4, hasFaceNormal );
  525. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  526. faceType = setBit( faceType, 6, hasFaceColor );
  527. faceType = setBit( faceType, 7, hasFaceVertexColor );
  528. faces.push( faceType );
  529. faces.push( face.a, face.b, face.c );
  530. faces.push( face.materialIndex );
  531. if ( hasFaceVertexUv ) {
  532. const faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
  533. faces.push( getUvIndex( faceVertexUvs[ 0 ] ), getUvIndex( faceVertexUvs[ 1 ] ), getUvIndex( faceVertexUvs[ 2 ] ) );
  534. }
  535. if ( hasFaceNormal ) {
  536. faces.push( getNormalIndex( face.normal ) );
  537. }
  538. if ( hasFaceVertexNormal ) {
  539. const vertexNormals = face.vertexNormals;
  540. faces.push( getNormalIndex( vertexNormals[ 0 ] ), getNormalIndex( vertexNormals[ 1 ] ), getNormalIndex( vertexNormals[ 2 ] ) );
  541. }
  542. if ( hasFaceColor ) {
  543. faces.push( getColorIndex( face.color ) );
  544. }
  545. if ( hasFaceVertexColor ) {
  546. const vertexColors = face.vertexColors;
  547. faces.push( getColorIndex( vertexColors[ 0 ] ), getColorIndex( vertexColors[ 1 ] ), getColorIndex( vertexColors[ 2 ] ) );
  548. }
  549. }
  550. function setBit( value, position, enabled ) {
  551. return enabled ? value | 1 << position : value & ~ ( 1 << position );
  552. }
  553. function getNormalIndex( normal ) {
  554. const hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  555. if ( normalsHash[ hash ] !== undefined ) {
  556. return normalsHash[ hash ];
  557. }
  558. normalsHash[ hash ] = normals.length / 3;
  559. normals.push( normal.x, normal.y, normal.z );
  560. return normalsHash[ hash ];
  561. }
  562. function getColorIndex( color ) {
  563. const hash = color.r.toString() + color.g.toString() + color.b.toString();
  564. if ( colorsHash[ hash ] !== undefined ) {
  565. return colorsHash[ hash ];
  566. }
  567. colorsHash[ hash ] = colors.length;
  568. colors.push( color.getHex() );
  569. return colorsHash[ hash ];
  570. }
  571. function getUvIndex( uv ) {
  572. const hash = uv.x.toString() + uv.y.toString();
  573. if ( uvsHash[ hash ] !== undefined ) {
  574. return uvsHash[ hash ];
  575. }
  576. uvsHash[ hash ] = uvs.length / 2;
  577. uvs.push( uv.x, uv.y );
  578. return uvsHash[ hash ];
  579. }
  580. data.data = {};
  581. data.data.vertices = vertices;
  582. data.data.normals = normals;
  583. if ( colors.length > 0 ) data.data.colors = colors;
  584. if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility
  585. data.data.faces = faces;
  586. return data;
  587. }
  588. clone() {
  589. /*
  590. // Handle primitives
  591. const parameters = this.parameters;
  592. if ( parameters !== undefined ) {
  593. const values = [];
  594. for ( const key in parameters ) {
  595. values.push( parameters[ key ] );
  596. }
  597. const geometry = Object.create( this.constructor.prototype );
  598. this.constructor.apply( geometry, values );
  599. return geometry;
  600. }
  601. return new this.constructor().copy( this );
  602. */
  603. return new Geometry().copy( this );
  604. }
  605. copy( source ) {
  606. // reset
  607. this.vertices = [];
  608. this.colors = [];
  609. this.faces = [];
  610. this.faceVertexUvs = [[]];
  611. this.morphTargets = [];
  612. this.morphNormals = [];
  613. this.skinWeights = [];
  614. this.skinIndices = [];
  615. this.lineDistances = [];
  616. this.boundingBox = null;
  617. this.boundingSphere = null; // name
  618. this.name = source.name; // vertices
  619. const vertices = source.vertices;
  620. for ( let i = 0, il = vertices.length; i < il; i ++ ) {
  621. this.vertices.push( vertices[ i ].clone() );
  622. } // colors
  623. const colors = source.colors;
  624. for ( let i = 0, il = colors.length; i < il; i ++ ) {
  625. this.colors.push( colors[ i ].clone() );
  626. } // faces
  627. const faces = source.faces;
  628. for ( let i = 0, il = faces.length; i < il; i ++ ) {
  629. this.faces.push( faces[ i ].clone() );
  630. } // face vertex uvs
  631. for ( let i = 0, il = source.faceVertexUvs.length; i < il; i ++ ) {
  632. const faceVertexUvs = source.faceVertexUvs[ i ];
  633. if ( this.faceVertexUvs[ i ] === undefined ) {
  634. this.faceVertexUvs[ i ] = [];
  635. }
  636. for ( let j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) {
  637. const uvs = faceVertexUvs[ j ],
  638. uvsCopy = [];
  639. for ( let k = 0, kl = uvs.length; k < kl; k ++ ) {
  640. const uv = uvs[ k ];
  641. uvsCopy.push( uv.clone() );
  642. }
  643. this.faceVertexUvs[ i ].push( uvsCopy );
  644. }
  645. } // morph targets
  646. const morphTargets = source.morphTargets;
  647. for ( let i = 0, il = morphTargets.length; i < il; i ++ ) {
  648. const morphTarget = {};
  649. morphTarget.name = morphTargets[ i ].name; // vertices
  650. if ( morphTargets[ i ].vertices !== undefined ) {
  651. morphTarget.vertices = [];
  652. for ( let j = 0, jl = morphTargets[ i ].vertices.length; j < jl; j ++ ) {
  653. morphTarget.vertices.push( morphTargets[ i ].vertices[ j ].clone() );
  654. }
  655. } // normals
  656. if ( morphTargets[ i ].normals !== undefined ) {
  657. morphTarget.normals = [];
  658. for ( let j = 0, jl = morphTargets[ i ].normals.length; j < jl; j ++ ) {
  659. morphTarget.normals.push( morphTargets[ i ].normals[ j ].clone() );
  660. }
  661. }
  662. this.morphTargets.push( morphTarget );
  663. } // morph normals
  664. const morphNormals = source.morphNormals;
  665. for ( let i = 0, il = morphNormals.length; i < il; i ++ ) {
  666. const morphNormal = {}; // vertex normals
  667. if ( morphNormals[ i ].vertexNormals !== undefined ) {
  668. morphNormal.vertexNormals = [];
  669. for ( let j = 0, jl = morphNormals[ i ].vertexNormals.length; j < jl; j ++ ) {
  670. const srcVertexNormal = morphNormals[ i ].vertexNormals[ j ];
  671. const destVertexNormal = {};
  672. destVertexNormal.a = srcVertexNormal.a.clone();
  673. destVertexNormal.b = srcVertexNormal.b.clone();
  674. destVertexNormal.c = srcVertexNormal.c.clone();
  675. morphNormal.vertexNormals.push( destVertexNormal );
  676. }
  677. } // face normals
  678. if ( morphNormals[ i ].faceNormals !== undefined ) {
  679. morphNormal.faceNormals = [];
  680. for ( let j = 0, jl = morphNormals[ i ].faceNormals.length; j < jl; j ++ ) {
  681. morphNormal.faceNormals.push( morphNormals[ i ].faceNormals[ j ].clone() );
  682. }
  683. }
  684. this.morphNormals.push( morphNormal );
  685. } // skin weights
  686. const skinWeights = source.skinWeights;
  687. for ( let i = 0, il = skinWeights.length; i < il; i ++ ) {
  688. this.skinWeights.push( skinWeights[ i ].clone() );
  689. } // skin indices
  690. const skinIndices = source.skinIndices;
  691. for ( let i = 0, il = skinIndices.length; i < il; i ++ ) {
  692. this.skinIndices.push( skinIndices[ i ].clone() );
  693. } // line distances
  694. const lineDistances = source.lineDistances;
  695. for ( let i = 0, il = lineDistances.length; i < il; i ++ ) {
  696. this.lineDistances.push( lineDistances[ i ] );
  697. } // bounding box
  698. const boundingBox = source.boundingBox;
  699. if ( boundingBox !== null ) {
  700. this.boundingBox = boundingBox.clone();
  701. } // bounding sphere
  702. const boundingSphere = source.boundingSphere;
  703. if ( boundingSphere !== null ) {
  704. this.boundingSphere = boundingSphere.clone();
  705. } // update flags
  706. this.elementsNeedUpdate = source.elementsNeedUpdate;
  707. this.verticesNeedUpdate = source.verticesNeedUpdate;
  708. this.uvsNeedUpdate = source.uvsNeedUpdate;
  709. this.normalsNeedUpdate = source.normalsNeedUpdate;
  710. this.colorsNeedUpdate = source.colorsNeedUpdate;
  711. this.lineDistancesNeedUpdate = source.lineDistancesNeedUpdate;
  712. this.groupsNeedUpdate = source.groupsNeedUpdate;
  713. return this;
  714. }
  715. toBufferGeometry() {
  716. const geometry = new DirectGeometry().fromGeometry( this );
  717. const buffergeometry = new THREE.BufferGeometry();
  718. const positions = new Float32Array( geometry.vertices.length * 3 );
  719. buffergeometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) );
  720. if ( geometry.normals.length > 0 ) {
  721. const normals = new Float32Array( geometry.normals.length * 3 );
  722. buffergeometry.setAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) );
  723. }
  724. if ( geometry.colors.length > 0 ) {
  725. const colors = new Float32Array( geometry.colors.length * 3 );
  726. buffergeometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) );
  727. }
  728. if ( geometry.uvs.length > 0 ) {
  729. const uvs = new Float32Array( geometry.uvs.length * 2 );
  730. buffergeometry.setAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) );
  731. }
  732. if ( geometry.uvs2.length > 0 ) {
  733. const uvs2 = new Float32Array( geometry.uvs2.length * 2 );
  734. buffergeometry.setAttribute( 'uv2', new THREE.BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) );
  735. } // groups
  736. buffergeometry.groups = geometry.groups; // morphs
  737. for ( const name in geometry.morphTargets ) {
  738. const array = [];
  739. const morphTargets = geometry.morphTargets[ name ];
  740. for ( let i = 0, l = morphTargets.length; i < l; i ++ ) {
  741. const morphTarget = morphTargets[ i ];
  742. const attribute = new THREE.Float32BufferAttribute( morphTarget.data.length * 3, 3 );
  743. attribute.name = morphTarget.name;
  744. array.push( attribute.copyVector3sArray( morphTarget.data ) );
  745. }
  746. buffergeometry.morphAttributes[ name ] = array;
  747. } // skinning
  748. if ( geometry.skinIndices.length > 0 ) {
  749. const skinIndices = new THREE.Float32BufferAttribute( geometry.skinIndices.length * 4, 4 );
  750. buffergeometry.setAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) );
  751. }
  752. if ( geometry.skinWeights.length > 0 ) {
  753. const skinWeights = new THREE.Float32BufferAttribute( geometry.skinWeights.length * 4, 4 );
  754. buffergeometry.setAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) );
  755. } //
  756. if ( geometry.boundingSphere !== null ) {
  757. buffergeometry.boundingSphere = geometry.boundingSphere.clone();
  758. }
  759. if ( geometry.boundingBox !== null ) {
  760. buffergeometry.boundingBox = geometry.boundingBox.clone();
  761. }
  762. return buffergeometry;
  763. }
  764. computeTangents() {
  765. console.error( 'THREE.Geometry: .computeTangents() has been removed.' );
  766. }
  767. computeLineDistances() {
  768. console.error( 'THREE.Geometry: .computeLineDistances() has been removed. Use THREE.Line.computeLineDistances() instead.' );
  769. }
  770. applyMatrix( matrix ) {
  771. console.warn( 'THREE.Geometry: .applyMatrix() has been renamed to .applyMatrix4().' );
  772. return this.applyMatrix4( matrix );
  773. }
  774. dispose() {
  775. this.dispatchEvent( {
  776. type: 'dispose'
  777. } );
  778. }
  779. static createBufferGeometryFromObject( object ) {
  780. let buffergeometry = new THREE.BufferGeometry();
  781. const geometry = object.geometry;
  782. if ( object.isPoints || object.isLine ) {
  783. const positions = new THREE.Float32BufferAttribute( geometry.vertices.length * 3, 3 );
  784. const colors = new THREE.Float32BufferAttribute( geometry.colors.length * 3, 3 );
  785. buffergeometry.setAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) );
  786. buffergeometry.setAttribute( 'color', colors.copyColorsArray( geometry.colors ) );
  787. if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) {
  788. const lineDistances = new THREE.Float32BufferAttribute( geometry.lineDistances.length, 1 );
  789. buffergeometry.setAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) );
  790. }
  791. if ( geometry.boundingSphere !== null ) {
  792. buffergeometry.boundingSphere = geometry.boundingSphere.clone();
  793. }
  794. if ( geometry.boundingBox !== null ) {
  795. buffergeometry.boundingBox = geometry.boundingBox.clone();
  796. }
  797. } else if ( object.isMesh ) {
  798. buffergeometry = geometry.toBufferGeometry();
  799. }
  800. return buffergeometry;
  801. }
  802. }
  803. Geometry.prototype.isGeometry = true;
  804. class DirectGeometry {
  805. constructor() {
  806. this.vertices = [];
  807. this.normals = [];
  808. this.colors = [];
  809. this.uvs = [];
  810. this.uvs2 = [];
  811. this.groups = [];
  812. this.morphTargets = {};
  813. this.skinWeights = [];
  814. this.skinIndices = []; // this.lineDistances = [];
  815. this.boundingBox = null;
  816. this.boundingSphere = null; // update flags
  817. this.verticesNeedUpdate = false;
  818. this.normalsNeedUpdate = false;
  819. this.colorsNeedUpdate = false;
  820. this.uvsNeedUpdate = false;
  821. this.groupsNeedUpdate = false;
  822. }
  823. computeGroups( geometry ) {
  824. const groups = [];
  825. let group, i;
  826. let materialIndex = undefined;
  827. const faces = geometry.faces;
  828. for ( i = 0; i < faces.length; i ++ ) {
  829. const face = faces[ i ]; // materials
  830. if ( face.materialIndex !== materialIndex ) {
  831. materialIndex = face.materialIndex;
  832. if ( group !== undefined ) {
  833. group.count = i * 3 - group.start;
  834. groups.push( group );
  835. }
  836. group = {
  837. start: i * 3,
  838. materialIndex: materialIndex
  839. };
  840. }
  841. }
  842. if ( group !== undefined ) {
  843. group.count = i * 3 - group.start;
  844. groups.push( group );
  845. }
  846. this.groups = groups;
  847. }
  848. fromGeometry( geometry ) {
  849. const faces = geometry.faces;
  850. const vertices = geometry.vertices;
  851. const faceVertexUvs = geometry.faceVertexUvs;
  852. const hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0;
  853. const hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0; // morphs
  854. const morphTargets = geometry.morphTargets;
  855. const morphTargetsLength = morphTargets.length;
  856. let morphTargetsPosition;
  857. if ( morphTargetsLength > 0 ) {
  858. morphTargetsPosition = [];
  859. for ( let i = 0; i < morphTargetsLength; i ++ ) {
  860. morphTargetsPosition[ i ] = {
  861. name: morphTargets[ i ].name,
  862. data: []
  863. };
  864. }
  865. this.morphTargets.position = morphTargetsPosition;
  866. }
  867. const morphNormals = geometry.morphNormals;
  868. const morphNormalsLength = morphNormals.length;
  869. let morphTargetsNormal;
  870. if ( morphNormalsLength > 0 ) {
  871. morphTargetsNormal = [];
  872. for ( let i = 0; i < morphNormalsLength; i ++ ) {
  873. morphTargetsNormal[ i ] = {
  874. name: morphNormals[ i ].name,
  875. data: []
  876. };
  877. }
  878. this.morphTargets.normal = morphTargetsNormal;
  879. } // skins
  880. const skinIndices = geometry.skinIndices;
  881. const skinWeights = geometry.skinWeights;
  882. const hasSkinIndices = skinIndices.length === vertices.length;
  883. const hasSkinWeights = skinWeights.length === vertices.length; //
  884. if ( vertices.length > 0 && faces.length === 0 ) {
  885. console.error( 'THREE.DirectGeometry: Faceless geometries are not supported.' );
  886. }
  887. for ( let i = 0; i < faces.length; i ++ ) {
  888. const face = faces[ i ];
  889. this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
  890. const vertexNormals = face.vertexNormals;
  891. if ( vertexNormals.length === 3 ) {
  892. this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] );
  893. } else {
  894. const normal = face.normal;
  895. this.normals.push( normal, normal, normal );
  896. }
  897. const vertexColors = face.vertexColors;
  898. if ( vertexColors.length === 3 ) {
  899. this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] );
  900. } else {
  901. const color = face.color;
  902. this.colors.push( color, color, color );
  903. }
  904. if ( hasFaceVertexUv === true ) {
  905. const vertexUvs = faceVertexUvs[ 0 ][ i ];
  906. if ( vertexUvs !== undefined ) {
  907. this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  908. } else {
  909. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i );
  910. this.uvs.push( new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() );
  911. }
  912. }
  913. if ( hasFaceVertexUv2 === true ) {
  914. const vertexUvs = faceVertexUvs[ 1 ][ i ];
  915. if ( vertexUvs !== undefined ) {
  916. this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] );
  917. } else {
  918. console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i );
  919. this.uvs2.push( new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() );
  920. }
  921. } // morphs
  922. for ( let j = 0; j < morphTargetsLength; j ++ ) {
  923. const morphTarget = morphTargets[ j ].vertices;
  924. morphTargetsPosition[ j ].data.push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] );
  925. }
  926. for ( let j = 0; j < morphNormalsLength; j ++ ) {
  927. const morphNormal = morphNormals[ j ].vertexNormals[ i ];
  928. morphTargetsNormal[ j ].data.push( morphNormal.a, morphNormal.b, morphNormal.c );
  929. } // skins
  930. if ( hasSkinIndices ) {
  931. this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] );
  932. }
  933. if ( hasSkinWeights ) {
  934. this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] );
  935. }
  936. }
  937. this.computeGroups( geometry );
  938. this.verticesNeedUpdate = geometry.verticesNeedUpdate;
  939. this.normalsNeedUpdate = geometry.normalsNeedUpdate;
  940. this.colorsNeedUpdate = geometry.colorsNeedUpdate;
  941. this.uvsNeedUpdate = geometry.uvsNeedUpdate;
  942. this.groupsNeedUpdate = geometry.groupsNeedUpdate;
  943. if ( geometry.boundingSphere !== null ) {
  944. this.boundingSphere = geometry.boundingSphere.clone();
  945. }
  946. if ( geometry.boundingBox !== null ) {
  947. this.boundingBox = geometry.boundingBox.clone();
  948. }
  949. return this;
  950. }
  951. }
  952. class Face3 {
  953. constructor( a, b, c, normal, color, materialIndex = 0 ) {
  954. this.a = a;
  955. this.b = b;
  956. this.c = c;
  957. this.normal = normal && normal.isVector3 ? normal : new THREE.Vector3();
  958. this.vertexNormals = Array.isArray( normal ) ? normal : [];
  959. this.color = color && color.isColor ? color : new THREE.Color();
  960. this.vertexColors = Array.isArray( color ) ? color : [];
  961. this.materialIndex = materialIndex;
  962. }
  963. clone() {
  964. return new this.constructor().copy( this );
  965. }
  966. copy( source ) {
  967. this.a = source.a;
  968. this.b = source.b;
  969. this.c = source.c;
  970. this.normal.copy( source.normal );
  971. this.color.copy( source.color );
  972. this.materialIndex = source.materialIndex;
  973. for ( let i = 0, il = source.vertexNormals.length; i < il; i ++ ) {
  974. this.vertexNormals[ i ] = source.vertexNormals[ i ].clone();
  975. }
  976. for ( let i = 0, il = source.vertexColors.length; i < il; i ++ ) {
  977. this.vertexColors[ i ] = source.vertexColors[ i ].clone();
  978. }
  979. return this;
  980. }
  981. }
  982. THREE.Face3 = Face3;
  983. THREE.Geometry = Geometry;
  984. } )();