webgl_modifier_subdivision.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - modifier - Subdivisions using Loop Subdivision Scheme</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: 'BoxGeometry', 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.computeFaceNormals();
  132. smooth.computeVertexNormals();
  133. modifier.modify( smooth );
  134. updateInfo();
  135. var faceABCD = "abcd";
  136. var color, f, p, n, vertexIndex;
  137. for ( i = 0; i < smooth.faces.length; i ++ ) {
  138. f = smooth.faces[ i ];
  139. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  140. for( var j = 0; j < n; j++ ) {
  141. vertexIndex = f[ faceABCD.charAt( j ) ];
  142. p = smooth.vertices[ vertexIndex ];
  143. color = new THREE.Color( 0xffffff );
  144. color.setHSL( ( p.y ) / 200 + 0.5, 1.0, 0.5 );
  145. f.vertexColors[ j ] = color;
  146. }
  147. }
  148. group = new THREE.Group();
  149. scene.add( group );
  150. var material = new THREE.MeshBasicMaterial( { color: 0xfefefe, wireframe: true, opacity: 0.5 } );
  151. var mesh = new THREE.Mesh( geometry, material )
  152. group.add( mesh );
  153. var meshmaterials = [
  154. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  155. new THREE.MeshBasicMaterial( { color: 0x405040, wireframe: true, opacity: 0.8, transparent: true } )
  156. ];
  157. cube = THREE.SceneUtils.createMultiMaterialObject( smooth, meshmaterials );
  158. var meshScale = params.meshScale ? params.meshScale : 1;
  159. cube.scale.x = meshScale;
  160. cube.scale.y = meshScale;
  161. cube.scale.z = meshScale;
  162. scene.add( cube );
  163. group.scale.copy( cube.scale );
  164. }
  165. function init() {
  166. container = document.createElement( 'div' );
  167. document.body.appendChild( container );
  168. info = document.createElement( 'div' );
  169. info.style.position = 'absolute';
  170. info.style.top = '10px';
  171. info.style.width = '100%';
  172. info.style.textAlign = 'center';
  173. info.innerHTML = 'Drag to spin the geometry ';
  174. container.appendChild( info );
  175. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  176. camera.position.z = 500;
  177. scene = new THREE.Scene();
  178. var light = new THREE.PointLight( 0xffffff, 1.5 );
  179. light.position.set( 1000, 1000, 2000 );
  180. scene.add( light );
  181. addStuff();
  182. renderer = new THREE.WebGLRenderer( { antialias: true } ); // WebGLRenderer CanvasRenderer
  183. renderer.setClearColor( 0xf0f0f0 );
  184. renderer.setPixelRatio( window.devicePixelRatio );
  185. renderer.setSize( window.innerWidth, window.innerHeight );
  186. container.appendChild( renderer.domElement );
  187. stats = new Stats();
  188. stats.domElement.style.position = 'absolute';
  189. stats.domElement.style.top = '0px';
  190. container.appendChild( stats.domElement );
  191. //
  192. controls = new THREE.OrbitControls( camera, renderer.domElement );
  193. window.addEventListener( 'resize', onWindowResize, false );
  194. }
  195. function onWindowResize() {
  196. camera.aspect = window.innerWidth / window.innerHeight;
  197. camera.updateProjectionMatrix();
  198. renderer.setSize( window.innerWidth, window.innerHeight );
  199. }
  200. //
  201. function animate() {
  202. requestAnimationFrame( animate );
  203. controls.update();
  204. render();
  205. stats.update();
  206. }
  207. function render() {
  208. renderer.render( scene, camera );
  209. }
  210. </script>
  211. </body>
  212. </html>