Skip to content

Commit 2b637ae

Browse files
authored
Merge pull request #32 from martinRenou/texture_position
UnderWater: Add texture position option
2 parents 92b1844 + ac989a0 commit 2b637ae

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Effects/Water/UnderWater.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface UnderWaterOptions extends BlockOptions {
3434
texture?: THREE.Texture;
3535

3636
textureScale?: number;
37+
texturePosition?: THREE.Vector3;
3738

3839
}
3940

@@ -51,6 +52,7 @@ class UnderWater extends Effect {
5152
this._defaultColor = options.defaultColor !== undefined ? options.defaultColor : this._defaultColor;
5253
this._texture = options.texture !== undefined ? options.texture : this._texture;
5354
this._textureScale = options.textureScale !== undefined ? options.textureScale : this._textureScale;
55+
this._texturePosition = options.texturePosition !== undefined ? options.texturePosition : this._texturePosition;
5456
}
5557

5658
// Shallow copy the meshes (Geometries are not copied, we only create new Materials)
@@ -139,6 +141,7 @@ class UnderWater extends Effect {
139141
// Fragment shader
140142
this.useTexturingNode = new Nodes.FloatNode(0);
141143
this.envTextureNode = new Nodes.TextureNode(new THREE.Texture());
144+
this.texturePositionNode = new Nodes.Vector3Node(this._texturePosition.x, this._texturePosition.y, this._texturePosition.z);
142145
this.textureScaleNode = new Nodes.FloatNode(this._textureScale);
143146
this.defaultColorNode = new Nodes.ColorNode(this._defaultColor);
144147

@@ -193,18 +196,20 @@ class UnderWater extends Effect {
193196
);
194197

195198
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;
200205
201206
return xaxis * textureBlending.x + yaxis * textureBlending.y + zaxis * textureBlending.z;
202207
}`
203208
);
204209

205210
const ifUsetexturingCall = new Nodes.FunctionCallNode(
206211
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]
208213
);
209214

210215
const elseUsetexturing = new Nodes.FunctionNode(
@@ -308,6 +313,11 @@ class UnderWater extends Effect {
308313
this.textureScaleNode.value = this._textureScale;
309314
}
310315

316+
set texturePosition (texturePosition: THREE.Vector3) {
317+
this._texturePosition = texturePosition;
318+
this.texturePositionNode.value = this._texturePosition;
319+
}
320+
311321
renderEnvMap (renderer: THREE.WebGLRenderer, lightCamera: THREE.Camera) {
312322
for (const nodeMesh of this.envMappingMeshes) {
313323
renderer.render(nodeMesh.mesh, lightCamera);
@@ -342,6 +352,7 @@ class UnderWater extends Effect {
342352
private useTexturingNode: Nodes.FloatNode;
343353
private envTextureNode: Nodes.TextureNode;
344354
private textureScaleNode: Nodes.FloatNode;
355+
private texturePositionNode: Nodes.Vector3Node;
345356
private defaultColorNode: Nodes.ColorNode;
346357
private causticsTextureNode: Nodes.TextureNode;
347358
private isUnderwaterNode: IdentityNode;
@@ -352,6 +363,7 @@ class UnderWater extends Effect {
352363
private _defaultColor: THREE.Color = new THREE.Color(0.951, 1., 0.825);
353364
private _texture: THREE.Texture | null = null;
354365
private _textureScale: number = 2;
366+
private _texturePosition: THREE.Vector3 = new THREE.Vector3(1., 1., 0.);
355367

356368
private initialized: boolean = false;
357369

0 commit comments

Comments
 (0)