Home Unity

unlit with shadows?

polycounter lvl 17
Offline / Send Message
bugo polycounter lvl 17
Has anyone ever tried achieving an unlit shader that receives dynamic shadows in Unity? I heard you can change on #pragma, but all the attributes noted on unity docs don't work. Any ideas?

Replies

  • Brendan
    Offline / Send Message
    Brendan polycounter lvl 8
    Why not use a custom lighting ramp that is all white for faces up to 90 degrees away from the sun, and all gray for faces from 90 to 180 degrees?

    Find a lighting ramp shader online, and make the ramp all white with 1 pixel of black at the dark edge.

    Actually, the Unity Toon (Ramp) Shader (No Outline) would be perfect for this.
  • Farfarer
    Offline / Send Message
    Farfarer polycounter lvl 17
    This should do it;
    1. Shader "Unlit With Shadows" {
    2. Properties {
    3. _Color ("Main Color", Color) = (1,1,1,1)
    4. _MainTex ("Base (RGB)", 2D) = "white" {}
    5. }
    6. SubShader {
    7. Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}
    8.  
    9. Pass {
    10. Tags {"LightMode" = "ForwardBase"}
    11. CGPROGRAM
    12. #pragma vertex vert
    13. #pragma fragment frag
    14. #pragma multi_compile_fwdbase
    15. #pragma fragmentoption ARB_fog_exp2
    16. #pragma fragmentoption ARB_precision_hint_fastest
    17. #include "UnityCG.cginc"
    18. #include "AutoLight.cginc"
    19. struct v2f
    20. {
    21. float4 pos : SV_POSITION;
    22. float2 uv : TEXCOORD0;
    23. LIGHTING_COORDS(1,2)
    24. };
    25.  
    26. float4 _MainTex_ST;
    27.  
    28. v2f vert (appdata_tan v)
    29. {
    30. v2f o;
    31. o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    32. o.uv = TRANSFORM_TEX (v.texcoord, _MainTex).xy;
    33. TRANSFER_VERTEX_TO_FRAGMENT(o);
    34. return o;
    35. }
    36.  
    37. sampler2D _MainTex;
    38.  
    39. fixed4 frag(v2f i) : COLOR
    40. {
    41. fixed atten = LIGHT_ATTENUATION(i); // Light attenuation + shadows.
    42. //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    43. return tex2D(_MainTex, i.uv) * atten;
    44. }
    45. ENDCG
    46. }
    47.  
    48. Pass {
    49. Tags {"LightMode" = "ForwardAdd"}
    50. Blend One One
    51. CGPROGRAM
    52. #pragma vertex vert
    53. #pragma fragment frag
    54. #pragma multi_compile_fwdadd_fullshadows
    55. #pragma fragmentoption ARB_fog_exp2
    56. #pragma fragmentoption ARB_precision_hint_fastest
    57. #include "UnityCG.cginc"
    58. #include "AutoLight.cginc"
    59. struct v2f
    60. {
    61. float4 pos : SV_POSITION;
    62. float2 uv : TEXCOORD0;
    63. LIGHTING_COORDS(1,2)
    64. };
    65.  
    66. float4 _MainTex_ST;
    67.  
    68. v2f vert (appdata_tan v)
    69. {
    70. v2f o;
    71. o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    72. o.uv = TRANSFORM_TEX (v.texcoord, _MainTex).xy;
    73. TRANSFER_VERTEX_TO_FRAGMENT(o);
    74. return o;
    75. }
    76.  
    77. sampler2D _MainTex;
    78.  
    79. fixed4 frag(v2f i) : COLOR
    80. {
    81. fixed atten = LIGHT_ATTENUATION(i); // Light attenuation + shadows.
    82. //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    83. return tex2D(_MainTex, i.uv) * atten;
    84. }
    85. ENDCG
    86. }
    87. }
    88. FallBack "VertexLit"
    89. }
  • bugo
    Offline / Send Message
    bugo polycounter lvl 17
    Oh man, this is perfect! thank you so much!
  • IsraelAlves
    Hi!

    Is there any way to use lights intensity in this shader? When I tried to change this parameter, nothing happens ( Only when I put 0 intensity, shadows dissapear ).

    I want to change color intensity only ( white to black )... but without a 3D effect, only planar color. If you know how to add normal map too, I really appreciate it!

    thanks!
  • Lansiculus
    So, I tried to implement the shadow pass into my own shader, but its not working? It is still a bit messy, but this is my first shader. This is my code:
    1. Shader "Custom/NoobShader_04" {
    2. Properties {
    3. _Color ("Color", Color) = (0,0.55,0.83,1)
    4. _Diffuse ("Diffuse Map", 2D) = "white" {}
    5. _Displacement ("Displacement Map", 2D) = "white" {}
    6. _Normal ("Normal Map", 2D) = "bump" {}
    7. _Cube ("Cube Map", cube) = ""{}
    8. _SpecColor ("Specular Color", Color) = (1,1,.1,1)
    9. _Shininess ("Shininess", float) = 10
    10. _Bumpiness ("Bumpiness", float) = 1
    11. _CubeStrength ("Cube Map Strength", float) = 1
    12. _Scale ("Wave Scale", float) = 0.7
    13. _Speed ("Speed", float) = 0.5
    14.  
    15. }
    16. SubShader {
    17. Pass{
    18. Tags { "LightMode" = "ForwardBase"}
    19. CGPROGRAM
    20. #pragma vertex vert
    21. #pragma fragment frag
    22. float4 _Color;
    23. sampler2D _Displacement;
    24. sampler2D _Diffuse;
    25. sampler2D _Normal;
    26. samplerCUBE _Cube;
    27. float4 _SpecColor;
    28. float _Shininess;
    29. float _Bumpiness;
    30. float _CubeStrength;
    31. float _Scale;
    32. float _Speed;
    33. float4 _LightColor0;
    34. struct VertexOutput
    35. {
    36. float4 pos : SV_POSITION;
    37. float4 posWorld : TEXCOORD1;
    38. float4 tex : TEXCOORD0;
    39. float3 normalWorld : TEXCOORD2;
    40. float3 tangentWorld : TEXCOORD3;
    41. float3 binormalWorld : TEXCOORD4;
    42. };
    43. struct VertexInput
    44. {
    45. float4 vertex : POSITION;
    46. float3 normal : NORMAL;
    47. float4 texcoord : TEXCOORD0;
    48. float4 tangent : TANGENT;
    49. };
    50. struct FragmentOutput
    51. {
    52. float4 color : COLOR;
    53. };
    54.  
    55. VertexOutput vert (VertexInput i)
    56. {
    57. VertexOutput VOUT;
    58. float4 disp = tex2Dlod(_Displacement, float4(i.texcoord.x + (_Time.x * _Speed), i.texcoord.y + (_Time.x * _Speed),0.0,0.0));
    59. float4 newPos = i.vertex;
    60. newPos.y += _Scale * disp.y;
    61. VOUT.posWorld = mul(_Object2World, newPos);
    62. VOUT.pos = mul(UNITY_MATRIX_MVP,newPos);
    63. VOUT.tex = i.texcoord;
    64. VOUT.normalWorld = normalize( mul(float4(i.normal,0.0),_World2Object).xyz);
    65. VOUT.tangentWorld = normalize( mul(_Object2World,i.tangent).xyz);
    66. VOUT.binormalWorld = normalize( cross(VOUT.normalWorld, VOUT.tangentWorld) * i.tangent.w);
    67. return VOUT;
    68. }
    69. FragmentOutput frag(VertexOutput v)
    70. {
    71. FragmentOutput FOUT;
    72. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    73. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - v.posWorld.xyz);
    74. float4 texN = tex2D(_Normal,float4(v.tex.x + (_Time.x * _Speed),v.tex.y + (_Time.x * _Speed) ,0.0,0.0));
    75. float3 localCoords = float3(2.0 * texN.ag - float2(1.0,1.0), 0.0);
    76. localCoords.z = _Bumpiness;
    77. float3x3 local2WorldTranspose = float3x3(
    78. v.tangentWorld,
    79. v.binormalWorld,
    80. v.normalWorld
    81. );
    82. float3 normalDirection = normalize(mul(localCoords,local2WorldTranspose));
    83. float3 reflectDir = reflect(viewDirection,v.normalWorld);
    84. float4 cubeRef = texCUBE(_Cube, reflectDir);
    85. //
    86. float3 diffuseReflection = (cubeRef * _CubeStrength) * _LightColor0 * saturate(dot(normalDirection,lightDirection));
    87. float3 specularReflection = _SpecColor.xyz * pow(saturate(dot(reflect(-lightDirection, normalDirection), viewDirection)), _Shininess);
    88. float3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection + specularReflection;
    89. //float4 tex = tex2D(_Diffuse,float4(v.tex.x * _Frequency + (_Time.x * _Speed), v.tex.y * _Frequency + (_Time.x * _Speed),0.0,0.0));
    90. //FOUT.color = float4(cubeRef * _Color.xyz * lightFinal , 1.0);
    91. FOUT.color = _Color * float4(lightFinal,0.0);
    92. return FOUT;
    93. }
    94. ENDCG
    95. }
    96. Pass {
    97. Tags {"LightMode" = "ForwardAdd"}
    98. Blend One One
    99. CGPROGRAM
    100. #pragma vertex vert
    101. #pragma fragment frag
    102. #pragma multi_compile_fwdadd_fullshadows
    103. #pragma fragmentoption ARB_fog_exp2
    104. #pragma fragmentoption ARB_precision_hint_fastest
    105. #include "UnityCG.cginc"
    106. #include "AutoLight.cginc"
    107. sampler2D _Displacement;
    108. float _Scale;
    109. float _Speed;
    110. struct v2f
    111. {
    112. float4 pos : SV_POSITION;
    113. float2 uv : TEXCOORD0;
    114. LIGHTING_COORDS(1,2)
    115. };
    116.  
    117. float4 _MainTex_ST;
    118.  
    119. v2f vert (appdata_tan v)
    120. {
    121. v2f o;
    122. float4 disp = tex2Dlod(_Displacement, float4(v.texcoord.x + (_Time.x * _Speed), v.texcoord.y + (_Time.x * _Speed),0.0,0.0));
    123. float4 newPos = v.vertex;
    124. newPos.y += _Scale * disp.y;
    125. o.pos = mul( UNITY_MATRIX_MVP, newPos);
    126. o.uv = TRANSFORM_TEX (v.texcoord, _MainTex).xy;
    127. TRANSFER_VERTEX_TO_FRAGMENT(o);
    128. return o;
    129. }
    130.  
    131. sampler2D _MainTex;
    132.  
    133. fixed4 frag(v2f i) : COLOR
    134. {
    135. fixed atten = LIGHT_ATTENUATION(i); // Light attenuation + shadows.
    136. //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    137. return tex2D(_MainTex, i.uv) * atten;
    138. }
    139. ENDCG
    140. }
    141. }
    142. FallBack "Diffuse"
    143. }
  • Farfarer
    Offline / Send Message
    Farfarer polycounter lvl 17
    There's nothing in your forward base shader to do with shadows. By default, only the base pass in forward rendering gets shadows in Unity (i.e. your most powerful direct light).
Sign In or Register to comment.