canvas_geometry_subdivison.html 7.4 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.TorusGeometry( 100, 60, 4, 8, Math.PI*2 );
  68. //geometry = new THREE.SphereGeometry( 200, 1, 1);
  69. //geometry = new THREE.CylinderGeometry( 50, 50, 200 );
  70. // geometry = new THREE.TextGeometry( '&', {
  71. // size: 200,
  72. // height: 50,
  73. // curveSegments: 1,
  74. // font: "helvetiker"
  75. //
  76. // });
  77. //PlaneGeometry not supported
  78. // quick fix for duplicated vertices
  79. geometry.checkDupVertices( true );
  80. //console.log(geometry);
  81. smooth = createSubdivision(geometry, 2);
  82. var PI2 = Math.PI * 2;
  83. var program = function ( context ) {
  84. context.beginPath();
  85. context.arc( 0, 0, 1, 0, PI2, true );
  86. context.closePath();
  87. context.fill();
  88. }
  89. var drawText = function(i) {
  90. return function ( context ) {
  91. context.beginPath();
  92. context.scale(0.1,-0.1);
  93. context.fillText(i, 0,0);
  94. };
  95. }
  96. group = new THREE.Object3D();
  97. group.position.y = 150;
  98. scene.add( group );
  99. // Debug new Points
  100. // for ( var i = 0; i < smooth.vertices.length; i++ ) {
  101. //
  102. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
  103. // particle.position = smooth.vertices[i].position;
  104. // var pos = smooth.vertices.position
  105. // particle.scale.x = particle.scale.y = 5;
  106. // group.add( particle );
  107. // }
  108. //Debug original points
  109. // for ( var i = 0; i < geometry.vertices.length; i++ ) {
  110. //
  111. // particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
  112. // particle.position = smooth.vertices[i].position;
  113. // var pos = geometry.vertices.position
  114. // particle.scale.x = particle.scale.y = 30;
  115. // group.add( particle );
  116. // }
  117. cube = new THREE.Mesh( smooth, new THREE.MeshBasicMaterial( { color: 0x405040, wireframe:true, opacity:0.8 } ) );
  118. //cube = new THREE.Mesh( smooth, new THREE.MeshFaceMaterial() );
  119. // material = new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
  120. // new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } );
  121. //
  122. cube.doubleSided = true;
  123. cube.position.y = 150;
  124. cube.overdraw = true;
  125. scene.add( cube );
  126. // Plane
  127. plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  128. plane.rotation.x = - 90 * ( Math.PI / 180 );
  129. plane.overdraw = true;
  130. scene.add( plane );
  131. renderer = new THREE.CanvasRenderer();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. container.appendChild( renderer.domElement );
  134. stats = new Stats();
  135. stats.domElement.style.position = 'absolute';
  136. stats.domElement.style.top = '0px';
  137. container.appendChild( stats.domElement );
  138. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  139. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  140. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  141. }
  142. //
  143. function onDocumentMouseDown( event ) {
  144. event.preventDefault();
  145. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  146. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  147. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  148. mouseXOnMouseDown = event.clientX - windowHalfX;
  149. targetRotationOnMouseDown = targetRotation;
  150. }
  151. function onDocumentMouseMove( event ) {
  152. mouseX = event.clientX - windowHalfX;
  153. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  154. }
  155. function onDocumentMouseUp( event ) {
  156. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  157. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  158. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  159. }
  160. function onDocumentMouseOut( event ) {
  161. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  162. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  163. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  164. }
  165. function onDocumentTouchStart( event ) {
  166. if ( event.touches.length == 1 ) {
  167. event.preventDefault();
  168. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  169. targetRotationOnMouseDown = targetRotation;
  170. }
  171. }
  172. function onDocumentTouchMove( event ) {
  173. if ( event.touches.length == 1 ) {
  174. event.preventDefault();
  175. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  176. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  177. }
  178. }
  179. //
  180. function animate() {
  181. requestAnimationFrame( animate );
  182. render();
  183. stats.update();
  184. }
  185. function render() {
  186. group.rotation.y = plane.rotation.z = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
  187. renderer.render( scene, camera );
  188. }
  189. </script>
  190. </body>
  191. </html>
粤ICP备19079148号