cube.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. background-color: #ffffff;
  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="http://github.com/mrdoob/stats.js/raw/master/build/stats.js"></script>
  19. <script type="text/javascript">
  20. var SCREEN_WIDTH = window.innerWidth;
  21. var SCREEN_HEIGHT = window.innerHeight;
  22. var AMOUNT = 100;
  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. camera = new Camera(0, 150, 300);
  42. camera.focus = 300;
  43. camera.target.y = 150;
  44. camera.updateMatrix();
  45. scene = new Scene();
  46. // Cube
  47. geometry = new Cube(200, 200, 200);
  48. for (var i = 0; i < geometry.faces.length; i++)
  49. geometry.faces[i].color.setRGBA( Math.floor( Math.random() * 128), Math.floor( Math.random() * 128 + 128), Math.floor( Math.random() * 128 + 128), 255 );
  50. cube = new Mesh(geometry, new FaceColorMaterial());
  51. cube.position.y = 150;
  52. cube.updateMatrix();
  53. scene.add(cube);
  54. // Plane
  55. plane = new Mesh(new Plane(200, 200), new ColorMaterial(0xeeeeee));
  56. plane.rotation.x = 90 * (Math.PI / 180);
  57. plane.updateMatrix();
  58. scene.add(plane);
  59. renderer = new CanvasRenderer();
  60. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  61. container.appendChild(renderer.viewport);
  62. stats = new Stats();
  63. container.appendChild(stats.getDisplayElement());
  64. document.addEventListener('mousedown', onDocumentMouseDown, false);
  65. document.addEventListener('touchstart', onDocumentTouchStart, false);
  66. }
  67. //
  68. function onDocumentMouseDown( event )
  69. {
  70. document.addEventListener('mousemove', onDocumentMouseMove, false);
  71. document.addEventListener('mouseup', onDocumentMouseUp, false);
  72. mouseXOnMouseDown = event.clientX - windowHalfX;
  73. targetRotationOnMouseDown = targetRotation;
  74. }
  75. function onDocumentMouseMove( event )
  76. {
  77. mouseX = event.clientX - windowHalfX;
  78. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  79. }
  80. function onDocumentMouseUp( event )
  81. {
  82. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  83. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  84. }
  85. function onDocumentTouchStart( event )
  86. {
  87. if(event.touches.length > 1)
  88. {
  89. event.preventDefault();
  90. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  91. targetRotationOnMouseDown = targetRotation;
  92. document.addEventListener('touchmove', onDocumentTouchMove, false);
  93. document.addEventListener('touchend', onDocumentTouchEnd, false);
  94. }
  95. }
  96. function onDocumentTouchMove( event )
  97. {
  98. if(event.touches.length == 1)
  99. {
  100. event.preventDefault();
  101. mouseX = event.touches[0].pageX - windowHalfX;
  102. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  103. }
  104. }
  105. function onDocumentTouchEnd( event )
  106. {
  107. if(event.touches.length == 0)
  108. {
  109. event.preventDefault();
  110. document.removeEventListener('touchmove', onDocumentTouchMove, false);
  111. document.removeEventListener('touchend', onDocumentTouchEnd, false);
  112. }
  113. }
  114. //
  115. function loop()
  116. {
  117. cube.rotation.y += (targetRotation - cube.rotation.y) * 0.05;
  118. cube.updateMatrix();
  119. plane.rotation.z = -cube.rotation.y;
  120. plane.updateMatrix();
  121. renderer.render(scene, camera);
  122. stats.update();
  123. }
  124. </script>
  125. </body>
  126. </html>
粤ICP备19079148号