- Home /
Error in shadow received by vertex displacement shader
I am currently working on small game project with a friend but seem to have run into a problem I am unable to fix regarding one of our shaders and shadows in Unity. The shader itself is quite simply and basically performs vertex displacement to simulate wind and alphatest in order to simulate cut outs in surfaces. The problem is this:
1) The shadows casted by the displaced vertices are not affected by the displacement by default.
2) I have solved this by adding the addshadows keyword to the surface shader (end of #pragma line).
3) Adding the addshadows keyword causes a "gradient-blinds" like error in shadows received.
4) Switching focus between programs randomly seem to fix/reintroduce the issue for all objects - even in builds!
Notes: We are currently using Forward rendering only and all shadows are realtime.
The inconsistent behaviour in 4) above is puzzling to me but hints at it being ordering issue of some kind - a order that is changed every time the unity program receives/loses focus.
Shader source:
Shader "Custom/WindIllumCutout" {
Properties {
_Color ("Color", Color ) = ( 1,1,1,1 )
_MainTex ("Base (RGB)", 2D) = "white" {}
_Direction ("Direction", Vector ) = ( 0,0,0,0 )
_Speed ("Speed", Float ) = 1.0
_WaveLength ("Wave Length", Float ) = 1.0
_Illum ("Illumin (A)", 2D) = "white" {}
_Emission ("Emission", Range(0,1)) = 0
_EmissionLM ("Emission (Lightmapper)", Float) = 0
_WindLike ("Wind Like", Range(0,1)) = 1.0
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"Queue"="AlphaTest"
"IgnoreProjector"="True"
"RenderType"="TransparentCutout"
}
Blend One OneMinusSrcAlpha
//AlphaTest Greater .01
Lod 200
CGPROGRAM
#pragma surface surf Lambert vertex:vert alphatest:_Cutoff addshadow
#include "TerrainEngine.cginc"
sampler2D _MainTex;
float4 _Color, _Direction;
float _Speed, _WaveLength;
sampler2D _Illum;
float _Emission;
float _WindIntensity = 1;
float _WindLike;
struct Input {
float2 uv_MainTex;
float2 uv_Illum;
float4 color : COLOR;
};
void vert( inout appdata_full v )
{
float intensity = lerp( 1, _WindIntensity, _WindLike );
float3 wpos = mul(_Object2World, v.vertex ).xyz * _WaveLength;
float4 t = _Time.xxxx * _Speed * float4( 1.0f, 0.333f, 0.111f, 0.07333f );
float4 p = SmoothTriangleWave( t + wpos.xyzz );
float4 o = mul(_World2Object, _Direction * p * v.color.r );
v.vertex.xyz += o.xyz * intensity;
}
void surf( Input IN, inout SurfaceOutput o )
{
half4 mainColor = tex2D(_MainTex, IN.uv_MainTex );
half illuminationAlpha = tex2D(_Illum, IN.uv_Illum).a;
half4 c = mainColor * _Color;
o.Emission = c.rgb * illuminationAlpha * _Emission;
o.Alpha = c.a * IN.color.a;
o.Albedo = c.rgb * o.Alpha;
}
ENDCG
}
FallBack "Transparent/Cutout/Diffuse"
}
I am currently stuck on this, so all comments and feedback is much appreciated!
Answer by Scarzzurs · Dec 08, 2014 at 02:42 PM
Yah! Problem solved!
An old colleague of mine suggested I tried to implement the shadow caster and collector passes on my own. When this gave exactly the same results as using addshadows I decided to remove the displacement altogether. This still caused the problem and thus I found that it had to be something really simple that was causing it.
Ultimately I found that the problem was caused by the line:
Blend One OneMinusSrcAlpha
So basically, adding a blend rule caused the problem and thus removing it solved the problem!
Your answer
Follow this Question
Related Questions
Why the shader responds differently to Tri-color ambient light vs. Skybox lighting? 0 Answers
World/screen space gradient overlay in surface shader 1 Answer
Adding Shadows to a Shader 1 Answer
Is there a way to have a transparent vertex shader recieve and cast Shadows? 0 Answers
Vertex program max instructions limit 2 Answers