123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - shaders - sky sun shader</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - sky + sun shader
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { Sky } from './jsm/objects/Sky.js';
- var camera, controls, scene, renderer;
- var sky, sunSphere;
- init();
- render();
- function initSky() {
- // Add Sky
- sky = new Sky();
- sky.scale.setScalar( 450000 );
- scene.add( sky );
- // Add Sun Helper
- sunSphere = new THREE.Mesh(
- new THREE.SphereBufferGeometry( 20000, 16, 8 ),
- new THREE.MeshBasicMaterial( { color: 0xffffff } )
- );
- sunSphere.position.y = - 700000;
- sunSphere.visible = false;
- scene.add( sunSphere );
- /// GUI
- var effectController = {
- turbidity: 10,
- rayleigh: 2,
- mieCoefficient: 0.005,
- mieDirectionalG: 0.8,
- luminance: 1,
- inclination: 0.49, // elevation / inclination
- azimuth: 0.25, // Facing front,
- sun: ! true
- };
- var distance = 400000;
- function guiChanged() {
- var uniforms = sky.material.uniforms;
- uniforms[ "turbidity" ].value = effectController.turbidity;
- uniforms[ "rayleigh" ].value = effectController.rayleigh;
- uniforms[ "mieCoefficient" ].value = effectController.mieCoefficient;
- uniforms[ "mieDirectionalG" ].value = effectController.mieDirectionalG;
- uniforms[ "luminance" ].value = effectController.luminance;
- var theta = Math.PI * ( effectController.inclination - 0.5 );
- var phi = 2 * Math.PI * ( effectController.azimuth - 0.5 );
- sunSphere.position.x = distance * Math.cos( phi );
- sunSphere.position.y = distance * Math.sin( phi ) * Math.sin( theta );
- sunSphere.position.z = distance * Math.sin( phi ) * Math.cos( theta );
- sunSphere.visible = effectController.sun;
- uniforms[ "sunPosition" ].value.copy( sunSphere.position );
- renderer.render( scene, camera );
- }
- var gui = new GUI();
- gui.add( effectController, "turbidity", 1.0, 20.0, 0.1 ).onChange( guiChanged );
- gui.add( effectController, "rayleigh", 0.0, 4, 0.001 ).onChange( guiChanged );
- gui.add( effectController, "mieCoefficient", 0.0, 0.1, 0.001 ).onChange( guiChanged );
- gui.add( effectController, "mieDirectionalG", 0.0, 1, 0.001 ).onChange( guiChanged );
- gui.add( effectController, "luminance", 0.0, 2 ).onChange( guiChanged );
- gui.add( effectController, "inclination", 0, 1, 0.0001 ).onChange( guiChanged );
- gui.add( effectController, "azimuth", 0, 1, 0.0001 ).onChange( guiChanged );
- gui.add( effectController, "sun" ).onChange( guiChanged );
- guiChanged();
- }
- function init() {
- camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 2000000 );
- camera.position.set( 0, 100, 2000 );
- scene = new THREE.Scene();
- var helper = new THREE.GridHelper( 10000, 2, 0xffffff, 0xffffff );
- scene.add( helper );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.addEventListener( 'change', render );
- //controls.maxPolarAngle = Math.PI / 2;
- controls.enableZoom = false;
- controls.enablePan = false;
- initSky();
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- render();
- }
- function render() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|