Skip to content

Commit 95216c3

Browse files
committed
Blur effect + increase textures resolution
Signed-off-by: martinRenou <martin.renou@gmail.com>
1 parent d1e9ffd commit 95216c3

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/Plugins/Water/UnderWater.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,30 @@ void main(void){
7777
const getEnvFragment = (underwater: String) => `
7878
uniform vec3 light;
7979
uniform sampler2D caustics;
80+
// TODO Make this a uniform
81+
const vec2 resolution = vec2(1024.);
8082
8183
varying vec3 lightPosition;
8284
varying vec3 worldPosition;
8385
varying float v${underwater};
8486
85-
const float bias = 0.005;
87+
const float bias = 0.001;
8688
8789
const vec3 underwaterColor = vec3(0.4, 0.9, 1.0);
8890
const vec3 overwaterColor = vec3(1.);
8991
92+
float blur(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {
93+
float intensity = 0.;
94+
vec2 off1 = vec2(1.3846153846) * direction;
95+
vec2 off2 = vec2(3.2307692308) * direction;
96+
intensity += texture2D(image, uv).x * 0.2270270270;
97+
intensity += texture2D(image, uv + (off1 / resolution)).x * 0.3162162162;
98+
intensity += texture2D(image, uv - (off1 / resolution)).x * 0.3162162162;
99+
intensity += texture2D(image, uv + (off2 / resolution)).x * 0.0702702703;
100+
intensity += texture2D(image, uv - (off2 / resolution)).x * 0.0702702703;
101+
return intensity;
102+
}
103+
90104
91105
void main() {
92106
// Compute flat shading normal (inefficient, should be computed on CPU once)
@@ -104,9 +118,13 @@ void main() {
104118
if (v${underwater} > 0.) {
105119
// Retrieve caustics information
106120
vec2 causticsInfo = texture2D(caustics, lightPosition.xy).zw;
107-
float causticsIntensity = causticsInfo.x;
108121
float causticsDepth = causticsInfo.y;
109122
123+
float causticsIntensity = 0.5 * (
124+
blur(caustics, lightPosition.xy, resolution, vec2(0., 0.5)) +
125+
blur(caustics, lightPosition.xy, resolution, vec2(0.5, 0.))
126+
);
127+
110128
if (causticsDepth > lightPosition.z - bias) {
111129
computedLightIntensity += causticsIntensity;
112130
}
@@ -229,7 +247,7 @@ class UnderWater extends Effect {
229247
return 1;
230248
}
231249

232-
static readonly envMapSize: number = 256;
250+
static readonly envMapSize: number = 1024;
233251
static envMappingTarget: THREE.WebGLRenderTarget = new THREE.WebGLRenderTarget(UnderWater.envMapSize, UnderWater.envMapSize, {type: THREE.FloatType});
234252
private envMappingMaterial: THREE.ShaderMaterial;
235253
private envMappingMeshes: THREE.Mesh[];

src/Plugins/Water/Water.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ class Water extends Effect {
371371
this.causticsMaterial.uniforms['envMap'].value = UnderWater.envMappingTarget.texture;
372372

373373
renderer.setRenderTarget(this.causticsTarget);
374+
renderer.setClearColor(black, 0);
374375
renderer.clear();
375376

376377
renderer.render(this.causticsMesh, this.lightCamera);
@@ -427,7 +428,7 @@ class Water extends Effect {
427428
private causticsNeedsUpdate: boolean = true;
428429
causticsEnabled: boolean = false;
429430

430-
private causticsSize: number = 512;
431+
private causticsSize: number = 1024;
431432
private causticsTarget: THREE.WebGLRenderTarget;
432433
private causticsMaterial: THREE.ShaderMaterial;
433434
private causticsMesh: THREE.Mesh;

0 commit comments

Comments
 (0)