Skip to content

Commit 9e4d329

Browse files
committed
UnderWater effect: expose defaultColor property
Signed-off-by: martinRenou <martin.renou@gmail.com>
1 parent 0c8d41e commit 9e4d329

7 files changed

Lines changed: 55 additions & 30 deletions

File tree

src/Block.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ abstract class Block extends Events {
176176
return block.boundingSphere;
177177
}
178178

179-
set defaultColor (defaultColor: string) {
179+
set defaultColor (defaultColor: THREE.Color) {
180180
for (const nodeMesh of this.meshes) {
181181
nodeMesh.defaultColor = defaultColor;
182182
}

src/EffectBlock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from './NodeMesh';
1010

1111
import {
12-
Block
12+
Block, BlockOptions
1313
} from './Block';
1414

1515
import {
@@ -37,7 +37,7 @@ type Input = string | ([string, string] | number)[];
3737
export
3838
class Effect extends Block {
3939

40-
constructor (parent: Block, input?: Input) {
40+
constructor (parent: Block, input?: Input, options?: BlockOptions) {
4141
super(parent.vertices, parent.data, parent.options);
4242

4343
this.parent = parent;

src/Effects/IsoSurface.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '../EffectBlock';
66

77
import {
8-
Block
8+
Block, BlockOptions
99
} from '../Block';
1010

1111
import {
@@ -22,7 +22,7 @@ import {
2222

2323

2424
export
25-
interface IsoSurfaceOptions {
25+
interface IsoSurfaceOptions extends BlockOptions {
2626

2727
value?: number;
2828

@@ -35,7 +35,7 @@ export
3535
class IsoSurface extends Effect {
3636

3737
constructor (parent: Block, input: Input, options?: IsoSurfaceOptions) {
38-
super(parent, input);
38+
super(parent, input, options);
3939

4040
if (this.parent.tetrahedronIndices == null) {
4141
throw 'Cannot compute IsoSurface on a Mesh that is not tetrahedron-based';

src/Effects/Threshold.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '../EffectBlock';
66

77
import {
8-
Block
8+
Block, BlockOptions
99
} from '../Block';
1010

1111
import {
@@ -22,7 +22,7 @@ import {
2222

2323

2424
export
25-
interface ThresholdOptions {
25+
interface ThresholdOptions extends BlockOptions {
2626

2727
min?: number;
2828
max?: number;
@@ -36,7 +36,7 @@ export
3636
class Threshold extends Effect {
3737

3838
constructor (parent: Block, input: Input, options?: ThresholdOptions) {
39-
super(parent, input);
39+
super(parent, input, options);
4040

4141
if (options) {
4242
this._min = options.min !== undefined ? options.min : this._min;

src/Effects/Water/UnderWater.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '../../Data';
1010

1111
import {
12-
Block
12+
Block, BlockOptions
1313
} from '../../Block';
1414

1515

@@ -87,6 +87,7 @@ void main(void){
8787
const getEnvFragment = (underwater: String) => `
8888
uniform vec3 light;
8989
uniform sampler2D caustics;
90+
uniform vec3 defaultColor;
9091
9192
#ifdef USE_TEXTURING
9293
uniform sampler2D envTexture;
@@ -96,7 +97,6 @@ uniform sampler2D envTexture;
9697
const vec2 resolution = vec2(1024.);
9798
const float scale = 1. / 0.5;
9899
const vec2 sandTextureResolution = vec2(512, 512);
99-
const vec3 sandColor = vec3(0.951, 1., 0.825);
100100
101101
vec3 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
**/
201211
export
202212
class 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;

src/Effects/Water/Water.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '../../EffectBlock';
77

88
import {
9-
Block
9+
Block, BlockOptions
1010
} from '../../Block';
1111

1212
import {
@@ -48,7 +48,7 @@ void main() {
4848

4949

5050
export
51-
interface WaterOptions {
51+
interface WaterOptions extends BlockOptions {
5252

5353
causticsEnabled?: boolean;
5454

@@ -66,7 +66,7 @@ export
6666
class Water extends Effect {
6767

6868
constructor (parent: Block, options?: WaterOptions) {
69-
super(parent);
69+
super(parent, undefined, options);
7070

7171
if (options) {
7272
this.causticsEnabled = options.causticsEnabled !== undefined ? options.causticsEnabled : this.causticsEnabled;

src/NodeMesh.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class NodeMesh {
9696
// We need to set this to false because we directly play with the position matrix
9797
this.mesh.matrixAutoUpdate = false;
9898

99-
this._defaultColor = '#6395b0';
99+
this._defaultColor = new THREE.Color('#6395b0');
100100
this.defaultColorNode = new Nodes.ColorNode(this._defaultColor);
101101
}
102102

@@ -198,10 +198,10 @@ class NodeMesh {
198198
return this.geometry.boundingSphere as THREE.Sphere;
199199
}
200200

201-
set defaultColor (defaultColor: string) {
201+
set defaultColor (defaultColor: THREE.Color) {
202202
this._defaultColor = defaultColor;
203203

204-
this.defaultColorNode.value = new THREE.Color(this._defaultColor);
204+
this.defaultColorNode.value = this._defaultColor;
205205
}
206206

207207
get defaultColor () {
@@ -317,7 +317,7 @@ class NodeMesh {
317317
private alphaOperators: NodeOperator<Nodes.Node>[] = [];
318318
private colorOperators: NodeOperator<Nodes.Node>[] = [];
319319

320-
private _defaultColor: string;
320+
private _defaultColor: THREE.Color;
321321
private defaultColorNode: Nodes.ColorNode;
322322

323323
private hasIndex: boolean;

0 commit comments

Comments
 (0)