forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOceanShaders.js
373 lines (302 loc) · 11.7 KB
/
OceanShaders.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* Author: Aleksandr Albert
// Website: www.routter.co.tt
// Description: A deep water ocean shader set
// based on an implementation of a Tessendorf Waves
// originally presented by David Li ( www.david.li/waves )
// The general method is to apply shaders to simulation Framebuffers
// and then sample these framebuffers when rendering the ocean mesh
// The set uses 7 shaders:
// -- Simulation shaders
// [1] ocean_sim_vertex -> Vertex shader used to set up a 2x2 simulation plane centered at (0,0)
// [2] ocean_subtransform -> Fragment shader used to subtransform the mesh (generates the displacement map)
// [3] ocean_initial_spectrum -> Fragment shader used to set intitial wave frequency at a texel coordinate
// [4] ocean_phase -> Fragment shader used to set wave phase at a texel coordinate
// [5] ocean_spectrum -> Fragment shader used to set current wave frequency at a texel coordinate
// [6] ocean_normal -> Fragment shader used to set face normals at a texel coordinate
// -- Rendering Shader
// [7] ocean_main -> Vertex and Fragment shader used to create the final render
*/
THREE.OceanShaders = {};
THREE.OceanShaders[ "ocean_sim_vertex" ] = {
vertexShader: [
"varying vec2 vUV;",
"void main (void) {",
" vUV = position.xy * 0.5 + 0.5;",
" gl_Position = vec4(position, 1.0 );",
"}"
].join( "\n" )
};
THREE.OceanShaders[ "ocean_subtransform" ] = {
uniforms: {
"u_input": { value: null },
"u_transformSize": { value: 512.0 },
"u_subtransformSize": { value: 250.0 }
},
fragmentShader: [
//GPU FFT using a Stockham formulation
"precision highp float;",
"#include <common>",
"uniform sampler2D u_input;",
"uniform float u_transformSize;",
"uniform float u_subtransformSize;",
"varying vec2 vUV;",
"vec2 multiplyComplex (vec2 a, vec2 b) {",
" return vec2(a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1]);",
"}",
"void main (void) {",
" #ifdef HORIZONTAL",
" float index = vUV.x * u_transformSize - 0.5;",
" #else",
" float index = vUV.y * u_transformSize - 0.5;",
" #endif",
" float evenIndex = floor(index / u_subtransformSize) * (u_subtransformSize * 0.5) + mod(index, u_subtransformSize * 0.5);",
//transform two complex sequences simultaneously
" #ifdef HORIZONTAL",
" vec4 even = texture2D(u_input, vec2(evenIndex + 0.5, gl_FragCoord.y) / u_transformSize).rgba;",
" vec4 odd = texture2D(u_input, vec2(evenIndex + u_transformSize * 0.5 + 0.5, gl_FragCoord.y) / u_transformSize).rgba;",
" #else",
" vec4 even = texture2D(u_input, vec2(gl_FragCoord.x, evenIndex + 0.5) / u_transformSize).rgba;",
" vec4 odd = texture2D(u_input, vec2(gl_FragCoord.x, evenIndex + u_transformSize * 0.5 + 0.5) / u_transformSize).rgba;",
" #endif",
" float twiddleArgument = -2.0 * PI * (index / u_subtransformSize);",
" vec2 twiddle = vec2(cos(twiddleArgument), sin(twiddleArgument));",
" vec2 outputA = even.xy + multiplyComplex(twiddle, odd.xy);",
" vec2 outputB = even.zw + multiplyComplex(twiddle, odd.zw);",
" gl_FragColor = vec4(outputA, outputB);",
"}"
].join( "\n" )
};
THREE.OceanShaders[ "ocean_initial_spectrum" ] = {
uniforms: {
"u_wind": { value: new THREE.Vector2( 10.0, 10.0 ) },
"u_resolution": { value: 512.0 },
"u_size": { value: 250.0 }
},
vertexShader: [
"void main (void) {",
" gl_Position = vec4(position, 1.0);",
"}"
].join( "\n" ),
fragmentShader: [
"precision highp float;",
"#include <common>",
"const float G = 9.81;",
"const float KM = 370.0;",
"const float CM = 0.23;",
"uniform vec2 u_wind;",
"uniform float u_resolution;",
"uniform float u_size;",
"float omega (float k) {",
" return sqrt(G * k * (1.0 + pow2(k / KM)));",
"}",
"#if __VERSION__ == 100",
"float tanh (float x) {",
" return (1.0 - exp(-2.0 * x)) / (1.0 + exp(-2.0 * x));",
"}",
"#endif",
"void main (void) {",
" vec2 coordinates = gl_FragCoord.xy - 0.5;",
" float n = (coordinates.x < u_resolution * 0.5) ? coordinates.x : coordinates.x - u_resolution;",
" float m = (coordinates.y < u_resolution * 0.5) ? coordinates.y : coordinates.y - u_resolution;",
" vec2 K = (2.0 * PI * vec2(n, m)) / u_size;",
" float k = length(K);",
" float l_wind = length(u_wind);",
" float Omega = 0.84;",
" float kp = G * pow2(Omega / l_wind);",
" float c = omega(k) / k;",
" float cp = omega(kp) / kp;",
" float Lpm = exp(-1.25 * pow2(kp / k));",
" float gamma = 1.7;",
" float sigma = 0.08 * (1.0 + 4.0 * pow(Omega, -3.0));",
" float Gamma = exp(-pow2(sqrt(k / kp) - 1.0) / 2.0 * pow2(sigma));",
" float Jp = pow(gamma, Gamma);",
" float Fp = Lpm * Jp * exp(-Omega / sqrt(10.0) * (sqrt(k / kp) - 1.0));",
" float alphap = 0.006 * sqrt(Omega);",
" float Bl = 0.5 * alphap * cp / c * Fp;",
" float z0 = 0.000037 * pow2(l_wind) / G * pow(l_wind / cp, 0.9);",
" float uStar = 0.41 * l_wind / log(10.0 / z0);",
" float alpham = 0.01 * ((uStar < CM) ? (1.0 + log(uStar / CM)) : (1.0 + 3.0 * log(uStar / CM)));",
" float Fm = exp(-0.25 * pow2(k / KM - 1.0));",
" float Bh = 0.5 * alpham * CM / c * Fm * Lpm;",
" float a0 = log(2.0) / 4.0;",
" float am = 0.13 * uStar / CM;",
" float Delta = tanh(a0 + 4.0 * pow(c / cp, 2.5) + am * pow(CM / c, 2.5));",
" float cosPhi = dot(normalize(u_wind), normalize(K));",
" float S = (1.0 / (2.0 * PI)) * pow(k, -4.0) * (Bl + Bh) * (1.0 + Delta * (2.0 * cosPhi * cosPhi - 1.0));",
" float dk = 2.0 * PI / u_size;",
" float h = sqrt(S / 2.0) * dk;",
" if (K.x == 0.0 && K.y == 0.0) {",
" h = 0.0;", //no DC term
" }",
" gl_FragColor = vec4(h, 0.0, 0.0, 0.0);",
"}"
].join( "\n" )
};
THREE.OceanShaders[ "ocean_phase" ] = {
uniforms: {
"u_phases": { value: null },
"u_deltaTime": { value: null },
"u_resolution": { value: null },
"u_size": { value: null }
},
fragmentShader: [
"precision highp float;",
"#include <common>",
"const float G = 9.81;",
"const float KM = 370.0;",
"varying vec2 vUV;",
"uniform sampler2D u_phases;",
"uniform float u_deltaTime;",
"uniform float u_resolution;",
"uniform float u_size;",
"float omega (float k) {",
" return sqrt(G * k * (1.0 + k * k / KM * KM));",
"}",
"void main (void) {",
" float deltaTime = 1.0 / 60.0;",
" vec2 coordinates = gl_FragCoord.xy - 0.5;",
" float n = (coordinates.x < u_resolution * 0.5) ? coordinates.x : coordinates.x - u_resolution;",
" float m = (coordinates.y < u_resolution * 0.5) ? coordinates.y : coordinates.y - u_resolution;",
" vec2 waveVector = (2.0 * PI * vec2(n, m)) / u_size;",
" float phase = texture2D(u_phases, vUV).r;",
" float deltaPhase = omega(length(waveVector)) * u_deltaTime;",
" phase = mod(phase + deltaPhase, 2.0 * PI);",
" gl_FragColor = vec4(phase, 0.0, 0.0, 0.0);",
"}"
].join( "\n" )
};
THREE.OceanShaders[ "ocean_spectrum" ] = {
uniforms: {
"u_size": { value: null },
"u_resolution": { value: null },
"u_choppiness": { value: null },
"u_phases": { value: null },
"u_initialSpectrum": { value: null }
},
fragmentShader: [
"precision highp float;",
"#include <common>",
"const float G = 9.81;",
"const float KM = 370.0;",
"varying vec2 vUV;",
"uniform float u_size;",
"uniform float u_resolution;",
"uniform float u_choppiness;",
"uniform sampler2D u_phases;",
"uniform sampler2D u_initialSpectrum;",
"vec2 multiplyComplex (vec2 a, vec2 b) {",
" return vec2(a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1]);",
"}",
"vec2 multiplyByI (vec2 z) {",
" return vec2(-z[1], z[0]);",
"}",
"float omega (float k) {",
" return sqrt(G * k * (1.0 + k * k / KM * KM));",
"}",
"void main (void) {",
" vec2 coordinates = gl_FragCoord.xy - 0.5;",
" float n = (coordinates.x < u_resolution * 0.5) ? coordinates.x : coordinates.x - u_resolution;",
" float m = (coordinates.y < u_resolution * 0.5) ? coordinates.y : coordinates.y - u_resolution;",
" vec2 waveVector = (2.0 * PI * vec2(n, m)) / u_size;",
" float phase = texture2D(u_phases, vUV).r;",
" vec2 phaseVector = vec2(cos(phase), sin(phase));",
" vec2 h0 = texture2D(u_initialSpectrum, vUV).rg;",
" vec2 h0Star = texture2D(u_initialSpectrum, vec2(1.0 - vUV + 1.0 / u_resolution)).rg;",
" h0Star.y *= -1.0;",
" vec2 h = multiplyComplex(h0, phaseVector) + multiplyComplex(h0Star, vec2(phaseVector.x, -phaseVector.y));",
" vec2 hX = -multiplyByI(h * (waveVector.x / length(waveVector))) * u_choppiness;",
" vec2 hZ = -multiplyByI(h * (waveVector.y / length(waveVector))) * u_choppiness;",
//no DC term
" if (waveVector.x == 0.0 && waveVector.y == 0.0) {",
" h = vec2(0.0);",
" hX = vec2(0.0);",
" hZ = vec2(0.0);",
" }",
" gl_FragColor = vec4(hX + multiplyByI(h), hZ);",
"}"
].join( "\n" )
};
THREE.OceanShaders[ "ocean_normals" ] = {
uniforms: {
"u_displacementMap": { value: null },
"u_resolution": { value: null },
"u_size": { value: null }
},
fragmentShader: [
"precision highp float;",
"varying vec2 vUV;",
"uniform sampler2D u_displacementMap;",
"uniform float u_resolution;",
"uniform float u_size;",
"void main (void) {",
" float texel = 1.0 / u_resolution;",
" float texelSize = u_size / u_resolution;",
" vec3 center = texture2D(u_displacementMap, vUV).rgb;",
" vec3 right = vec3(texelSize, 0.0, 0.0) + texture2D(u_displacementMap, vUV + vec2(texel, 0.0)).rgb - center;",
" vec3 left = vec3(-texelSize, 0.0, 0.0) + texture2D(u_displacementMap, vUV + vec2(-texel, 0.0)).rgb - center;",
" vec3 top = vec3(0.0, 0.0, -texelSize) + texture2D(u_displacementMap, vUV + vec2(0.0, -texel)).rgb - center;",
" vec3 bottom = vec3(0.0, 0.0, texelSize) + texture2D(u_displacementMap, vUV + vec2(0.0, texel)).rgb - center;",
" vec3 topRight = cross(right, top);",
" vec3 topLeft = cross(top, left);",
" vec3 bottomLeft = cross(left, bottom);",
" vec3 bottomRight = cross(bottom, right);",
" gl_FragColor = vec4(normalize(topRight + topLeft + bottomLeft + bottomRight), 1.0);",
"}"
].join( "\n" )
};
THREE.OceanShaders[ "ocean_main" ] = {
uniforms: {
"u_displacementMap": { value: null },
"u_normalMap": { value: null },
"u_geometrySize": { value: null },
"u_size": { value: null },
"u_projectionMatrix": { value: null },
"u_viewMatrix": { value: null },
"u_cameraPosition": { value: null },
"u_skyColor": { value: null },
"u_oceanColor": { value: null },
"u_sunDirection": { value: null },
"u_exposure": { value: null }
},
vertexShader: [
"precision highp float;",
"varying vec3 vPos;",
"varying vec2 vUV;",
"uniform mat4 u_projectionMatrix;",
"uniform mat4 u_viewMatrix;",
"uniform float u_size;",
"uniform float u_geometrySize;",
"uniform sampler2D u_displacementMap;",
"void main (void) {",
" vec3 newPos = position + texture2D(u_displacementMap, uv).rgb * (u_geometrySize / u_size);",
" vPos = newPos;",
" vUV = uv;",
" gl_Position = u_projectionMatrix * u_viewMatrix * vec4(newPos, 1.0);",
"}"
].join( "\n" ),
fragmentShader: [
"precision highp float;",
"varying vec3 vPos;",
"varying vec2 vUV;",
"uniform sampler2D u_displacementMap;",
"uniform sampler2D u_normalMap;",
"uniform vec3 u_cameraPosition;",
"uniform vec3 u_oceanColor;",
"uniform vec3 u_skyColor;",
"uniform vec3 u_sunDirection;",
"uniform float u_exposure;",
"vec3 hdr (vec3 color, float exposure) {",
" return 1.0 - exp(-color * exposure);",
"}",
"void main (void) {",
" vec3 normal = texture2D(u_normalMap, vUV).rgb;",
" vec3 view = normalize(u_cameraPosition - vPos);",
" float fresnel = 0.02 + 0.98 * pow(1.0 - dot(normal, view), 5.0);",
" vec3 sky = fresnel * u_skyColor;",
" float diffuse = clamp(dot(normal, normalize(u_sunDirection)), 0.0, 1.0);",
" vec3 water = (1.0 - fresnel) * u_oceanColor * u_skyColor * diffuse;",
" vec3 color = sky + water;",
" gl_FragColor = vec4(hdr(color, u_exposure), 1.0);",
"}"
].join( "\n" )
};