Skip to content

Commit 604f412

Browse files
authored
WaterMesh: Improve lighting support (#30247)
* WaterMesh: Improve lighting support * Update WaterMesh.js * Update WaterMesh.js * Update WaterMesh.js * cleanup
1 parent 7d72e71 commit 604f412

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

examples/jsm/objects/WaterMesh.js

+25-20
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import {
22
Color,
33
Mesh,
44
Vector3,
5-
NodeMaterial
5+
MeshLambertNodeMaterial
66
} from 'three/webgpu';
77

8-
import { Fn, add, cameraPosition, div, normalize, positionWorld, sub, time, texture, vec2, vec3, vec4, max, dot, reflect, pow, length, float, uniform, reflector, mul, mix } from 'three/tsl';
8+
import { Fn, add, cameraPosition, div, normalize, positionWorld, sub, time, texture, vec2, vec3, max, dot, reflect, pow, length, float, uniform, reflector, mul, mix, diffuseColor } from 'three/tsl';
99

1010
/**
1111
* Work based on :
@@ -18,15 +18,15 @@ class WaterMesh extends Mesh {
1818

1919
constructor( geometry, options ) {
2020

21-
const material = new NodeMaterial();
21+
const material = new MeshLambertNodeMaterial();
2222

2323
super( geometry, material );
2424

2525
this.isWater = true;
2626

2727
this.resolution = options.resolution !== undefined ? options.resolution : 0.5;
2828

29-
// uniforms
29+
// Uniforms
3030

3131
this.waterNormals = texture( options.waterNormals );
3232
this.alpha = uniform( options.alpha !== undefined ? options.alpha : 1.0 );
@@ -58,25 +58,32 @@ class WaterMesh extends Mesh {
5858

5959
} );
6060

61-
const fragmentNode = Fn( () => {
61+
const noise = getNoise( positionWorld.xz.mul( this.size ) );
62+
const surfaceNormal = normalize( noise.xzy.mul( 1.5, 1.0, 1.5 ) );
6263

63-
const noise = getNoise( positionWorld.xz.mul( this.size ) );
64-
const surfaceNormal = normalize( noise.xzy.mul( 1.5, 1.0, 1.5 ) );
64+
const worldToEye = cameraPosition.sub( positionWorld );
65+
const eyeDirection = normalize( worldToEye );
6566

66-
const diffuseLight = vec3( 0 ).toVar();
67-
const specularLight = vec3( 0 ).toVar();
67+
const reflection = normalize( reflect( this.sunDirection.negate(), surfaceNormal ) );
68+
const direction = max( 0.0, dot( eyeDirection, reflection ) );
69+
const specularLight = pow( direction, 100 ).mul( this.sunColor ).mul( 2.0 );
70+
const diffuseLight = max( dot( this.sunDirection, surfaceNormal ), 0.0 ).mul( this.sunColor ).mul( 0.5 );
6871

69-
const worldToEye = cameraPosition.sub( positionWorld );
70-
const eyeDirection = normalize( worldToEye );
72+
const distance = length( worldToEye );
7173

72-
const reflection = normalize( reflect( this.sunDirection.negate(), surfaceNormal ) );
73-
const direction = max( 0.0, dot( eyeDirection, reflection ) );
74-
specularLight.addAssign( pow( direction, 100 ).mul( this.sunColor ).mul( 2.0 ) );
75-
diffuseLight.addAssign( max( dot( this.sunDirection, surfaceNormal ), 0.0 ).mul( this.sunColor ).mul( 0.5 ) );
74+
const distortion = surfaceNormal.xz.mul( float( 0.001 ).add( float( 1.0 ).div( distance ) ) ).mul( this.distortionScale );
7675

77-
const distance = length( worldToEye );
76+
// Material
7877

79-
const distortion = surfaceNormal.xz.mul( float( 0.001 ).add( float( 1.0 ).div( distance ) ) ).mul( this.distortionScale );
78+
material.transparent = true;
79+
80+
material.opacityNode = this.alpha;
81+
82+
material.shadowPositionNode = positionWorld.add( distortion );
83+
84+
material.setupOutgoingLight = () => diffuseColor.rgb; // backwards compatibility
85+
86+
material.colorNode = Fn( () => {
8087

8188
const mirrorSampler = reflector();
8289
mirrorSampler.uvNode = mirrorSampler.uvNode.add( distortion );
@@ -90,12 +97,10 @@ class WaterMesh extends Mesh {
9097
const scatter = max( 0.0, dot( surfaceNormal, eyeDirection ) ).mul( this.waterColor );
9198
const albedo = mix( this.sunColor.mul( diffuseLight ).mul( 0.3 ).add( scatter ), mirrorSampler.rgb.mul( specularLight ).add( mirrorSampler.rgb.mul( 0.9 ) ).add( vec3( 0.1 ) ), reflectance );
9299

93-
return vec4( albedo, this.alpha );
100+
return albedo;
94101

95102
} )();
96103

97-
material.fragmentNode = fragmentNode;
98-
99104
}
100105

101106
}

0 commit comments

Comments
 (0)