Search Unity

Question Shader effect shows black when transparency alpha set to 0 on skybox

Discussion in 'Image Effects' started by bbudian0, Dec 14, 2021.

  1. bbudian0

    bbudian0

    Joined:
    Sep 11, 2015
    Posts:
    7
    I'm not exactly sure how to title this (probably because I don't know the real issue.

    I'm thinking it's something how the clear flags are handled but I'm not sure.

    Here is the effect I have right now. I'm tracking objects and showing a glow around them as they approach the plane. It works fine if I have something behind the plane but when It's just the skybox it paints over what was there before rather than showing just the skybox behind the desired effect.
    OpaqueBlendEnabled.gif

    Here is the relevant shader code:

    Code (CSharp):
    1. Shader "Unlit/BoundaryField"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.         _EffectColor ("Effect Color", Color) = (1,1,1,1)
    8.         _Distance ("Max Effect Distance", float) = 1
    9.         [Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull", Float) = 2
    10.         [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("Src Blend", Float) = 1
    11.         [Enum(UnityEngine.Rendering.BlendMode)] _DestBlend ("Dest Blend", Float) = 0
    12.     }
    13.     SubShader
    14.     {
    15.         Tags
    16.         {
    17.             "RenderType"="Opaque"
    18.             "RenderPipeline" = "UniversalPipeline"
    19.             "Queue" = "Geometry"
    20.         }
    21.         Pass
    22.         {
    23.             LOD 100
    24.          
    25.             Blend [_SrcBlend] [_DestBlend]
    26.             Cull [_Cull]
    27.             ZWrite On
    28.  
    29.             HLSLPROGRAM
    30.             #pragma vertex vert
    31.             #pragma fragment frag
    32.  
    33.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    34.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
    35.  
    36.             struct appdata
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 float2 uv : TEXCOORD0;
    40.             };
    41.  
    42.             struct v2f
    43.             {
    44.                 float2 uv : TEXCOORD0;
    45.                 float4 vertex : SV_POSITION;
    46.             };
    47.  
    48.             sampler2D _MainTex;
    49.             float4 _MainTex_ST;
    50.             float _Distance;
    51.             float4 _Color;
    52.             float4 _EffectColor;
    53.             vector _Positions[1000];
    54.             float _ArraySize;
    55.  
    56.             v2f vert(appdata v)
    57.             {
    58.                 v2f o;
    59.                 o.vertex = TransformObjectToHClip(v.vertex.xyz);
    60.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    61.                 return o;
    62.             }
    63.  
    64.             half4 frag(v2f i) : SV_Target
    65.             {
    66.                 float2 uv = i.vertex.xy / _ScaledScreenParams.xy;
    67.                 #if UNITY_REVERSED_Z
    68.                 real depth = SampleSceneDepth(uv);
    69.                 #else
    70.                     real depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(uv));
    71.                 #endif
    72.  
    73.                 float3 pixelWorldPos = ComputeWorldSpacePosition(uv, depth, UNITY_MATRIX_I_VP);
    74.  
    75.                 float4 color = tex2D(_MainTex, i.uv) * _Color;
    76.  
    77.                 for (int iObject = 0; iObject < _ArraySize; iObject++)
    78.                 {
    79.                     vector position = _Positions[iObject];
    80.                     float dist = distance(pixelWorldPos.xyz, position.xyz);
    81.  
    82.                     if (dist <= _Distance)
    83.                     {
    84.                         color = lerp(color, _EffectColor, 1 -(dist / _Distance));
    85.                     }
    86.                 }
    87.  
    88.                 return color;
    89.             }
    90.             ENDHLSL
    91.         }
    92.     }
    93. }
     
    Last edited: Dec 14, 2021