webgl_modifier_subdivision.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.js"></script>
  18. <script src="js/controls/OrbitControls.js"></script>
  19. <script src="js/modifiers/BufferSubdivisionModifier.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, mesh, material;
  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. var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, opacity: 0.15, transparent: true } );
  34. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  35. var smoothMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: THREE.VertexColors } )
  36. // Cube
  37. var materials = [];
  38. for ( var i = 0; i < 6; i ++ ) {
  39. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
  40. }
  41. var geometriesParams = [
  42. { type: 'BoxGeometry', args: [ 200, 200, 200, 2, 2, 2, materials ] },
  43. { type: 'TorusGeometry', args: [ 100, 60, 4, 8, Math.PI * 2 ] },
  44. { type: 'TorusKnotGeometry', args: [ 100, 30 ], scale: 0.25, meshScale: 4 },
  45. { type: 'SphereGeometry', args: [ 100, 3, 3 ], meshScale: 2 },
  46. { type: 'IcosahedronGeometry', args: [ 100, 1 ], meshScale: 2 },
  47. { type: 'CylinderGeometry', args: [ 25, 75, 200, 8, 3 ]} ,
  48. { type: 'OctahedronGeometry', args: [200, 0], meshScale: 2 },
  49. { type: 'LatheGeometry', args: [ [
  50. new THREE.Vector2( 0, 0 ),
  51. new THREE.Vector2( 50, 50 ),
  52. new THREE.Vector2( 10, 100 ),
  53. new THREE.Vector2( 50, 150 ),
  54. new THREE.Vector2( 0, 200 ) ] ]},
  55. { type: 'TextGeometry', args: ['&', {
  56. size: 200,
  57. height: 50,
  58. curveSegments: 1
  59. }]},
  60. { type: 'PlaneGeometry', args: [ 200, 200, 4, 4 ] }
  61. ];
  62. var loader = new THREE.FontLoader();
  63. loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {
  64. geometriesParams[ 8 ].args[ 1 ].font = font;
  65. } );
  66. var loader = new THREE.JSONLoader();
  67. loader.load( 'models/json/WaltHeadLo.json', function ( geometry ) {
  68. geometriesParams.push({type: 'WaltHead', args: [ ], meshScale: 6 });
  69. THREE.WaltHead = function() {
  70. return geometry.clone();
  71. };
  72. updateInfo()
  73. });
  74. var loader2 = new THREE.JSONLoader();
  75. loader2.load( 'models/json/suzanne_geometry.json', function ( geometry ) {
  76. geometriesParams.push({type: 'Suzanne', args: [ ], scale: 100, meshScale:2 });
  77. THREE.Suzanne = function() {
  78. return geometry.clone();
  79. };
  80. updateInfo()
  81. } );
  82. var info;
  83. var subdivisions = 2;
  84. var geometryIndex = 0;
  85. // start scene
  86. init();
  87. animate();
  88. function nextSubdivision( x ) {
  89. subdivisions = Math.max( 0, subdivisions + x );
  90. addStuff();
  91. }
  92. function nextGeometry() {
  93. geometryIndex ++;
  94. if ( geometryIndex > geometriesParams.length - 1 ) {
  95. geometryIndex = 0;
  96. }
  97. addStuff();
  98. }
  99. function switchGeometry(i) {
  100. geometryIndex = i;
  101. addStuff();
  102. }
  103. function updateInfo() {
  104. var params = geometriesParams[ geometryIndex ];
  105. var dropdown = '<select id="dropdown" onchange="switchGeometry(this.value)">';
  106. for ( var i = 0; i < geometriesParams.length; i ++ ) {
  107. dropdown += '<option value="' + i + '"';
  108. dropdown += (geometryIndex == i) ? ' selected' : '';
  109. dropdown += '>' + geometriesParams[i].type + '</option>';
  110. }
  111. dropdown += '</select>';
  112. info.innerHTML = 'Drag to spin THREE.' + params.type +
  113. '<br><br>Subdivisions: ' + subdivisions +
  114. ' <a href="#" onclick="nextSubdivision(1); return false;">more</a>/<a href="#" onclick="nextSubdivision(-1); return false;">less</a>' +
  115. '<br>Geometry: ' + dropdown + ' <a href="#" onclick="nextGeometry();return false;">next</a>' +
  116. '<br><br>Vertices count: before ' + geometry.vertices.length + ' after ' + (smooth.attributes.position.array.length/3) +
  117. '<br>Face count: before ' + geometry.faces.length + ' after ' + (smooth.attributes.position.array.length / 3)
  118. }
  119. function addStuff() {
  120. if ( cube !== undefined ) {
  121. geometry.dispose();
  122. smooth.dispose();
  123. scene.remove( group );
  124. scene.remove( cube );
  125. }
  126. var modifier = new THREE.BufferSubdivisionModifier( subdivisions );
  127. var params = geometriesParams[ geometryIndex ];
  128. geometry = createSomething( THREE[ params.type ], params.args );
  129. // Scale Geometry
  130. if ( params.scale ) {
  131. geometry.scale( params.scale, params.scale, params.scale );
  132. }
  133. smooth = modifier.modify( geometry );
  134. updateInfo();
  135. // var TypedArrayHelper = function (size, registers, register_type, array_type, unit_size, accessors)
  136. var colors = new TypedArrayHelper( 0, 1, THREE.Color, Float32Array, 3, [ 'r', 'g', 'b' ] );
  137. for ( var i = 0; i < smooth.attributes.position.array.length / 3; i ++ ) {
  138. var hue = ( smooth.attributes.position.array[ ( i * 3 ) + 1 ] / 200 ) + 0.5;
  139. colors.register[ 0 ].setHSL( hue, 1, 0.5 );
  140. colors.push_element( colors.register[ 0 ] );
  141. }
  142. colors.trim_size();
  143. smooth.addAttribute( 'color', new THREE.BufferAttribute( colors.buffer, 3 ) );
  144. group = new THREE.Group();
  145. scene.add( group );
  146. mesh = new THREE.Mesh( geometry, material );
  147. group.add( mesh );
  148. cube = new THREE.Mesh( smooth, smoothMaterial );
  149. var wireframe = new THREE.Mesh( smooth, wireframeMaterial );
  150. cube.add( wireframe );
  151. cube.scale.setScalar( params.meshScale ? params.meshScale : 1 );
  152. scene.add( cube );
  153. group.scale.copy( cube.scale );
  154. }
  155. function init() {
  156. container = document.createElement( 'div' );
  157. document.body.appendChild( container );
  158. info = document.createElement( 'div' );
  159. info.style.position = 'absolute';
  160. info.style.top = '10px';
  161. info.style.width = '100%';
  162. info.style.textAlign = 'center';
  163. info.innerHTML = 'Drag to spin the geometry ';
  164. container.appendChild( info );
  165. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  166. camera.position.z = 500;
  167. scene = new THREE.Scene();
  168. scene.background = new THREE.Color( 0xf0f0f0 );
  169. var light = new THREE.PointLight( 0xffffff, 1.5 );
  170. light.position.set( 1000, 1000, 2000 );
  171. scene.add( light );
  172. addStuff();
  173. renderer = new THREE.WebGLRenderer( { antialias: true } );
  174. renderer.setPixelRatio( window.devicePixelRatio );
  175. renderer.setSize( window.innerWidth, window.innerHeight );
  176. container.appendChild( renderer.domElement );
  177. stats = new Stats();
  178. container.appendChild( stats.dom );
  179. //
  180. controls = new THREE.OrbitControls( camera, renderer.domElement );
  181. window.addEventListener( 'resize', onWindowResize, false );
  182. }
  183. function onWindowResize() {
  184. camera.aspect = window.innerWidth / window.innerHeight;
  185. camera.updateProjectionMatrix();
  186. renderer.setSize( window.innerWidth, window.innerHeight );
  187. }
  188. //
  189. function animate() {
  190. requestAnimationFrame( animate );
  191. stats.begin();
  192. render();
  193. stats.end();
  194. }
  195. function render() {
  196. renderer.render( scene, camera );
  197. }
  198. </script>
  199. </body>
  200. </html>