#Shader distortion speeds up when camera moves

11 messages · Page 1 of 1 (latest)

hard goblet
#

DIstortion behaves weird when camera moves

Shader code:

shader_type spatial;
render_mode unshaded;

uniform sampler2D shape_texture;

uniform float distortionView : hint_range(0.0, 0.3, 0.005) = 0.03;
uniform float speedView : hint_range(0.0, 1.0, 0.005) = 0.5;
uniform sampler2D noiseViewX;
uniform sampler2D noiseViewY;
uniform sampler2D screenTexture : hint_screen_texture;
uniform vec3 tintColor : source_color;
uniform float fresnelAmount : hint_range(0.0, 5.0, 0.1);

float fresnel(float amount, vec3 normal, vec3 view) {
    return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0)), amount);
}

void fragment() {
    vec4 shape = texture(shape_texture, UV);

    if(shape.a < 0.5){
        discard;
    }
    
    vec4 worldPos = MODEL_MATRIX * vec4(VERTEX, 1.0);
    float noiseValueX = (texture(noiseViewX, worldPos.xy * 0.1 + (TIME * speedView)).r * 2.0) - 1.0; // Range: -1.0 to 1.0
    float noiseValueY = (texture(noiseViewY, worldPos.xy * 0.1 + (TIME * speedView)).r * 2.0) - 1.0; // Range: -1.0 to 1.0
    vec2 noiseDistort = vec2(noiseValueX, noiseValueY) * distortionView;
    vec3 distortedScreenTexture = vec3(texture(screenTexture, SCREEN_UV + noiseDistort).rgb);
    vec3 fresnelTint = (tintColor * fresnel(fresnelAmount, NORMAL, VIEW));
    ALBEDO = distortedScreenTexture + fresnelTint;
}
hard goblet
#

@cinder plover maybe you can help 🙂

fervent zodiac
#

you have screenspace distortion

#

is it maybe not that that's resulting in the distortion that follows the camera?

grave horizon
#

This line is wrong, because in the fragment shader VERTEX is given in view space
vec4 worldPos = MODEL_MATRIX * vec4(VERTEX, 1.0);

hard goblet
#

@grave horizon @fervent zodiac

#

there is an instance of the shader for each water tile, if i don't do the woldPos thing the effect repeats itself for every instance and looks very bad

hard goblet
#

update:

    vec4 worldPos = vec4(VERTEX, 1.0);

changed this line so the effect doesnt restart when changing tiles, still the effect speeds up in some directions, maybe i should adjust it passing the direction and speed of the camera through a script 🧐

grave horizon
hard goblet