Skip to content

Commit 11dc07a

Browse files
authored
Sky: Add WebGPURenderer version. (#29013)
* Sky: Add `WebGPURenderer` version. * E2E: Update screenshot.
1 parent e117afe commit 11dc07a

File tree

4 files changed

+327
-0
lines changed

4 files changed

+327
-0
lines changed

examples/files.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@
395395
"webgpu_skinning",
396396
"webgpu_skinning_instancing",
397397
"webgpu_skinning_points",
398+
"webgpu_sky",
398399
"webgpu_sprites",
399400
"webgpu_storage_buffer",
400401
"webgpu_texturegrad",

examples/jsm/objects/SkyGPU.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import {
2+
BackSide,
3+
BoxGeometry,
4+
Mesh,
5+
NodeMaterial,
6+
Vector3
7+
} from 'three';
8+
import { float, tslFn, vec3, acos, add, mul, clamp, cos, dot, exp, max, mix, modelViewProjection, normalize, positionWorld, pow, smoothstep, sub, varying, varyingProperty, vec4, uniform } from 'three/tsl';
9+
10+
/**
11+
* Based on "A Practical Analytic Model for Daylight"
12+
* aka The Preetham Model, the de facto standard analytic skydome model
13+
* https://www.researchgate.net/publication/220720443_A_Practical_Analytic_Model_for_Daylight
14+
*
15+
* First implemented by Simon Wallner
16+
* http://simonwallner.at/project/atmospheric-scattering/
17+
*
18+
* Improved by Martin Upitis
19+
* http://blenderartists.org/forum/showthread.php?245954-preethams-sky-impementation-HDR
20+
*
21+
* Three.js integration by zz85 http://twitter.com/blurspline
22+
*/
23+
24+
class Sky extends Mesh {
25+
26+
constructor() {
27+
28+
const material = new NodeMaterial();
29+
30+
super( new BoxGeometry( 1, 1, 1 ), material );
31+
32+
this.turbidity = uniform( 2 );
33+
this.rayleigh = uniform( 1 );
34+
this.mieCoefficient = uniform( 0.005 );
35+
this.mieDirectionalG = uniform( 0.8 );
36+
this.sunPosition = uniform( new Vector3() );
37+
this.up = uniform( new Vector3( 0, 1, 0 ) );
38+
this.cameraPosition = uniform( new Vector3() ).label( 'cameraPosition' ).onRenderUpdate( ( { camera }, self ) => self.value.setFromMatrixPosition( camera.matrixWorld ) ); // TODO replace with cameraPosition from CameraNode
39+
40+
this.isSky = true;
41+
42+
const vertexNode = /*@__PURE__*/ tslFn( () => {
43+
44+
// constants for atmospheric scattering
45+
const e = float( 2.71828182845904523536028747135266249775724709369995957 );
46+
// const pi = float( 3.141592653589793238462643383279502884197169 );
47+
48+
// wavelength of used primaries, according to preetham
49+
// const lambda = vec3( 680E-9, 550E-9, 450E-9 );
50+
// this pre-calcuation replaces older TotalRayleigh(vec3 lambda) function:
51+
// (8.0 * pow(pi, 3.0) * pow(pow(n, 2.0) - 1.0, 2.0) * (6.0 + 3.0 * pn)) / (3.0 * N * pow(lambda, vec3(4.0)) * (6.0 - 7.0 * pn))
52+
const totalRayleigh = vec3( 5.804542996261093E-6, 1.3562911419845635E-5, 3.0265902468824876E-5 );
53+
54+
// mie stuff
55+
// K coefficient for the primaries
56+
// const v = float( 4.0 );
57+
// const K = vec3( 0.686, 0.678, 0.666 );
58+
// MieConst = pi * pow( ( 2.0 * pi ) / lambda, vec3( v - 2.0 ) ) * K
59+
const MieConst = vec3( 1.8399918514433978E14, 2.7798023919660528E14, 4.0790479543861094E14 );
60+
61+
// earth shadow hack
62+
// cutoffAngle = pi / 1.95;
63+
const cutoffAngle = float( 1.6110731556870734 );
64+
const steepness = float( 1.5 );
65+
const EE = float( 1000.0 );
66+
67+
// varying sun position
68+
69+
const vSunDirection = normalize( this.sunPosition );
70+
varyingProperty( 'vec3', 'vSunDirection' ).assign( vSunDirection );
71+
72+
// varying sun intensity
73+
74+
const angle = dot( vSunDirection, this.up );
75+
const zenithAngleCos = clamp( angle, - 1, 1 );
76+
const sunIntensity = EE.mul( max( 0.0, float( 1.0 ).sub( pow( e, cutoffAngle.sub( acos( zenithAngleCos ) ).div( steepness ).negate() ) ) ) );
77+
varyingProperty( 'float', 'vSunE' ).assign( sunIntensity );
78+
79+
// varying sun fade
80+
81+
const vSunfade = float( 1.0 ).sub( clamp( float( 1.0 ).sub( exp( this.sunPosition.y.div( 450000.0 ) ) ), 0, 1 ) );
82+
varyingProperty( 'float', 'vSunfade' ).assign( vSunfade );
83+
84+
// varying vBetaR
85+
86+
const rayleighCoefficient = this.rayleigh.sub( float( 1.0 ).mul( float( 1.0 ).sub( vSunfade ) ) );
87+
88+
// extinction (absorbtion + out scattering)
89+
// rayleigh coefficients
90+
varyingProperty( 'vec3', 'vBetaR' ).assign( totalRayleigh.mul( rayleighCoefficient ) );
91+
92+
// varying vBetaM
93+
94+
const c = float( 0.2 ).mul( this.turbidity ).mul( 10E-18 );
95+
const totalMie = float( 0.434 ).mul( c ).mul( MieConst );
96+
97+
varyingProperty( 'vec3', 'vBetaM' ).assign( totalMie.mul( this.mieCoefficient ) );
98+
99+
// position
100+
101+
const position = modelViewProjection();
102+
position.z.assign( position.w ); // set z to camera.far
103+
104+
return position;
105+
106+
} )();
107+
108+
const fragmentNode = /*@__PURE__*/ tslFn( () => {
109+
110+
const vSunDirection = varying( vec3(), 'vSunDirection' );
111+
const vSunE = varying( float(), 'vSunE' );
112+
const vSunfade = varying( float(), 'vSunfade' );
113+
const vBetaR = varying( vec3(), 'vBetaR' );
114+
const vBetaM = varying( vec3(), 'vBetaM' );
115+
116+
// constants for atmospheric scattering
117+
const pi = float( 3.141592653589793238462643383279502884197169 );
118+
119+
// optical length at zenith for molecules
120+
const rayleighZenithLength = float( 8.4E3 );
121+
const mieZenithLength = float( 1.25E3 );
122+
// 66 arc seconds -> degrees, and the cosine of that
123+
const sunAngularDiameterCos = float( 0.999956676946448443553574619906976478926848692873900859324 );
124+
125+
// 3.0 / ( 16.0 * pi )
126+
const THREE_OVER_SIXTEENPI = float( 0.05968310365946075 );
127+
// 1.0 / ( 4.0 * pi )
128+
const ONE_OVER_FOURPI = float( 0.07957747154594767 );
129+
130+
//
131+
132+
const direction = normalize( positionWorld.sub( this.cameraPosition ) );
133+
134+
// optical length
135+
// cutoff angle at 90 to avoid singularity in next formula.
136+
const zenithAngle = acos( max( 0.0, dot( this.up, direction ) ) );
137+
const inverse = float( 1.0 ).div( cos( zenithAngle ).add( float( 0.15 ).mul( pow( float( 93.885 ).sub( zenithAngle.mul( 180.0 ).div( pi ) ), - 1.253 ) ) ) );
138+
const sR = rayleighZenithLength.mul( inverse );
139+
const sM = mieZenithLength.mul( inverse );
140+
141+
// combined extinction factor
142+
const Fex = exp( mul( vBetaR, sR ).add( mul( vBetaM, sM ) ).negate() );
143+
144+
// in scattering
145+
const cosTheta = dot( direction, vSunDirection );
146+
147+
// betaRTheta
148+
149+
const c = cosTheta.mul( 0.5 ).add( 0.5 );
150+
const rPhase = THREE_OVER_SIXTEENPI.mul( float( 1.0 ).add( pow( c, 2.0 ) ) );
151+
const betaRTheta = vBetaR.mul( rPhase );
152+
153+
// betaMTheta
154+
155+
const g2 = pow( this.mieDirectionalG, 2.0 );
156+
const inv = float( 1.0 ).div( pow( float( 1.0 ).sub( float( 2.0 ).mul( this.mieDirectionalG ).mul( cosTheta ) ).add( g2 ), 1.5 ) );
157+
const mPhase = ONE_OVER_FOURPI.mul( float( 1.0 ).sub( g2 ) ).mul( inv );
158+
const betaMTheta = vBetaM.mul( mPhase );
159+
160+
const Lin = pow( vSunE.mul( add( betaRTheta, betaMTheta ).div( add( vBetaR, vBetaM ) ) ).mul( sub( 1.0, Fex ) ), vec3( 1.5 ) );
161+
Lin.mulAssign( mix( vec3( 1.0 ), pow( vSunE.mul( add( betaRTheta, betaMTheta ).div( add( vBetaR, vBetaM ) ) ).mul( Fex ), vec3( 1.0 / 2.0 ) ), clamp( pow( sub( 1.0, dot( this.up, vSunDirection ) ), 5.0 ), 0.0, 1.0 ) ) );
162+
163+
// nightsky
164+
165+
const L0 = vec3( 0.1 ).mul( Fex );
166+
167+
// composition + solar disc
168+
const sundisk = smoothstep( sunAngularDiameterCos, sunAngularDiameterCos.add( 0.00002 ), cosTheta );
169+
L0.addAssign( vSunE.mul( 19000.0 ).mul( Fex ).mul( sundisk ) );
170+
171+
const texColor = add( Lin, L0 ).mul( 0.04 ).add( vec3( 0.0, 0.0003, 0.00075 ) );
172+
173+
const retColor = pow( texColor, vec3( float( 1.0 ).div( float( 1.2 ).add( vSunfade.mul( 1.2 ) ) ) ) );
174+
175+
return vec4( retColor, 1.0 );
176+
177+
} )();
178+
179+
material.normals = false;
180+
material.side = BackSide;
181+
material.depthWrite = false;
182+
183+
material.vertexNode = vertexNode;
184+
material.fragmentNode = fragmentNode;
185+
186+
}
187+
188+
}
189+
190+
export { Sky };

examples/screenshots/webgpu_sky.jpg

10.5 KB
Loading

examples/webgpu_sky.html

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>three.js webgpu - sky</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+
11+
<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - sky + sun shader
12+
</div>
13+
14+
<script type="importmap">
15+
{
16+
"imports": {
17+
"three": "../build/three.webgpu.js",
18+
"three/tsl": "../build/three.webgpu.js",
19+
"three/addons/": "./jsm/"
20+
}
21+
}
22+
</script>
23+
24+
<script type="module">
25+
26+
import * as THREE from 'three';
27+
28+
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
29+
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
30+
import { Sky } from 'three/addons/objects/SkyGPU.js';
31+
32+
let camera, scene, renderer;
33+
34+
let sky, sun;
35+
36+
init();
37+
38+
function initSky() {
39+
40+
// Add Sky
41+
sky = new Sky();
42+
sky.scale.setScalar( 450000 );
43+
scene.add( sky );
44+
45+
sun = new THREE.Vector3();
46+
47+
/// GUI
48+
49+
const effectController = {
50+
turbidity: 10,
51+
rayleigh: 3,
52+
mieCoefficient: 0.005,
53+
mieDirectionalG: 0.7,
54+
elevation: 2,
55+
azimuth: 180,
56+
exposure: renderer.toneMappingExposure
57+
};
58+
59+
function guiChanged() {
60+
61+
sky.turbidity.value = effectController.turbidity;
62+
sky.rayleigh.value = effectController.rayleigh;
63+
sky.mieCoefficient.value = effectController.mieCoefficient;
64+
sky.mieDirectionalG.value = effectController.mieDirectionalG;
65+
66+
const phi = THREE.MathUtils.degToRad( 90 - effectController.elevation );
67+
const theta = THREE.MathUtils.degToRad( effectController.azimuth );
68+
69+
sun.setFromSphericalCoords( 1, phi, theta );
70+
71+
sky.sunPosition.value.copy( sun );
72+
73+
renderer.toneMappingExposure = effectController.exposure;
74+
75+
}
76+
77+
const gui = new GUI();
78+
79+
gui.add( effectController, 'turbidity', 0.0, 20.0, 0.1 ).onChange( guiChanged );
80+
gui.add( effectController, 'rayleigh', 0.0, 4, 0.001 ).onChange( guiChanged );
81+
gui.add( effectController, 'mieCoefficient', 0.0, 0.1, 0.001 ).onChange( guiChanged );
82+
gui.add( effectController, 'mieDirectionalG', 0.0, 1, 0.001 ).onChange( guiChanged );
83+
gui.add( effectController, 'elevation', 0, 90, 0.1 ).onChange( guiChanged );
84+
gui.add( effectController, 'azimuth', - 180, 180, 0.1 ).onChange( guiChanged );
85+
gui.add( effectController, 'exposure', 0, 1, 0.0001 ).onChange( guiChanged );
86+
87+
guiChanged();
88+
89+
}
90+
91+
function init() {
92+
93+
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 2000000 );
94+
camera.position.set( 0, 100, 2000 );
95+
96+
scene = new THREE.Scene();
97+
98+
renderer = new THREE.WebGPURenderer();
99+
renderer.setPixelRatio( window.devicePixelRatio );
100+
renderer.setSize( window.innerWidth, window.innerHeight );
101+
renderer.setAnimationLoop( animate );
102+
renderer.toneMapping = THREE.ACESFilmicToneMapping;
103+
renderer.toneMappingExposure = 0.5;
104+
document.body.appendChild( renderer.domElement );
105+
106+
const controls = new OrbitControls( camera, renderer.domElement );
107+
//controls.maxPolarAngle = Math.PI / 2;
108+
controls.enableZoom = false;
109+
controls.enablePan = false;
110+
111+
initSky();
112+
113+
window.addEventListener( 'resize', onWindowResize );
114+
115+
}
116+
117+
function onWindowResize() {
118+
119+
camera.aspect = window.innerWidth / window.innerHeight;
120+
camera.updateProjectionMatrix();
121+
122+
renderer.setSize( window.innerWidth, window.innerHeight );
123+
124+
}
125+
126+
function animate() {
127+
128+
renderer.render( scene, camera );
129+
130+
}
131+
132+
</script>
133+
134+
</body>
135+
136+
</html>

0 commit comments

Comments
 (0)