geometry_cube.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - geometry - cube</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  7. <style type="text/css">
  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 type="text/javascript" src="../build/three.js"></script>
  18. <script type="text/javascript" src="primitives/Cube.js"></script>
  19. <script type="text/javascript" src="primitives/Plane.js"></script>
  20. <script type="text/javascript" src="http://github.com/mrdoob/stats.js/raw/master/build/stats.js"></script>
  21. <script type="text/javascript">
  22. var SCREEN_WIDTH = window.innerWidth;
  23. var SCREEN_HEIGHT = window.innerHeight;
  24. var container;
  25. var stats;
  26. var camera;
  27. var scene;
  28. var 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. init();
  37. setInterval(loop, 1000/60);
  38. function init() {
  39. container = document.createElement('div');
  40. document.body.appendChild(container);
  41. var info = document.createElement('div');
  42. info.style.position = 'absolute';
  43. info.style.top = '10px';
  44. info.style.width = '100%';
  45. info.style.textAlign = 'center';
  46. info.innerHTML = 'Drag to spin the cube';
  47. container.appendChild(info);
  48. camera = new THREE.Camera( 70, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  49. camera.position.y = 150;
  50. camera.position.z = 500;
  51. camera.target.position.y = 150;
  52. scene = new THREE.Scene();
  53. // Cube
  54. geometry = new Cube(200, 200, 200);
  55. for (var i = 0; i < geometry.faces.length; i++) {
  56. geometry.faces[i].color.setRGBA( Math.random() * 0.5, Math.random() * 0.5 + 0.5, Math.random() * 0.5 + 0.5, 1 );
  57. }
  58. cube = new THREE.Mesh(geometry, new THREE.FaceColorFillMaterial());
  59. cube.position.y = 150;
  60. scene.add(cube);
  61. // Plane
  62. plane = new THREE.Mesh(new Plane(200, 200), new THREE.ColorFillMaterial(0xe0e0e0));
  63. plane.rotation.x = -90 * (Math.PI / 180);
  64. scene.add(plane);
  65. renderer = new THREE.CanvasRenderer();
  66. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  67. container.appendChild(renderer.domElement);
  68. stats = new Stats();
  69. stats.domElement.style.position = 'absolute';
  70. stats.domElement.style.top = '0px';
  71. container.appendChild(stats.domElement);
  72. document.addEventListener('mousedown', onDocumentMouseDown, false);
  73. document.addEventListener('touchstart', onDocumentTouchStart, false);
  74. document.addEventListener('touchmove', onDocumentTouchMove, false);
  75. }
  76. //
  77. function onDocumentMouseDown( event ) {
  78. event.preventDefault();
  79. document.addEventListener('mousemove', onDocumentMouseMove, false);
  80. document.addEventListener('mouseup', onDocumentMouseUp, false);
  81. document.addEventListener('mouseout', onDocumentMouseOut, false);
  82. mouseXOnMouseDown = event.clientX - windowHalfX;
  83. targetRotationOnMouseDown = targetRotation;
  84. }
  85. function onDocumentMouseMove( event ) {
  86. mouseX = event.clientX - windowHalfX;
  87. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;
  88. }
  89. function onDocumentMouseUp( event ) {
  90. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  91. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  92. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  93. }
  94. function onDocumentMouseOut( event ) {
  95. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  96. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  97. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  98. }
  99. function onDocumentTouchStart( event ) {
  100. if(event.touches.length == 1) {
  101. event.preventDefault();
  102. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  103. targetRotationOnMouseDown = targetRotation;
  104. }
  105. }
  106. function onDocumentTouchMove( event ) {
  107. if(event.touches.length == 1) {
  108. event.preventDefault();
  109. mouseX = event.touches[0].pageX - windowHalfX;
  110. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  111. }
  112. }
  113. //
  114. function loop() {
  115. plane.rotation.z = cube.rotation.y += (targetRotation - cube.rotation.y) * 0.05;
  116. renderer.render(scene, camera);
  117. stats.update();
  118. }
  119. </script>
  120. </body>
  121. </html>