SubdivisionModifier.js 8.9 KB

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