Search Unity

Material using custom shader not affected by spotlight only directional

Discussion in 'Shaders' started by TheLordBaski, Jul 4, 2017.

  1. TheLordBaski

    TheLordBaski

    Joined:
    Nov 22, 2016
    Posts:
    2
    Code (CSharp):
    1. Shader "Drages/Planet"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture (RGB)", 2D) = "black" {}
    6.         _Color("Color", Color) = (0, 0, 0, 1)
    7.         _AtmoColor("Atmosphere Color", Color) = (0.5, 0.5, 1.0, 1)
    8.         _Size("Size", Float) = 0.1
    9.         _Falloff("Falloff", Float) = 5
    10.         _FalloffPlanet("Falloff Planet", Float) = 5
    11.         _Transparency("Transparency", Float) = 15
    12.         _TransparencyPlanet("Transparency Planet", Float) = 1
    13.     }
    14.     SubShader
    15.     {
    16.         Tags {"LightMode" = "ForwardBase"}
    17.         Pass
    18.         {
    19.             Name "PlanetBase"
    20.             Cull Back
    21.             CGPROGRAM
    22.                 #pragma vertex vert
    23.                 #pragma fragment frag
    24.                 #pragma fragmentoption ARB_fog_exp2
    25.                 #pragma fragmentoption ARB_precision_hint_fastest
    26.                 #include "UnityCG.cginc"
    27.                 uniform sampler2D _MainTex;
    28.                 uniform float4 _MainTex_ST;
    29.                 uniform float4 _Color;
    30.                 uniform float4 _AtmoColor;
    31.                 uniform float _FalloffPlanet;
    32.                 uniform float _TransparencyPlanet;
    33.                 struct v2f
    34.                 {
    35.                     float4 pos : SV_POSITION;
    36.                     float3 normal : TEXCOORD0;
    37.                     float3 worldvertpos : TEXCOORD1;
    38.                     float2 texcoord : TEXCOORD2;
    39.                 };
    40.                 v2f vert(appdata_base v)
    41.                 {
    42.                     v2f o;
    43.                     o.pos = UnityObjectToClipPos (v.vertex);
    44.                     o.normal = mul((float3x3)unity_ObjectToWorld, v.normal);
    45.                     o.worldvertpos = mul(unity_ObjectToWorld, v.vertex).xyz;
    46.                     o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    47.                     return o;
    48.                 }
    49.                 float4 frag(v2f i) : COLOR
    50.                 {
    51.                     i.normal = normalize(i.normal);
    52.                     float3 viewdir = normalize(_WorldSpaceCameraPos-i.worldvertpos);
    53.                     float4 atmo = _AtmoColor;
    54.                     atmo.a = pow(1.0-saturate(dot(viewdir, i.normal)), _FalloffPlanet);
    55.                     atmo.a *= _TransparencyPlanet*_Color;
    56.                     float4 color = tex2D(_MainTex, i.texcoord)*_Color;
    57.                     color.rgb = lerp(color.rgb, atmo.rgb, atmo.a);
    58.                     return color*dot(_WorldSpaceLightPos0, i.normal);
    59.                 }
    60.             ENDCG
    61.         }
    62.         Tags {"LightMode" = "ForwardBase" "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    63.         Pass
    64.         {
    65.             Name "FORWARD"
    66.             Cull Front
    67.             Blend SrcAlpha One
    68.             ZWrite Off
    69.             CGPROGRAM
    70.                 #pragma vertex vert
    71.                 #pragma fragment frag
    72.                 #pragma fragmentoption ARB_fog_exp2
    73.                 #pragma fragmentoption ARB_precision_hint_fastest
    74.                 #include "UnityCG.cginc"
    75.                 uniform float4 _Color;
    76.                 uniform float4 _AtmoColor;
    77.                 uniform float _Size;
    78.                 uniform float _Falloff;
    79.                 uniform float _Transparency;
    80.                 struct v2f
    81.                 {
    82.                     float4 pos : SV_POSITION;
    83.                     float3 normal : TEXCOORD0;
    84.                     float3 worldvertpos : TEXCOORD1;
    85.                 };
    86.                 v2f vert(appdata_base v)
    87.                 {
    88.                     v2f o;
    89.                     v.vertex.xyz += v.normal*_Size;
    90.                     o.pos = UnityObjectToClipPos (v.vertex);
    91.                     o.normal = mul((float3x3)unity_ObjectToWorld, v.normal);
    92.                     o.worldvertpos = mul(unity_ObjectToWorld, v.vertex);
    93.                     return o;
    94.                 }
    95.                 float4 frag(v2f i) : COLOR
    96.                 {
    97.                     i.normal = normalize(i.normal);
    98.                     float3 viewdir = normalize(i.worldvertpos-_WorldSpaceCameraPos);
    99.                     float4 color = _AtmoColor;
    100.                     color.a = dot(viewdir, i.normal);
    101.                     color.a *=dot(i.normal, _WorldSpaceLightPos0);
    102.                     color.a = saturate(color.a);
    103.                     color.a = pow(color.a, _Falloff);
    104.                     color.a *= _Transparency;
    105.                     return color;
    106.                 }
    107.             ENDCG
    108.         }
    109.     }
    110.     FallBack "Diffuse"
    111. }
    Hi,
    I got planet shader up here. I can affect it by directional light, but i cannot not to do it working with spotlight.
    What am i'm missing?
     
    aparajithsairam likes this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Shader passes with the "LightMode" = "ForwardBase" tag only handle the primary directional light and ambient light. Additional point and spot lights are handled with an additional shader pass (or passes) with the "LightMode" = "ForwardAdd" tag. You'll also need to implement a lot more code in your shader to properly handle the spotlight attenuation.

    http://catlikecoding.com/unity/tutorials/rendering/part-5/