fundamentals-with-light.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Fundamentals with light</title>
  8. </head>
  9. <body>
  10. <canvas id="c"></canvas>
  11. </body>
  12. <script type="module">
  13. import * as THREE from '../../build/three.module.js';
  14. function main() {
  15. const canvas = document.querySelector('#c');
  16. const renderer = new THREE.WebGLRenderer({canvas});
  17. const fov = 75;
  18. const aspect = 2; // the canvas default
  19. const near = 0.1;
  20. const far = 5;
  21. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  22. camera.position.z = 2;
  23. const scene = new THREE.Scene();
  24. {
  25. const color = 0xFFFFFF;
  26. const intensity = 1;
  27. const light = new THREE.DirectionalLight(color, intensity);
  28. light.position.set(-1, 2, 4);
  29. scene.add(light);
  30. }
  31. const boxWidth = 1;
  32. const boxHeight = 1;
  33. const boxDepth = 1;
  34. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  35. const material = new THREE.MeshPhongMaterial({color: 0x44aa88}); // greenish blue
  36. const cube = new THREE.Mesh(geometry, material);
  37. scene.add(cube);
  38. function render(time) {
  39. time *= 0.001; // convert time to seconds
  40. cube.rotation.x = time;
  41. cube.rotation.y = time;
  42. renderer.render(scene, camera);
  43. requestAnimationFrame(render);
  44. }
  45. requestAnimationFrame(render);
  46. }
  47. main();
  48. </script>
  49. </html>