OceanShaders.js 11.7 KB
Newer Older
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
/* 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
*/

import {
	Vector2
} from "../../../build/three.module.js";

G
Garrett Johnson 已提交
29 30
var OceanShaders = {};
OceanShaders[ "ocean_sim_vertex" ] = {
31
	vertexShader: [
G
Garrett Johnson 已提交
32
		"varying vec2 vUV;",
33

G
Garrett Johnson 已提交
34 35 36 37 38
		"void main (void) {",
		"	vUV = position.xy * 0.5 + 0.5;",
		"	gl_Position = vec4(position, 1.0 );",
		"}"
	].join( "\n" )
39
};
G
Garrett Johnson 已提交
40
OceanShaders[ "ocean_subtransform" ] = {
41 42 43 44 45 46 47 48
	uniforms: {
		"u_input": { value: null },
		"u_transformSize": { value: 512.0 },
		"u_subtransformSize": { value: 250.0 }
	},
	fragmentShader: [
		//GPU FFT using a Stockham formulation

G
Garrett Johnson 已提交
49 50
		"precision highp float;",
		"#include <common>",
51

G
Garrett Johnson 已提交
52 53 54
		"uniform sampler2D u_input;",
		"uniform float u_transformSize;",
		"uniform float u_subtransformSize;",
55

G
Garrett Johnson 已提交
56
		"varying vec2 vUV;",
57

G
Garrett Johnson 已提交
58 59 60
		"vec2 multiplyComplex (vec2 a, vec2 b) {",
		"	return vec2(a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1]);",
		"}",
61

G
Garrett Johnson 已提交
62 63 64 65 66 67
		"void main (void) {",
		"	#ifdef HORIZONTAL",
		"	float index = vUV.x * u_transformSize - 0.5;",
		"	#else",
		"	float index = vUV.y * u_transformSize - 0.5;",
		"	#endif",
68

G
Garrett Johnson 已提交
69
		"	float evenIndex = floor(index / u_subtransformSize) * (u_subtransformSize * 0.5) + mod(index, u_subtransformSize * 0.5);",
70

G
Garrett Johnson 已提交
71 72 73 74 75 76 77 78
		//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",
79

G
Garrett Johnson 已提交
80 81
		"	float twiddleArgument = -2.0 * PI * (index / u_subtransformSize);",
		"	vec2 twiddle = vec2(cos(twiddleArgument), sin(twiddleArgument));",
82

G
Garrett Johnson 已提交
83 84
		"	vec2 outputA = even.xy + multiplyComplex(twiddle, odd.xy);",
		"	vec2 outputB = even.zw + multiplyComplex(twiddle, odd.zw);",
85

G
Garrett Johnson 已提交
86 87 88
		"	gl_FragColor = vec4(outputA, outputB);",
		"}"
	].join( "\n" )
89
};
G
Garrett Johnson 已提交
90
OceanShaders[ "ocean_initial_spectrum" ] = {
91 92 93 94 95 96
	uniforms: {
		"u_wind": { value: new Vector2( 10.0, 10.0 ) },
		"u_resolution": { value: 512.0 },
		"u_size": { value: 250.0 }
	},
	vertexShader: [
G
Garrett Johnson 已提交
97 98 99 100
		"void main (void) {",
		"	gl_Position = vec4(position, 1.0);",
		"}"
	].join( "\n" ),
101
	fragmentShader: [
G
Garrett Johnson 已提交
102 103
		"precision highp float;",
		"#include <common>",
104

G
Garrett Johnson 已提交
105 106 107
		"const float G = 9.81;",
		"const float KM = 370.0;",
		"const float CM = 0.23;",
108

G
Garrett Johnson 已提交
109 110 111
		"uniform vec2 u_wind;",
		"uniform float u_resolution;",
		"uniform float u_size;",
112

G
Garrett Johnson 已提交
113 114 115
		"float omega (float k) {",
		"	return sqrt(G * k * (1.0 + pow2(k / KM)));",
		"}",
116

G
Garrett Johnson 已提交
117 118 119 120 121
		"#if __VERSION__ == 100",
		"float tanh (float x) {",
		"	return (1.0 - exp(-2.0 * x)) / (1.0 + exp(-2.0 * x));",
		"}",
		"#endif",
122

G
Garrett Johnson 已提交
123 124
		"void main (void) {",
		"	vec2 coordinates = gl_FragCoord.xy - 0.5;",
125

G
Garrett Johnson 已提交
126 127
		"	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;",
128

G
Garrett Johnson 已提交
129 130
		"	vec2 K = (2.0 * PI * vec2(n, m)) / u_size;",
		"	float k = length(K);",
131

G
Garrett Johnson 已提交
132
		"	float l_wind = length(u_wind);",
133

G
Garrett Johnson 已提交
134 135
		"	float Omega = 0.84;",
		"	float kp = G * pow2(Omega / l_wind);",
136

G
Garrett Johnson 已提交
137 138
		"	float c = omega(k) / k;",
		"	float cp = omega(kp) / kp;",
139

G
Garrett Johnson 已提交
140 141 142 143 144 145 146 147
		"	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;",
148

G
Garrett Johnson 已提交
149 150 151 152 153
		"	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;",
154

G
Garrett Johnson 已提交
155 156 157
		"	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));",
158

G
Garrett Johnson 已提交
159
		"	float cosPhi = dot(normalize(u_wind), normalize(K));",
160

G
Garrett Johnson 已提交
161
		"	float S = (1.0 / (2.0 * PI)) * pow(k, -4.0) * (Bl + Bh) * (1.0 + Delta * (2.0 * cosPhi * cosPhi - 1.0));",
162

G
Garrett Johnson 已提交
163 164
		"	float dk = 2.0 * PI / u_size;",
		"	float h = sqrt(S / 2.0) * dk;",
165

G
Garrett Johnson 已提交
166 167 168 169 170 171
		"	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" )
172
};
G
Garrett Johnson 已提交
173
OceanShaders[ "ocean_phase" ] = {
174 175 176 177 178 179 180
	uniforms: {
		"u_phases": { value: null },
		"u_deltaTime": { value: null },
		"u_resolution": { value: null },
		"u_size": { value: null }
	},
	fragmentShader: [
G
Garrett Johnson 已提交
181 182
		"precision highp float;",
		"#include <common>",
183

G
Garrett Johnson 已提交
184 185
		"const float G = 9.81;",
		"const float KM = 370.0;",
186

G
Garrett Johnson 已提交
187
		"varying vec2 vUV;",
188

G
Garrett Johnson 已提交
189 190 191 192
		"uniform sampler2D u_phases;",
		"uniform float u_deltaTime;",
		"uniform float u_resolution;",
		"uniform float u_size;",
193

G
Garrett Johnson 已提交
194 195 196
		"float omega (float k) {",
		"	return sqrt(G * k * (1.0 + k * k / KM * KM));",
		"}",
197

G
Garrett Johnson 已提交
198 199 200 201 202 203
		"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;",
204

G
Garrett Johnson 已提交
205 206 207
		"	float phase = texture2D(u_phases, vUV).r;",
		"	float deltaPhase = omega(length(waveVector)) * u_deltaTime;",
		"	phase = mod(phase + deltaPhase, 2.0 * PI);",
208

G
Garrett Johnson 已提交
209 210 211
		"	gl_FragColor = vec4(phase, 0.0, 0.0, 0.0);",
		"}"
	].join( "\n" )
212
};
G
Garrett Johnson 已提交
213
OceanShaders[ "ocean_spectrum" ] = {
214 215 216 217 218 219 220 221
	uniforms: {
		"u_size": { value: null },
		"u_resolution": { value: null },
		"u_choppiness": { value: null },
		"u_phases": { value: null },
		"u_initialSpectrum": { value: null }
	},
	fragmentShader: [
G
Garrett Johnson 已提交
222 223
		"precision highp float;",
		"#include <common>",
224

G
Garrett Johnson 已提交
225 226
		"const float G = 9.81;",
		"const float KM = 370.0;",
227

G
Garrett Johnson 已提交
228
		"varying vec2 vUV;",
229

G
Garrett Johnson 已提交
230 231 232 233 234
		"uniform float u_size;",
		"uniform float u_resolution;",
		"uniform float u_choppiness;",
		"uniform sampler2D u_phases;",
		"uniform sampler2D u_initialSpectrum;",
235

G
Garrett Johnson 已提交
236 237 238
		"vec2 multiplyComplex (vec2 a, vec2 b) {",
		"	return vec2(a[0] * b[0] - a[1] * b[1], a[1] * b[0] + a[0] * b[1]);",
		"}",
239

G
Garrett Johnson 已提交
240 241 242
		"vec2 multiplyByI (vec2 z) {",
		"	return vec2(-z[1], z[0]);",
		"}",
243

G
Garrett Johnson 已提交
244 245 246
		"float omega (float k) {",
		"	return sqrt(G * k * (1.0 + k * k / KM * KM));",
		"}",
247

G
Garrett Johnson 已提交
248 249 250 251 252
		"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;",
253

G
Garrett Johnson 已提交
254 255
		"	float phase = texture2D(u_phases, vUV).r;",
		"	vec2 phaseVector = vec2(cos(phase), sin(phase));",
256

G
Garrett Johnson 已提交
257 258 259
		"	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;",
260

G
Garrett Johnson 已提交
261
		"	vec2 h = multiplyComplex(h0, phaseVector) + multiplyComplex(h0Star, vec2(phaseVector.x, -phaseVector.y));",
262

G
Garrett Johnson 已提交
263 264
		"	vec2 hX = -multiplyByI(h * (waveVector.x / length(waveVector))) * u_choppiness;",
		"	vec2 hZ = -multiplyByI(h * (waveVector.y / length(waveVector))) * u_choppiness;",
265

G
Garrett Johnson 已提交
266 267 268 269 270 271
		//no DC term
		"	if (waveVector.x == 0.0 && waveVector.y == 0.0) {",
		"		h = vec2(0.0);",
		"		hX = vec2(0.0);",
		"		hZ = vec2(0.0);",
		"	}",
272

G
Garrett Johnson 已提交
273 274 275
		"	gl_FragColor = vec4(hX + multiplyByI(h), hZ);",
		"}"
	].join( "\n" )
276
};
G
Garrett Johnson 已提交
277
OceanShaders[ "ocean_normals" ] = {
278 279 280 281 282 283
	uniforms: {
		"u_displacementMap": { value: null },
		"u_resolution": { value: null },
		"u_size": { value: null }
	},
	fragmentShader: [
G
Garrett Johnson 已提交
284
		"precision highp float;",
285

G
Garrett Johnson 已提交
286
		"varying vec2 vUV;",
287

G
Garrett Johnson 已提交
288 289 290
		"uniform sampler2D u_displacementMap;",
		"uniform float u_resolution;",
		"uniform float u_size;",
291

G
Garrett Johnson 已提交
292 293 294
		"void main (void) {",
		"	float texel = 1.0 / u_resolution;",
		"	float texelSize = u_size / u_resolution;",
295

G
Garrett Johnson 已提交
296 297 298 299 300
		"	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;",
301

G
Garrett Johnson 已提交
302 303 304 305
		"	vec3 topRight = cross(right, top);",
		"	vec3 topLeft = cross(top, left);",
		"	vec3 bottomLeft = cross(left, bottom);",
		"	vec3 bottomRight = cross(bottom, right);",
306

G
Garrett Johnson 已提交
307 308 309
		"	gl_FragColor = vec4(normalize(topRight + topLeft + bottomLeft + bottomRight), 1.0);",
		"}"
	].join( "\n" )
310
};
G
Garrett Johnson 已提交
311
OceanShaders[ "ocean_main" ] = {
312 313 314 315 316 317 318 319 320 321 322 323 324 325
	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: [
G
Garrett Johnson 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		"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" ),
344
	fragmentShader: [
G
Garrett Johnson 已提交
345
		"precision highp float;",
346

G
Garrett Johnson 已提交
347 348
		"varying vec3 vPos;",
		"varying vec2 vUV;",
349

G
Garrett Johnson 已提交
350 351 352 353 354 355 356
		"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;",
357

G
Garrett Johnson 已提交
358 359 360
		"vec3 hdr (vec3 color, float exposure) {",
		"	return 1.0 - exp(-color * exposure);",
		"}",
361

G
Garrett Johnson 已提交
362 363
		"void main (void) {",
		"	vec3 normal = texture2D(u_normalMap, vUV).rgb;",
364

G
Garrett Johnson 已提交
365 366 367
		"	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;",
368

G
Garrett Johnson 已提交
369 370
		"	float diffuse = clamp(dot(normal, normalize(u_sunDirection)), 0.0, 1.0);",
		"	vec3 water = (1.0 - fresnel) * u_oceanColor * u_skyColor * diffuse;",
371

G
Garrett Johnson 已提交
372
		"	vec3 color = sky + water;",
373

G
Garrett Johnson 已提交
374 375 376
		"	gl_FragColor = vec4(hdr(color, u_exposure), 1.0);",
		"}"
	].join( "\n" )
377 378 379
};

export { OceanShaders };