canvas_geometry_subdivison.html 7.3 KB

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