MobileARShadow.shader 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //This is based on a shader from https://alastaira.wordpress.com/2014/12/30/adding-shadows-to-a-unity-vertexfragment-shader-in-7-easy-steps/
  2. Shader "Custom/MobileARShadow"
  3. {
  4. SubShader {
  5. Pass {
  6. // 1.) This will be the base forward rendering pass in which ambient, vertex, and
  7. // main directional light will be applied. Additional lights will need additional passes
  8. // using the "ForwardAdd" lightmode.
  9. // see: http://docs.unity3d.com/Manual/SL-PassTags.html
  10. Tags { "LightMode" = "ForwardBase" "RenderType"="Opaque" "Queue"="Geometry+1" "ForceNoShadowCasting"="True" }
  11. LOD 150
  12. Blend Zero SrcColor
  13. ZWrite On
  14. CGPROGRAM
  15. #pragma vertex vert
  16. #pragma fragment frag
  17. #include "UnityCG.cginc"
  18. // 2.) This matches the "forward base" of the LightMode tag to ensure the shader compiles
  19. // properly for the forward bass pass. As with the LightMode tag, for any additional lights
  20. // this would be changed from _fwdbase to _fwdadd.
  21. #pragma multi_compile_fwdbase
  22. // 3.) Reference the Unity library that includes all the lighting shadow macros
  23. #include "AutoLight.cginc"
  24. struct v2f
  25. {
  26. float4 pos : SV_POSITION;
  27. // 4.) The LIGHTING_COORDS macro (defined in AutoLight.cginc) defines the parameters needed to sample
  28. // the shadow map. The (0,1) specifies which unused TEXCOORD semantics to hold the sampled values -
  29. // As I'm not using any texcoords in this shader, I can use TEXCOORD0 and TEXCOORD1 for the shadow
  30. // sampling. If I was already using TEXCOORD for UV coordinates, say, I could specify
  31. // LIGHTING_COORDS(1,2) instead to use TEXCOORD1 and TEXCOORD2.
  32. LIGHTING_COORDS(0,1)
  33. };
  34. v2f vert(appdata_base v) {
  35. v2f o;
  36. o.pos = UnityObjectToClipPos (v.vertex);
  37. // 5.) The TRANSFER_VERTEX_TO_FRAGMENT macro populates the chosen LIGHTING_COORDS in the v2f structure
  38. // with appropriate values to sample from the shadow/lighting map
  39. TRANSFER_VERTEX_TO_FRAGMENT(o);
  40. return o;
  41. }
  42. fixed4 frag(v2f i) : COLOR {
  43. // 6.) The LIGHT_ATTENUATION samples the shadowmap (using the coordinates calculated by TRANSFER_VERTEX_TO_FRAGMENT
  44. // and stored in the structure defined by LIGHTING_COORDS), and returns the value as a float.
  45. float attenuation = LIGHT_ATTENUATION(i);
  46. return fixed4(1.0,1.0,1.0,1.0) * attenuation;
  47. }
  48. ENDCG
  49. }
  50. }
  51. // 7.) To receive or cast a shadow, shaders must implement the appropriate "Shadow Collector" or "Shadow Caster" pass.
  52. // Although we haven't explicitly done so in this shader, if these passes are missing they will be read from a fallback
  53. // shader instead, so specify one here to import the collector/caster passes used in that fallback.
  54. Fallback "VertexLit"
  55. }