Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice
  2. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

Resolved Video alpha appears shiny in custom shader

Discussion in 'AR' started by IMMcClerin, Apr 12, 2021.

  1. IMMcClerin


    Feb 26, 2019
    Hey team! I've built an AR tracking app and am beginning to implement 3D environments. When the app recognizes the image, the card becomes a window into a 3D room. (If you're interested in some resources: here's a great custom shader someone graciously shared that explains this antichamber effect).

    SITUATION: I've ordered several 2D objects and have begun adding animated fires containing alpha channels. While I've been able to successfully make the alpha transparent, a shiny glow is reflected on each video plane. This makes the edges of the video planes visible and discolors all of the planes behind them. Below is an example of my work in progress.

    You'll notices two lines near the top on the background that are a little darker (those are the video layers). They're outlined in red below.

    PROBLEM: I'm unsure how to get my custom shader to remove those lines so that the alpha channel on the animated fire videos are completely transparent. Utilizing the Standard shader (rendered to Cutout) will successfully remove this glare and restore the true color of the image (example below). However, it's no use to me as it breaks the antichamber effect. The fire exists beyond my tracked object instead of existing within the "window." I'd like to keep everything matted out.

    QUESTION: I've struggled with multiple combinations of blend modes and passes to tweak the antichamber shader so that it will have the same alpha behavior as the "Cutout" render mode. How can I tweak this shader so that the alpha channel on the video layers remain fully transparent?

    Here is the shader as it exists:

    Code (CSharp):
    1. // Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax.
    3. Shader "Custom/Stencil Object" {
    4.     Properties {
    5.         _Color ("Color", Color) = (1,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.         _RefNumber("Reference number", Int) = 1
    10.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    11.     }
    12.     SubShader {
    13.         Tags {"RenderType"="Transparent" "ForceNoShadowCasting"="True"}
    14.         LOD 200
    16.         Stencil {
    17.             Ref [_RefNumber]
    18.             Comp equal
    19.         }
    22.         CGPROGRAM
    23.         // Physically based Standard lighting model, and enable shadows on all light types
    24.         #pragma surface surf Standard fullforwardshadows alpha
    27.         // Use shader model 3.0 target, to get nicer looking lighting
    28.         #pragma target 3.0
    30.         sampler2D _MainTex;
    31.         sampler2D _CutTex;
    33.         struct Input {
    34.             float2 uv_MainTex;
    35.         };
    37.         half _Glossiness;
    38.         half _Metallic;
    39.         fixed4 _Color;
    40.         float _Cutoff;
    42.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    43.         // See for more information about instancing.
    44.         // #pragma instancing_options assumeuniformscaling
    46.             // put more per-instance properties here
    47.         UNITY_INSTANCING_BUFFER_END(Props)
    51.         void surf (Input IN, inout SurfaceOutputStandard o) {
    52.             // Albedo comes from a texture tinted by color
    53.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    54.             float ca = tex2D(_CutTex, IN.uv_MainTex).a;
    55.             o.Albedo = c.rgb;
    56.             // Metallic and smoothness come from slider variables
    57.             o.Metallic = _Metallic;
    58.             o.Smoothness = _Glossiness;
    59.             o.Alpha = c.a;
    60.         }
    61.         ENDCG
    62.     }
    63.     FallBack "Diffuse"
    64. }
    Note: this question has also posted in the Unity Answers forum.
    Last edited: Apr 16, 2021
  2. IMMcClerin


    Feb 26, 2019
    SOLVED: change the optional parameter on line 24 from