/
water.vertex.fx
153 lines (119 loc) · 3.32 KB
/
water.vertex.fx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
precision highp float;
// Attributes
attribute vec3 position;
#ifdef NORMAL
attribute vec3 normal;
#endif
#ifdef UV1
attribute vec2 uv;
#endif
#ifdef UV2
attribute vec2 uv2;
#endif
#ifdef VERTEXCOLOR
attribute vec4 color;
#endif
#include<bonesDeclaration>
// Uniforms
#include<instancesDeclaration>
uniform mat4 view;
uniform mat4 viewProjection;
#ifdef BUMP
varying vec2 vNormalUV;
#ifdef BUMPSUPERIMPOSE
varying vec2 vNormalUV2;
#endif
uniform mat4 normalMatrix;
uniform vec2 vNormalInfos;
#endif
#ifdef POINTSIZE
uniform float pointSize;
#endif
// Output
varying vec3 vPositionW;
#ifdef NORMAL
varying vec3 vNormalW;
#endif
#ifdef VERTEXCOLOR
varying vec4 vColor;
#endif
#include<clipPlaneVertexDeclaration>
#include<fogVertexDeclaration>
#include<__decl__lightFragment>[0..maxSimultaneousLights]
#include<logDepthDeclaration>
// Water uniforms
uniform mat4 worldReflectionViewProjection;
uniform vec2 windDirection;
uniform float waveLength;
uniform float time;
uniform float windForce;
uniform float waveHeight;
uniform float waveSpeed;
// Water varyings
varying vec3 vPosition;
varying vec3 vRefractionMapTexCoord;
varying vec3 vReflectionMapTexCoord;
void main(void) {
#include<instancesVertex>
#include<bonesVertex>
vec4 worldPos = finalWorld * vec4(position, 1.0);
vPositionW = vec3(worldPos);
#ifdef NORMAL
vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));
#endif
// Texture coordinates
#ifndef UV1
vec2 uv = vec2(0., 0.);
#endif
#ifndef UV2
vec2 uv2 = vec2(0., 0.);
#endif
#ifdef BUMP
if (vNormalInfos.x == 0.)
{
vNormalUV = vec2(normalMatrix * vec4((uv * 1.0) / waveLength + time * windForce * windDirection, 1.0, 0.0));
#ifdef BUMPSUPERIMPOSE
vNormalUV2 = vec2(normalMatrix * vec4((uv * 0.721) / waveLength + time * 1.2 * windForce * windDirection, 1.0, 0.0));
#endif
}
else
{
vNormalUV = vec2(normalMatrix * vec4((uv2 * 1.0) / waveLength + time * windForce * windDirection , 1.0, 0.0));
#ifdef BUMPSUPERIMPOSE
vNormalUV2 = vec2(normalMatrix * vec4((uv2 * 0.721) / waveLength + time * 1.2 * windForce * windDirection , 1.0, 0.0));
#endif
}
#endif
// Clip plane
#include<clipPlaneVertex>
// Fog
#include<fogVertex>
// Shadows
#include<shadowsVertex>[0..maxSimultaneousLights]
// Vertex color
#ifdef VERTEXCOLOR
vColor = color;
#endif
// Point size
#ifdef POINTSIZE
gl_PointSize = pointSize;
#endif
vec3 p = position;
float newY = (sin(((p.x / 0.05) + time * waveSpeed)) * waveHeight * windDirection.x * 5.0)
+ (cos(((p.z / 0.05) + time * waveSpeed)) * waveHeight * windDirection.y * 5.0);
p.y += abs(newY);
gl_Position = viewProjection * finalWorld * vec4(p, 1.0);
#ifdef REFLECTION
worldPos = viewProjection * finalWorld * vec4(p, 1.0);
// Water
vPosition = position;
vRefractionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
vRefractionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
vRefractionMapTexCoord.z = worldPos.w;
worldPos = worldReflectionViewProjection * vec4(position, 1.0);
vReflectionMapTexCoord.x = 0.5 * (worldPos.w + worldPos.x);
vReflectionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
vReflectionMapTexCoord.z = worldPos.w;
#endif
#include<logDepthVertex>
}