Skip to content

Commit 5e91943

Browse files
committed
Examples: Improved webgpu_reflection.
1 parent 5b9d8ef commit 5e91943

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed
-1.44 KB
Loading

examples/webgpu_reflection.html

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import * as THREE from 'three/webgpu';
3131

32-
import { abs, blendOverlay, color, float, Fn, instancedBufferAttribute, materialColor, normalWorldGeometry, pass, positionGeometry, positionLocal, reflector, screenUV, sin, sub, texture, time, uniform, uv, vec3 } from 'three/tsl';
32+
import { abs, blendOverlay, color, float, Fn, instancedBufferAttribute, materialColor, max, normalWorldGeometry, pass, positionGeometry, positionLocal, pow2, reflector, screenUV, sin, sub, texture, time, uniform, uv, vec2, vec3 } from 'three/tsl';
3333
import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
3434

3535
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
@@ -55,7 +55,7 @@
5555
camera.position.set( 4, 2, 4 );
5656

5757
scene = new THREE.Scene();
58-
scene.fog = new THREE.Fog( 0x4195a4, 1, 25 );
58+
scene.fog = new THREE.Fog( 0x4195a4, 1, 20 );
5959
scene.backgroundNode = normalWorldGeometry.y.mix( color( 0x4195a4 ), color( 0x0066ff ) );
6060
camera.lookAt( 0, 1, 0 );
6161

@@ -79,18 +79,16 @@
7979
floorColor.wrapS = THREE.RepeatWrapping;
8080
floorColor.wrapT = THREE.RepeatWrapping;
8181
floorColor.colorSpace = THREE.SRGBColorSpace;
82+
floorColor.repeat.set( 15, 15 );
8283

8384
const floorNormal = await textureLoader.loadAsync( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
8485
floorNormal.wrapS = THREE.RepeatWrapping;
8586
floorNormal.wrapT = THREE.RepeatWrapping;
8687
floorNormal.repeat.set( 15, 15 );
8788

88-
const boxMap = await textureLoader.loadAsync( 'textures/edge3.jpg' );
89-
boxMap.colorSpace = THREE.SRGBColorSpace;
90-
9189
// tree
9290

93-
const treeMesh = createTreeMesh( boxMap );
91+
const treeMesh = createTreeMesh();
9492
treeMesh.castShadow = true;
9593
treeMesh.receiveShadow = true;
9694
scene.add( treeMesh );
@@ -100,13 +98,14 @@
10098
const floorUV = uv().mul( 15 );
10199
const floorNormalOffset = texture( floorNormal, floorUV ).xy.mul( 2 ).sub( 1 ).mul( .02 );
102100

103-
const reflection = reflector( { resolution: 0.5 } ); // 0.5 is half of the rendering view
101+
const reflection = reflector( { resolution: 0.2 } );
104102
reflection.target.rotateX( - Math.PI / 2 );
105103
reflection.uvNode = reflection.uvNode.add( floorNormalOffset );
106104
scene.add( reflection.target );
107105

108106
const floorMaterial = new THREE.MeshPhongNodeMaterial();
109-
floorMaterial.colorNode = texture( floorColor, floorUV ).add( reflection );
107+
floorMaterial.colorNode = texture( floorColor, floorUV );
108+
floorMaterial.emissiveNode = reflection.mul( 0.25 );
110109
floorMaterial.normalMap = floorNormal;
111110
floorMaterial.normalScale.set( 0.2, - 0.2 );
112111

@@ -202,9 +201,7 @@
202201

203202
}
204203

205-
206-
207-
function createTreeMesh( boxMap ) {
204+
function createTreeMesh() {
208205

209206
const maxSteps = 5;
210207
const lengthMult = 0.8;
@@ -239,7 +236,7 @@
239236

240237
size = size / 100;
241238

242-
const subSteps = 200;
239+
const subSteps = 50;
243240

244241
// below loop generates the instanced data for a tree part
245242

@@ -255,13 +252,13 @@
255252
newPosition.set( x, y, z ).lerp( new THREE.Vector3( newX, newY, newZ ), percent );
256253
position.copy( newPosition );
257254

258-
position.x += Math.random() * ( size * 3 ) - ( size * 1.5 );
259-
position.y += Math.random() * ( size * 3 ) - ( size * 1.5 );
260-
position.z += Math.random() * ( size * 3 ) - ( size * 1.5 );
255+
position.x += random() * size * 3;
256+
position.y += random() * size * 3;
257+
position.z += random() * size * 3;
261258

262259
positions.push( position.x, position.y, position.z );
263260

264-
const scale = Math.random();
261+
const scale = Math.random() + 5;
265262

266263
// normal
267264

@@ -301,7 +298,7 @@
301298
createTreePart( angle, 0, 0, 0, 16, 0 );
302299

303300
const geometry = new THREE.BoxGeometry();
304-
const material = new THREE.MeshStandardNodeMaterial( { map: boxMap } );
301+
const material = new THREE.MeshStandardNodeMaterial();
305302
const mesh = new THREE.Mesh( geometry, material );
306303
mesh.scale.setScalar( 0.05 );
307304
mesh.count = instanceCount;
@@ -349,9 +346,17 @@
349346

350347
} )();
351348

349+
const squareEdge = Fn( () => {
350+
351+
const pos = uv().sub( vec2( 0.5, 0.5 ) );
352+
const squareDistance = max( abs( pos.x ), abs( pos.y ) );
353+
return squareDistance.div( 0.5 ).clamp( 0.85, 1 ).sub( 0.5 ).mul( 2.0 );
354+
355+
} )();
356+
352357
material.colorNode = Fn( () => {
353358

354-
return materialColor.mul( instanceColor );
359+
return squareEdge.sub( instanceColor );
355360

356361
} )();
357362

@@ -365,7 +370,7 @@
365370
const dif2 = abs( instanceTime.sub( uniformEffector2 ) ).toConst();
366371
const effect2 = dif2.lessThanEqual( 0.15 ).select( sub( 0.15, dif2 ).mul( sub( 1.7, instanceTime ).mul( 10 ) ), effect1 );
367372

368-
return vec3( effect1, 0, effect2 ).mul( instanceColor );
373+
return pow2( vec3( effect1, 0, effect2 ) ).mul( instanceColor );
369374

370375
} )();
371376

0 commit comments

Comments
 (0)