scenegraph-sun.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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( { antialias: true, canvas } );
  38. renderer.useLegacyLights = false;
  39. const fov = 40;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 1000;
  43. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  44. camera.position.set( 0, 50, 0 );
  45. camera.up.set( 0, 0, 1 );
  46. camera.lookAt( 0, 0, 0 );
  47. const scene = new THREE.Scene();
  48. {
  49. const color = 0xFFFFFF;
  50. const intensity = 500;
  51. const light = new THREE.PointLight( color, intensity );
  52. scene.add( light );
  53. }
  54. // an array of objects who's rotation to update
  55. const objects = [];
  56. const radius = 1;
  57. const widthSegments = 6;
  58. const heightSegments = 6;
  59. const sphereGeometry = new THREE.SphereGeometry(
  60. radius, widthSegments, heightSegments );
  61. const sunMaterial = new THREE.MeshPhongMaterial( { emissive: 0xFFFF00 } );
  62. const sunMesh = new THREE.Mesh( sphereGeometry, sunMaterial );
  63. sunMesh.scale.set( 5, 5, 5 );
  64. scene.add( sunMesh );
  65. objects.push( sunMesh );
  66. function resizeRendererToDisplaySize( renderer ) {
  67. const canvas = renderer.domElement;
  68. const width = canvas.clientWidth;
  69. const height = canvas.clientHeight;
  70. const needResize = canvas.width !== width || canvas.height !== height;
  71. if ( needResize ) {
  72. renderer.setSize( width, height, false );
  73. }
  74. return needResize;
  75. }
  76. function render( time ) {
  77. time *= 0.001;
  78. if ( resizeRendererToDisplaySize( renderer ) ) {
  79. const canvas = renderer.domElement;
  80. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  81. camera.updateProjectionMatrix();
  82. }
  83. objects.forEach( ( obj ) => {
  84. obj.rotation.y = time;
  85. } );
  86. renderer.render( scene, camera );
  87. requestAnimationFrame( render );
  88. }
  89. requestAnimationFrame( render );
  90. }
  91. main();
  92. </script>
  93. </html>