Search Unity

How to check whether surface is within light/shadow? (i.e. for photosensitive shader)

Discussion in 'Shaders' started by hadynlander, May 24, 2015.

  1. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    41
    Hello everyone!

    I'm trying to make my shadows look like watercolour splats / ink-blots. So far I've written a modified version of the standard shader that can take a grayscale map and produce the ink effect, like so:

    ink1.png
    (ignore the fact that light/dark areas get inverted - that'll change)

    For my next trick, I want to replace the user-supplied grayscale texture with unity's real-time shadow maps. The problem is that I have no idea how to access them / whether it's even possible to do so from within a surface shader.

    For the sake of simplifying the problem - I would be crazy-grateful if anybody could show me how I would go about modifying the stripped down shader included below, such that shadowed areas are _ColorA and non-shadowed areas are _ColorB:

    Code (csharp):
    1.  
    2. Shader "Custom/CustomShadow" {
    3.     Properties {
    4.         _ColorA ("Color A", Color) = (1,1,0,1)      
    5.         _ColorB ("Color B", Color) = (0,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 200
    13.        
    14.         CGPROGRAM
    15.         // Physically based Standard lighting model, and enable shadows on all light types
    16.         #pragma surface surf Standard fullforwardshadows
    17.  
    18.         // Use shader model 3.0 target, to get nicer looking lighting
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _MainTex;
    22.  
    23.         struct Input {
    24.             float2 uv_MainTex;
    25.         };
    26.  
    27.         half _Glossiness;
    28.         half _Metallic;
    29.         fixed4 _ColorA;
    30.         fixed4 _ColorB;
    31.  
    32.         void surf (Input IN, inout SurfaceOutputStandard o) {
    33.             // Albedo comes from a texture tinted by color
    34.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _ColorA;
    35.             o.Albedo = c.rgb;
    36.             // Metallic and smoothness come from slider variables
    37.             o.Metallic = _Metallic;
    38.             o.Smoothness = _Glossiness;
    39.             o.Alpha = c.a;
    40.         }
    41.         ENDCG
    42.     }
    43.     FallBack "Diffuse"
    44. }
    45.  

    If anybody can show me how to go about sampling shadows in an example like that, I'm pretty sure I can figure out the rest.

    If there's no easy solution, I would appreciate any advice that might point me in the right direction.

    Thanks for your time!

    -Hadyn