threejs-fundamentals-with-light.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 src="resources/threejs/r94/three.min.js"></script>
  13. <script>
  14. 'use strict';
  15. function main() {
  16. const canvas = document.querySelector('#c');
  17. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  18. const fov = 75;
  19. const aspect = 2; // the canvas default
  20. const zNear = 0.1;
  21. const zFar = 5;
  22. const camera = new THREE.PerspectiveCamera(fov, aspect, zNear, zFar);
  23. camera.position.z = 2;
  24. const scene = new THREE.Scene();
  25. {
  26. const color = 0xFFFFFF;
  27. const intensity = 1;
  28. const light = new THREE.DirectionalLight(color, intensity);
  29. light.position.set(-1, 2, 4);
  30. scene.add(light);
  31. }
  32. const boxWidth = 1;
  33. const boxHeight = 1;
  34. const boxDepth = 1;
  35. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  36. const material = new THREE.MeshPhongMaterial({color: 0x44aa88}); // greenish blue
  37. const cube = new THREE.Mesh(geometry, material);
  38. scene.add(cube);
  39. function render(time) {
  40. time *= 0.001; // convert time to seconds
  41. cube.rotation.x = time;
  42. cube.rotation.y = time;
  43. renderer.render(scene, camera);
  44. requestAnimationFrame(render);
  45. }
  46. requestAnimationFrame(render);
  47. }
  48. main();
  49. </script>
  50. </html>