SubdivisionModifier.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * @author zz85 / http://twitter.com/blurspline / http://www.lab4games.net/zz85/blog
  3. * @author centerionware / http://www.centerionware.com
  4. *
  5. * Subdivision Geometry Modifier
  6. * using Loop Subdivision Scheme
  7. *
  8. * References:
  9. * http://graphics.stanford.edu/~mdfisher/subdivision.html
  10. * http://www.holmes3d.net/graphics/subdivision/
  11. * http://www.cs.rutgers.edu/~decarlo/readings/subdiv-sg00c.pdf
  12. *
  13. * Known Issues:
  14. * - currently doesn't handle "Sharp Edges"
  15. */
  16. THREE.SubdivisionModifier = function ( subdivisions ) {
  17. this.subdivisions = ( subdivisions === undefined ) ? 1 : subdivisions;
  18. };
  19. // Applies the "modify" pattern
  20. THREE.SubdivisionModifier.prototype.modify = function ( geometry ) {
  21. var repeats = this.subdivisions;
  22. while ( repeats -- > 0 ) {
  23. this.smooth( geometry );
  24. }
  25. geometry.computeFaceNormals();
  26. geometry.computeVertexNormals();
  27. };
  28. ( function() {
  29. // Some constants
  30. var WARNINGS = ! true; // Set to true for development
  31. var ABC = [ 'a', 'b', 'c' ];
  32. function getEdge( a, b, map ) {
  33. var vertexIndexA = Math.min( a, b );
  34. var vertexIndexB = Math.max( a, b );
  35. var key = vertexIndexA + "_" + vertexIndexB;
  36. return map[ key ];
  37. }
  38. function processEdge( a, b, vertices, map, face, metaVertices ) {
  39. var vertexIndexA = Math.min( a, b );
  40. var vertexIndexB = Math.max( a, b );
  41. var key = vertexIndexA + "_" + vertexIndexB;
  42. var edge;
  43. if ( key in map ) {
  44. edge = map[ key ];
  45. } else {
  46. var vertexA = vertices[ vertexIndexA ];
  47. var vertexB = vertices[ vertexIndexB ];
  48. edge = {
  49. a: vertexA, // pointer reference
  50. b: vertexB,
  51. newEdge: null,
  52. // aIndex: a, // numbered reference
  53. // bIndex: b,
  54. faces: [] // pointers to face
  55. };
  56. map[ key ] = edge;
  57. }
  58. edge.faces.push( face );
  59. metaVertices[ a ].edges.push( edge );
  60. metaVertices[ b ].edges.push( edge );
  61. }
  62. function generateLookups( vertices, faces, metaVertices, edges ) {
  63. var i, il, face, edge;
  64. for ( i = 0, il = vertices.length; i < il; i ++ ) {
  65. metaVertices[ i ] = { edges: [] };
  66. }
  67. for ( i = 0, il = faces.length; i < il; i ++ ) {
  68. face = faces[ i ];
  69. processEdge( face.a, face.b, vertices, edges, face, metaVertices );
  70. processEdge( face.b, face.c, vertices, edges, face, metaVertices );
  71. processEdge( face.c, face.a, vertices, edges, face, metaVertices );
  72. }
  73. }
  74. function newFace( newFaces, a, b, c ) {
  75. newFaces.push( new THREE.Face3( a, b, c ) );
  76. }
  77. function midpoint( a, b ) {
  78. return ( Math.abs( b - a ) / 2 ) + Math.min( a, b );
  79. }
  80. function newUv( newUvs, a, b, c ) {
  81. newUvs.push( [ a.clone(), b.clone(), c.clone() ] );
  82. }
  83. /////////////////////////////
  84. // Performs one iteration of Subdivision
  85. THREE.SubdivisionModifier.prototype.smooth = function ( geometry ) {
  86. var tmp = new THREE.Vector3();
  87. var oldVertices, oldFaces, oldUvs;
  88. var newVertices, newFaces, newUVs = [];
  89. var n, l, i, il, j, k;
  90. var metaVertices, sourceEdges;
  91. // new stuff.
  92. var sourceEdges, newEdgeVertices, newSourceVertices;
  93. oldVertices = geometry.vertices; // { x, y, z}
  94. oldFaces = geometry.faces; // { a: oldVertex1, b: oldVertex2, c: oldVertex3 }
  95. oldUvs = geometry.faceVertexUvs[ 0 ];
  96. var hasUvs = oldUvs !== undefined && oldUvs.length > 0;
  97. /******************************************************
  98. *
  99. * Step 0: Preprocess Geometry to Generate edges Lookup
  100. *
  101. *******************************************************/
  102. metaVertices = new Array( oldVertices.length );
  103. sourceEdges = {}; // Edge => { oldVertex1, oldVertex2, faces[] }
  104. generateLookups( oldVertices, oldFaces, metaVertices, sourceEdges );
  105. /******************************************************
  106. *
  107. * Step 1.
  108. * For each edge, create a new Edge Vertex,
  109. * then position it.
  110. *
  111. *******************************************************/
  112. newEdgeVertices = [];
  113. var other, currentEdge, newEdge, face;
  114. var edgeVertexWeight, adjacentVertexWeight, connectedFaces;
  115. for ( i in sourceEdges ) {
  116. currentEdge = sourceEdges[ i ];
  117. newEdge = new THREE.Vector3();
  118. edgeVertexWeight = 3 / 8;
  119. adjacentVertexWeight = 1 / 8;
  120. connectedFaces = currentEdge.faces.length;
  121. // check how many linked faces. 2 should be correct.
  122. if ( connectedFaces != 2 ) {
  123. // if length is not 2, handle condition
  124. edgeVertexWeight = 0.5;
  125. adjacentVertexWeight = 0;
  126. if ( connectedFaces != 1 ) {
  127. if ( WARNINGS ) console.warn( 'Subdivision Modifier: Number of connected faces != 2, is: ', connectedFaces, currentEdge );
  128. }
  129. }
  130. newEdge.addVectors( currentEdge.a, currentEdge.b ).multiplyScalar( edgeVertexWeight );
  131. tmp.set( 0, 0, 0 );
  132. for ( j = 0; j < connectedFaces; j ++ ) {
  133. face = currentEdge.faces[ j ];
  134. for ( k = 0; k < 3; k ++ ) {
  135. other = oldVertices[ face[ ABC[ k ] ] ];
  136. if ( other !== currentEdge.a && other !== currentEdge.b ) break;
  137. }
  138. tmp.add( other );
  139. }
  140. tmp.multiplyScalar( adjacentVertexWeight );
  141. newEdge.add( tmp );
  142. currentEdge.newEdge = newEdgeVertices.length;
  143. newEdgeVertices.push( newEdge );
  144. // console.log(currentEdge, newEdge);
  145. }
  146. /******************************************************
  147. *
  148. * Step 2.
  149. * Reposition each source vertices.
  150. *
  151. *******************************************************/
  152. var beta, sourceVertexWeight, connectingVertexWeight;
  153. var connectingEdge, connectingEdges, oldVertex, newSourceVertex;
  154. newSourceVertices = [];
  155. for ( i = 0, il = oldVertices.length; i < il; i ++ ) {
  156. oldVertex = oldVertices[ i ];
  157. // find all connecting edges (using lookupTable)
  158. connectingEdges = metaVertices[ i ].edges;
  159. n = connectingEdges.length;
  160. if ( n == 3 ) {
  161. beta = 3 / 16;
  162. } else if ( n > 3 ) {
  163. beta = 3 / ( 8 * n ); // Warren's modified formula
  164. }
  165. // Loop's original beta formula
  166. // beta = 1 / n * ( 5/8 - Math.pow( 3/8 + 1/4 * Math.cos( 2 * Math. PI / n ), 2) );
  167. sourceVertexWeight = 1 - n * beta;
  168. connectingVertexWeight = beta;
  169. if ( n <= 2 ) {
  170. // crease and boundary rules
  171. // console.warn('crease and boundary rules');
  172. if ( n == 2 ) {
  173. if ( WARNINGS ) console.warn( '2 connecting edges', connectingEdges );
  174. sourceVertexWeight = 3 / 4;
  175. connectingVertexWeight = 1 / 8;
  176. // sourceVertexWeight = 1;
  177. // connectingVertexWeight = 0;
  178. } else if ( n == 1 ) {
  179. if ( WARNINGS ) console.warn( 'only 1 connecting edge' );
  180. } else if ( n == 0 ) {
  181. if ( WARNINGS ) console.warn( '0 connecting edges' );
  182. }
  183. }
  184. newSourceVertex = oldVertex.clone().multiplyScalar( sourceVertexWeight );
  185. tmp.set( 0, 0, 0 );
  186. for ( j = 0; j < n; j ++ ) {
  187. connectingEdge = connectingEdges[ j ];
  188. other = connectingEdge.a !== oldVertex ? connectingEdge.a : connectingEdge.b;
  189. tmp.add( other );
  190. }
  191. tmp.multiplyScalar( connectingVertexWeight );
  192. newSourceVertex.add( tmp );
  193. newSourceVertices.push( newSourceVertex );
  194. }
  195. /******************************************************
  196. *
  197. * Step 3.
  198. * Generate Faces between source vertices
  199. * and edge vertices.
  200. *
  201. *******************************************************/
  202. newVertices = newSourceVertices.concat( newEdgeVertices );
  203. var sl = newSourceVertices.length, edge1, edge2, edge3;
  204. newFaces = [];
  205. var uv, x0, x1, x2;
  206. var x3 = new THREE.Vector2();
  207. var x4 = new THREE.Vector2();
  208. var x5 = new THREE.Vector2();
  209. for ( i = 0, il = oldFaces.length; i < il; i ++ ) {
  210. face = oldFaces[ i ];
  211. // find the 3 new edges vertex of each old face
  212. edge1 = getEdge( face.a, face.b, sourceEdges ).newEdge + sl;
  213. edge2 = getEdge( face.b, face.c, sourceEdges ).newEdge + sl;
  214. edge3 = getEdge( face.c, face.a, sourceEdges ).newEdge + sl;
  215. // create 4 faces.
  216. newFace( newFaces, edge1, edge2, edge3 );
  217. newFace( newFaces, face.a, edge1, edge3 );
  218. newFace( newFaces, face.b, edge2, edge1 );
  219. newFace( newFaces, face.c, edge3, edge2 );
  220. // create 4 new uv's
  221. if ( hasUvs ) {
  222. uv = oldUvs[ i ];
  223. x0 = uv[ 0 ];
  224. x1 = uv[ 1 ];
  225. x2 = uv[ 2 ];
  226. x3.set( midpoint( x0.x, x1.x ), midpoint( x0.y, x1.y ) );
  227. x4.set( midpoint( x1.x, x2.x ), midpoint( x1.y, x2.y ) );
  228. x5.set( midpoint( x0.x, x2.x ), midpoint( x0.y, x2.y ) );
  229. newUv( newUVs, x3, x4, x5 );
  230. newUv( newUVs, x0, x3, x5 );
  231. newUv( newUVs, x1, x4, x3 );
  232. newUv( newUVs, x2, x5, x4 );
  233. }
  234. }
  235. // Overwrite old arrays
  236. geometry.vertices = newVertices;
  237. geometry.faces = newFaces;
  238. if ( hasUvs ) geometry.faceVertexUvs[ 0 ] = newUVs;
  239. // console.log('done');
  240. };
  241. } )();