99} from '../../Data' ;
1010
1111import {
12- Block
12+ Block , BlockOptions
1313} from '../../Block' ;
1414
1515
@@ -87,6 +87,7 @@ void main(void){
8787const getEnvFragment = ( underwater : String ) => `
8888uniform vec3 light;
8989uniform sampler2D caustics;
90+ uniform vec3 defaultColor;
9091
9192#ifdef USE_TEXTURING
9293uniform sampler2D envTexture;
@@ -96,7 +97,6 @@ uniform sampler2D envTexture;
9697const vec2 resolution = vec2(1024.);
9798const float scale = 1. / 0.5;
9899const vec2 sandTextureResolution = vec2(512, 512);
99- const vec3 sandColor = vec3(0.951, 1., 0.825);
100100
101101vec3 texColor;
102102
@@ -167,9 +167,9 @@ void main() {
167167 vec3 zaxis = texture2D(envTexture, worldPosition.xy * scale).xyz;
168168#else
169169 // Tri-planar mapping on the generated sand texture
170- vec3 xaxis = vec3(noise(worldPosition.yz * 100. * scale) * 0.2 + 0.9) * sandColor ;
171- vec3 yaxis = vec3(noise(worldPosition.xz * 100. * scale) * 0.2 + 0.9) * sandColor ;
172- vec3 zaxis = vec3(noise(worldPosition.xy * 100. * scale) * 0.2 + 0.9) * sandColor ;
170+ vec3 xaxis = vec3(noise(worldPosition.yz * 100. * scale) * 0.2 + 0.9) * defaultColor ;
171+ vec3 yaxis = vec3(noise(worldPosition.xz * 100. * scale) * 0.2 + 0.9) * defaultColor ;
172+ vec3 zaxis = vec3(noise(worldPosition.xy * 100. * scale) * 0.2 + 0.9) * defaultColor ;
173173#endif
174174 texColor = xaxis * textureBlending.x + yaxis * textureBlending.y + zaxis * textureBlending.z;
175175
@@ -195,20 +195,35 @@ void main() {
195195` ;
196196
197197
198+ export
199+ interface UnderWaterOptions extends BlockOptions {
200+
201+ defaultColor ?: THREE . Color ;
202+
203+ texture ?: THREE . Texture ;
204+
205+ }
206+
207+
198208/**
199209 * Block that receives the caustics.
200210 **/
201211export
202212class UnderWater extends Effect {
203213
204- constructor ( parent : Block , input : Input ) {
205- super ( parent , input ) ;
214+ constructor ( parent : Block , input : Input , options ?: UnderWaterOptions ) {
215+ super ( parent , input , options ) ;
206216
207217 // Remove meshes, we won't use NodeMeshes here
208218 this . meshes = [ ] ;
209219
210220 this . inputComponent = this . inputs [ 0 ] ;
211221
222+ if ( options ) {
223+ this . _defaultColor = options . defaultColor !== undefined ? options . defaultColor : this . _defaultColor ;
224+ this . _texture = options . texture !== undefined ? options . texture : this . _texture ;
225+ }
226+
212227 // Initialize environment mapping and environment material
213228 this . envMappingMaterial = new THREE . ShaderMaterial ( {
214229 vertexShader : envMappingVertex ,
@@ -221,12 +236,13 @@ class UnderWater extends Effect {
221236 uniforms : {
222237 light : { value : null } ,
223238 caustics : { value : null } ,
224- envTexture : { value : null } ,
239+ envTexture : { value : this . _texture } ,
240+ defaultColor : { value : this . _defaultColor } ,
225241 lightProjectionMatrix : { value : null } ,
226242 lightViewMatrix : { value : null }
227243 } ,
228244 defines : {
229- USE_TEXTURING : false
245+ USE_TEXTURING : this . _texture !== null
230246 } ,
231247 extensions : {
232248 derivatives : true
@@ -274,18 +290,25 @@ class UnderWater extends Effect {
274290 this . envMaterial . uniforms [ 'caustics' ] . value = causticsTexture ;
275291 }
276292
277- setTexture ( texture : THREE . Texture | null ) {
278- if ( texture === null ) {
293+ set defaultColor ( value : THREE . Color ) {
294+ this . _defaultColor = value ;
295+ this . envMaterial . uniforms [ 'defaultColor' ] . value = value ;
296+ }
297+
298+ set texture ( texture : THREE . Texture | null ) {
299+ this . _texture = texture ;
300+
301+ if ( this . _texture === null ) {
279302 this . envMaterial . uniforms [ 'envTexture' ] . value = null ;
280303 this . envMaterial . defines [ 'USE_TEXTURING' ] = false ;
281304
282305 return ;
283306 }
284307
285- texture . wrapS = THREE . RepeatWrapping ;
286- texture . wrapT = THREE . RepeatWrapping ;
308+ this . _texture . wrapS = THREE . RepeatWrapping ;
309+ this . _texture . wrapT = THREE . RepeatWrapping ;
287310
288- this . envMaterial . uniforms [ 'envTexture' ] . value = texture ;
311+ this . envMaterial . uniforms [ 'envTexture' ] . value = this . _texture ;
289312 this . envMaterial . defines [ 'USE_TEXTURING' ] = true ;
290313 }
291314
@@ -333,6 +356,8 @@ class UnderWater extends Effect {
333356 private envMeshes : THREE . Mesh [ ] ;
334357
335358 private envMaterial : THREE . ShaderMaterial ;
359+ private _defaultColor : THREE . Color = new THREE . Color ( 0.951 , 1. , 0.825 ) ;
360+ private _texture : THREE . Texture | null = null ;
336361
337362 private initialized : boolean = false ;
338363 private inputComponent : Component ;
0 commit comments