scenegraph-sun.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 - Scenegraph - Sun</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. function main() {
  36. const canvas = document.querySelector('#c');
  37. const renderer = new THREE.WebGLRenderer({canvas});
  38. const fov = 40;
  39. const aspect = 2; // the canvas default
  40. const near = 0.1;
  41. const far = 1000;
  42. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  43. camera.position.set(0, 50, 0);
  44. camera.up.set(0, 0, 1);
  45. camera.lookAt(0, 0, 0);
  46. const scene = new THREE.Scene();
  47. {
  48. const color = 0xFFFFFF;
  49. const intensity = 3;
  50. const light = new THREE.PointLight(color, intensity);
  51. scene.add(light);
  52. }
  53. // an array of objects who's rotation to update
  54. const objects = [];
  55. const radius = 1;
  56. const widthSegments = 6;
  57. const heightSegments = 6;
  58. const sphereGeometry = new THREE.SphereGeometry(
  59. radius, widthSegments, heightSegments);
  60. const sunMaterial = new THREE.MeshPhongMaterial({emissive: 0xFFFF00});
  61. const sunMesh = new THREE.Mesh(sphereGeometry, sunMaterial);
  62. sunMesh.scale.set(5, 5, 5);
  63. scene.add(sunMesh);
  64. objects.push(sunMesh);
  65. function resizeRendererToDisplaySize(renderer) {
  66. const canvas = renderer.domElement;
  67. const width = canvas.clientWidth;
  68. const height = canvas.clientHeight;
  69. const needResize = canvas.width !== width || canvas.height !== height;
  70. if (needResize) {
  71. renderer.setSize(width, height, false);
  72. }
  73. return needResize;
  74. }
  75. function render(time) {
  76. time *= 0.001;
  77. if (resizeRendererToDisplaySize(renderer)) {
  78. const canvas = renderer.domElement;
  79. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  80. camera.updateProjectionMatrix();
  81. }
  82. objects.forEach((obj) => {
  83. obj.rotation.y = time;
  84. });
  85. renderer.render(scene, camera);
  86. requestAnimationFrame(render);
  87. }
  88. requestAnimationFrame(render);
  89. }
  90. main();
  91. </script>
  92. </html>