Search Unity

Shader: Water with simple reflection and refraction effects.

Discussion in 'Shaders' started by Ofreyre, May 28, 2018.

  1. Ofreyre

    Ofreyre

    Joined:
    Jul 17, 2013
    Posts:
    28
    Hi,

    I'm trying to make a shader for water with simple reflection and refraction effects.

    I'm trying first with reflection. My idea for the reflection is to render from a position bellow the main camera, with the inverse angle X of the main camera, to a render texture. Then blend that texture in the water shader.

    The main problem is that somewhere in the transformation from object space to clip space, to ndc space to uv I'm making some mistake.

    Here is the code of the shader.



    Shader "Custom/Water"
    {
    Properties{
    _ReflectionTex("Reflection Texture", 2D) = "white" {}
    }
    SubShader{
    Tags{ "RenderType" = "Opaque" }
    CGPROGRAM

    #pragma surface surf Lambert


    struct Input {
    float2 reflection;
    };

    sampler2D _ReflectionTex;

    void vert(inout appdata_full v)
    {
    Input o;

    //object space to homogeneous clip space.
    float4 clipPos = UnityObjectToClipPos(v.vertex);

    //From homogeneous clip space to normalized device space.
    clipPos.xy = (clipPos.xy / clipPos.w);

    //Invert Y for reflection
    clipPos.y *= -1;

    //To uv coordinates.
    clipPos.xy = 0.5 * clipPos.xy + 0.5;

    o.reflection = clipPos.xy;
    }

    void surf(Input IN, inout SurfaceOutput o) {
    o.Albedo = tex2D(_ReflectionTex, IN.reflection).rgb;
    }

    ENDCG
    }
    Fallback "Diffuse"
    }



    Any ideas?

    Thanks