cube.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 AMOUNT = 100;
  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. {
  40. container = document.createElement('div');
  41. document.body.appendChild(container);
  42. var info = document.createElement('div');
  43. info.style.position = 'absolute';
  44. info.style.top = '10px';
  45. info.style.width = '100%';
  46. info.style.textAlign = 'center';
  47. info.innerHTML = 'Drag to spin the cube';
  48. container.appendChild(info);
  49. camera = new Camera(0, 150, 300);
  50. camera.focus = 300;
  51. camera.target.y = 150;
  52. camera.updateMatrix();
  53. scene = new Scene();
  54. // Cube
  55. geometry = new Cube(200, 200, 200);
  56. for (var i = 0; i < geometry.faces.length; i++)
  57. geometry.faces[i].color.setRGBA( Math.floor( Math.random() * 128), Math.floor( Math.random() * 128 + 128), Math.floor( Math.random() * 128 + 128), 255 );
  58. cube = new Mesh(geometry, new FaceColorMaterial());
  59. cube.position.y = 150;
  60. cube.updateMatrix();
  61. scene.add(cube);
  62. // Plane
  63. plane = new Mesh(new Plane(200, 200), new ColorMaterial(0xeeeeee));
  64. plane.rotation.x = 90 * (Math.PI / 180);
  65. plane.updateMatrix();
  66. scene.add(plane);
  67. renderer = new CanvasRenderer();
  68. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  69. container.appendChild(renderer.viewport);
  70. stats = new Stats();
  71. container.appendChild(stats.getDisplayElement());
  72. document.addEventListener('mousedown', onDocumentMouseDown, false);
  73. document.addEventListener('touchstart', onDocumentTouchStart, false);
  74. }
  75. //
  76. function onDocumentMouseDown( event )
  77. {
  78. document.addEventListener('mousemove', onDocumentMouseMove, false);
  79. document.addEventListener('mouseup', onDocumentMouseUp, false);
  80. mouseXOnMouseDown = event.clientX - windowHalfX;
  81. targetRotationOnMouseDown = targetRotation;
  82. }
  83. function onDocumentMouseMove( event )
  84. {
  85. mouseX = event.clientX - windowHalfX;
  86. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  87. }
  88. function onDocumentMouseUp( event )
  89. {
  90. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  91. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  92. }
  93. function onDocumentTouchStart( event )
  94. {
  95. if(event.touches.length > 1)
  96. {
  97. event.preventDefault();
  98. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  99. targetRotationOnMouseDown = targetRotation;
  100. document.addEventListener('touchmove', onDocumentTouchMove, false);
  101. document.addEventListener('touchend', onDocumentTouchEnd, false);
  102. }
  103. }
  104. function onDocumentTouchMove( event )
  105. {
  106. if(event.touches.length == 1)
  107. {
  108. event.preventDefault();
  109. mouseX = event.touches[0].pageX - windowHalfX;
  110. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  111. }
  112. }
  113. function onDocumentTouchEnd( event )
  114. {
  115. if(event.touches.length == 0)
  116. {
  117. event.preventDefault();
  118. document.removeEventListener('touchmove', onDocumentTouchMove, false);
  119. document.removeEventListener('touchend', onDocumentTouchEnd, false);
  120. }
  121. }
  122. //
  123. function loop()
  124. {
  125. cube.rotation.y += (targetRotation - cube.rotation.y) * 0.05;
  126. cube.updateMatrix();
  127. plane.rotation.z = -cube.rotation.y;
  128. plane.updateMatrix();
  129. renderer.render(scene, camera);
  130. stats.update();
  131. }
  132. </script>
  133. </body>
  134. </html>
粤ICP备19079148号