Search Unity

Shader novice needs help with "simple" water shader

Discussion in 'Shaders' started by judgerudge, Nov 15, 2017.

  1. judgerudge

    judgerudge

    Joined:
    Nov 15, 2017
    Posts:
    1
    For some reason I cant post to Unity answers... but anyway, im a total novice to shaders still and im trying to write a simple water surface shader that has 2 offsetting normal maps, with rim lighting based off those normals. Not quite sure what i've done wrong (probably alot haha) but my knowledge is quite limited and I cant really find any other answers. Any help would be appreciated!




    Shader "Custom/Simple Water"
    {
    Properties
    {
    _MainTex("Base (RGB)", 2D) = "white" {}
    _Normal1("Normal 1", 2D) = "bump"
    _Normal2("Normal 2", 2D) = "bump"
    _Color("Color", Color) = (1,1,1,1)
    _RimColor("Rim Color", Color) = (0,0,0,0)
    _RimPower("Rim Power", Range(0.1,2.0)) = 3.0
    _ScrollXSpeed1("X 1", Range(0,10)) = 1
    _ScrollXSpeed2("X 2", Range(0,10)) = 1
    _ScrollYSpeed1("Y 1", Range(0,10)) = 1
    _ScrollyYSpeed2("Y 2", Range(0,10)) = 1
    }
    SubShader
    {
    Tags{ "RenderType" = "Transparent" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert

    struct Input
    {
    float2 uv_Normal1;
    float2 uv_Normal2;
    float3 viewDir;
    };

    sampler2D _MainTex;
    sampler2D _Normal1;
    sampler2D _Normal2;
    float4 _RimColor;
    float _RimPower;
    fixed4 _Color;

    void surf (Input IN, inout SurfaceOutput 0)
    {
    fixed2 scrolledUV1 = IN.uv_Normal1;
    fixed2 scrolledUV2 = IN.uv_Normal2;

    fixed xScrollValue1 = _ScrollXSpeed1 * _Time;
    fixed yScrollValue1 = _ScrollYSpeed1 * _Time;

    fixed xScrollValue2 = _ScrollXSpeed2 * _Time;
    fixed yScrollValue2 = _ScrollYSpeed2 * _Time;

    scrolledUV1 += fixed2(xScrollValue1, yScrollValue1);
    scrolledUV2 += fixed2(xScrollValue2, yScrollValue2);
    half4 c = tex2D(_Normal1, _Normal2, scrolledUV1, scrolledUV2);

    o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
    o.Albedo += c.rgb;

    half rim = 1.0 - saturate(dot(normalize(IN.viewDir, o.Normal));
    o.Emission = _RimColor.rgb * pow(rim, _RimPower);
    o.Alpha = c.a;
    }
    ENDCG
    }
    Fallback "Diffuse"
    }
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    _Time is a Vector4. You need to pick a channel (probably x or y) and do something like _Time.x.

    half4 c = insane stuff. This won't work either. You need to sample each map separately then combine them. There are a handful of ways to do this depending how many instructions you want to spend, you can easily google this bit.

    Also, you don't add your normal to your Albedo. After you combine your normals assign them to o.Normal.

    o.Emission... you should avoid the implicit cast here and add .xxx to the end of pow(rim, _RimPower).