Skip to content

Commit f99318e

Browse files
committed
feat: ocean surface shader
1 parent 94bd5ee commit f99318e

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

graphics/programs/ocean.ts

+37-11
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,47 @@ float getFoam(in vec2 xz) {
106106
107107
vec2 dxdx_dzdz = dxdx_dzdz0 * croppinesses[0] + dxdx_dzdz1 * croppinesses[1] + dxdx_dzdz2 * croppinesses[2];
108108
float dxdz = dxdz0 * croppinesses[0] + dxdz1 * croppinesses[1] + dxdz2 * croppinesses[2];
109-
110-
return pow(-min(0.0f, det(jacobian(dxdx_dzdz.x, dxdz, dxdx_dzdz.y)) - foamSpreading), foamContrast);
109+
110+
float val = det(jacobian(dxdx_dzdz.x, dxdz, dxdx_dzdz.y));
111+
return pow(-min(0.0f, val - foamSpreading), foamContrast);
112+
}
113+
114+
vec3 surface(in vec3 normal, in vec3 view) {
115+
const vec3 upwelling = vec3(0.0, 0.2, 0.3);
116+
const vec3 sky = vec3(0.69, 0.84, 1.0);
117+
const vec3 mist = vec3(0.34, 0.42, 0.5);
118+
const float nShell = 1.34f;
119+
const float kDiffuse = 0.91f;
120+
121+
float reflectivity;
122+
float costhetai = abs(dot(normal, normalize(view)));
123+
float thetai = acos(costhetai);
124+
float sinthetat = sin(thetai) / nShell;
125+
float thetat = asin(sinthetat);
126+
127+
if(thetai == 0.0)
128+
{
129+
reflectivity = (nShell - 1.0f) / (nShell + 1.0f);
130+
reflectivity = reflectivity * reflectivity;
131+
}
132+
else
133+
{
134+
float fs = sin(thetat - thetai) / sin(thetat + thetai);
135+
float ts = tan(thetat - thetai) / tan(thetat + thetai);
136+
reflectivity = 0.5 * (fs * fs + ts * ts );
137+
}
138+
139+
float falloff = exp(-length(view) * 1.0e-3) * kDiffuse;
140+
vec3 surf = reflectivity * sky + (1.0f - reflectivity) * upwelling;
141+
return falloff * surf + (1.0f - falloff) * mist;
111142
}
112143
113144
void main()
114145
{
115-
vec4 albedo = vec4(0, 0.62, 0.77, 1.0);
146+
float f = getFoam(_xz);
116147
vec3 n = getNormal(_xz);
117-
vec3 l = normalize(pos - _position);
118-
float nol = dot(n, l) * 0.9 + 0.1;
119-
color = albedo * vec4(vec3(nol), 1.0f) + getFoam(_xz) * 0.5;
120-
121-
if(!gl_FrontFacing) {
122-
color = vec4(1.0f);
123-
}
124-
148+
const vec3 foam = vec3(1.0f);
149+
vec3 water = surface(n, pos - _position);
150+
color = vec4(mix(water, foam, f), 1.0f);
125151
}
126152
`;

gui.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export const defaultParams: GuiParams = {
5252
],
5353
resolution: 256,
5454
wind: vec2.fromValues(4, 11),
55-
alignment: 0.0,
56-
foamSpreading: 1.0,
57-
foamContrast: 2.0,
55+
alignment: 1.0e-2,
56+
foamSpreading: 1.2,
57+
foamContrast: 6.5,
5858
randomSeed: 0,
5959
tileRenderer: {
6060
resolution: 256,
@@ -151,7 +151,7 @@ export class Gui {
151151
.add(this.params, 'foamSpreading', 0, 2)
152152
.step(0.1)
153153
.name('Foam spreading');
154-
gui.add(this.params, 'foamContrast', 0, 2).step(0.1).name('Foam contrast');
154+
gui.add(this.params, 'foamContrast', 0, 8).step(0.1).name('Foam contrast');
155155
gui.add(this.params, 'randomSeed', 0, 1024).step(1).name('Random seed');
156156

157157
let i = 0;
@@ -162,8 +162,8 @@ export class Gui {
162162
group.add(cascade, 'size', 0, 1000.0).step(1).name('Size');
163163
group.add(cascade, 'croppiness', -2, 2).step(0.1).name('Croppiness');
164164
group.add(cascade, 'strength', 0, 10).step(0.1).name('Strength');
165-
group.add(cascade, 'minWave', 0, 100).step(1).name('Min wave length');
166-
group.add(cascade, 'maxWave', 0, 100).step(1).name('Max wave length');
165+
group.add(cascade, 'minWave', 0, 1000).step(1).name('Min wave length');
166+
group.add(cascade, 'maxWave', 0, 1000).step(1).name('Max wave length');
167167
}
168168
}
169169
}

0 commit comments

Comments
 (0)