duck

Directional Planar Overhead Shadow Shader

Jul 21st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. Shader "Directional Planar Overhead Shadow" {
  2. Properties {
  3. _Color ("Color", Color) = (0,0,0,0)
  4. _GroundHeight ("Ground Height", Float) = 0
  5. }
  6. SubShader {
  7. Tags { "RenderType" = "Opaque" }
  8. Offset -1.0, -2.0
  9. CGPROGRAM
  10. #pragma surface surf Lambert vertex:vert noambient nolightmap noforwardadd approxview halfasview exclude_path:prepass
  11. struct Input {
  12. float2 uv_MainTex;
  13. };
  14. float4 _Color;
  15. float _GroundHeight;
  16. float4 _LightDirection;
  17. float4x4 _WorldToReceiver;
  18.  
  19. void vert (inout appdata_full v) {
  20. // transform into worldspace
  21. float4 world_space_vertex = mul( _Object2World, v.vertex );
  22. float4 lightDirection = _LightDirection;
  23.  
  24. float4 world2ReceiverRow1 = float4(_WorldToReceiver[0][1], _WorldToReceiver[1][1], _WorldToReceiver[2][1], _WorldToReceiver[3][1]);
  25. float distanceOfVertex = dot(world2ReceiverRow1, world_space_vertex);
  26. float lengthOfLightDirectionInY = dot(world2ReceiverRow1, lightDirection);
  27.  
  28. if (distanceOfVertex > 0.0 && lengthOfLightDirectionInY < 0.0)
  29. {
  30. lightDirection = lightDirection * (distanceOfVertex / (-lengthOfLightDirectionInY));
  31. }
  32. else
  33. {
  34. // we don't move vertex if below plane, or if light is directed upwards
  35. lightDirection = float4(0.0, 0.0, 0.0, 0.0);
  36. }
  37.  
  38. world_space_vertex += lightDirection;
  39.  
  40. // transform back into local space
  41. v.vertex = mul( _World2Object, world_space_vertex );
  42.  
  43. }
  44.  
  45. void surf (Input IN, inout SurfaceOutput o) {
  46. o.Albedo = _Color;
  47. }
  48. ENDCG
  49. }
  50. Fallback "Diffuse"
  51. }
Add Comment
Please, Sign In to add comment