Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Can't add transparency to sprite with shader

Discussion in 'Shaders' started by RendergonPolygons, Dec 7, 2020.

  1. RendergonPolygons

    RendergonPolygons

    Joined:
    Oct 9, 2019
    Posts:
    98
    Hello all

    I have a texture (it's what the camera of a mobile phone sees - backgroundcamera texture) and I render a snapshot of it on a sprite. I'm am trying to apply transparency to the pixels of the sprite. Problem is the sprite always shows the original camera background image regardless what I try (set alpha to 0, change the colour of the camera background image, etc).

    In the sprite object I define:
    • thisSprite = Sprite.Create(rgbBackgroundTexResult, new Rect(0.0f, 0.0f, rgbBackgroundTexResult.width, rgbBackgroundTexResult.height), new Vector2(0.5f, 0.5f), 100);
    • private static readonly string _currentSpriteTexturePropertyName = "_CurrentSpriteTexture";
    • m_SpriteShaderMaterial.SetTexture(_currentSpriteTexturePropertyName, _spriteTexture);
    • and I assign CameraBackgroundWithSomeTransparency Shader to the material of the sprite (m_SpriteShaderMaterial).

    Can someone please shed some light what I'm doing wrong? I'm on the built-in render pipeline.

    Thanks a bunch for your help !

    Code (CSharp):
    1. Shader "Unlit/CameraBackgroundWithSomeTransparency "
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("_CurrentSpriteTexture", 2D) = "white" {}
    6.         _Color("Color",Color)= (0.2,1,0.2,1)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.         ZWrite Off
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv : TEXCOORD0;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv : TEXCOORD0;
    30.                 float4 vertex : SV_POSITION;
    31.             };
    32.  
    33.             sampler2D _MainTex;
    34.             fixed4 _Color;
    35.             float4 _MainTex_ST;
    36.  
    37.             v2f vert (appdata v)
    38.             {
    39.                 v2f o;
    40.                 o.vertex = UnityObjectToClipPos(v.vertex);
    41.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    42.  
    43.                 return o;
    44.             }
    45.             fixed4 frag (v2f i) : COLOR
    46.             {
    47.                 fixed4 col = tex2D(_MainTex, i.uv);
    48.                 col.a = 0;
    49.                 //col.rgb *= _Color;//I tried this but it didn't change any colors for the background image
    50.  
    51.                 return col;
    52.             }
    53.             ENDCG
    54.         }
    55.     }
    56. }
     
    Last edited: Dec 9, 2020