webgl_geometry_subdivison.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - subdivisions</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/RequestAnimationFrame.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script src="../src/core/Geometry.js"></script>
  21. <script src="../src/extras/geometries/CubeGeometry.js"></script>
  22. <script src="../src/extras/geometries/CylinderGeometry.js"></script>
  23. <script src="../src/extras/geometries/TorusGeometry.js"></script>
  24. <script src="../src/extras/modifiers/SubdivisionModifier.js"></script>
  25. <script src="fonts/helvetiker_regular.typeface.js"></script>
  26. <script src="obj/WaltHead.js"></script>
  27. <!-- -->
  28. <script>
  29. var container, stats;
  30. var camera, scene, renderer;
  31. var cube, plane;
  32. var targetYRotation = targetXRotation = 0;
  33. var targetYRotationOnMouseDown = targetXRotationOnMouseDown = 0;
  34. var mouseX = 0, mouseY = 0;
  35. var mouseXOnMouseDown = 0;
  36. var windowHalfX = window.innerWidth / 2;
  37. var windowHalfY = window.innerHeight / 2;
  38. // Create new object by parameters
  39. var createSomething = function( klass, args ) {
  40. var F = function( klass, args ) {
  41. return klass.apply( this, args );
  42. }
  43. F.prototype = klass.prototype;
  44. return new F( klass, args );
  45. };
  46. // Cube
  47. var materials = [];
  48. for ( var i = 0; i < 6; i ++ ) {
  49. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
  50. }
  51. THREE.WaltHead = WaltHead;
  52. var geometriesParams = [
  53. {type: 'CubeGeometry', args: [ 200, 200, 200, 2, 2, 2, materials ] },
  54. {type: 'TorusGeometry', args: [ 100, 60, 4, 8, Math.PI*2 ] },
  55. {type: 'SphereGeometry', args: [ 100, 3, 3 ] },
  56. {type: 'IcosahedronGeometry', args: [ 1 ], scale: 200 },
  57. {type: 'CylinderGeometry', args: [ 25, 75, 200, 8, 3 ]} ,
  58. {type: 'TextGeometry', args: ['&', {
  59. size: 200,
  60. height: 50,
  61. curveSegments: 1,
  62. font: "helvetiker"
  63. }]},
  64. {type: 'PlaneGeometry', args: [ 200, 200, 4, 4 ] },
  65. {type: 'WaltHead', args: [ ], scale: 6 }
  66. ];
  67. var info;
  68. var subdivisions = 2;
  69. var geometryIndex = 0;
  70. // start scene
  71. init();
  72. animate();
  73. function nextSubdivision( x ) {
  74. subdivisions = Math.max( 0, subdivisions + x );
  75. addStuff();
  76. }
  77. function nextGeometry() {
  78. geometryIndex ++;
  79. if ( geometryIndex > geometriesParams.length - 1 ) {
  80. geometryIndex = 0;
  81. }
  82. addStuff();
  83. }
  84. function addStuff() {
  85. if ( cube ) {
  86. scene.remove( group );
  87. scene.remove( cube );
  88. }
  89. var modifier = new THREE.SubdivisionModifier( subdivisions );
  90. var params = geometriesParams[ geometryIndex ];
  91. geometry = createSomething( THREE[ params.type ], params.args );
  92. // Cloning original geometry for debuging
  93. smooth = THREE.GeometryUtils.clone( geometry );
  94. // mergeVertices(); is run in case of duplicated vertices
  95. smooth.mergeVertices();
  96. modifier.modify( smooth );
  97. info.innerHTML = 'Drag to spin the geometry THREE.' + params.type
  98. + ' with ' + modifier.subdivisions + ' subdivisions' +
  99. '<br>Subdivision <a href="#" onclick="nextSubdivision(1); return false;">+</a>/<a href="#" onclick="nextSubdivision(-1); return false;">-</a>' +
  100. '<br>Geometry: <a href="#" onclick="nextGeometry();return false;">next</a>' +
  101. '<br>vertices count: before ' + geometry.vertices.length + ' after ' + smooth.vertices.length +
  102. '<br>face count: before ' + geometry.faces.length + ' after ' + smooth.faces.length
  103. ; //+ params.args;
  104. var faceABCD = "abcd";
  105. var color, f, p, n, vertexIndex;
  106. for ( i = 0; i < smooth.faces.length; i ++ ) {
  107. f = smooth.faces[ i ];
  108. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  109. for( var j = 0; j < n; j++ ) {
  110. vertexIndex = f[ faceABCD.charAt( j ) ];
  111. p = smooth.vertices[ vertexIndex ].position;
  112. color = new THREE.Color( 0xffffff );
  113. color.setHSV( ( p.y ) / 200 + 0.5, 1.0, 1.0 );
  114. f.vertexColors[ j ] = color;
  115. }
  116. }
  117. group = new THREE.Object3D();
  118. group.add( new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: true } ) ) );
  119. scene.add( group );
  120. // Debug new Points
  121. // var PI2 = Math.PI * 2;
  122. // var program = function ( context ) {
  123. //
  124. // context.beginPath();
  125. // context.arc( 0, 0, 1, 0, PI2, true );
  126. // context.closePath();
  127. // context.fill();
  128. //
  129. // }
  130. //
  131. // for ( var i = 0; i < smooth.vertices.length; i++ ) {
  132. //
  133. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  134. // particle.position = smooth.vertices[i].position;
  135. // var pos = smooth.vertices.position
  136. // particle.scale.x = particle.scale.y = 5;
  137. // group.add( particle );
  138. // }
  139. //Debug original points
  140. // var drawText = function(i) {
  141. //
  142. // return function ( context ) {
  143. //
  144. // context.beginPath();
  145. // context.scale(0.1,-0.1);
  146. //
  147. // context.fillText(i, 0,0);
  148. //
  149. // };
  150. //
  151. // }
  152. // for ( var i = 0; i < geometry.vertices.length; i++ ) {
  153. //
  154. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
  155. // particle.position = smooth.vertices[i].position;
  156. // var pos = geometry.vertices.position
  157. // particle.scale.x = particle.scale.y = 30;
  158. // group.add( particle );
  159. // }
  160. //
  161. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, vertexColors: THREE.VertexColors } );
  162. cube = new THREE.Mesh( smooth, material );
  163. var toscale = params.scale ? params.scale : 1;
  164. cube.scale.x = toscale;
  165. cube.scale.y = toscale;
  166. cube.scale.z = toscale;
  167. scene.add( cube );
  168. group.scale.copy( cube.scale );
  169. }
  170. function init() {
  171. container = document.createElement( 'div' );
  172. document.body.appendChild( container );
  173. info = document.createElement( 'div' );
  174. info.style.position = 'absolute';
  175. info.style.top = '10px';
  176. info.style.width = '100%';
  177. info.style.textAlign = 'center';
  178. info.innerHTML = 'Drag to spin the geometry ';
  179. container.appendChild( info );
  180. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  181. camera.position.z = 500;
  182. scene = new THREE.Scene();
  183. var light = new THREE.PointLight( 0xffffff, 1.5 );
  184. light.position.set( 1000, 1000, 2000 );
  185. scene.add( light );
  186. addStuff();
  187. renderer = new THREE.WebGLRenderer(); // WebGLRenderer CanvasRenderer
  188. renderer.setSize( window.innerWidth, window.innerHeight );
  189. container.appendChild( renderer.domElement );
  190. stats = new Stats();
  191. stats.domElement.style.position = 'absolute';
  192. stats.domElement.style.top = '0px';
  193. container.appendChild( stats.domElement );
  194. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  195. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  196. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  197. }
  198. //
  199. function onDocumentMouseDown( event ) {
  200. event.preventDefault();
  201. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  202. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  203. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  204. mouseXOnMouseDown = event.clientX - windowHalfX;
  205. mouseYOnMouseDown = event.clientY - windowHalfY;
  206. targetYRotationOnMouseDown = targetYRotation;
  207. targetXRotationOnMouseDown = targetXRotation;
  208. }
  209. function onDocumentMouseMove( event ) {
  210. mouseX = event.clientX - windowHalfX;
  211. mouseY = event.clientY - windowHalfY;
  212. targetYRotation = targetYRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  213. targetXRotation = targetXRotationOnMouseDown + ( mouseY - mouseYOnMouseDown ) * 0.02;
  214. }
  215. function onDocumentMouseUp( event ) {
  216. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  217. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  218. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  219. }
  220. function onDocumentMouseOut( event ) {
  221. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  222. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  223. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  224. }
  225. function onDocumentTouchStart( event ) {
  226. if ( event.touches.length == 1 ) {
  227. event.preventDefault();
  228. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  229. targetRotationOnMouseDown = targetRotation;
  230. }
  231. }
  232. function onDocumentTouchMove( event ) {
  233. if ( event.touches.length == 1 ) {
  234. event.preventDefault();
  235. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  236. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  237. }
  238. }
  239. //
  240. function animate() {
  241. requestAnimationFrame( animate );
  242. render();
  243. stats.update();
  244. }
  245. function render() {
  246. group.rotation.x = cube.rotation.x += ( targetXRotation - cube.rotation.x ) * 0.05;
  247. group.rotation.y = cube.rotation.y += ( targetYRotation - cube.rotation.y ) * 0.05;
  248. renderer.render( scene, camera );
  249. }
  250. </script>
  251. </body>
  252. </html>