@@ -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.
3636const int MAX_ITERATIONS = 50;
3737
38- const vec3 zero = vec3(0.);
39-
4038
4139void 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
9688const causticsFragment = `
9789// TODO Make it a uniform
98- const float causticsFactor = 0.5 ;
90+ const float causticsFactor = 0.2 ;
9991
10092varying vec3 oldPosition;
10193varying 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
154156export
155157class 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