Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Soft Edge and Stencils

Discussion in 'Shaders' started by TobyADev, Oct 16, 2019.

  1. TobyADev

    TobyADev

    Joined:
    Feb 26, 2016
    Posts:
    4
    TLDR : Shader which lets you see the skybox through other objects.
    I am trying to create a stencil shader which exposes a discovered area for an AR application. I have tried quite a few solutions but am not at all experienced with working with shaders. Im certain there must be a simple solution I have missed. In the example below I am using an Alpha Mask for what to draw, but I cannot get the black to be the shade of the skybox instead, I approached this hoping that I could set a value to 0 late into the que in order to prevent it rendering over the skybox but have had no luck. Most recent shader attempts are below, in varying levels of abandonment and frustration.
    There is a fairly complex heirarchy needed, so the mask needs to be a seperate object/material from what is being rendered.
    Ive been running in circles a bit, help would be appreciated.
    upload_2019-10-16_20-28-43.png
    upload_2019-10-16_20-52-1.png
    Code (csharp):
    1. Shader "SurfaceFadeMask"
    2. {
    3.      Properties {
    4.         _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
    5.         _Color ("Main Color", Color) = (0,0,0,0)
    6.         _Stencil("Stencil Texture (RGB)", 2D) = "white" {}
    7.      }
    8.      Subshader {
    9.         Tags {
    10.           "Queue"="Transparent"
    11.           "RenderType"="Transparent"
    12.         }
    13.                 Stencil
    14.         {
    15.             Ref 1
    16.             Comp Equal
    17.             Pass Keep
    18.         }
    19.         ZTest Greater
    20.         CGPROGRAM
    21.         #pragma surface surf Lambert alpha
    22.          
    23.           struct Input {
    24.            float2 uv_MainTex;
    25.           };
    26.  
    27.           half4 _Color;
    28.           sampler2D _MainTex;
    29.           sampler2D _Stencil;
    30.          
    31.             void surf (Input IN, inout SurfaceOutput o)
    32.             {
    33.              half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    34.              o.Emission = c.rgb;
    35.              o.Alpha = c.a - tex2D(_Stencil, IN.uv_MainTex).a;
    36.            
    37.             }
    38.         ENDCG
    39.      }
    40. }
    Code (csharp):
    1.  
    2. Shader "OcclusionShader"
    3. {
    4.     SubShader {
    5.             Pass {
    6.                 // Render the Occlusion shader before all
    7.                 // opaque geometry to prime the depth buffer.
    8.                 Tags { "Queue"="Geometry" }
    9.  
    10.                 ZWrite On
    11.                 ZTest LEqual
    12.                 ColorMask RGBA
    13.                 Blend SrcAlpha OneMinusSrcAlpha
    14.                 CGPROGRAM
    15.                 #pragma vertex vert
    16.                 #pragma fragment frag
    17.  
    18.                 #include "UnityCG.cginc"
    19.  
    20.                 struct appdata
    21.                 {
    22.                     float4 vertex : POSITION;
    23.                 };
    24.  
    25.                 struct v2f
    26.                 {
    27.                     float4 position : SV_POSITION;
    28.                 };
    29.  
    30.                 v2f vert (appdata input)
    31.                 {
    32.                     v2f output;
    33.                     output.position = UnityObjectToClipPos(input.vertex);
    34.                     return output;
    35.                 }
    36.  
    37.                 fixed4 frag (v2f input) : SV_Target
    38.                 {
    39.                     return 0.5;
    40.                 }
    41.                 ENDCG
    42.             }
    43.     }
    44. }
    45.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,651
    A soft edge and stencil masks are mutually exclusive. You can have one or the other, not both.

    However if all you need really is just to show the skybox through objects, then I'd setup a second camera that only renders the skybox to a render texture, then have your "hole" be an object using screen space UVs to show the skybox render texture.
     
  3. TobyADev

    TobyADev

    Joined:
    Feb 26, 2016
    Posts:
    4
    Thanks for the quick reply mate. I think you were somewhere in half the threads I read on this stuff :p
    If I was able to sort out a method without stencils, would there be a way to do a soft occlusion material which blends all the way to the skybox or am I going back to the drawing board?