fundamentals-with-light.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. <!-- Import maps polyfill -->
  13. <!-- Remove this when import maps will be widely supported -->
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../../build/three.module.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. function main() {
  25. const canvas = document.querySelector( '#c' );
  26. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  27. renderer.useLegacyLights = false;
  28. const fov = 75;
  29. const aspect = 2; // the canvas default
  30. const near = 0.1;
  31. const far = 5;
  32. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  33. camera.position.z = 2;
  34. const scene = new THREE.Scene();
  35. {
  36. const color = 0xFFFFFF;
  37. const intensity = 3;
  38. const light = new THREE.DirectionalLight( color, intensity );
  39. light.position.set( - 1, 2, 4 );
  40. scene.add( light );
  41. }
  42. const boxWidth = 1;
  43. const boxHeight = 1;
  44. const boxDepth = 1;
  45. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  46. const material = new THREE.MeshPhongMaterial( { color: 0x44aa88 } ); // greenish blue
  47. const cube = new THREE.Mesh( geometry, material );
  48. scene.add( cube );
  49. function render( time ) {
  50. time *= 0.001; // convert time to seconds
  51. cube.rotation.x = time;
  52. cube.rotation.y = time;
  53. renderer.render( scene, camera );
  54. requestAnimationFrame( render );
  55. }
  56. requestAnimationFrame( render );
  57. }
  58. main();
  59. </script>
  60. </html>