index.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. html,
  5. body {
  6. margin: 0;
  7. padding: 0;
  8. background-color: #121212;
  9. }
  10. #demo {
  11. width: 66%;
  12. height: 66%;
  13. margin: 10% auto auto auto;
  14. display: block;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="demo"></div>
  20. <script src="./three.min.js"></script>
  21. <script type="text/javascript">
  22. var plane;
  23. var camera;
  24. var renderer;
  25. var velocity = { x: 0.005, y: -0.01 };
  26. var oscillator = 0;
  27. var targetObject;
  28. var spotLight;
  29. var demoContainer = document.getElementById("demo");
  30. window.addEventListener("resize", function (event) {
  31. demoContainer = document.getElementById("demo");
  32. camera = new THREE.PerspectiveCamera(66, demoContainer.clientWidth / demoContainer.clientHeight, 0.1, 1000);
  33. camera.position.z = 2.4;
  34. renderer.setSize(demoContainer.clientWidth, demoContainer.clientHeight);
  35. });
  36. document.addEventListener("DOMContentLoaded", function () {
  37. var scene = new THREE.Scene();
  38. scene.background = new THREE.Color(0x000000);
  39. camera = new THREE.PerspectiveCamera(66, demoContainer.clientWidth / demoContainer.clientHeight, 0.1, 1000);
  40. renderer = new THREE.WebGLRenderer();
  41. renderer.setPixelRatio(window.devicePixelRatio);
  42. renderer.setSize(demoContainer.clientWidth, demoContainer.clientHeight);
  43. var texture = new THREE.TextureLoader().load('./midas.jpg');
  44. var geometry = new THREE.PlaneGeometry(2, 2, 8, 8);
  45. var material = new THREE.MeshLambertMaterial({ map: texture, color: 0xffffff, side: THREE.DoubleSide });
  46. plane = new THREE.Mesh(geometry, material);
  47. scene.add(plane);
  48. camera.position.z = 2.4;
  49. // var light = new THREE.AmbientLight( 0xffffff );
  50. // scene.add( light );
  51. targetObject = new THREE.Object3D();
  52. scene.add(targetObject);
  53. spotLight = new THREE.SpotLight(0xffffff);
  54. spotLight.position.set(0, 0, 10);
  55. spotLight.angle = 0.20;
  56. spotLight.penumbra = 1;
  57. spotLight.target = targetObject;
  58. scene.add(spotLight);
  59. demoContainer.appendChild(renderer.domElement);
  60. function animate() {
  61. requestAnimationFrame(animate);
  62. update();
  63. renderer.render(scene, camera);
  64. }
  65. animate();
  66. });
  67. function update() {
  68. oscillator += 1;
  69. oscillator %= 360;
  70. plane.position.z = Math.sin(oscillator * Math.PI / 180);
  71. // plane.rotation.x = Math.cos(oscillator * Math.PI / 180);
  72. // plane.rotation.y = Math.cos(oscillator * Math.PI / 180);
  73. plane.rotation.z = Math.cos(oscillator * Math.PI / 180) / 8;
  74. targetObject.position.x = Math.cos(oscillator * Math.PI / 180);
  75. targetObject.position.y = Math.sin(oscillator * Math.PI / 180);
  76. }
  77. </script>
  78. </body>
  79. </html>