@@ -2,10 +2,10 @@ import {
2
2
Color ,
3
3
Mesh ,
4
4
Vector3 ,
5
- NodeMaterial
5
+ MeshLambertNodeMaterial
6
6
} from 'three/webgpu' ;
7
7
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' ;
9
9
10
10
/**
11
11
* Work based on :
@@ -18,15 +18,15 @@ class WaterMesh extends Mesh {
18
18
19
19
constructor ( geometry , options ) {
20
20
21
- const material = new NodeMaterial ( ) ;
21
+ const material = new MeshLambertNodeMaterial ( ) ;
22
22
23
23
super ( geometry , material ) ;
24
24
25
25
this . isWater = true ;
26
26
27
27
this . resolution = options . resolution !== undefined ? options . resolution : 0.5 ;
28
28
29
- // uniforms
29
+ // Uniforms
30
30
31
31
this . waterNormals = texture ( options . waterNormals ) ;
32
32
this . alpha = uniform ( options . alpha !== undefined ? options . alpha : 1.0 ) ;
@@ -58,25 +58,32 @@ class WaterMesh extends Mesh {
58
58
59
59
} ) ;
60
60
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 ) ) ;
62
63
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 ) ;
65
66
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 ) ;
68
71
69
- const worldToEye = cameraPosition . sub ( positionWorld ) ;
70
- const eyeDirection = normalize ( worldToEye ) ;
72
+ const distance = length ( worldToEye ) ;
71
73
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 ) ;
76
75
77
- const distance = length ( worldToEye ) ;
76
+ // Material
78
77
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 ( ( ) => {
80
87
81
88
const mirrorSampler = reflector ( ) ;
82
89
mirrorSampler . uvNode = mirrorSampler . uvNode . add ( distortion ) ;
@@ -90,12 +97,10 @@ class WaterMesh extends Mesh {
90
97
const scatter = max ( 0.0 , dot ( surfaceNormal , eyeDirection ) ) . mul ( this . waterColor ) ;
91
98
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 ) ;
92
99
93
- return vec4 ( albedo , this . alpha ) ;
100
+ return albedo ;
94
101
95
102
} ) ( ) ;
96
103
97
- material . fragmentNode = fragmentNode ;
98
-
99
104
}
100
105
101
106
}
0 commit comments