Skip to content

Commit 1dd66f3

Browse files
Mugen87mrdoob
andauthored
Examples: Refactor Sky usage. (#21681)
* Examples: Refactor Sky usage. * Examples: Clean up. Co-authored-by: Mr.doob <[email protected]>
1 parent be73dd5 commit 1dd66f3

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed
348 Bytes
Loading
-8 Bytes
Loading

examples/webgl_shaders_ocean.html

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,18 @@
9494
skyUniforms[ 'mieDirectionalG' ].value = 0.8;
9595

9696
const parameters = {
97-
inclination: 0.02,
98-
azimuth: 0.045
97+
elevation: 2,
98+
azimuth: 180
9999
};
100100

101101
const pmremGenerator = new THREE.PMREMGenerator( renderer );
102102

103103
function updateSun() {
104104

105-
const theta = 0.5 * Math.PI * ( 1 - parameters.inclination );
106-
const phi = 2 * Math.PI * ( parameters.azimuth - 0.5 );
105+
const phi = THREE.MathUtils.degToRad( 90 - parameters.elevation );
106+
const theta = THREE.MathUtils.degToRad( parameters.azimuth );
107107

108-
sun.x = Math.sin( theta ) * Math.sin( phi );
109-
sun.y = Math.cos( theta );
110-
sun.z = Math.sin( theta ) * Math.cos( phi );
108+
sun.setFromSphericalCoords( 1, phi, theta );
111109

112110
sky.material.uniforms[ 'sunPosition' ].value.copy( sun );
113111
water.material.uniforms[ 'sunDirection' ].value.copy( sun ).normalize();
@@ -145,8 +143,8 @@
145143
const gui = new GUI();
146144

147145
const folderSky = gui.addFolder( 'Sky' );
148-
folderSky.add( parameters, 'inclination', - 1, 1, 0.0001 ).onChange( updateSun );
149-
folderSky.add( parameters, 'azimuth', 0, 1, 0.0001 ).onChange( updateSun );
146+
folderSky.add( parameters, 'elevation', 0, 90, 0.1 ).onChange( updateSun );
147+
folderSky.add( parameters, 'azimuth', - 180, 180, 0.1 ).onChange( updateSun );
150148
folderSky.open();
151149

152150
const waterUniforms = water.material.uniforms;

examples/webgl_shaders_sky.html

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,25 @@
4242
rayleigh: 3,
4343
mieCoefficient: 0.005,
4444
mieDirectionalG: 0.7,
45-
inclination: 0.02, // elevation / inclination
46-
azimuth: 0, // Facing front
45+
elevation: 2,
46+
azimuth: 180,
4747
exposure: renderer.toneMappingExposure
4848
};
4949

5050
function guiChanged() {
5151

5252
const uniforms = sky.material.uniforms;
53-
uniforms[ "turbidity" ].value = effectController.turbidity;
54-
uniforms[ "rayleigh" ].value = effectController.rayleigh;
55-
uniforms[ "mieCoefficient" ].value = effectController.mieCoefficient;
56-
uniforms[ "mieDirectionalG" ].value = effectController.mieDirectionalG;
53+
uniforms[ 'turbidity' ].value = effectController.turbidity;
54+
uniforms[ 'rayleigh' ].value = effectController.rayleigh;
55+
uniforms[ 'mieCoefficient' ].value = effectController.mieCoefficient;
56+
uniforms[ 'mieDirectionalG' ].value = effectController.mieDirectionalG;
5757

58-
const theta = 0.5 * Math.PI * ( 1 - effectController.inclination );
59-
const phi = 2 * Math.PI * ( effectController.azimuth - 0.5 );
58+
const phi = THREE.MathUtils.degToRad( 90 - effectController.elevation );
59+
const theta = THREE.MathUtils.degToRad( effectController.azimuth );
6060

61-
sun.x = Math.sin( theta ) * Math.sin( phi );
62-
sun.y = Math.cos( theta );
63-
sun.z = Math.sin( theta ) * Math.cos( phi );
61+
sun.setFromSphericalCoords( 1, phi, theta );
6462

65-
uniforms[ "sunPosition" ].value.copy( sun );
63+
uniforms[ 'sunPosition' ].value.copy( sun );
6664

6765
renderer.toneMappingExposure = effectController.exposure;
6866
renderer.render( scene, camera );
@@ -71,13 +69,13 @@
7169

7270
const gui = new GUI();
7371

74-
gui.add( effectController, "turbidity", 0.0, 20.0, 0.1 ).onChange( guiChanged );
75-
gui.add( effectController, "rayleigh", 0.0, 4, 0.001 ).onChange( guiChanged );
76-
gui.add( effectController, "mieCoefficient", 0.0, 0.1, 0.001 ).onChange( guiChanged );
77-
gui.add( effectController, "mieDirectionalG", 0.0, 1, 0.001 ).onChange( guiChanged );
78-
gui.add( effectController, "inclination", -1, 1, 0.0001 ).onChange( guiChanged );
79-
gui.add( effectController, "azimuth", 0, 1, 0.0001 ).onChange( guiChanged );
80-
gui.add( effectController, "exposure", 0, 1, 0.0001 ).onChange( guiChanged );
72+
gui.add( effectController, 'turbidity', 0.0, 20.0, 0.1 ).onChange( guiChanged );
73+
gui.add( effectController, 'rayleigh', 0.0, 4, 0.001 ).onChange( guiChanged );
74+
gui.add( effectController, 'mieCoefficient', 0.0, 0.1, 0.001 ).onChange( guiChanged );
75+
gui.add( effectController, 'mieDirectionalG', 0.0, 1, 0.001 ).onChange( guiChanged );
76+
gui.add( effectController, 'elevation', 0, 90, 0.1 ).onChange( guiChanged );
77+
gui.add( effectController, 'azimuth', - 180, 180, 0.1 ).onChange( guiChanged );
78+
gui.add( effectController, 'exposure', 0, 1, 0.0001 ).onChange( guiChanged );
8179

8280
guiChanged();
8381

0 commit comments

Comments
 (0)