Skip to content

Commit abf99f5

Browse files
authored
Examples: Add webgpu_reflection_blurred (#31116)
* fix clear texture and facing away if `bounce` is `false` * add `premult`, `unpremult` * cleanup * add `hashBlur` options { mask, premultipliedAlpha } * add docs and cleanup * add `webgpu_reflection_blurred` example * update webgpu_reflection_blurred * update webgpu_reflection_blurred * Update webgpu_reflection_blurred.html
1 parent 5f3c69c commit abf99f5

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed

examples/files.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@
414414
"webgpu_postprocessing",
415415
"webgpu_procedural_texture",
416416
"webgpu_reflection",
417+
"webgpu_reflection_blurred",
417418
"webgpu_refraction",
418419
"webgpu_rendertarget_2d-array_3d",
419420
"webgpu_rtt",
102 KB
Loading
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>three.js webgpu - blurred reflection</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">
12+
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - blurred reflection
13+
</div>
14+
15+
<script type="importmap">
16+
{
17+
"imports": {
18+
"three": "../build/three.webgpu.js",
19+
"three/webgpu": "../build/three.webgpu.js",
20+
"three/tsl": "../build/three.tsl.js",
21+
"three/addons/": "./jsm/"
22+
}
23+
}
24+
</script>
25+
26+
<script type="module">
27+
28+
import * as THREE from 'three';
29+
import { Fn, vec4, fract, abs, uniform, pow, color, max, length, rangeFogFactor, sub, reflector, normalWorld, hue, time, mix, positionWorld } from 'three/tsl';
30+
31+
import { hashBlur } from 'three/addons/tsl/display/hashBlur.js';
32+
33+
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
34+
35+
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
36+
37+
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
38+
39+
import Stats from 'three/addons/libs/stats.module.js';
40+
41+
let camera, scene, renderer;
42+
let model, mixer, clock;
43+
let controls;
44+
let stats;
45+
let gui;
46+
47+
init();
48+
49+
async function init() {
50+
51+
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
52+
camera.position.set( - 2.5, 2, 2.5 );
53+
camera.lookAt( 0, .4, 0 );
54+
55+
scene = new THREE.Scene();
56+
scene.backgroundNode = hue( normalWorld.y.mix( 0, color( 0x0066ff ) ).mul( .1 ), time );
57+
58+
const waterAmbientLight = new THREE.HemisphereLight( 0xffffff, 0x0066ff, 10 );
59+
scene.add( waterAmbientLight );
60+
61+
clock = new THREE.Clock();
62+
63+
// animated model
64+
65+
const gltfLoader = new GLTFLoader();
66+
gltfLoader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
67+
68+
model = gltf.scene;
69+
model.children[ 0 ].children[ 0 ].castShadow = true;
70+
71+
mixer = new THREE.AnimationMixer( model );
72+
73+
const action = mixer.clipAction( gltf.animations[ 0 ] );
74+
action.play();
75+
76+
scene.add( model );
77+
78+
} );
79+
80+
// textures
81+
82+
const textureLoader = new THREE.TextureLoader();
83+
84+
const uvMap = textureLoader.load( 'textures/uv_grid_directx.jpg' );
85+
uvMap.colorSpace = THREE.SRGBColorSpace;
86+
87+
// uv map for debugging
88+
89+
const uvMaterial = new THREE.MeshStandardNodeMaterial( {
90+
map: uvMap,
91+
side: THREE.DoubleSide
92+
} );
93+
94+
const uvMesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), uvMaterial );
95+
uvMesh.position.set( 0, 1, - 3 );
96+
scene.add( uvMesh );
97+
98+
// circle effect
99+
100+
const drawCircle = Fn( ( [ pos, radius, width, power, color, timer = time.mul( .5 ) ] ) => {
101+
102+
// https://www.shadertoy.com/view/3tdSRn
103+
104+
const dist1 = length( pos );
105+
dist1.assign( fract( dist1.mul( 5.0 ).sub( fract( timer ) ) ) );
106+
const dist2 = dist1.sub( radius );
107+
const intensity = pow( radius.div( abs( dist2 ) ), width );
108+
const col = color.rgb.mul( intensity ).mul( power ).mul( max( sub( 0.8, abs( dist2 ) ), 0.0 ) );
109+
110+
return col;
111+
112+
} );
113+
114+
const circleFadeY = positionWorld.y.mul( .7 ).oneMinus().max( 0 );
115+
const animatedColor = mix( color( 0x74ccf4 ), color( 0x7f00c5 ), positionWorld.xz.distance( 0 ).div( 10 ).clamp() );
116+
const animatedCircle = hue( drawCircle( positionWorld.xz.mul( .1 ), 0.5, 0.8, .01, animatedColor ).mul( circleFadeY ), time );
117+
118+
const floorLight = new THREE.PointLight( 0xffffff );
119+
floorLight.colorNode = animatedCircle.mul( 50 );
120+
scene.add( floorLight );
121+
122+
// reflection
123+
124+
const roughness = uniform( .9 );
125+
const radius = uniform( 0.2 );
126+
127+
const reflection = reflector( { resolution: 1, depth: true, bounces: false } ); // 0.5 is half of the rendering view
128+
const reflectionDepth = reflection.getDepthNode();
129+
reflection.target.rotateX( - Math.PI / 2 );
130+
scene.add( reflection.target );
131+
132+
const floorMaterial = new THREE.MeshStandardNodeMaterial();
133+
floorMaterial.transparent = true;
134+
floorMaterial.colorNode = Fn( () => {
135+
136+
// ranges adjustment
137+
138+
const radiusRange = mix( 0.01, 0.1, radius ); // range [ 0.01, 0.1 ]
139+
const roughnessRange = mix( 0.3, 0.03, roughness ); // range [ 0.03, 0.3 ]
140+
141+
// blur the reflection
142+
143+
const reflectionBlurred = hashBlur( reflection, radiusRange, {
144+
mask: reflectionDepth,
145+
premultipliedAlpha: true
146+
} );
147+
148+
// reflection composite
149+
150+
const reflectionMask = reflectionBlurred.a.mul( reflectionDepth ).remapClamp( 0, roughnessRange );
151+
const reflectionItensity = .1;
152+
const reflectionMixFactor = reflectionMask.mul( roughness.mul( 2 ).min( 1 ) );
153+
const reflectionFinal = mix( reflection.rgb, reflectionBlurred.rgb, reflectionMixFactor ).mul( reflectionItensity );
154+
155+
// mix reflection with animated circle
156+
157+
const output = animatedCircle.add( reflectionFinal );
158+
159+
// falloff opacity by distance like an opacity-fog
160+
161+
const opacity = rangeFogFactor( 7, 25 ).oneMinus();
162+
163+
// final output
164+
165+
return vec4( output, opacity );
166+
167+
} )();
168+
169+
const floor = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), floorMaterial );
170+
floor.position.set( 0, 0, 0 );
171+
scene.add( floor );
172+
173+
// renderer
174+
175+
renderer = new THREE.WebGPURenderer( { antialias: true } );
176+
renderer.setPixelRatio( window.devicePixelRatio );
177+
renderer.setSize( window.innerWidth, window.innerHeight );
178+
renderer.setAnimationLoop( animate );
179+
renderer.toneMapping = THREE.NeutralToneMapping;
180+
renderer.toneMappingExposure = 1.3;
181+
document.body.appendChild( renderer.domElement );
182+
183+
gui = new GUI();
184+
gui.add( roughness, 'value', 0, 1 ).name( 'roughness' );
185+
gui.add( radius, 'value', 0, 1 ).name( 'radius' );
186+
187+
stats = new Stats();
188+
document.body.appendChild( stats.dom );
189+
190+
controls = new OrbitControls( camera, renderer.domElement );
191+
controls.minDistance = 1;
192+
controls.maxDistance = 10;
193+
controls.maxPolarAngle = Math.PI / 2;
194+
//controls.autoRotate = true;
195+
controls.autoRotateSpeed = - .1;
196+
controls.target.set( 0, .5, 0 );
197+
controls.update();
198+
199+
// events
200+
201+
window.addEventListener( 'resize', onWindowResize );
202+
203+
}
204+
205+
function onWindowResize() {
206+
207+
camera.aspect = window.innerWidth / window.innerHeight;
208+
camera.updateProjectionMatrix();
209+
210+
renderer.setSize( window.innerWidth, window.innerHeight );
211+
212+
}
213+
214+
function animate() {
215+
216+
stats.update();
217+
218+
controls.update();
219+
220+
const delta = clock.getDelta();
221+
222+
if ( model ) {
223+
224+
mixer.update( delta );
225+
226+
}
227+
228+
renderer.render( scene, camera );
229+
230+
}
231+
232+
</script>
233+
</body>
234+
</html>

0 commit comments

Comments
 (0)