Search Unity

Sprite depth sobel screen space filter

Discussion in 'Shaders' started by Graph, May 10, 2017.

  1. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    Heya, I've gotten myself a little lost in what I'm trying to do it would seem.

    What I'm after visually:
    shaderTarget.png
    (paper model of the targeted aesthetic)

    Now I figured that I could just make a screenSpace/imageEffect shader running a Sobel function to make the "glow" similar to what I've done here on a sprite shader:
    capture_05092017_193158.jpg
    (single colored sprite renderers with directional sampling to simulate simple shading, code below if you want it)
    Code (CSharp):
    1.             void surf(Input IN, inout SurfaceOutput o)
    2.             {
    3.                 fixed4 offsets = _Offset * 0.001;
    4.                 fixed2 dir = fixed2(cos(_Angle), sin(_Angle));
    5.                 ///////////////////
    6.  
    7.                 fixed4 base = offsets.xyxy * dir.xyxy;
    8.                 fixed4 uv01 = IN.uv_MainTex.xyxy + base * d01;
    9.                 fixed4 uv23 = IN.uv_MainTex.xyxy + base * d23;
    10.                 fixed4 uv45 = IN.uv_MainTex.xyxy + base * d45;
    11.                 fixed4 uv67 = IN.uv_MainTex.xyxy + base * d67;
    12.                 ///////////////
    13.  
    14.                 fixed4 color = fixed4(0,0,0,0);
    15.  
    16.                 color += 0.195 * tex2D(_MainTex, uv01.xy);
    17.                 color += 0.195 * tex2D(_MainTex, uv01.zw);
    18.  
    19.                 color += 0.140 * tex2D(_MainTex, uv23.xy);
    20.                 color += 0.140 * tex2D(_MainTex, uv23.zw);
    21.  
    22.                 color += 0.100 * tex2D(_MainTex, uv45.xy);
    23.                 color += 0.100 * tex2D(_MainTex, uv45.zw);
    24.  
    25.                 color += 0.065 * tex2D(_MainTex, uv67.xy);
    26.                 color += 0.065 * tex2D(_MainTex, uv67.zw);
    27.  
    28.                 o.Albedo.rgb = blendScreen(IN.color.rgb, color.a);
    29.                 o.Alpha = tex2D(_MainTex, IN.uv_MainTex).a * IN.color.a;
    30.             }

    But I can't even seem to find a proper way to get the sprite depth into or have the actual alphacutout shape translate into the depth buffer to even begin playing with mathematical ways of solving this.

    If anybody has any ideas, experiences or dare I say solutions please let me know.
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Sobel is for edge detection and doesn't really get you a smooth gradient from a step.

    I'd take more samples (from the z-buffer). Closer to SSAO really.
     
  3. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    Yeah detecting an edge and projecting it at different lengths in a common direction, like i did in the example code.. that's not a problem.
    The problem is getting the sprites into said depth buffer properly in the first place.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Looks 2D. If that's the case you'll save time and effort just biting the bullet and authoring the style like this.
     
    Martin_H likes this.