Skip to content

Commit f47f687

Browse files
committed
skybox reflections
1 parent 4daca59 commit f47f687

File tree

6 files changed

+49
-12
lines changed

6 files changed

+49
-12
lines changed

gui.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ export const defaultParams: GuiParams = {
4949
maxWave: 7,
5050
},
5151
],
52-
resolution: 256,
52+
resolution: 2,
5353
wind: vec2.fromValues(2, 2),
5454
alignment: 1.0e-2,
55-
foamSpreading: 1.0,
56-
foamContrast: 6.5,
55+
foamSpreading: 1.2,
56+
foamContrast: 7.2,
5757
randomSeed: 0,
5858
tileRenderer: {
5959
resolution: 256,

renderer/programs/fragment.glsl.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ uniform float foamSpreading;
1111
uniform float foamContrast;
1212
uniform float sizes[3];
1313
uniform float croppinesses[3];
14+
uniform samplerCube env;
1415
1516
uniform sampler2D dx_hy_dz_dxdz0;
1617
uniform sampler2D sx_sz_dxdx_dzdz0;
@@ -66,15 +67,39 @@ float getFoam(in vec2 xz) {
6667
float dxdz = dxdz0 * croppinesses[0] + dxdz1 * croppinesses[1] + dxdz2 * croppinesses[2];
6768
6869
float val = det(jacobian(dxdx_dzdz.x, dxdz, dxdx_dzdz.y));
69-
return pow(-min(0.0f, val - foamSpreading), foamContrast);
70+
return abs(pow(-min(0.0f, val - foamSpreading), foamContrast));
71+
}
72+
73+
74+
vec3 gammaCorrection(const vec3 color) {
75+
return pow(color, vec3(1.0f / 2.2f));
76+
}
77+
78+
vec3 ACESFilm(vec3 x){
79+
return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
80+
}
81+
82+
float fresnelSchlick(vec3 view, vec3 normal){
83+
float cosTheta = dot(normal, normalize(view));
84+
float F0 = 0.02;
85+
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
7086
}
7187
7288
vec3 surface(in vec3 normal, in vec3 view) {
7389
const vec3 upwelling = vec3(0.0, 0.2, 0.3);
74-
const vec3 sky = vec3(0.69, 0.84, 1.0);
7590
const vec3 mist = vec3(0.34, 0.42, 0.5);
7691
const float nShell = 1.34f;
77-
const float kDiffuse = 0.91f;
92+
const float kDiffuse = 1.0f;
93+
const vec3 sunIndensity = vec3(0.42f, 0.39f, 0.19f) * 1.0e2;
94+
vec3 sunDir = normalize(vec3(1.0f, 1.0f, 10.0f));
95+
96+
vec3 ref = reflect(-view, normal);
97+
ref.y = max(ref.y, 1.0e-0);
98+
ref = normalize(ref);
99+
100+
vec3 sky = ACESFilm(textureLod(env, ref, 0.0f).rgb) + pow(max(dot(ref, sunDir), 0.0f), 500.0f) * sunIndensity;
101+
sky = gammaCorrection(sky);
102+
//sky = vec3(0.69, 0.84, 1.0);
78103
79104
float reflectivity;
80105
float costhetai = abs(dot(normal, normalize(view)));
@@ -94,14 +119,16 @@ vec3 surface(in vec3 normal, in vec3 view) {
94119
reflectivity = 0.5 * (fs * fs + ts * ts );
95120
}
96121
97-
float falloff = exp(-length(view) * 1.0e-3) * kDiffuse;
122+
// reflectivity = fresnelSchlick(view,normal);
123+
124+
float falloff = 1.0f; // min(exp(-(length(view) - 1000.0f) * 1.0e-2), 1.0f) * kDiffuse;
98125
vec3 surf = reflectivity * sky + (1.0f - reflectivity) * upwelling;
99126
return falloff * surf + (1.0f - falloff) * mist;
100127
}
101128
102129
void main()
103130
{
104-
float f = getFoam(_xz);
131+
float f = getFoam(_xz) ;
105132
vec3 n = getNormal(_xz);
106133
const vec3 foam = vec3(1.0f);
107134
vec3 water = surface(n, pos - _position);

renderer/programs/skybox-fragment.glsl.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ vec3 toneMapping(const vec3 color) {
1818
return vec3(1.0f) - exp(-color * exposure);
1919
}
2020
21+
vec3 ACESFilm(vec3 x){
22+
return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
23+
}
24+
2125
void main()
2226
{
2327
vec3 background = textureLod(env, normalize(_pos), 0.0f).rgb;
24-
color = vec4(gammaCorrection(toneMapping(background)), 1.0f);
28+
color = vec4(gammaCorrection(ACESFilm(background)), 1.0f);
2529
}
2630
`;

renderer/quad-tree-renderer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import fs from './programs/fragment.glsl';
1414

1515
// @ts-ignore:
1616
import { createPlane } from './mesh';
17+
import { Cubemap } from '../graphics/gpu';
1718

1819
declare const createPlane: (resolution: number, wired: boolean) => Mesh;
1920

@@ -93,7 +94,7 @@ export class QuadTreeOceanRenderer {
9394
});
9495
}
9596

96-
public render(camera: Camera, oceanField: OceanField) {
97+
public render(camera: Camera, oceanField: OceanField, env: Cubemap) {
9798
if (!this.frustum) {
9899
this.frustum = new Frustum(camera);
99100
} else {
@@ -119,6 +120,7 @@ export class QuadTreeOceanRenderer {
119120
],
120121
oceanField.dataMaps
121122
);
123+
this.gpu.setProgramCubemap(this.shader, 'env', env, 6);
122124
for (let i = 0; i < oceanField.params.cascades.length; i++) {
123125
this.gpu.setProgramVariable(
124126
this.shader,

renderer/skybox-renderer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class SkyboxRenderer {
2424
);
2525
this.gpu.setProgram(this.shader);
2626
this.gpu.setProgramCubemap(this.shader, 'env', skybox, 0);
27-
this.gpu.setProgramVariable(this.shader, 'exposure', 'float', 3.0);
27+
// this.gpu.setProgramVariable(this.shader, 'exposure', 'float', 3.0);
2828
this.gpu.setProgramVariable(this.shader, 'gamma', 'float', 2.2);
2929
this.gpu.setProgramVariable(this.shader, 'viewMat', 'mat4', camera.view);
3030
this.gpu.setProgramVariable(

viewport.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ export class Viewport {
128128
} else if (type === 'grid') {
129129
this.projectedGridRenderer.render(this.cameraController.camera, field);
130130
} else if (type === 'quad-tree') {
131-
this.quadTreeRenderer.render(this.cameraController.camera, field);
131+
this.quadTreeRenderer.render(
132+
this.cameraController.camera,
133+
field,
134+
this.skybox
135+
);
132136
} else {
133137
this.plateRenderer.render(this.cameraController.camera, field);
134138
}

0 commit comments

Comments
 (0)