Skip to content

Commit e544877

Browse files
authored
Sky: Make sun disc optional. (mrdoob#33031)
1 parent 0390210 commit e544877

4 files changed

Lines changed: 45 additions & 10 deletions

File tree

examples/jsm/objects/Sky.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ import {
2525
* sky.scale.setScalar( 10000 );
2626
* scene.add( sky );
2727
* ```
28+
*
29+
* It can be useful to hide the sun disc when generating an environment map to avoid artifacts
30+
*
31+
* ```js
32+
* // disable before rendering environment map
33+
* sky.material.uniforms.showSunDisc.value = false;
34+
* // ...
35+
* // re-enable before scene sky box rendering
36+
* sky.material.uniforms.showSunDisc.value = true;
37+
* ```
2838
*
2939
* @augments Mesh
3040
* @three_import import { Sky } from 'three/addons/objects/Sky.js';
@@ -78,6 +88,7 @@ Sky.SkyShader = {
7888
'cloudCoverage': { value: 0.4 },
7989
'cloudDensity': { value: 0.4 },
8090
'cloudElevation': { value: 0.5 },
91+
'showSunDisc': { value: 1 },
8192
'time': { value: 0.0 }
8293
},
8394

@@ -167,6 +178,7 @@ Sky.SkyShader = {
167178
uniform float cloudCoverage;
168179
uniform float cloudDensity;
169180
uniform float cloudElevation;
181+
uniform float showSunDisc;
170182
uniform float time;
171183
172184
// Cloud noise functions
@@ -256,8 +268,8 @@ Sky.SkyShader = {
256268
vec3 L0 = vec3( 0.1 ) * Fex;
257269
258270
// composition + solar disc
259-
float sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta );
260-
L0 += ( vSunE * 19000.0 * Fex ) * sundisk;
271+
float sundisc = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos + 0.00002, cosTheta ) * showSunDisc;
272+
L0 += ( vSunE * 19000.0 * Fex ) * sundisc;
261273
262274
vec3 texColor = ( Lin + L0 ) * 0.04 + vec3( 0.0, 0.0003, 0.00075 );
263275

examples/jsm/objects/SkyMesh.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ import { Fn, float, vec2, vec3, acos, add, mul, clamp, cos, dot, exp, max, mix,
2626
* scene.add( sky );
2727
* ```
2828
*
29+
* It can be useful to hide the sun disc when generating an environment map to avoid artifacts
30+
*
31+
* ```js
32+
* // disable before rendering environment map
33+
* sky.showSunDisc.value = false;
34+
* // ...
35+
* // re-enable before scene sky box rendering
36+
* sky.showSunDisc.value = true;
37+
* ```
38+
*
2939
* @augments Mesh
3040
* @three_import import { SkyMesh } from 'three/addons/objects/SkyMesh.js';
3141
*/
@@ -117,6 +127,13 @@ class SkyMesh extends Mesh {
117127
*/
118128
this.cloudElevation = uniform( 0.5 );
119129

130+
/**
131+
* Whether to render the solar disc.
132+
*
133+
* @type {UniformNode<float>}
134+
*/
135+
this.showSunDisc = uniform( 1 );
136+
120137
/**
121138
* This flag can be used for type testing.
122139
*
@@ -146,8 +163,8 @@ class SkyMesh extends Mesh {
146163
const vertexNode = /*@__PURE__*/ Fn( () => {
147164

148165
// constants for atmospheric scattering
149-
const e = float( 2.71828182845904523536028747135266249775724709369995957 );
150-
// const pi = float( 3.141592653589793238462643383279502884197169 );
166+
const e = float( 2.718281828459045 );
167+
// const pi = float( 3.141592653589793 );
151168

152169
// wavelength of used primaries, according to preetham
153170
// const lambda = vec3( 680E-9, 550E-9, 450E-9 );
@@ -211,13 +228,13 @@ class SkyMesh extends Mesh {
211228
const colorNode = /*@__PURE__*/ Fn( () => {
212229

213230
// constants for atmospheric scattering
214-
const pi = float( 3.141592653589793238462643383279502884197169 );
231+
const pi = float( 3.141592653589793 );
215232

216233
// optical length at zenith for molecules
217234
const rayleighZenithLength = float( 8.4E3 );
218235
const mieZenithLength = float( 1.25E3 );
219236
// 66 arc seconds -> degrees, and the cosine of that
220-
const sunAngularDiameterCos = float( 0.999956676946448443553574619906976478926848692873900859324 );
237+
const sunAngularDiameterCos = float( 0.9999566769464484 );
221238

222239
// 3.0 / ( 16.0 * pi )
223240
const THREE_OVER_SIXTEENPI = float( 0.05968310365946075 );
@@ -262,8 +279,8 @@ class SkyMesh extends Mesh {
262279
const L0 = vec3( 0.1 ).mul( Fex );
263280

264281
// composition + solar disc
265-
const sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos.add( 0.00002 ), cosTheta );
266-
L0.addAssign( vSunE.mul( 19000.0 ).mul( Fex ).mul( sundisk ) );
282+
const sundisc = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos.add( 0.00002 ), cosTheta ).mul( this.showSunDisc );
283+
L0.addAssign( vSunE.mul( 19000.0 ).mul( Fex ).mul( sundisc ) );
267284

268285
const texColor = add( Lin, L0 ).mul( 0.04 ).add( vec3( 0.0, 0.0003, 0.00075 ) ).toVar();
269286

examples/webgl_shaders_sky.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
exposure: renderer.toneMappingExposure,
5656
cloudCoverage: 0.4,
5757
cloudDensity: 0.4,
58-
cloudElevation: 0.5
58+
cloudElevation: 0.5,
59+
showSunDisc: true
5960
};
6061

6162
function guiChanged() {
@@ -68,6 +69,7 @@
6869
uniforms[ 'cloudCoverage' ].value = effectController.cloudCoverage;
6970
uniforms[ 'cloudDensity' ].value = effectController.cloudDensity;
7071
uniforms[ 'cloudElevation' ].value = effectController.cloudElevation;
72+
uniforms[ 'showSunDisc' ].value = effectController.showSunDisc;
7173

7274
const phi = THREE.MathUtils.degToRad( 90 - effectController.elevation );
7375
const theta = THREE.MathUtils.degToRad( effectController.azimuth );
@@ -89,6 +91,7 @@
8991
gui.add( effectController, 'elevation', 0, 90, 0.1 ).onChange( guiChanged );
9092
gui.add( effectController, 'azimuth', - 180, 180, 0.1 ).onChange( guiChanged );
9193
gui.add( effectController, 'exposure', 0, 1, 0.0001 ).onChange( guiChanged );
94+
gui.add( effectController, 'showSunDisc' ).onChange( guiChanged );
9295

9396
const folderClouds = gui.addFolder( 'Clouds' );
9497
folderClouds.add( effectController, 'cloudCoverage', 0, 1, 0.01 ).name( 'coverage' ).onChange( guiChanged );

examples/webgpu_sky.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
exposure: renderer.toneMappingExposure,
6868
cloudCoverage: 0.4,
6969
cloudDensity: 0.4,
70-
cloudElevation: 0.5
70+
cloudElevation: 0.5,
71+
showSunDisc: true
7172
};
7273

7374
function guiChanged() {
@@ -79,6 +80,7 @@
7980
sky.cloudCoverage.value = effectController.cloudCoverage;
8081
sky.cloudDensity.value = effectController.cloudDensity;
8182
sky.cloudElevation.value = effectController.cloudElevation;
83+
sky.showSunDisc.value = effectController.showSunDisc;
8284

8385
const phi = THREE.MathUtils.degToRad( 90 - effectController.elevation );
8486
const theta = THREE.MathUtils.degToRad( effectController.azimuth );
@@ -100,6 +102,7 @@
100102
gui.add( effectController, 'elevation', 0, 90, 0.1 ).onChange( guiChanged );
101103
gui.add( effectController, 'azimuth', - 180, 180, 0.1 ).onChange( guiChanged );
102104
gui.add( effectController, 'exposure', 0, 1, 0.0001 ).onChange( guiChanged );
105+
gui.add( effectController, 'showSunDisc' ).onChange( guiChanged );
103106

104107
const folderClouds = gui.addFolder( 'Clouds' );
105108
folderClouds.add( effectController, 'cloudCoverage', 0, 1, 0.01 ).name( 'coverage' ).onChange( guiChanged );

0 commit comments

Comments
 (0)