canvas_geometry2_cube.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.min.js"></script>
  18. <script src="js/libs/stats.min.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, renderer;
  22. var cube, plane;
  23. var targetRotation = 0;
  24. var targetRotationOnMouseDown = 0;
  25. var mouseX = 0;
  26. var mouseXOnMouseDown = 0;
  27. var windowHalfX = window.innerWidth / 2;
  28. var windowHalfY = window.innerHeight / 2;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. var info = document.createElement( 'div' );
  35. info.style.position = 'absolute';
  36. info.style.top = '10px';
  37. info.style.width = '100%';
  38. info.style.textAlign = 'center';
  39. info.innerHTML = 'Drag to spin the cube';
  40. container.appendChild( info );
  41. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  42. camera.position.y = 150;
  43. camera.position.z = 500;
  44. scene = new THREE.Scene();
  45. // Cube
  46. console.time( 'box' );
  47. var geometry = new THREE.BoxGeometry2( 200, 200, 200, 20, 20, 20 );
  48. console.timeEnd( 'box' );
  49. /*
  50. for ( var i = 0; i < geometry.faces.length; i += 2 ) {
  51. var hex = Math.random() * 0xffffff;
  52. geometry.faces[ i ].color.setHex( hex );
  53. geometry.faces[ i + 1 ].color.setHex( hex );
  54. }
  55. */
  56. var material = new THREE.MeshBasicMaterial( { /*vertexColors: THREE.FaceColors,*/ overdraw: 0.5, wireframe: true } );
  57. cube = new THREE.Mesh( geometry, material );
  58. cube.position.y = 150;
  59. scene.add( cube );
  60. // Plane
  61. console.time( 'plane' );
  62. var geometry = new THREE.PlaneGeometry2( 200, 200, 20, 20 );
  63. console.timeEnd( 'plane' );
  64. geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
  65. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, overdraw: 0.5, wireframe: true } );
  66. plane = new THREE.Mesh( geometry, material );
  67. scene.add( plane );
  68. renderer = new THREE.CanvasRenderer();
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. container.appendChild( renderer.domElement );
  71. stats = new Stats();
  72. stats.domElement.style.position = 'absolute';
  73. stats.domElement.style.top = '0px';
  74. container.appendChild( stats.domElement );
  75. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  76. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  77. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  78. //
  79. window.addEventListener( 'resize', onWindowResize, false );
  80. }
  81. function onWindowResize() {
  82. windowHalfX = window.innerWidth / 2;
  83. windowHalfY = window.innerHeight / 2;
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. //
  89. function onDocumentMouseDown( event ) {
  90. event.preventDefault();
  91. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  92. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  93. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  94. mouseXOnMouseDown = event.clientX - windowHalfX;
  95. targetRotationOnMouseDown = targetRotation;
  96. }
  97. function onDocumentMouseMove( event ) {
  98. mouseX = event.clientX - windowHalfX;
  99. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  100. }
  101. function onDocumentMouseUp( event ) {
  102. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  103. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  104. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  105. }
  106. function onDocumentMouseOut( event ) {
  107. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  108. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  109. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  110. }
  111. function onDocumentTouchStart( event ) {
  112. if ( event.touches.length === 1 ) {
  113. event.preventDefault();
  114. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  115. targetRotationOnMouseDown = targetRotation;
  116. }
  117. }
  118. function onDocumentTouchMove( event ) {
  119. if ( event.touches.length === 1 ) {
  120. event.preventDefault();
  121. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  122. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  123. }
  124. }
  125. //
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. stats.update();
  130. }
  131. function render() {
  132. plane.rotation.y = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
  133. renderer.render( scene, camera );
  134. }
  135. </script>
  136. </body>
  137. </html>