Skip to content

Commit d572e3c

Browse files
committed
Make caustics optional
Signed-off-by: martinRenou <martin.renou@gmail.com>
1 parent aa93554 commit d572e3c

2 files changed

Lines changed: 29 additions & 23 deletions

File tree

src/Plugins/Water/UnderWater.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ void main() {
101101
102102
computedLightIntensity += 0.2 * lightIntensity;
103103
104-
// Retrieve caustics information
105-
vec2 causticsInfo = texture2D(caustics, lightPosition.xy).zw;
106-
float causticsIntensity = causticsInfo.x;
107-
float causticsDepth = causticsInfo.y;
104+
if (v${underwater} < 0.) {
105+
// Retrieve caustics information
106+
vec2 causticsInfo = texture2D(caustics, lightPosition.xy).zw;
107+
float causticsIntensity = causticsInfo.x;
108+
float causticsDepth = causticsInfo.y;
108109
109-
if (causticsDepth > lightPosition.z - bias) {
110-
computedLightIntensity += causticsIntensity;
111-
}
110+
if (causticsDepth > lightPosition.z - bias) {
111+
computedLightIntensity += causticsIntensity;
112+
}
112113
113-
if (v${underwater} < 0.) {
114114
gl_FragColor = vec4(underwaterColor * computedLightIntensity, 1.);
115115
} else {
116116
gl_FragColor = vec4(overwaterColor * computedLightIntensity, 1.);

src/Plugins/Water/Water.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ const float eta = 0.7504;
3535
// if after this number of attempts we did not find the intersection, the result will be wrong.
3636
const int MAX_ITERATIONS = 50;
3737
38-
const vec3 zero = vec3(0.);
39-
4038
4139
void main() {
4240
vec4 modelPosition = modelMatrix * vec4(position, 1.);
@@ -80,12 +78,6 @@ void main() {
8078
8179
newPosition = environment.xyz;
8280
83-
// If we did not find a proper solution in the environment map
84-
// we try not to set the newPosition to vec3(0.)
85-
if (all(equal(newPosition, zero))) {
86-
newPosition = oldPosition;
87-
}
88-
8981
vec4 projectedEnvPosition = projectionMatrix * viewMatrix * vec4(newPosition, 1.0);
9082
depth = 0.5 + 0.5 * projectedEnvPosition.z / projectedEnvPosition.w;
9183
@@ -95,7 +87,7 @@ void main() {
9587

9688
const causticsFragment = `
9789
// TODO Make it a uniform
98-
const float causticsFactor = 0.5;
90+
const float causticsFactor = 0.2;
9991
10092
varying vec3 oldPosition;
10193
varying vec3 newPosition;
@@ -147,28 +139,41 @@ void main() {
147139
`;
148140

149141

142+
export
143+
interface WaterOptions {
144+
145+
causticsEnabled?: boolean;
146+
147+
underWaterBlocks?: UnderWater[];
148+
149+
}
150+
151+
150152
/**
151153
* Displays beautiful water and computes real-time caustics.
152154
**/
153155
// TODO Inherit from something else than Effect
154156
export
155157
class Water extends Effect {
156158

157-
constructor (parent: Block, underWaterBlocks: UnderWater[]) {
159+
constructor (parent: Block, options?: WaterOptions) {
158160
super(parent);
159161

160-
this.underWaterBlocks = underWaterBlocks;
162+
if (options) {
163+
this.causticsEnabled = options.causticsEnabled !== undefined ? options.causticsEnabled : this.causticsEnabled;
164+
this.underWaterBlocks = options.underWaterBlocks !== undefined ? options.underWaterBlocks : this.underWaterBlocks;
165+
}
161166

162167
// Remove meshes, only the water and the environment will stay
163168
this.meshes = [];
164169

165170
// Initialize the light camera
166171
// TODO Use the same directional light as the scene
167172
// TODO Compute clip planes values depending on the mesh + env bounding box
168-
const light = new THREE.Vector3(0., 0., -1.);
173+
const light = new THREE.Vector3(-0.2, -0.2, -1.);
169174
const factor = 0.5925048158409217;
170175
this.lightCamera = new THREE.OrthographicCamera(-1.61 * factor, 1.61 * factor, 0.5 * factor, -0.5 * factor, 0., 2.);
171-
this.lightCamera.position.set(0., 0., 1.2);
176+
this.lightCamera.position.set(1.2 * 0.2, 1.2 * 0.2, 1.2);
172177
this.lightCamera.lookAt(0, 0, 0);
173178

174179
// Set the light to the underWaterBlocks
@@ -264,7 +269,7 @@ class Water extends Effect {
264269
* Update the caustics texture if needed.
265270
*/
266271
private _beforeRenderHook (renderer: THREE.WebGLRenderer): void {
267-
if (this.causticsNeedsUpdate) {
272+
if (this.causticsNeedsUpdate && this.causticsEnabled) {
268273
// Update environment map texture
269274
renderer.setRenderTarget(UnderWater.envMappingTarget);
270275
renderer.setClearColor(black, 0);
@@ -329,6 +334,7 @@ class Water extends Effect {
329334
private lightCamera: THREE.OrthographicCamera;
330335

331336
private causticsNeedsUpdate: boolean = true;
337+
causticsEnabled: boolean = false;
332338

333339
private causticsSize: number = 512;
334340
private causticsTarget: THREE.WebGLRenderTarget;
@@ -340,6 +346,6 @@ class Water extends Effect {
340346

341347
private waterGeometry: THREE.BufferGeometry;
342348

343-
private underWaterBlocks: UnderWater[];
349+
private underWaterBlocks: UnderWater[] = [];
344350

345351
}

0 commit comments

Comments
 (0)