2
0

webgl_shaders_sky.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. let camera, scene, renderer;
  18. let sky, sun;
  19. init();
  20. render();
  21. function initSky() {
  22. // Add Sky
  23. sky = new Sky();
  24. sky.scale.setScalar( 450000 );
  25. scene.add( sky );
  26. sun = new THREE.Vector3();
  27. /// GUI
  28. const effectController = {
  29. turbidity: 10,
  30. rayleigh: 3,
  31. mieCoefficient: 0.005,
  32. mieDirectionalG: 0.7,
  33. inclination: 0.49, // elevation / inclination
  34. azimuth: 0.25, // Facing front,
  35. exposure: renderer.toneMappingExposure
  36. };
  37. function guiChanged() {
  38. const uniforms = sky.material.uniforms;
  39. uniforms[ "turbidity" ].value = effectController.turbidity;
  40. uniforms[ "rayleigh" ].value = effectController.rayleigh;
  41. uniforms[ "mieCoefficient" ].value = effectController.mieCoefficient;
  42. uniforms[ "mieDirectionalG" ].value = effectController.mieDirectionalG;
  43. const theta = Math.PI * ( effectController.inclination - 0.5 );
  44. const phi = 2 * Math.PI * ( effectController.azimuth - 0.5 );
  45. sun.x = Math.cos( phi );
  46. sun.y = Math.sin( phi ) * Math.sin( theta );
  47. sun.z = Math.sin( phi ) * Math.cos( theta );
  48. uniforms[ "sunPosition" ].value.copy( sun );
  49. renderer.toneMappingExposure = effectController.exposure;
  50. renderer.render( scene, camera );
  51. }
  52. const gui = new GUI();
  53. gui.add( effectController, "turbidity", 0.0, 20.0, 0.1 ).onChange( guiChanged );
  54. gui.add( effectController, "rayleigh", 0.0, 4, 0.001 ).onChange( guiChanged );
  55. gui.add( effectController, "mieCoefficient", 0.0, 0.1, 0.001 ).onChange( guiChanged );
  56. gui.add( effectController, "mieDirectionalG", 0.0, 1, 0.001 ).onChange( guiChanged );
  57. gui.add( effectController, "inclination", 0, 1, 0.0001 ).onChange( guiChanged );
  58. gui.add( effectController, "azimuth", 0, 1, 0.0001 ).onChange( guiChanged );
  59. gui.add( effectController, "exposure", 0, 1, 0.0001 ).onChange( guiChanged );
  60. guiChanged();
  61. }
  62. function init() {
  63. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 2000000 );
  64. camera.position.set( 0, 100, 2000 );
  65. scene = new THREE.Scene();
  66. const helper = new THREE.GridHelper( 10000, 2, 0xffffff, 0xffffff );
  67. scene.add( helper );
  68. renderer = new THREE.WebGLRenderer();
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.outputEncoding = THREE.sRGBEncoding;
  72. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  73. renderer.toneMappingExposure = 0.5;
  74. document.body.appendChild( renderer.domElement );
  75. const controls = new OrbitControls( camera, renderer.domElement );
  76. controls.addEventListener( 'change', render );
  77. //controls.maxPolarAngle = Math.PI / 2;
  78. controls.enableZoom = false;
  79. controls.enablePan = false;
  80. initSky();
  81. window.addEventListener( 'resize', onWindowResize );
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. render();
  88. }
  89. function render() {
  90. renderer.render( scene, camera );
  91. }
  92. </script>
  93. </body>
  94. </html>