BufferSubdivisionModifier.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * @author zz85 / http://twitter.com/blurspline / http://www.lab4games.net/zz85/blog
  3. * @author Matthew Adams / http://www.centerionware.com - added UV support and rewrote to use buffergeometry.
  4. *
  5. * Subdivision Geometry Modifier using Loop Subdivision Scheme for Geometry / BufferGeometry
  6. *
  7. * References:
  8. * http://graphics.stanford.edu/~mdfisher/subdivision.html
  9. * http://www.holmes3d.net/graphics/subdivision/
  10. * http://www.cs.rutgers.edu/~decarlo/readings/subdiv-sg00c.pdf
  11. *
  12. * Known Issues:
  13. * - currently doesn't handle "Sharp Edges"
  14. * - no checks to prevent breaking when uv's don't exist.
  15. * - vertex colors are unsupported.
  16. * **DDS Images when using corrected uv's passed to subdivision modifier will have their uv's flipy'd within the correct uv set
  17. * **Either flipy the DDS image, or use shaders. Don't try correcting the uv's before passing into subdiv (eg: v=1-v).
  18. *
  19. * @input THREE.Geometry, or index'd THREE.BufferGeometry with faceUV's (Not vertex uv's)
  20. * @output non-indexed vertex points, uv's, normals.
  21. *
  22. * The TypedArrayHelper class is designed to assist managing typed arrays, and to allow the removal of all 'new Vector3, new Face3, new Vector2'.
  23. *
  24. * It will automatically resize them if trying to push a new element to an array that isn't long enough
  25. * It provides 'registers' that the units can be mapped to. This allows a small set of objects
  26. * (ex: vector3's, face3's, vector2's) to be allocated then used, to eliminate any need to rewrite all
  27. * the features those classes offer while not requiring some_huge_number to be allocated.
  28. * It should be moved into it's own file honestly, then included before the BufferSubdivisionModifier - maybe in three's core?
  29. *
  30. */
  31. THREE.Face3.prototype.set = function( a, b, c ) {
  32. this.a = a;
  33. this.b = b;
  34. this.c = c;
  35. };
  36. var TypedArrayHelper = function( size, registers, register_type, array_type, unit_size, accessors ) {
  37. this.array_type = array_type;
  38. this.register_type = register_type;
  39. this.unit_size = unit_size;
  40. this.accessors = accessors;
  41. this.buffer = new array_type( size * unit_size );
  42. this.register = [];
  43. this.length = 0;
  44. this.real_length = size;
  45. this.available_registers = registers;
  46. for ( var i = 0; i < registers; i++ ) {
  47. this.register.push( new register_type() );
  48. }
  49. };
  50. TypedArrayHelper.prototype = {
  51. constructor: TypedArrayHelper,
  52. index_to_register: function( index, register, isLoop ) {
  53. var base = index * this.unit_size;
  54. if ( register >= this.available_registers ) {
  55. throw new Error( 'THREE.BufferSubdivisionModifier: Not enough registers in TypedArrayHelper.' );
  56. }
  57. if ( index > this.length ) {
  58. throw new Error( 'THREE.BufferSubdivisionModifier: Index is out of range in TypedArrayHelper.' );
  59. }
  60. for ( var i = 0; i < this.unit_size; i++ ) {
  61. ( this.register[ register ] )[ this.accessors[ i ] ] = this.buffer[ base + i ];
  62. }
  63. },
  64. resize: function( new_size ) {
  65. if ( new_size === 0 ) {
  66. new_size = 8;
  67. }
  68. if ( new_size < this.length ) {
  69. this.buffer = this.buffer.subarray( 0, this.length * this.unit_size );
  70. } else {
  71. var nBuffer;
  72. if ( this.buffer.length < new_size * this.unit_size ) {
  73. nBuffer = new this.array_type( new_size * this.unit_size );
  74. nBuffer.set( this.buffer );
  75. this.buffer = nBuffer;
  76. this.real_length = new_size;
  77. } else {
  78. nBuffer = new this.array_type( new_size * this.unit_size );
  79. nBuffer.set( this.buffer.subarray( 0, this.length * this.unit_size ) );
  80. this.buffer = nBuffer;
  81. this.real_length = new_size;
  82. }
  83. }
  84. },
  85. from_existing: function( oldArray ) {
  86. var new_size = oldArray.length;
  87. this.buffer = new this.array_type( new_size );
  88. this.buffer.set( oldArray );
  89. this.length = oldArray.length / this.unit_size;
  90. this.real_length = this.length;
  91. },
  92. push_element: function( vector ) {
  93. if ( this.length + 1 > this.real_length ) {
  94. this.resize( this.real_length * 2 );
  95. }
  96. var bpos = this.length * this.unit_size;
  97. for ( var i = 0; i < this.unit_size; i++ ) {
  98. this.buffer[ bpos + i ] = vector[ this.accessors[ i ] ];
  99. }
  100. this.length++;
  101. },
  102. trim_size: function() {
  103. if ( this.length < this.real_length ) {
  104. this.resize( this.length );
  105. }
  106. }
  107. };
  108. function convertGeometryToIndexedBuffer( geometry ) {
  109. var BGeom = new THREE.BufferGeometry();
  110. // create a new typed array
  111. var vertArray = new TypedArrayHelper( geometry.vertices.length, 0, THREE.Vector3, Float32Array, 3, [ 'x', 'y', 'z' ] );
  112. var indexArray = new TypedArrayHelper( geometry.faces.length, 0, THREE.Face3, Uint32Array, 3, [ 'a', 'b', 'c' ] );
  113. var uvArray = new TypedArrayHelper( geometry.faceVertexUvs[0].length * 3 * 3, 0, THREE.Vector2, Float32Array, 2, [ 'x', 'y' ] );
  114. var i, il;
  115. for ( i = 0, il = geometry.vertices.length; i < il; i++ ) {
  116. vertArray.push_element( geometry.vertices[ i ] );
  117. }
  118. for ( i = 0, il = geometry.faces.length; i < il; i++ ) {
  119. indexArray.push_element( geometry.faces[ i ] );
  120. }
  121. for ( i = 0, il = geometry.faceVertexUvs[ 0 ].length; i < il; i++ ) {
  122. uvArray.push_element( geometry.faceVertexUvs[ 0 ][ i ][ 0 ] );
  123. uvArray.push_element( geometry.faceVertexUvs[ 0 ][ i ][ 1 ] );
  124. uvArray.push_element( geometry.faceVertexUvs[ 0 ][ i ][ 2 ] );
  125. }
  126. indexArray.trim_size();
  127. vertArray.trim_size();
  128. uvArray.trim_size();
  129. BGeom.setIndex( new THREE.BufferAttribute( indexArray.buffer, 3 ) );
  130. BGeom.addAttribute( 'position', new THREE.BufferAttribute( vertArray.buffer, 3 ) );
  131. BGeom.addAttribute( 'uv', new THREE.BufferAttribute( uvArray.buffer, 2 ) );
  132. return BGeom;
  133. }
  134. function compute_vertex_normals( geometry ) {
  135. var ABC = [ 'a', 'b', 'c' ];
  136. var XYZ = [ 'x', 'y', 'z' ];
  137. var XY = [ 'x', 'y' ];
  138. var oldVertices = new TypedArrayHelper( 0, 5, THREE.Vector3, Float32Array, 3, XYZ );
  139. var oldFaces = new TypedArrayHelper( 0, 3, THREE.Face3, Uint32Array, 3, ABC );
  140. oldVertices.from_existing( geometry.getAttribute( 'position' ).array );
  141. var newNormals = new TypedArrayHelper( oldVertices.length * 3, 4, THREE.Vector3, Float32Array, 3, XYZ );
  142. var newNormalFaces = new TypedArrayHelper( oldVertices.length, 1, function () { this.x = 0; }, Float32Array, 1, [ 'x' ] );
  143. newNormals.length = oldVertices.length;
  144. oldFaces.from_existing( geometry.index.array );
  145. var a, b, c;
  146. var i, j, jl;
  147. var my_weight;
  148. var full_weights = [ 0.0, 0.0, 0.0 ];
  149. for ( i = 0, il = oldFaces.length; i < il; i++ ) {
  150. oldFaces.index_to_register( i, 0 );
  151. oldVertices.index_to_register( oldFaces.register[ 0 ].a, 0 );
  152. oldVertices.index_to_register( oldFaces.register[ 0 ].b, 1 );
  153. oldVertices.index_to_register( oldFaces.register[ 0 ].c, 2 );
  154. newNormals.register[ 0 ].subVectors( oldVertices.register[ 1 ], oldVertices.register[ 0 ] );
  155. newNormals.register[ 1 ].subVectors( oldVertices.register[ 2 ], oldVertices.register[ 1 ] );
  156. newNormals.register[ 0 ].cross( newNormals.register[ 1 ] );
  157. my_weight = Math.abs( newNormals.register[ 0 ].length() );
  158. newNormalFaces.buffer[ oldFaces.register[ 0 ].a ] += my_weight;
  159. newNormalFaces.buffer[ oldFaces.register[ 0 ].b ] += my_weight;
  160. newNormalFaces.buffer[ oldFaces.register[ 0 ].c ] += my_weight;
  161. }
  162. var tmpx, tmpy, tmpz;
  163. var t_len;
  164. for ( i = 0, il = oldFaces.length; i < il; i++ ) {
  165. oldFaces.index_to_register( i, 0 );
  166. oldVertices.index_to_register( oldFaces.register[ 0 ].a, 0 );
  167. oldVertices.index_to_register( oldFaces.register[ 0 ].b, 1 );
  168. oldVertices.index_to_register( oldFaces.register[ 0 ].c, 2 );
  169. newNormals.register[ 0 ].subVectors( oldVertices.register[ 1 ], oldVertices.register[ 0 ] );
  170. newNormals.register[ 1 ].subVectors( oldVertices.register[ 2 ], oldVertices.register[ 0 ] );
  171. newNormals.register[ 3 ].set( 0, 0, 0 );
  172. newNormals.register[ 3 ].x = ( newNormals.register[ 0 ].y * newNormals.register[ 1 ].z ) - ( newNormals.register[ 0 ].z * newNormals.register[ 1 ].y );
  173. newNormals.register[ 3 ].y = ( newNormals.register[ 0 ].z * newNormals.register[ 1 ].x ) - ( newNormals.register[ 0 ].x * newNormals.register[ 1 ].z );
  174. newNormals.register[ 3 ].z = ( newNormals.register[ 0 ].x * newNormals.register[ 1 ].y ) - ( newNormals.register[ 0 ].y * newNormals.register[ 1 ].x );
  175. newNormals.register[ 0 ].cross( newNormals.register[ 1 ] );
  176. my_weight = Math.abs( newNormals.register[ 0 ].length() );
  177. full_weights[ 0 ] = ( my_weight / newNormalFaces.buffer[ oldFaces.register[ 0 ].a ] );
  178. full_weights[ 1 ] = ( my_weight / newNormalFaces.buffer[ oldFaces.register[ 0 ].b ] );
  179. full_weights[ 2 ] = ( my_weight / newNormalFaces.buffer[ oldFaces.register[ 0 ].c ] );
  180. tmpx = newNormals.register[ 3 ].x * full_weights[ 0 ];
  181. tmpy = newNormals.register[ 3 ].y * full_weights[ 0 ];
  182. tmpz = newNormals.register[ 3 ].z * full_weights[ 0 ];
  183. newNormals.buffer[ ( oldFaces.register[ 0 ].a * 3 ) + 0 ] += newNormals.register[ 3 ].x * full_weights[ 0 ];
  184. newNormals.buffer[ ( oldFaces.register[ 0 ].a * 3 ) + 1 ] += newNormals.register[ 3 ].y * full_weights[ 0 ];
  185. newNormals.buffer[ ( oldFaces.register[ 0 ].a * 3 ) + 2 ] += newNormals.register[ 3 ].z * full_weights[ 0 ];
  186. newNormals.buffer[ ( oldFaces.register[ 0 ].b * 3 ) + 0 ] += newNormals.register[ 3 ].x * full_weights[ 1 ];
  187. newNormals.buffer[ ( oldFaces.register[ 0 ].b * 3 ) + 1 ] += newNormals.register[ 3 ].y * full_weights[ 1 ];
  188. newNormals.buffer[ ( oldFaces.register[ 0 ].b * 3 ) + 2 ] += newNormals.register[ 3 ].z * full_weights[ 1 ];
  189. newNormals.buffer[ ( oldFaces.register[ 0 ].c * 3 ) + 0 ] += newNormals.register[ 3 ].x * full_weights[ 2 ];
  190. newNormals.buffer[ ( oldFaces.register[ 0 ].c * 3 ) + 1 ] += newNormals.register[ 3 ].y * full_weights[ 2 ];
  191. newNormals.buffer[ ( oldFaces.register[ 0 ].c * 3 ) + 2 ] += newNormals.register[ 3 ].z * full_weights[ 2 ];
  192. }
  193. newNormals.trim_size();
  194. geometry.addAttribute( 'normal', new THREE.BufferAttribute( newNormals.buffer, 3 ) );
  195. }
  196. function unIndexIndexedGeometry( geometry ) {
  197. var ABC = [ 'a', 'b', 'c' ];
  198. var XYZ = [ 'x', 'y', 'z' ];
  199. var XY = [ 'x', 'y' ];
  200. var oldVertices = new TypedArrayHelper( 0, 3, THREE.Vector3, Float32Array, 3, XYZ );
  201. var oldFaces = new TypedArrayHelper( 0, 3, THREE.Face3, Uint32Array, 3, ABC );
  202. var oldUvs = new TypedArrayHelper( 0, 3, THREE.Vector2, Float32Array, 2, XY );
  203. var oldNormals = new TypedArrayHelper( 0, 3, THREE.Vector3, Float32Array, 3, XYZ );
  204. oldVertices.from_existing( geometry.getAttribute( 'position' ).array );
  205. oldFaces.from_existing( geometry.index.array );
  206. oldUvs.from_existing( geometry.getAttribute( 'uv' ).array );
  207. compute_vertex_normals( geometry );
  208. oldNormals.from_existing( geometry.getAttribute( 'normal' ).array );
  209. var newVertices = new TypedArrayHelper( oldFaces.length * 3, 3, THREE.Vector3, Float32Array, 3, XYZ );
  210. var newNormals = new TypedArrayHelper( oldFaces.length * 3, 3, THREE.Vector3, Float32Array, 3, XYZ );
  211. var newUvs = new TypedArrayHelper( oldFaces.length * 3, 3, THREE.Vector2, Float32Array, 2, XY );
  212. var v, w;
  213. for ( var i = 0, il = oldFaces.length; i < il; i++ ) {
  214. oldFaces.index_to_register( i, 0 );
  215. oldVertices.index_to_register( oldFaces.register[ 0 ].a, 0 );
  216. oldVertices.index_to_register( oldFaces.register[ 0 ].b, 1 );
  217. oldVertices.index_to_register( oldFaces.register[ 0 ].c, 2 );
  218. newVertices.push_element( oldVertices.register[ 0 ] );
  219. newVertices.push_element( oldVertices.register[ 1 ] );
  220. newVertices.push_element( oldVertices.register[ 2 ] );
  221. if ( oldUvs.length !== 0 ) {
  222. oldUvs.index_to_register( ( i * 3 ) + 0, 0 );
  223. oldUvs.index_to_register( ( i * 3 ) + 1, 1 );
  224. oldUvs.index_to_register( ( i * 3 ) + 2, 2 );
  225. newUvs.push_element( oldUvs.register[ 0 ] );
  226. newUvs.push_element( oldUvs.register[ 1 ] );
  227. newUvs.push_element( oldUvs.register[ 2 ] );
  228. }
  229. oldNormals.index_to_register( oldFaces.register[ 0 ].a, 0 );
  230. oldNormals.index_to_register( oldFaces.register[ 0 ].b, 1 );
  231. oldNormals.index_to_register( oldFaces.register[ 0 ].c, 2 );
  232. newNormals.push_element( oldNormals.register[ 0 ] );
  233. newNormals.push_element( oldNormals.register[ 1 ] );
  234. newNormals.push_element( oldNormals.register[ 2 ] );
  235. }
  236. newVertices.trim_size();
  237. newUvs.trim_size();
  238. newNormals.trim_size();
  239. geometry.index = null;
  240. geometry.addAttribute( 'position', new THREE.BufferAttribute( newVertices.buffer, 3 ) );
  241. geometry.addAttribute( 'normal', new THREE.BufferAttribute( newNormals.buffer, 3 ) );
  242. if ( newUvs.length !== 0 ) {
  243. geometry.addAttribute( 'uv', new THREE.BufferAttribute( newUvs.buffer, 2 ) );
  244. }
  245. return geometry;
  246. }
  247. THREE.BufferSubdivisionModifier = function( subdivisions ) {
  248. this.subdivisions = ( subdivisions === undefined ) ? 1 : subdivisions;
  249. };
  250. THREE.BufferSubdivisionModifier.prototype.modify = function( geometry ) {
  251. if ( geometry instanceof THREE.Geometry ) {
  252. geometry.mergeVertices();
  253. if ( typeof geometry.normals === 'undefined' ) {
  254. geometry.normals = [];
  255. }
  256. geometry = convertGeometryToIndexedBuffer( geometry );
  257. } else if ( !( geometry instanceof THREE.BufferGeometry ) ) {
  258. console.error( 'THREE.BufferSubdivisionModifier: Geometry is not an instance of THREE.BufferGeometry or THREE.Geometry' );
  259. }
  260. var repeats = this.subdivisions;
  261. while ( repeats -- > 0 ) {
  262. this.smooth( geometry );
  263. }
  264. return unIndexIndexedGeometry( geometry );
  265. };
  266. var edge_type = function ( a, b ) {
  267. this.a = a;
  268. this.b = b;
  269. this.faces = [];
  270. this.newEdge = null;
  271. };
  272. ( function () {
  273. // Some constants
  274. var ABC = [ 'a', 'b', 'c' ];
  275. var XYZ = [ 'x', 'y', 'z' ];
  276. var XY = [ 'x', 'y' ];
  277. function getEdge( a, b, map ) {
  278. var key = Math.min( a, b ) + '_' + Math.max( a, b );
  279. return map[ key ];
  280. }
  281. function processEdge( a, b, vertices, map, face, metaVertices ) {
  282. var vertexIndexA = Math.min( a, b );
  283. var vertexIndexB = Math.max( a, b );
  284. var key = vertexIndexA + '_' + vertexIndexB;
  285. var edge;
  286. if ( key in map ) {
  287. edge = map[ key ];
  288. } else {
  289. edge = new edge_type( vertexIndexA,vertexIndexB );
  290. map[key] = edge;
  291. }
  292. edge.faces.push( face );
  293. metaVertices[ a ].edges.push( edge );
  294. metaVertices[ b ].edges.push( edge );
  295. }
  296. function generateLookups( vertices, faces, metaVertices, edges ) {
  297. var i, il, face, edge;
  298. for ( i = 0, il = vertices.length; i < il; i++ ) {
  299. metaVertices[ i ] = { edges: [] };
  300. }
  301. for ( i = 0, il = faces.length; i < il; i++ ) {
  302. faces.index_to_register( i, 0 );
  303. face = faces.register[ 0 ]; // Faces is now a TypedArrayHelper class, not a face3.
  304. processEdge( face.a, face.b, vertices, edges, i, metaVertices );
  305. processEdge( face.b, face.c, vertices, edges, i, metaVertices );
  306. processEdge( face.c, face.a, vertices, edges, i, metaVertices );
  307. }
  308. }
  309. function newFace( newFaces, face ) {
  310. newFaces.push_element( face );
  311. }
  312. function midpoint( a, b ) {
  313. return ( Math.abs( b - a ) / 2 ) + Math.min( a, b );
  314. }
  315. function newUv( newUvs, a, b, c ) {
  316. newUvs.push_element( a );
  317. newUvs.push_element( b );
  318. newUvs.push_element( c );
  319. }
  320. /////////////////////////////
  321. // Performs one iteration of Subdivision
  322. THREE.BufferSubdivisionModifier.prototype.smooth = function ( geometry ) {
  323. var oldVertices, oldFaces, oldUvs;
  324. var newVertices, newFaces, newUVs;
  325. var n, l, i, il, j, k;
  326. var metaVertices, sourceEdges;
  327. oldVertices = new TypedArrayHelper( 0, 3, THREE.Vector3, Float32Array, 3, XYZ );
  328. oldFaces = new TypedArrayHelper( 0, 3, THREE.Face3, Uint32Array, 3, ABC );
  329. oldUvs = new TypedArrayHelper( 0, 3, THREE.Vector2, Float32Array, 2, XY );
  330. oldVertices.from_existing( geometry.getAttribute( 'position' ).array );
  331. oldFaces.from_existing( geometry.index.array );
  332. oldUvs.from_existing( geometry.getAttribute( 'uv' ).array );
  333. var doUvs = false;
  334. if ( typeof oldUvs !== 'undefined' && oldUvs.length !== 0 ) {
  335. doUvs = true;
  336. }
  337. /******************************************************
  338. *
  339. * Step 0: Preprocess Geometry to Generate edges Lookup
  340. *
  341. *******************************************************/
  342. metaVertices = new Array( oldVertices.length );
  343. sourceEdges = {}; // Edge => { oldVertex1, oldVertex2, faces[] }
  344. generateLookups( oldVertices, oldFaces, metaVertices, sourceEdges );
  345. /******************************************************
  346. *
  347. * Step 1.
  348. * For each edge, create a new Edge Vertex,
  349. * then position it.
  350. *
  351. *******************************************************/
  352. newVertices = new TypedArrayHelper( ( geometry.getAttribute( 'position' ).array.length * 2 ) / 3, 2, THREE.Vector3, Float32Array, 3, XYZ );
  353. var other, currentEdge, newEdge, face;
  354. var edgeVertexWeight, adjacentVertexWeight, connectedFaces;
  355. var tmp = newVertices.register[ 1 ];
  356. for ( i in sourceEdges ) {
  357. currentEdge = sourceEdges[ i ];
  358. newEdge = newVertices.register[ 0 ];
  359. edgeVertexWeight = 3 / 8;
  360. adjacentVertexWeight = 1 / 8;
  361. connectedFaces = currentEdge.faces.length;
  362. // check how many linked faces. 2 should be correct.
  363. if ( connectedFaces !== 2 ) {
  364. // if length is not 2, handle condition
  365. edgeVertexWeight = 0.5;
  366. adjacentVertexWeight = 0;
  367. }
  368. oldVertices.index_to_register( currentEdge.a, 0 );
  369. oldVertices.index_to_register( currentEdge.b, 1 );
  370. newEdge.addVectors( oldVertices.register[ 0 ], oldVertices.register[ 1 ] ).multiplyScalar( edgeVertexWeight );
  371. tmp.set( 0, 0, 0 );
  372. for ( j = 0; j < connectedFaces; j++ ) {
  373. oldFaces.index_to_register( currentEdge.faces[ j ], 0 );
  374. face = oldFaces.register[ 0 ];
  375. for ( k = 0; k < 3; k++ ) {
  376. oldVertices.index_to_register( face[ ABC[ k ] ], 2 );
  377. other = oldVertices.register[ 2 ];
  378. if ( face[ ABC[ k ] ] !== currentEdge.a && face[ ABC[ k ] ] !== currentEdge.b) {
  379. break;
  380. }
  381. }
  382. tmp.add( other );
  383. }
  384. tmp.multiplyScalar( adjacentVertexWeight );
  385. newEdge.add( tmp );
  386. currentEdge.newEdge = newVertices.length;
  387. newVertices.push_element( newEdge );
  388. }
  389. var edgeLength = newVertices.length;
  390. /******************************************************
  391. *
  392. * Step 2.
  393. * Reposition each source vertices.
  394. *
  395. *******************************************************/
  396. var beta, sourceVertexWeight, connectingVertexWeight;
  397. var connectingEdge, connectingEdges, oldVertex, newSourceVertex;
  398. for ( i = 0, il = oldVertices.length; i < il; i++ ) {
  399. oldVertices.index_to_register( i, 0, XYZ );
  400. oldVertex = oldVertices.register[ 0 ];
  401. // find all connecting edges (using lookupTable)
  402. connectingEdges = metaVertices[ i ].edges;
  403. n = connectingEdges.length;
  404. if ( n === 3 ) {
  405. beta = 3 / 16;
  406. } else if (n > 3) {
  407. beta = 3 / (8 * n); // Warren's modified formula
  408. }
  409. // Loop's original beta formula
  410. // beta = 1 / n * ( 5/8 - Math.pow( 3/8 + 1/4 * Math.cos( 2 * Math. PI / n ), 2) );
  411. sourceVertexWeight = 1 - n * beta;
  412. connectingVertexWeight = beta;
  413. if ( n <= 2 ) {
  414. // crease and boundary rules
  415. if ( n === 2 ) {
  416. sourceVertexWeight = 3 / 4;
  417. connectingVertexWeight = 1 / 8;
  418. }
  419. }
  420. newSourceVertex = oldVertex.multiplyScalar( sourceVertexWeight );
  421. tmp.set( 0, 0, 0 );
  422. for ( j = 0; j < n; j++ ) {
  423. connectingEdge = connectingEdges[ j ];
  424. other = connectingEdge.a !== i ? connectingEdge.a : connectingEdge.b;
  425. oldVertices.index_to_register( other, 1, XYZ );
  426. tmp.add( oldVertices.register[ 1 ] );
  427. }
  428. tmp.multiplyScalar( connectingVertexWeight );
  429. newSourceVertex.add( tmp );
  430. newVertices.push_element( newSourceVertex,XYZ );
  431. }
  432. /******************************************************
  433. *
  434. * Step 3.
  435. * Generate faces between source vertices and edge vertices.
  436. *
  437. *******************************************************/
  438. var edge1, edge2, edge3;
  439. newFaces = new TypedArrayHelper( ( geometry.index.array.length * 4 ) / 3, 1, THREE.Face3, Float32Array, 3, ABC );
  440. newUVs = new TypedArrayHelper( ( geometry.getAttribute( 'uv' ).array.length * 4 ) / 2, 3, THREE.Vector2, Float32Array, 2, XY );
  441. var x3 = newUVs.register[ 0 ];
  442. var x4 = newUVs.register[ 1 ];
  443. var x5 = newUVs.register[ 2 ];
  444. var tFace = newFaces.register[ 0 ];
  445. for ( i = 0, il = oldFaces.length; i < il; i++ ) {
  446. oldFaces.index_to_register( i, 0 );
  447. face = oldFaces.register[ 0 ];
  448. // find the 3 new edges vertex of each old face
  449. // The new source verts are added after the new edge verts now..
  450. edge1 = getEdge( face.a, face.b, sourceEdges ).newEdge;
  451. edge2 = getEdge( face.b, face.c, sourceEdges ).newEdge;
  452. edge3 = getEdge( face.c, face.a, sourceEdges ).newEdge;
  453. // create 4 faces.
  454. tFace.set( edge1, edge2, edge3 );
  455. newFace( newFaces, tFace );
  456. tFace.set( face.a + edgeLength, edge1, edge3 );
  457. newFace( newFaces, tFace );
  458. tFace.set( face.b + edgeLength, edge2, edge1 );
  459. newFace( newFaces, tFace );
  460. tFace.set( face.c + edgeLength, edge3, edge2 );
  461. newFace( newFaces, tFace );
  462. /*
  463. 0___________________C___________________2
  464. \ /\ /
  465. \ / \ F4 /
  466. \ F2 / \ /
  467. \ / \ /
  468. \ / \ /
  469. \ / F1 \ /
  470. \/_______________________\/
  471. A \ / B
  472. \ F3 /
  473. \ /
  474. \ /
  475. \ /
  476. \ /
  477. \ /
  478. \ /
  479. \/
  480. 1
  481. Draw orders:
  482. F1: ABC x3,x4,x5
  483. F2: 0AC x0,x3,x5
  484. F3: 1BA x1,x4,x3
  485. F4: 2CB x2,x5,x4
  486. 0: x0
  487. 1: x1
  488. 2: x2
  489. A: x3
  490. B: x4
  491. C: x5
  492. */
  493. if ( doUvs === true ) {
  494. oldUvs.index_to_register( ( i * 3 ) + 0, 0 );
  495. oldUvs.index_to_register( ( i * 3 ) + 1, 1 );
  496. oldUvs.index_to_register( ( i * 3 ) + 2, 2 );
  497. x0 = oldUvs.register[ 0 ]; //uv[0];
  498. x1 = oldUvs.register[ 1 ]; //uv[1];
  499. x2 = oldUvs.register[ 2 ]; //uv[2];
  500. x3.set( midpoint( x0.x, x1.x ), midpoint( x0.y, x1.y ) );
  501. x4.set( midpoint( x1.x, x2.x ), midpoint( x1.y, x2.y ) );
  502. x5.set( midpoint( x0.x, x2.x ), midpoint( x0.y, x2.y ) );
  503. newUv( newUVs, x3, x4, x5 );
  504. newUv( newUVs, x0, x3, x5 );
  505. newUv( newUVs, x1, x4, x3 );
  506. newUv( newUVs, x2, x5, x4 );
  507. }
  508. }
  509. // Overwrite old arrays
  510. newFaces.trim_size();
  511. newVertices.trim_size();
  512. newUVs.trim_size();
  513. geometry.setIndex( new THREE.BufferAttribute( newFaces.buffer ,3 ) );
  514. geometry.addAttribute( 'position', new THREE.BufferAttribute( newVertices.buffer, 3 ) );
  515. geometry.addAttribute( 'uv', new THREE.BufferAttribute( newUVs.buffer, 2 ) );
  516. };
  517. } ) ();