canvas_geometry_subdivison.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - cube</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/geometries/SubdivisionGeometry.js"></script>
  25. <script src="fonts/helvetiker_regular.typeface.js"></script>
  26. <script>
  27. var container, stats;
  28. var camera, scene, renderer;
  29. var cube, plane;
  30. var targetRotation = 0;
  31. var targetRotationOnMouseDown = 0;
  32. var mouseX = 0;
  33. var mouseXOnMouseDown = 0;
  34. var windowHalfX = window.innerWidth / 2;
  35. var windowHalfY = window.innerHeight / 2;
  36. // Create subdivision geometry
  37. function createSubdivision(geometry, repeats) {
  38. repeats = (repeats === undefined ) ? 1 : repeats;
  39. var smooth = geometry;
  40. while (repeats--) {
  41. smooth = new THREE.SubdivisionGeometry(smooth);
  42. }
  43. return smooth;
  44. }
  45. init();
  46. animate();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. var info = document.createElement( 'div' );
  51. info.style.position = 'absolute';
  52. info.style.top = '10px';
  53. info.style.width = '100%';
  54. info.style.textAlign = 'center';
  55. info.innerHTML = 'Drag to spin the cube';
  56. container.appendChild( info );
  57. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  58. camera.position.y = 150;
  59. camera.position.z = 500;
  60. camera.target.position.y = 150;
  61. scene = new THREE.Scene();
  62. // Cube
  63. var materials = [];
  64. for ( var i = 0; i < 6; i ++ ) {
  65. materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
  66. }
  67. //geometry = new THREE.CubeGeometry( 200, 200, 200, 2, 2, 2, materials );
  68. geometry = new THREE.CubeGeometry( 200, 200, 200, 1, 1, 3, materials );
  69. //geometry = new THREE.IcosahedronGeometry( 1 );
  70. //geometry = new THREE.TorusGeometry( 100, 60, 4, 8, Math.PI*2 );
  71. //geometry = new THREE.SphereGeometry( 200, 1, 1);
  72. //geometry = new THREE.CylinderGeometry( 50, 50, 200 );
  73. // geometry = new THREE.TextGeometry( '&', {
  74. // size: 200,
  75. // height: 50,
  76. // curveSegments: 1,
  77. // font: "helvetiker"
  78. //
  79. // });
  80. //PlaneGeometry not supported
  81. // make sure mergeVertices(); is run to fix quick duplicated vertices
  82. geometry.mergeVertices();
  83. //console.log(geometry);
  84. smooth = createSubdivision(geometry, 2);
  85. var PI2 = Math.PI * 2;
  86. var program = function ( context ) {
  87. context.beginPath();
  88. context.arc( 0, 0, 1, 0, PI2, true );
  89. context.closePath();
  90. context.fill();
  91. }
  92. var drawText = function(i) {
  93. return function ( context ) {
  94. context.beginPath();
  95. context.scale(0.1,-0.1);
  96. context.fillText(i, 0,0);
  97. };
  98. }
  99. group = new THREE.Object3D();
  100. group.position.y = 150;
  101. scene.add( group );
  102. // Debug new Points
  103. // for ( var i = 0; i < smooth.vertices.length; i++ ) {
  104. //
  105. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  106. // particle.position = smooth.vertices[i].position;
  107. // var pos = smooth.vertices.position
  108. // particle.scale.x = particle.scale.y = 5;
  109. // group.add( particle );
  110. // }
  111. //Debug original points
  112. // for ( var i = 0; i < geometry.vertices.length; i++ ) {
  113. //
  114. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
  115. // particle.position = smooth.vertices[i].position;
  116. // var pos = geometry.vertices.position
  117. // particle.scale.x = particle.scale.y = 30;
  118. // group.add( particle );
  119. // }
  120. cube = new THREE.Mesh( smooth, new THREE.MeshBasicMaterial( { color: 0x405040, wireframe:true, opacity:0.8 } ) );
  121. //cube = new THREE.Mesh( smooth, new THREE.MeshFaceMaterial() );
  122. // material = new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  123. // new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } );
  124. // cube.scale.x = 200;
  125. // cube.scale.y = 200;
  126. // cube.scale.z = 200;
  127. //
  128. cube.doubleSided = true;
  129. cube.position.y = 150;
  130. cube.overdraw = true;
  131. scene.add( cube );
  132. // Plane
  133. plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  134. plane.rotation.x = - 90 * ( Math.PI / 180 );
  135. plane.overdraw = true;
  136. scene.add( plane );
  137. renderer = new THREE.CanvasRenderer();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. container.appendChild( renderer.domElement );
  140. stats = new Stats();
  141. stats.domElement.style.position = 'absolute';
  142. stats.domElement.style.top = '0px';
  143. container.appendChild( stats.domElement );
  144. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  145. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  146. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  147. }
  148. //
  149. function onDocumentMouseDown( event ) {
  150. event.preventDefault();
  151. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  152. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  153. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  154. mouseXOnMouseDown = event.clientX - windowHalfX;
  155. targetRotationOnMouseDown = targetRotation;
  156. }
  157. function onDocumentMouseMove( event ) {
  158. mouseX = event.clientX - windowHalfX;
  159. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  160. }
  161. function onDocumentMouseUp( event ) {
  162. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  163. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  164. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  165. }
  166. function onDocumentMouseOut( event ) {
  167. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  168. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  169. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  170. }
  171. function onDocumentTouchStart( event ) {
  172. if ( event.touches.length == 1 ) {
  173. event.preventDefault();
  174. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  175. targetRotationOnMouseDown = targetRotation;
  176. }
  177. }
  178. function onDocumentTouchMove( event ) {
  179. if ( event.touches.length == 1 ) {
  180. event.preventDefault();
  181. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  182. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  183. }
  184. }
  185. //
  186. function animate() {
  187. requestAnimationFrame( animate );
  188. render();
  189. stats.update();
  190. }
  191. function render() {
  192. group.rotation.y = plane.rotation.z = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
  193. renderer.render( scene, camera );
  194. }
  195. </script>
  196. </body>
  197. </html>