webgl_shaders_sky.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - shaders - sky sun shader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - sky + sun shader
  11. </div>
  12. <script type="module">
  13. import * as THREE from '../build/three.module.js';
  14. import { GUI } from './jsm/libs/dat.gui.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { Sky } from './jsm/objects/Sky.js';
  17. var camera, controls, scene, renderer;
  18. var sky, sunSphere;
  19. init();
  20. render();
  21. function initSky() {
  22. // Add Sky
  23. sky = new Sky();
  24. sky.scale.setScalar( 450000 );
  25. scene.add( sky );
  26. // Add Sun Helper
  27. sunSphere = new THREE.Mesh(
  28. new THREE.SphereBufferGeometry( 20000, 16, 8 ),
  29. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  30. );
  31. sunSphere.position.y = - 700000;
  32. sunSphere.visible = false;
  33. scene.add( sunSphere );
  34. /// GUI
  35. var effectController = {
  36. turbidity: 10,
  37. rayleigh: 2,
  38. mieCoefficient: 0.005,
  39. mieDirectionalG: 0.8,
  40. luminance: 1,
  41. inclination: 0.49, // elevation / inclination
  42. azimuth: 0.25, // Facing front,
  43. sun: ! true
  44. };
  45. var distance = 400000;
  46. function guiChanged() {
  47. var uniforms = sky.material.uniforms;
  48. uniforms[ "turbidity" ].value = effectController.turbidity;
  49. uniforms[ "rayleigh" ].value = effectController.rayleigh;
  50. uniforms[ "mieCoefficient" ].value = effectController.mieCoefficient;
  51. uniforms[ "mieDirectionalG" ].value = effectController.mieDirectionalG;
  52. uniforms[ "luminance" ].value = effectController.luminance;
  53. var theta = Math.PI * ( effectController.inclination - 0.5 );
  54. var phi = 2 * Math.PI * ( effectController.azimuth - 0.5 );
  55. sunSphere.position.x = distance * Math.cos( phi );
  56. sunSphere.position.y = distance * Math.sin( phi ) * Math.sin( theta );
  57. sunSphere.position.z = distance * Math.sin( phi ) * Math.cos( theta );
  58. sunSphere.visible = effectController.sun;
  59. uniforms[ "sunPosition" ].value.copy( sunSphere.position );
  60. renderer.render( scene, camera );
  61. }
  62. var gui = new GUI();
  63. gui.add( effectController, "turbidity", 1.0, 20.0, 0.1 ).onChange( guiChanged );
  64. gui.add( effectController, "rayleigh", 0.0, 4, 0.001 ).onChange( guiChanged );
  65. gui.add( effectController, "mieCoefficient", 0.0, 0.1, 0.001 ).onChange( guiChanged );
  66. gui.add( effectController, "mieDirectionalG", 0.0, 1, 0.001 ).onChange( guiChanged );
  67. gui.add( effectController, "luminance", 0.0, 2 ).onChange( guiChanged );
  68. gui.add( effectController, "inclination", 0, 1, 0.0001 ).onChange( guiChanged );
  69. gui.add( effectController, "azimuth", 0, 1, 0.0001 ).onChange( guiChanged );
  70. gui.add( effectController, "sun" ).onChange( guiChanged );
  71. guiChanged();
  72. }
  73. function init() {
  74. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 2000000 );
  75. camera.position.set( 0, 100, 2000 );
  76. scene = new THREE.Scene();
  77. var helper = new THREE.GridHelper( 10000, 2, 0xffffff, 0xffffff );
  78. scene.add( helper );
  79. renderer = new THREE.WebGLRenderer();
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. document.body.appendChild( renderer.domElement );
  83. controls = new OrbitControls( camera, renderer.domElement );
  84. controls.addEventListener( 'change', render );
  85. //controls.maxPolarAngle = Math.PI / 2;
  86. controls.enableZoom = false;
  87. controls.enablePan = false;
  88. initSky();
  89. window.addEventListener( 'resize', onWindowResize, false );
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. render();
  96. }
  97. function render() {
  98. renderer.render( scene, camera );
  99. }
  100. </script>
  101. </body>
  102. </html>