webgl_geometry_subdivision.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - Subdivisions with Catmull-Clark</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.min.js"></script>
  18. <script src="js/controls/OrbitControls.js"></script>
  19. <script src="js/modifiers/SubdivisionModifier.js"></script>
  20. <script src="js/libs/stats.min.js"></script>
  21. <script src="fonts/helvetiker_regular.typeface.js"></script>
  22. <script>
  23. var container, stats;
  24. var camera, controls, scene, renderer;
  25. var cube, plane;
  26. // Create new object by parameters
  27. var createSomething = function( klass, args ) {
  28. var F = function( klass, args ) {
  29. return klass.apply( this, args );
  30. }
  31. F.prototype = klass.prototype;
  32. return new F( klass, args );
  33. };
  34. // Cube
  35. var materials = [];
  36. for ( var i = 0; i < 6; i ++ ) {
  37. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
  38. }
  39. var geometriesParams = [
  40. { type: 'CubeGeometry', args: [ 200, 200, 200, 2, 2, 2, materials ] },
  41. { type: 'TorusGeometry', args: [ 100, 60, 4, 8, Math.PI*2 ] },
  42. { type: 'TorusKnotGeometry', args: [ ], scale:0.25, meshScale:4 },
  43. { type: 'SphereGeometry', args: [ 100, 3, 3 ], meshScale:2 },
  44. { type: 'IcosahedronGeometry', args: [ 100, 1 ], meshScale:2 },
  45. { type: 'CylinderGeometry', args: [ 25, 75, 200, 8, 3 ]} ,
  46. { type: 'OctahedronGeometry', args: [200, 0], meshScale:2 },
  47. { type: 'LatheGeometry', args: [ [
  48. new THREE.Vector3(0,0,0),
  49. new THREE.Vector3(0,50,50),
  50. new THREE.Vector3(0,10,100),
  51. new THREE.Vector3(0,50,150),
  52. new THREE.Vector3(0,0,200) ] ]},
  53. { type: 'TextGeometry', args: ['&', {
  54. size: 200,
  55. height: 50,
  56. curveSegments: 1,
  57. font: "helvetiker"
  58. }]},
  59. { type: 'PlaneGeometry', args: [ 200, 200, 4, 4 ] }
  60. ];
  61. var loader = new THREE.JSONLoader();
  62. loader.load( 'obj/WaltHeadLo.js', function ( geometry ) {
  63. geometriesParams.push({type: 'WaltHead', args: [ ], meshScale: 6 });
  64. THREE.WaltHead = function() {
  65. return geometry.clone();
  66. };
  67. updateInfo()
  68. });
  69. var loader2 = new THREE.JSONLoader();
  70. loader2.load( 'obj/Suzanne.js', function ( geometry ) {
  71. geometriesParams.push({type: 'Suzanne', args: [ ], scale: 100, meshScale:2 });
  72. THREE.Suzanne = function() {
  73. return geometry.clone();
  74. };
  75. updateInfo()
  76. } );
  77. var info;
  78. var subdivisions = 2;
  79. var geometryIndex = 0;
  80. // start scene
  81. init();
  82. animate();
  83. function nextSubdivision( x ) {
  84. subdivisions = Math.max( 0, subdivisions + x );
  85. addStuff();
  86. }
  87. function nextGeometry() {
  88. geometryIndex ++;
  89. if ( geometryIndex > geometriesParams.length - 1 ) {
  90. geometryIndex = 0;
  91. }
  92. addStuff();
  93. }
  94. function switchGeometry(i) {
  95. geometryIndex = i;
  96. addStuff();
  97. }
  98. function updateInfo() {
  99. var params = geometriesParams[ geometryIndex ];
  100. var dropdown = '<select id="dropdown" onchange="switchGeometry(this.value)">';
  101. for ( i = 0; i < geometriesParams.length; i ++ ) {
  102. dropdown += '<option value="' + i + '"';
  103. dropdown += (geometryIndex == i) ? ' selected' : '';
  104. dropdown += '>' + geometriesParams[i].type + '</option>';
  105. }
  106. dropdown += '</select>';
  107. info.innerHTML = 'Drag to spin THREE.' + params.type +
  108. '<br><br>Subdivisions: ' + subdivisions +
  109. ' <a href="#" onclick="nextSubdivision(1); return false;">more</a>/<a href="#" onclick="nextSubdivision(-1); return false;">less</a>' +
  110. '<br>Geometry: ' + dropdown + ' <a href="#" onclick="nextGeometry();return false;">next</a>' +
  111. '<br><br>Vertices count: before ' + geometry.vertices.length + ' after ' + smooth.vertices.length +
  112. '<br>Face count: before ' + geometry.faces.length + ' after ' + smooth.faces.length
  113. ; //+ params.args;
  114. }
  115. function addStuff() {
  116. if ( cube ) {
  117. scene.remove( group );
  118. scene.remove( cube );
  119. }
  120. var modifier = new THREE.SubdivisionModifier( subdivisions );
  121. var params = geometriesParams[ geometryIndex ];
  122. geometry = createSomething( THREE[ params.type ], params.args );
  123. // Scale Geometry
  124. if ( params.scale ) {
  125. geometry.applyMatrix( new THREE.Matrix4().makeScale( params.scale, params.scale, params.scale ) );
  126. }
  127. // Cloning original geometry for debuging
  128. smooth = geometry.clone();
  129. // mergeVertices(); is run in case of duplicated vertices
  130. smooth.mergeVertices();
  131. smooth.computeCentroids();
  132. smooth.computeFaceNormals();
  133. smooth.computeVertexNormals();
  134. modifier.modify( smooth );
  135. updateInfo();
  136. var faceABCD = "abcd";
  137. var color, f, p, n, vertexIndex;
  138. for ( i = 0; i < smooth.faces.length; i ++ ) {
  139. f = smooth.faces[ i ];
  140. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  141. for( var j = 0; j < n; j++ ) {
  142. vertexIndex = f[ faceABCD.charAt( j ) ];
  143. p = smooth.vertices[ vertexIndex ];
  144. color = new THREE.Color( 0xffffff );
  145. color.setHSL( ( p.y ) / 200 + 0.5, 1.0, 0.5 );
  146. f.vertexColors[ j ] = color;
  147. }
  148. }
  149. group = new THREE.Object3D();
  150. scene.add( group );
  151. var material = new THREE.MeshBasicMaterial( { color: 0xfefefe, wireframe: true, opacity: 0.5 } );
  152. var mesh = new THREE.Mesh( geometry, material )
  153. group.add( mesh );
  154. group.add( new THREE.FaceNormalsHelper( mesh ) );
  155. var debugNewPoints = false;
  156. var debugOldPoints = false;
  157. // Debug new Points
  158. if (debugNewPoints) {
  159. var PI2 = Math.PI * 2;
  160. var program = function ( context ) {
  161. context.beginPath();
  162. context.arc( 0, 0, 1, 0, PI2, true );
  163. context.closePath();
  164. context.fill();
  165. }
  166. for ( var i = 0; i < smooth.vertices.length; i++ ) {
  167. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  168. particle.position = smooth.vertices[ i ];
  169. var pos = smooth.vertices.position
  170. particle.scale.x = particle.scale.y = 5;
  171. group.add( particle );
  172. }
  173. }
  174. //Debug original points
  175. if (debugOldPoints) {
  176. var drawText = function(i) {
  177. return function ( context ) {
  178. context.beginPath();
  179. context.scale(0.1,-0.1);
  180. context.fillText(i, 0,0);
  181. };
  182. }
  183. for ( var i = 0; i < geometry.vertices.length; i++ ) {
  184. particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
  185. particle.position = smooth.vertices[ i ];
  186. var pos = geometry.vertices.position
  187. particle.scale.x = particle.scale.y = 30;
  188. group.add( particle );
  189. }
  190. }
  191. var meshmaterials = [
  192. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  193. new THREE.MeshBasicMaterial( { color: 0x405040, wireframe: true, opacity: 0.8, transparent: true } )
  194. ];
  195. cube = THREE.SceneUtils.createMultiMaterialObject( smooth, meshmaterials );
  196. var meshScale = params.meshScale ? params.meshScale : 1;
  197. cube.scale.x = meshScale;
  198. cube.scale.y = meshScale;
  199. cube.scale.z = meshScale;
  200. scene.add( cube );
  201. group.scale.copy( cube.scale );
  202. }
  203. function init() {
  204. container = document.createElement( 'div' );
  205. document.body.appendChild( container );
  206. info = document.createElement( 'div' );
  207. info.style.position = 'absolute';
  208. info.style.top = '10px';
  209. info.style.width = '100%';
  210. info.style.textAlign = 'center';
  211. info.innerHTML = 'Drag to spin the geometry ';
  212. container.appendChild( info );
  213. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  214. camera.position.z = 500;
  215. controls = new THREE.OrbitControls( camera );
  216. scene = new THREE.Scene();
  217. var light = new THREE.PointLight( 0xffffff, 1.5 );
  218. light.position.set( 1000, 1000, 2000 );
  219. scene.add( light );
  220. addStuff();
  221. renderer = new THREE.WebGLRenderer( { antialias: true } ); // WebGLRenderer CanvasRenderer
  222. renderer.setSize( window.innerWidth, window.innerHeight );
  223. container.appendChild( renderer.domElement );
  224. stats = new Stats();
  225. stats.domElement.style.position = 'absolute';
  226. stats.domElement.style.top = '0px';
  227. container.appendChild( stats.domElement );
  228. //
  229. window.addEventListener( 'resize', onWindowResize, false );
  230. }
  231. function onWindowResize() {
  232. camera.aspect = window.innerWidth / window.innerHeight;
  233. camera.updateProjectionMatrix();
  234. renderer.setSize( window.innerWidth, window.innerHeight );
  235. }
  236. //
  237. function animate() {
  238. requestAnimationFrame( animate );
  239. controls.update();
  240. render();
  241. stats.update();
  242. }
  243. function render() {
  244. renderer.render( scene, camera );
  245. }
  246. </script>
  247. </body>
  248. </html>