cube.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. {
  10. font-family: Monospace;
  11. background-color: #ffffff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <script type="text/javascript" src="../../build/three.js"></script>
  19. <script type="text/javascript" src="http://github.com/mrdoob/stats.js/raw/master/build/stats.js"></script>
  20. <script type="text/javascript">
  21. var SCREEN_WIDTH = window.innerWidth;
  22. var SCREEN_HEIGHT = window.innerHeight;
  23. var container;
  24. var stats;
  25. var camera;
  26. var scene;
  27. var 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. init();
  36. setInterval(loop, 1000/60);
  37. function init()
  38. {
  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 Camera(0, 150, 400);
  49. camera.focus = 300;
  50. camera.target.y = 150;
  51. camera.updateMatrix();
  52. scene = new 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.floor( Math.random() * 128), Math.floor( Math.random() * 128 + 128), Math.floor( Math.random() * 128 + 128), 255 );
  57. cube = new Mesh(geometry, new FaceColorMaterial());
  58. cube.position.y = 150;
  59. cube.updateMatrix();
  60. scene.add(cube);
  61. // Plane
  62. plane = new Mesh(new Plane(200, 200), new ColorMaterial(0xeeeeee));
  63. plane.rotation.x = 90 * (Math.PI / 180);
  64. plane.updateMatrix();
  65. scene.add(plane);
  66. renderer = new CanvasRenderer();
  67. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  68. container.appendChild(renderer.viewport);
  69. stats = new Stats();
  70. container.appendChild(stats.getDisplayElement());
  71. document.addEventListener('mousedown', onDocumentMouseDown, false);
  72. document.addEventListener('touchstart', onDocumentTouchStart, false);
  73. document.addEventListener('touchmove', onDocumentTouchMove, false);
  74. }
  75. //
  76. function onDocumentMouseDown( event )
  77. {
  78. event.preventDefault();
  79. document.addEventListener('mousemove', onDocumentMouseMove, false);
  80. document.addEventListener('mouseup', onDocumentMouseUp, false);
  81. mouseXOnMouseDown = event.clientX - windowHalfX;
  82. targetRotationOnMouseDown = targetRotation;
  83. }
  84. function onDocumentMouseMove( event )
  85. {
  86. mouseX = event.clientX - windowHalfX;
  87. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  88. }
  89. function onDocumentMouseUp( event )
  90. {
  91. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  92. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  93. }
  94. function onDocumentTouchStart( event )
  95. {
  96. if(event.touches.length == 1)
  97. {
  98. event.preventDefault();
  99. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  100. targetRotationOnMouseDown = targetRotation;
  101. }
  102. }
  103. function onDocumentTouchMove( event )
  104. {
  105. if(event.touches.length == 1)
  106. {
  107. event.preventDefault();
  108. mouseX = event.touches[0].pageX - windowHalfX;
  109. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  110. }
  111. }
  112. //
  113. function loop()
  114. {
  115. cube.rotation.y += (targetRotation - cube.rotation.y) * 0.05;
  116. cube.updateMatrix();
  117. plane.rotation.z = -cube.rotation.y;
  118. plane.updateMatrix();
  119. renderer.render(scene, camera);
  120. stats.update();
  121. }
  122. </script>
  123. </body>
  124. </html>