@@ -34,6 +34,7 @@ interface UnderWaterOptions extends BlockOptions {
34
34
texture ?: THREE . Texture ;
35
35
36
36
textureScale ?: number ;
37
+ texturePosition ?: THREE . Vector3 ;
37
38
38
39
}
39
40
@@ -51,6 +52,7 @@ class UnderWater extends Effect {
51
52
this . _defaultColor = options . defaultColor !== undefined ? options . defaultColor : this . _defaultColor ;
52
53
this . _texture = options . texture !== undefined ? options . texture : this . _texture ;
53
54
this . _textureScale = options . textureScale !== undefined ? options . textureScale : this . _textureScale ;
55
+ this . _texturePosition = options . texturePosition !== undefined ? options . texturePosition : this . _texturePosition ;
54
56
}
55
57
56
58
// Shallow copy the meshes (Geometries are not copied, we only create new Materials)
@@ -139,6 +141,7 @@ class UnderWater extends Effect {
139
141
// Fragment shader
140
142
this . useTexturingNode = new Nodes . FloatNode ( 0 ) ;
141
143
this . envTextureNode = new Nodes . TextureNode ( new THREE . Texture ( ) ) ;
144
+ this . texturePositionNode = new Nodes . Vector3Node ( this . _texturePosition . x , this . _texturePosition . y , this . _texturePosition . z ) ;
142
145
this . textureScaleNode = new Nodes . FloatNode ( this . _textureScale ) ;
143
146
this . defaultColorNode = new Nodes . ColorNode ( this . _defaultColor ) ;
144
147
@@ -193,18 +196,20 @@ class UnderWater extends Effect {
193
196
) ;
194
197
195
198
const ifUsetexturing = new Nodes . FunctionNode (
196
- `vec3 getTextureColor${ this . id } (sampler2D envTexture, vec3 worldPosition, float scale, vec3 textureBlending){
197
- vec3 xaxis = texture2D(envTexture, worldPosition.yz * scale).xyz;
198
- vec3 yaxis = texture2D(envTexture, worldPosition.xz * scale).xyz;
199
- vec3 zaxis = texture2D(envTexture, worldPosition.xy * scale).xyz;
199
+ `vec3 getTextureColor${ this . id } (sampler2D envTexture, vec3 worldPosition, vec3 texturePosition, float scale, vec3 textureBlending){
200
+ float texScale = 1. / scale;
201
+
202
+ vec3 xaxis = texture2D(envTexture, (texturePosition.yz + worldPosition.yz) * texScale).xyz;
203
+ vec3 yaxis = texture2D(envTexture, (texturePosition.xz + worldPosition.xz) * texScale).xyz;
204
+ vec3 zaxis = texture2D(envTexture, (texturePosition.xy + worldPosition.xy) * texScale).xyz;
200
205
201
206
return xaxis * textureBlending.x + yaxis * textureBlending.y + zaxis * textureBlending.z;
202
207
}`
203
208
) ;
204
209
205
210
const ifUsetexturingCall = new Nodes . FunctionCallNode (
206
211
ifUsetexturing ,
207
- [ this . envTextureNode , new Nodes . PositionNode ( Nodes . PositionNode . WORLD ) , this . textureScaleNode , textureBlending ]
212
+ [ this . envTextureNode , new Nodes . PositionNode ( Nodes . PositionNode . WORLD ) , this . texturePositionNode , this . textureScaleNode , textureBlending ]
208
213
) ;
209
214
210
215
const elseUsetexturing = new Nodes . FunctionNode (
@@ -308,6 +313,11 @@ class UnderWater extends Effect {
308
313
this . textureScaleNode . value = this . _textureScale ;
309
314
}
310
315
316
+ set texturePosition ( texturePosition : THREE . Vector3 ) {
317
+ this . _texturePosition = texturePosition ;
318
+ this . texturePositionNode . value = this . _texturePosition ;
319
+ }
320
+
311
321
renderEnvMap ( renderer : THREE . WebGLRenderer , lightCamera : THREE . Camera ) {
312
322
for ( const nodeMesh of this . envMappingMeshes ) {
313
323
renderer . render ( nodeMesh . mesh , lightCamera ) ;
@@ -342,6 +352,7 @@ class UnderWater extends Effect {
342
352
private useTexturingNode : Nodes . FloatNode ;
343
353
private envTextureNode : Nodes . TextureNode ;
344
354
private textureScaleNode : Nodes . FloatNode ;
355
+ private texturePositionNode : Nodes . Vector3Node ;
345
356
private defaultColorNode : Nodes . ColorNode ;
346
357
private causticsTextureNode : Nodes . TextureNode ;
347
358
private isUnderwaterNode : IdentityNode ;
@@ -352,6 +363,7 @@ class UnderWater extends Effect {
352
363
private _defaultColor : THREE . Color = new THREE . Color ( 0.951 , 1. , 0.825 ) ;
353
364
private _texture : THREE . Texture | null = null ;
354
365
private _textureScale : number = 2 ;
366
+ private _texturePosition : THREE . Vector3 = new THREE . Vector3 ( 1. , 1. , 0. ) ;
355
367
356
368
private initialized : boolean = false ;
357
369
0 commit comments