Search Unity

Problem with Light direction shader

Discussion in 'Shaders' started by alexander11, Mar 19, 2018.

  1. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Hi, I need some help with a light direction shader. I'm trying to do this in the picture.
    Captureh7.png
    As you can see by using the light and adding a custom gradient based on the light direction. Now I can do this with a colorRamp, but I would like to know how to do it with colors instead of a texture then creating my own gradient with that.

    I have been trying to achieve whats in the picture above, but its not getting the light direction for some reason, but it is doing the gradient. I don't know how to fix this.
    I will have the code and shader setting configuration below.
    Captureh9.PNG

    *Shader Configuration*
    Captureh10.PNG
    *Here is the code*

    Shader "LightDirGradient" {
    Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Color1 ("Bottom Color", Color) = (0,0,1,1)
    _Color2 ("Top Color", Color) = (1,0,0,1)
    _Scale ("Scale", Float) = 1
    _Offset ("Offset", Float) = 0
    _Glossiness ("Smoothness", Range(0,1)) = 0.5
    _Metallic ("Metallic", Range(0,1)) = 0.0
    //_LightDir ("LightDir", Vector) = (0, 0, 0, 0)
    }
    SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200
    CGPROGRAM
    #pragma surface surf Standard fullforwardshadows vertex:vert
    #pragma target 3.0
    sampler2D _MainTex;
    half _Glossiness;
    half _Metallic;
    fixed4 _Color1;
    fixed4 _Color2;
    float4 _LightDir;
    fixed _Scale;
    fixed _Offset;
    struct Input {
    float2 uv_MainTex;
    float3 lightDir;
    };
    void vert (inout appdata_full v, out Input o){
    UNITY_INITIALIZE_OUTPUT(Input,o);
    float4 vertWorldPos = mul(_Object2World, v.vertex);
    half dotP = (dot(normalize(v.vertex.xyz - vertWorldPos), _WorldSpaceLightPos0.xyz));
    o.lightDir = _LightColor0.rgb * dotP;
    }
    void surf (Input IN, inout SurfaceOutputStandard o) {
    half4 c = tex2D(_MainTex, IN.uv_MainTex);
    c.rgb *= lerp(_Color1,_Color2, (IN.lightDir.xyz * _Scale) +_Offset );
    o.Albedo = c.rgb;
    o.Alpha = c.a;
    o.Metallic = _Metallic;
    o.Smoothness = _Glossiness;
    }
    ENDCG
    }
    Fallback "Diffuse"
    }
     
  2. Kumo-Kairo

    Kumo-Kairo

    Joined:
    Sep 2, 2013
    Posts:
    343
    You are not specifying the render pass in which this shader would be called. If you're using Forward rendering, you have to set a pass with a LightMode = "ForwardBase" or Unity won't supply any of the _WorldSpaceLightPos0 uniforms for you