Search Unity

Shader problem: Transparency removed with Opacity changes

Discussion in 'General Graphics' started by matt7188, Jun 19, 2022.

  1. matt7188

    matt7188

    Joined:
    Apr 10, 2018
    Posts:
    9
    Hello! I am trying to make a shader that takes in 2 textures and blends them together, and then the final output needs to have opacity as a variable. To that end, I have gotten it all worked out, EXCEPT, whenever one of the inputs has transparency, the opacity element removes the transparency and replaces it with this: upload_2022-6-19_16-40-50.png

    my code so far:

    Code (CSharp):
    1. Shader "Custom/TonyShader2D"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _OffTex ("Other Texture", 2D) = "white" {}
    7.         _Strength ("Blend Strength Between Textures", Range(0,1)) = 1
    8.         _Opacity ("Opacity", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "Queue"="Transparent"  "RenderType"="Transparent" }
    13.  
    14.          Blend SrcAlpha OneMinusSrcAlpha
    15.         Pass
    16.         {
    17.         CGPROGRAM
    18.  
    19.         #pragma vertex vert
    20.         #pragma fragment frag
    21.        
    22.         #include "UnityCG.cginc"
    23.  
    24.  
    25.         sampler2D _MainTex;
    26.         sampler2D _OffTex;
    27.  
    28.         half _Opacity;
    29.         half _Strength;
    30.  
    31.         float4 _MainTex_ST;
    32.         float4 _OffTex_ST;
    33.  
    34.         struct Input
    35.         {
    36.             float2 uv_MainTex;
    37.             float2 uv_OffTex;
    38.         };
    39.  
    40.  
    41.             struct v2f
    42.             {
    43.                 float4 vertex : SV_POSITION;
    44.                 float2 uv : TEXCOORD0;
    45.                 float2  uv2 : TEXCOORD1;
    46.             };
    47.  
    48.             v2f vert(appdata_base v)
    49.             {
    50.                 v2f o;
    51.                 o.vertex = UnityObjectToClipPos(v.vertex);
    52.                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
    53.                 o.uv2 = TRANSFORM_TEX (v.texcoord, _OffTex);
    54.                 return o;
    55.             }
    56.  
    57.  
    58.         //void surf (Input IN, inout SurfaceOutputStandard o)
    59.         float4 frag(v2f i) : SV_Target
    60.         {
    61.             fixed4 c = tex2D (_MainTex, i.uv);
    62.             //c.a= _Opacity;
    63.             fixed4 c2 = tex2D (_OffTex, i.uv2);
    64.             //c2.a= _Opacity;
    65.             fixed4 c3 = lerp (c, c2, _Strength);
    66.             c3.a= _Opacity;
    67.             return c3;
    68.         }
    69.         ENDCG
    70.     }
    71. }}

    Any Ideas?
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,929
    This is to be expected: you're throwing away your texture's alpha channel and entirely replacing with your _Opacity value.

    Code (CSharp):
    1. c3.a = _Opacity;
    You should multiply (modulate) your alpha channel by _Opacity instead:

    Code (CSharp):
    1. c3.a *= _Opacity;
     
  3. matt7188

    matt7188

    Joined:
    Apr 10, 2018
    Posts:
    9
    Thank you!!