webgl_modifier_subdivision.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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>
  22. var container, stats;
  23. var camera, controls, scene, renderer;
  24. var cube, plane;
  25. // Create new object by parameters
  26. var createSomething = function( klass, args ) {
  27. var F = function( klass, args ) {
  28. return klass.apply( this, args );
  29. };
  30. F.prototype = klass.prototype;
  31. return new F( klass, args );
  32. };
  33. // Cube
  34. var materials = [];
  35. for ( var i = 0; i < 6; i ++ ) {
  36. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
  37. }
  38. var geometriesParams = [
  39. { type: 'BoxGeometry', args: [ 200, 200, 200, 2, 2, 2, materials ] },
  40. { type: 'TorusGeometry', args: [ 100, 60, 4, 8, Math.PI*2 ] },
  41. { type: 'TorusKnotGeometry', args: [ ], scale:0.25, meshScale:4 },
  42. { type: 'SphereGeometry', args: [ 100, 3, 3 ], meshScale:2 },
  43. { type: 'IcosahedronGeometry', args: [ 100, 1 ], meshScale:2 },
  44. { type: 'CylinderGeometry', args: [ 25, 75, 200, 8, 3 ]} ,
  45. { type: 'OctahedronGeometry', args: [200, 0], meshScale:2 },
  46. { type: 'LatheGeometry', args: [ [
  47. new THREE.Vector2(0,0),
  48. new THREE.Vector2(50,50),
  49. new THREE.Vector2(10,100),
  50. new THREE.Vector2(50,150),
  51. new THREE.Vector2(0,200) ] ]},
  52. { type: 'TextGeometry', args: ['&', {
  53. size: 200,
  54. height: 50,
  55. curveSegments: 1
  56. }]},
  57. { type: 'PlaneGeometry', args: [ 200, 200, 4, 4 ] }
  58. ];
  59. var loader = new THREE.FontLoader();
  60. loader.load( 'fonts/helvetiker_regular.typeface.js', function ( font ) {
  61. geometriesParams[ 8 ].args[ 1 ].font = font;
  62. } );
  63. var loader = new THREE.JSONLoader();
  64. loader.load( 'obj/WaltHeadLo.js', function ( geometry ) {
  65. geometriesParams.push({type: 'WaltHead', args: [ ], meshScale: 6 });
  66. THREE.WaltHead = function() {
  67. return geometry.clone();
  68. };
  69. updateInfo()
  70. });
  71. var loader2 = new THREE.JSONLoader();
  72. loader2.load( 'obj/Suzanne.js', function ( geometry ) {
  73. geometriesParams.push({type: 'Suzanne', args: [ ], scale: 100, meshScale:2 });
  74. THREE.Suzanne = function() {
  75. return geometry.clone();
  76. };
  77. updateInfo()
  78. } );
  79. var info;
  80. var subdivisions = 2;
  81. var geometryIndex = 0;
  82. // start scene
  83. init();
  84. animate();
  85. function nextSubdivision( x ) {
  86. subdivisions = Math.max( 0, subdivisions + x );
  87. addStuff();
  88. }
  89. function nextGeometry() {
  90. geometryIndex ++;
  91. if ( geometryIndex > geometriesParams.length - 1 ) {
  92. geometryIndex = 0;
  93. }
  94. addStuff();
  95. }
  96. function switchGeometry(i) {
  97. geometryIndex = i;
  98. addStuff();
  99. }
  100. function updateInfo() {
  101. var params = geometriesParams[ geometryIndex ];
  102. var dropdown = '<select id="dropdown" onchange="switchGeometry(this.value)">';
  103. for ( i = 0; i < geometriesParams.length; i ++ ) {
  104. dropdown += '<option value="' + i + '"';
  105. dropdown += (geometryIndex == i) ? ' selected' : '';
  106. dropdown += '>' + geometriesParams[i].type + '</option>';
  107. }
  108. dropdown += '</select>';
  109. info.innerHTML = 'Drag to spin THREE.' + params.type +
  110. '<br><br>Subdivisions: ' + subdivisions +
  111. ' <a href="#" onclick="nextSubdivision(1); return false;">more</a>/<a href="#" onclick="nextSubdivision(-1); return false;">less</a>' +
  112. '<br>Geometry: ' + dropdown + ' <a href="#" onclick="nextGeometry();return false;">next</a>' +
  113. '<br><br>Vertices count: before ' + geometry.vertices.length + ' after ' + smooth.vertices.length +
  114. '<br>Face count: before ' + geometry.faces.length + ' after ' + smooth.faces.length
  115. ; //+ params.args;
  116. }
  117. function addStuff() {
  118. if ( cube ) {
  119. scene.remove( group );
  120. scene.remove( cube );
  121. }
  122. var modifier = new THREE.SubdivisionModifier( subdivisions );
  123. var params = geometriesParams[ geometryIndex ];
  124. geometry = createSomething( THREE[ params.type ], params.args );
  125. // Scale Geometry
  126. if ( params.scale ) {
  127. geometry.scale( params.scale, params.scale, params.scale );
  128. }
  129. // Cloning original geometry for debuging
  130. smooth = geometry.clone();
  131. // mergeVertices(); is run in case of duplicated vertices
  132. smooth.mergeVertices();
  133. smooth.computeFaceNormals();
  134. smooth.computeVertexNormals();
  135. modifier.modify( smooth );
  136. updateInfo();
  137. var faceABCD = "abcd";
  138. var color, f, p, n, vertexIndex;
  139. for ( i = 0; i < smooth.faces.length; i ++ ) {
  140. f = smooth.faces[ i ];
  141. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  142. for( var j = 0; j < n; j++ ) {
  143. vertexIndex = f[ faceABCD.charAt( j ) ];
  144. p = smooth.vertices[ vertexIndex ];
  145. color = new THREE.Color( 0xffffff );
  146. color.setHSL( ( p.y ) / 200 + 0.5, 1.0, 0.5 );
  147. f.vertexColors[ j ] = color;
  148. }
  149. }
  150. group = new THREE.Group();
  151. scene.add( group );
  152. var material = new THREE.MeshBasicMaterial( { color: 0xfefefe, wireframe: true, opacity: 0.5 } );
  153. var mesh = new THREE.Mesh( geometry, material );
  154. group.add( mesh );
  155. var meshmaterials = [
  156. new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors, shininess: 0 } ),
  157. new THREE.MeshBasicMaterial( { color: 0x405040, wireframe: true, opacity: 0.8, transparent: true } )
  158. ];
  159. cube = THREE.SceneUtils.createMultiMaterialObject( smooth, meshmaterials );
  160. var meshScale = params.meshScale ? params.meshScale : 1;
  161. cube.scale.x = meshScale;
  162. cube.scale.y = meshScale;
  163. cube.scale.z = meshScale;
  164. scene.add( cube );
  165. group.scale.copy( cube.scale );
  166. }
  167. function init() {
  168. container = document.createElement( 'div' );
  169. document.body.appendChild( container );
  170. info = document.createElement( 'div' );
  171. info.style.position = 'absolute';
  172. info.style.top = '10px';
  173. info.style.width = '100%';
  174. info.style.textAlign = 'center';
  175. info.innerHTML = 'Drag to spin the geometry ';
  176. container.appendChild( info );
  177. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  178. camera.position.z = 500;
  179. scene = new THREE.Scene();
  180. var light = new THREE.PointLight( 0xffffff, 1.5 );
  181. light.position.set( 1000, 1000, 2000 );
  182. scene.add( light );
  183. addStuff();
  184. renderer = new THREE.WebGLRenderer( { antialias: true } ); // WebGLRenderer CanvasRenderer
  185. renderer.setClearColor( 0xf0f0f0 );
  186. renderer.setPixelRatio( window.devicePixelRatio );
  187. renderer.setSize( window.innerWidth, window.innerHeight );
  188. container.appendChild( renderer.domElement );
  189. stats = new Stats();
  190. stats.domElement.style.position = 'absolute';
  191. stats.domElement.style.top = '0px';
  192. container.appendChild( stats.domElement );
  193. //
  194. controls = new THREE.OrbitControls( camera, renderer.domElement );
  195. window.addEventListener( 'resize', onWindowResize, false );
  196. }
  197. function onWindowResize() {
  198. camera.aspect = window.innerWidth / window.innerHeight;
  199. camera.updateProjectionMatrix();
  200. renderer.setSize( window.innerWidth, window.innerHeight );
  201. }
  202. //
  203. function animate() {
  204. requestAnimationFrame( animate );
  205. controls.update();
  206. render();
  207. stats.update();
  208. }
  209. function render() {
  210. renderer.render( scene, camera );
  211. }
  212. </script>
  213. </body>
  214. </html>