SubdivisionModifier.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * @author zz85 / http://twitter.com/blurspline / http://www.lab4games.net/zz85/blog
  3. *
  4. * Subdivision Geometry Modifier
  5. * using Catmull-Clark Subdivision Surfaces
  6. * for creating smooth geometry meshes
  7. *
  8. * Note: a modifier modifies vertices and faces of geometry,
  9. * so use geometry.clone() if original geometry needs to be retained
  10. *
  11. * Readings:
  12. * http://en.wikipedia.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
  13. * http://www.rorydriscoll.com/2008/08/01/catmull-clark-subdivision-the-basics/
  14. * http://xrt.wikidot.com/blog:31
  15. * "Subdivision Surfaces in Character Animation"
  16. *
  17. * (on boundary edges)
  18. * http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
  19. * https://graphics.stanford.edu/wikis/cs148-09-summer/Assignment3Description
  20. *
  21. * Supports:
  22. * Closed and Open geometries.
  23. *
  24. * TODO:
  25. * crease vertex and "semi-sharp" features
  26. * selective subdivision
  27. */
  28. THREE.Face4Stub = function ( a, b, c, d, normal, color, materialIndex ) {
  29. this.a = a;
  30. this.b = b;
  31. this.c = c;
  32. this.d = d;
  33. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  34. this.vertexNormals = normal instanceof Array ? normal : [ ];
  35. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  36. this.vertexColors = color instanceof Array ? color : [];
  37. this.vertexTangents = [];
  38. this.materialIndex = materialIndex !== undefined ? materialIndex : 0;
  39. this.centroid = new THREE.Vector3();
  40. };
  41. THREE.GeometryUtils.convertFace4s = function(geometry) {
  42. // return geometry;
  43. var faces = geometry.faces;
  44. var faceVertexUvs = geometry.faceVertexUvs[0];
  45. var newfaces = [];
  46. var newfaceVertexUvs = [];
  47. var f, fl, face, uv;
  48. for (f=0, fl=faces.length; f < fl; f++) {
  49. face = faces[f];
  50. uv = faceVertexUvs[f];
  51. if ( face instanceof THREE.Face3 ) {
  52. newfaces.push(face);
  53. if (uv) newfaceVertexUvs.push(uv);
  54. } else {
  55. newfaces.push( new THREE.Face3( face.a, face.b, face.c, null, face.color, face.materialIndex) );
  56. newfaces.push( new THREE.Face3( face.d, face.a, face.c, null, face.color, face.materialIndex) );
  57. if (uv) newfaceVertexUvs.push([uv[0], uv[1], uv[2]]);
  58. if (uv) newfaceVertexUvs.push([uv[3], uv[0], uv[2]]);
  59. }
  60. }
  61. geometry.faces = newfaces;
  62. geometry.faceVertexUvs = [newfaceVertexUvs];
  63. }
  64. THREE.SubdivisionModifier = function ( subdivisions ) {
  65. this.subdivisions = (subdivisions === undefined ) ? 1 : subdivisions;
  66. // Settings
  67. this.useOldVertexColors = false;
  68. this.supportUVs = true;
  69. this.debug = false;
  70. };
  71. // Applies the "modify" pattern
  72. THREE.SubdivisionModifier.prototype.modify = function ( geometry ) {
  73. var repeats = this.subdivisions;
  74. while ( repeats-- > 0 ) {
  75. this.smooth( geometry );
  76. }
  77. THREE.GeometryUtils.convertFace4s( geometry );
  78. delete geometry.__tmpVertices;
  79. geometry.computeCentroids();
  80. geometry.computeFaceNormals();
  81. geometry.computeVertexNormals();
  82. };
  83. /// REFACTORING THIS OUT
  84. THREE.GeometryUtils.orderedKey = function ( a, b ) {
  85. return Math.min( a, b ) + "_" + Math.max( a, b );
  86. };
  87. // Returns a hashmap - of { edge_key: face_index }
  88. THREE.GeometryUtils.computeEdgeFaces = function ( geometry ) {
  89. var i, il, v1, v2, j, k,
  90. face, faceIndices, faceIndex,
  91. edge,
  92. hash,
  93. edgeFaceMap = {};
  94. var orderedKey = THREE.GeometryUtils.orderedKey;
  95. function mapEdgeHash( hash, i ) {
  96. if ( edgeFaceMap[ hash ] === undefined ) {
  97. edgeFaceMap[ hash ] = [];
  98. }
  99. edgeFaceMap[ hash ].push( i );
  100. }
  101. // construct vertex -> face map
  102. for( i = 0, il = geometry.faces.length; i < il; i ++ ) {
  103. face = geometry.faces[ i ];
  104. if ( face instanceof THREE.Face3 ) {
  105. hash = orderedKey( face.a, face.b );
  106. mapEdgeHash( hash, i );
  107. hash = orderedKey( face.b, face.c );
  108. mapEdgeHash( hash, i );
  109. hash = orderedKey( face.c, face.a );
  110. mapEdgeHash( hash, i );
  111. } else if ( face instanceof THREE.Face4Stub ) {
  112. hash = orderedKey( face.a, face.b );
  113. mapEdgeHash( hash, i );
  114. hash = orderedKey( face.b, face.c );
  115. mapEdgeHash( hash, i );
  116. hash = orderedKey( face.c, face.d );
  117. mapEdgeHash( hash, i );
  118. hash = orderedKey( face.d, face.a );
  119. mapEdgeHash( hash, i );
  120. }
  121. }
  122. // extract faces
  123. // var edges = [];
  124. //
  125. // var numOfEdges = 0;
  126. // for (i in edgeFaceMap) {
  127. // numOfEdges++;
  128. //
  129. // edge = edgeFaceMap[i];
  130. // edges.push(edge);
  131. //
  132. // }
  133. //debug('edgeFaceMap', edgeFaceMap, 'geometry.edges',geometry.edges, 'numOfEdges', numOfEdges);
  134. return edgeFaceMap;
  135. }
  136. /////////////////////////////
  137. // Performs an iteration of Catmull-Clark Subdivision
  138. THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
  139. //debug( 'running smooth' );
  140. // New set of vertices, faces and uvs
  141. var newVertices = [], newFaces = [], newUVs = [];
  142. function v( x, y, z ) {
  143. newVertices.push( new THREE.Vector3( x, y, z ) );
  144. }
  145. var scope = this;
  146. var orderedKey = THREE.GeometryUtils.orderedKey;
  147. var computeEdgeFaces = THREE.GeometryUtils.computeEdgeFaces;
  148. function assert() {
  149. if (scope.debug && console && console.assert) console.assert.apply(console, arguments);
  150. }
  151. function debug() {
  152. if (scope.debug) console.log.apply(console, arguments);
  153. }
  154. function warn() {
  155. if (console)
  156. console.log.apply(console, arguments);
  157. }
  158. function f4( a, b, c, d, oldFace, orders, facei ) {
  159. // TODO move vertex selection over here!
  160. var newFace = new THREE.Face4Stub( a, b, c, d, null, oldFace.color, oldFace.materialIndex );
  161. if (scope.useOldVertexColors) {
  162. newFace.vertexColors = [];
  163. var color, tmpColor, order;
  164. for (var i=0;i<4;i++) {
  165. order = orders[i];
  166. color = new THREE.Color(),
  167. color.setRGB(0,0,0);
  168. for (var j=0, jl=0; j<order.length;j++) {
  169. tmpColor = oldFace.vertexColors[order[j]-1];
  170. color.r += tmpColor.r;
  171. color.g += tmpColor.g;
  172. color.b += tmpColor.b;
  173. }
  174. color.r /= order.length;
  175. color.g /= order.length;
  176. color.b /= order.length;
  177. newFace.vertexColors[i] = color;
  178. }
  179. }
  180. newFaces.push( newFace );
  181. if (scope.supportUVs) {
  182. var aUv = [
  183. getUV(a, ''),
  184. getUV(b, facei),
  185. getUV(c, facei),
  186. getUV(d, facei)
  187. ];
  188. if (!aUv[0]) debug('a :( ', a+':'+facei);
  189. else if (!aUv[1]) debug('b :( ', b+':'+facei);
  190. else if (!aUv[2]) debug('c :( ', c+':'+facei);
  191. else if (!aUv[3]) debug('d :( ', d+':'+facei);
  192. else
  193. newUVs.push( aUv );
  194. }
  195. }
  196. var originalPoints = oldGeometry.vertices;
  197. var originalFaces = oldGeometry.faces;
  198. var originalVerticesLength = originalPoints.length;
  199. var newPoints = originalPoints.concat(); // New set of vertices to work on
  200. var facePoints = [], // these are new points on exisiting faces
  201. edgePoints = {}; // these are new points on exisiting edges
  202. var sharpEdges = {}, sharpVertices = []; // Mark edges and vertices to prevent smoothening on them
  203. // TODO: handle this correctly.
  204. var uvForVertices = {}; // Stored in {vertex}:{old face} format
  205. function debugCoreStuff() {
  206. console.log('facePoints', facePoints, 'edgePoints', edgePoints);
  207. console.log('edgeFaceMap', edgeFaceMap, 'vertexEdgeMap', vertexEdgeMap);
  208. }
  209. function getUV(vertexNo, oldFaceNo) {
  210. var j,jl;
  211. var key = vertexNo+':'+oldFaceNo;
  212. var theUV = uvForVertices[key];
  213. if (!theUV) {
  214. if (vertexNo>=originalVerticesLength && vertexNo < (originalVerticesLength + originalFaces.length)) {
  215. debug('face pt');
  216. } else {
  217. debug('edge pt');
  218. }
  219. warn('warning, UV not found for', key);
  220. return null;
  221. }
  222. return theUV;
  223. // Original faces -> Vertex Nos.
  224. // new Facepoint -> Vertex Nos.
  225. // edge Points
  226. }
  227. function addUV(vertexNo, oldFaceNo, value) {
  228. var key = vertexNo+':'+oldFaceNo;
  229. if (!(key in uvForVertices)) {
  230. uvForVertices[key] = value;
  231. } else {
  232. warn('dup vertexNo', vertexNo, 'oldFaceNo', oldFaceNo, 'value', value, 'key', key, uvForVertices[key]);
  233. }
  234. }
  235. // Step 1
  236. // For each face, add a face point
  237. // Set each face point to be the centroid of all original points for the respective face.
  238. // debug(oldGeometry);
  239. var i, il, j, jl, face;
  240. // For Uvs
  241. var uvs = oldGeometry.faceVertexUvs[0];
  242. var abcd = 'abcd', vertice;
  243. debug('originalFaces, uvs, originalVerticesLength', originalFaces.length, uvs.length, originalVerticesLength);
  244. if (scope.supportUVs)
  245. for (i=0, il = uvs.length; i<il; i++ ) {
  246. for (j=0,jl=uvs[i].length;j<jl;j++) {
  247. vertice = originalFaces[i][abcd.charAt(j)];
  248. addUV(vertice, i, uvs[i][j]);
  249. }
  250. }
  251. if (uvs.length == 0) scope.supportUVs = false;
  252. // Additional UVs check, if we index original
  253. var uvCount = 0;
  254. for (var u in uvForVertices) {
  255. uvCount++;
  256. }
  257. if (!uvCount) {
  258. scope.supportUVs = false;
  259. debug('no uvs');
  260. }
  261. var avgUv ;
  262. for (i=0, il = originalFaces.length; i<il ;i++) {
  263. face = originalFaces[ i ];
  264. facePoints.push( face.centroid );
  265. newPoints.push( face.centroid );
  266. if (!scope.supportUVs) continue;
  267. // Prepare subdivided uv
  268. avgUv = new THREE.Vector2();
  269. if ( face instanceof THREE.Face3 ) {
  270. avgUv.x = getUV( face.a, i ).x + getUV( face.b, i ).x + getUV( face.c, i ).x;
  271. avgUv.y = getUV( face.a, i ).y + getUV( face.b, i ).y + getUV( face.c, i ).y;
  272. avgUv.x /= 3;
  273. avgUv.y /= 3;
  274. } else if ( face instanceof THREE.Face4Stub ) {
  275. avgUv.x = getUV( face.a, i ).x + getUV( face.b, i ).x + getUV( face.c, i ).x + getUV( face.d, i ).x;
  276. avgUv.y = getUV( face.a, i ).y + getUV( face.b, i ).y + getUV( face.c, i ).y + getUV( face.d, i ).y;
  277. avgUv.x /= 4;
  278. avgUv.y /= 4;
  279. }
  280. addUV(originalVerticesLength + i, '', avgUv);
  281. }
  282. // Step 2
  283. // For each edge, add an edge point.
  284. // Set each edge point to be the average of the two neighbouring face points and its two original endpoints.
  285. var edgeFaceMap = computeEdgeFaces ( oldGeometry ); // Edge Hash -> Faces Index eg { edge_key: [face_index, face_index2 ]}
  286. var edge, faceIndexA, faceIndexB, avg;
  287. // debug('edgeFaceMap', edgeFaceMap);
  288. var edgeCount = 0;
  289. var edgeVertex, edgeVertexA, edgeVertexB;
  290. ////
  291. var vertexEdgeMap = {}; // Gives edges connecting from each vertex
  292. var vertexFaceMap = {}; // Gives faces connecting from each vertex
  293. function addVertexEdgeMap(vertex, edge) {
  294. if (vertexEdgeMap[vertex]===undefined) {
  295. vertexEdgeMap[vertex] = [];
  296. }
  297. vertexEdgeMap[vertex].push(edge);
  298. }
  299. function addVertexFaceMap(vertex, face, edge) {
  300. if (vertexFaceMap[vertex]===undefined) {
  301. vertexFaceMap[vertex] = {};
  302. }
  303. vertexFaceMap[vertex][face] = edge;
  304. // vertexFaceMap[vertex][face] = null;
  305. }
  306. // Prepares vertexEdgeMap and vertexFaceMap
  307. for (i in edgeFaceMap) { // This is for every edge
  308. edge = edgeFaceMap[i];
  309. edgeVertex = i.split('_');
  310. edgeVertexA = edgeVertex[0];
  311. edgeVertexB = edgeVertex[1];
  312. // Maps an edgeVertex to connecting edges
  313. addVertexEdgeMap(edgeVertexA, [edgeVertexA, edgeVertexB] );
  314. addVertexEdgeMap(edgeVertexB, [edgeVertexA, edgeVertexB] );
  315. for (j=0,jl=edge.length;j<jl;j++) {
  316. face = edge[j];
  317. addVertexFaceMap(edgeVertexA, face, i);
  318. addVertexFaceMap(edgeVertexB, face, i);
  319. }
  320. // {edge vertex: { face1: edge_key, face2: edge_key.. } }
  321. // this thing is fishy right now.
  322. if (edge.length < 2) {
  323. // edge is "sharp";
  324. sharpEdges[i] = true;
  325. sharpVertices[edgeVertexA] = true;
  326. sharpVertices[edgeVertexB] = true;
  327. }
  328. }
  329. for (i in edgeFaceMap) {
  330. edge = edgeFaceMap[i];
  331. faceIndexA = edge[0]; // face index a
  332. faceIndexB = edge[1]; // face index b
  333. edgeVertex = i.split('_');
  334. edgeVertexA = edgeVertex[0];
  335. edgeVertexB = edgeVertex[1];
  336. avg = new THREE.Vector3();
  337. //debug(i, faceIndexB,facePoints[faceIndexB]);
  338. assert(edge.length > 0, 'an edge without faces?!');
  339. if (edge.length==1) {
  340. avg.add( originalPoints[ edgeVertexA ] );
  341. avg.add( originalPoints[ edgeVertexB ] );
  342. avg.multiplyScalar( 0.5 );
  343. sharpVertices[newPoints.length] = true;
  344. } else {
  345. avg.add( facePoints[ faceIndexA ] );
  346. avg.add( facePoints[ faceIndexB ] );
  347. avg.add( originalPoints[ edgeVertexA ] );
  348. avg.add( originalPoints[ edgeVertexB ] );
  349. avg.multiplyScalar( 0.25 );
  350. }
  351. edgePoints[i] = originalVerticesLength + originalFaces.length + edgeCount;
  352. newPoints.push( avg );
  353. edgeCount ++;
  354. if (!scope.supportUVs) {
  355. continue;
  356. }
  357. // Prepare subdivided uv
  358. avgUv = new THREE.Vector2();
  359. avgUv.x = getUV(edgeVertexA, faceIndexA).x + getUV(edgeVertexB, faceIndexA).x;
  360. avgUv.y = getUV(edgeVertexA, faceIndexA).y + getUV(edgeVertexB, faceIndexA).y;
  361. avgUv.x /= 2;
  362. avgUv.y /= 2;
  363. addUV(edgePoints[i], faceIndexA, avgUv);
  364. if (edge.length>=2) {
  365. assert(edge.length == 2, 'did we plan for more than 2 edges?');
  366. avgUv = new THREE.Vector2();
  367. avgUv.x = getUV(edgeVertexA, faceIndexB).x + getUV(edgeVertexB, faceIndexB).x;
  368. avgUv.y = getUV(edgeVertexA, faceIndexB).y + getUV(edgeVertexB, faceIndexB).y;
  369. avgUv.x /= 2;
  370. avgUv.y /= 2;
  371. addUV(edgePoints[i], faceIndexB, avgUv);
  372. }
  373. }
  374. debug('-- Step 2 done');
  375. // Step 3
  376. // For each face point, add an edge for every edge of the face,
  377. // connecting the face point to each edge point for the face.
  378. var facePt, currentVerticeIndex;
  379. var hashAB, hashBC, hashCD, hashDA, hashCA;
  380. var abc123 = ['123', '12', '2', '23'];
  381. var bca123 = ['123', '23', '3', '31'];
  382. var cab123 = ['123', '31', '1', '12'];
  383. var abc1234 = ['1234', '12', '2', '23'];
  384. var bcd1234 = ['1234', '23', '3', '34'];
  385. var cda1234 = ['1234', '34', '4', '41'];
  386. var dab1234 = ['1234', '41', '1', '12'];
  387. for (i=0, il = facePoints.length; i<il ;i++) { // for every face
  388. facePt = facePoints[i];
  389. face = originalFaces[i];
  390. currentVerticeIndex = originalVerticesLength+ i;
  391. if ( face instanceof THREE.Face3 ) {
  392. // create 3 face4s
  393. hashAB = orderedKey( face.a, face.b );
  394. hashBC = orderedKey( face.b, face.c );
  395. hashCA = orderedKey( face.c, face.a );
  396. f4( currentVerticeIndex, edgePoints[hashAB], face.b, edgePoints[hashBC], face, abc123, i );
  397. f4( currentVerticeIndex, edgePoints[hashBC], face.c, edgePoints[hashCA], face, bca123, i );
  398. f4( currentVerticeIndex, edgePoints[hashCA], face.a, edgePoints[hashAB], face, cab123, i );
  399. } else if ( face instanceof THREE.Face4Stub ) {
  400. // create 4 face4s
  401. hashAB = orderedKey( face.a, face.b );
  402. hashBC = orderedKey( face.b, face.c );
  403. hashCD = orderedKey( face.c, face.d );
  404. hashDA = orderedKey( face.d, face.a );
  405. f4( currentVerticeIndex, edgePoints[hashAB], face.b, edgePoints[hashBC], face, abc1234, i );
  406. f4( currentVerticeIndex, edgePoints[hashBC], face.c, edgePoints[hashCD], face, bcd1234, i );
  407. f4( currentVerticeIndex, edgePoints[hashCD], face.d, edgePoints[hashDA], face, cda1234, i );
  408. f4( currentVerticeIndex, edgePoints[hashDA], face.a, edgePoints[hashAB], face, dab1234, i );
  409. } else {
  410. debug('face should be a face!', face);
  411. }
  412. }
  413. newVertices = newPoints;
  414. // Step 4
  415. // For each original point P,
  416. // take the average F of all n face points for faces touching P,
  417. // and take the average R of all n edge midpoints for edges touching P,
  418. // where each edge midpoint is the average of its two endpoint vertices.
  419. // Move each original point to the point
  420. var F = new THREE.Vector3();
  421. var R = new THREE.Vector3();
  422. var n;
  423. for (i=0, il = originalPoints.length; i<il; i++) {
  424. // (F + 2R + (n-3)P) / n
  425. if (vertexEdgeMap[i]===undefined) continue;
  426. F.set(0,0,0);
  427. R.set(0,0,0);
  428. var newPos = new THREE.Vector3(0,0,0);
  429. var f = 0; // this counts number of faces, original vertex is connected to (also known as valance?)
  430. for (j in vertexFaceMap[i]) {
  431. F.add(facePoints[j]);
  432. f++;
  433. }
  434. var sharpEdgeCount = 0;
  435. n = vertexEdgeMap[i].length; // given a vertex, return its connecting edges
  436. // Are we on the border?
  437. var boundary_case = f != n;
  438. // if (boundary_case) {
  439. // console.error('moo', 'o', i, 'faces touched', f, 'edges', n, n == 2);
  440. // }
  441. for (j=0;j<n;j++) {
  442. if (
  443. sharpEdges[
  444. orderedKey(vertexEdgeMap[i][j][0],vertexEdgeMap[i][j][1])
  445. ]) {
  446. sharpEdgeCount++;
  447. }
  448. }
  449. // if ( sharpEdgeCount==2 ) {
  450. // continue;
  451. // // Do not move vertex if there's 2 connecting sharp edges.
  452. // }
  453. /*
  454. if (sharpEdgeCount>2) {
  455. // TODO
  456. }
  457. */
  458. F.divideScalar(f);
  459. var boundary_edges = 0;
  460. if (boundary_case) {
  461. var bb_edge;
  462. for (j=0; j<n;j++) {
  463. edge = vertexEdgeMap[i][j];
  464. bb_edge = edgeFaceMap[orderedKey(edge[0], edge[1])].length == 1
  465. if (bb_edge) {
  466. var midPt = originalPoints[edge[0]].clone().add(originalPoints[edge[1]]).divideScalar(2);
  467. R.add(midPt);
  468. boundary_edges++;
  469. }
  470. }
  471. R.divideScalar(4);
  472. // console.log(j + ' --- ' + n + ' --- ' + boundary_edges);
  473. assert(boundary_edges == 2, 'should have only 2 boundary edges');
  474. } else {
  475. for (j=0; j<n;j++) {
  476. edge = vertexEdgeMap[i][j];
  477. var midPt = originalPoints[edge[0]].clone().add(originalPoints[edge[1]]).divideScalar(2);
  478. R.add(midPt);
  479. }
  480. R.divideScalar(n);
  481. }
  482. // Sum the formula
  483. newPos.add(originalPoints[i]);
  484. if (boundary_case) {
  485. newPos.divideScalar(2);
  486. newPos.add(R);
  487. } else {
  488. newPos.multiplyScalar(n - 3);
  489. newPos.add(F);
  490. newPos.add(R.multiplyScalar(2));
  491. newPos.divideScalar(n);
  492. }
  493. newVertices[i] = newPos;
  494. }
  495. var newGeometry = oldGeometry; // Let's pretend the old geometry is now new :P
  496. newGeometry.vertices = newVertices;
  497. newGeometry.faces = newFaces;
  498. newGeometry.faceVertexUvs[ 0 ] = newUVs;
  499. delete newGeometry.__tmpVertices; // makes __tmpVertices undefined :P
  500. newGeometry.computeCentroids();
  501. newGeometry.computeFaceNormals();
  502. newGeometry.computeVertexNormals();
  503. };