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

Transparent pixel overwrites background content

Discussion in 'Shaders' started by r-pedra, Aug 25, 2017.

  1. r-pedra

    r-pedra

    Joined:
    Dec 4, 2015
    Posts:
    104
    Hi,
    We are working on an app intended to let the user paint on something.
    Things are rendered using NGUI.
    Every layer is a UITexture, and we paint in each layer depending on what the user wants.

    We want to save the image, so we are using a RenderTexture to capture all the layers and the original texture and saves its content.

    The problem is that the result in the scene is different from the result in the image we save.
    This first image is captured in the scene view, everything is correct.

    This second image is captured in Gimp and is the result of the save with the RenderTexture.


    As you can see around each element that I paint, it write some transparency that is overwriting the background texture.



    Here is the shader I use for each layer:

    Code (CSharp):
    1. Shader "Custom/MultiplyShader" {
    2.  
    3.     Properties
    4.  
    5.     {
    6.  
    7.         _MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
    8.  
    9.         _BlendTex ("Blended Texture (RGB)  (A = Transparency)", 2D) = "white"
    10.  
    11.     }
    12.  
    13.     SubShader {
    14.  
    15.         Tags
    16.  
    17.         {
    18.  
    19.             "Queue" = "Transparent"
    20.  
    21.             "IgnoreProjector" = "True"
    22.  
    23.             "RenderType" = "Transparent"
    24.  
    25.             "DisableBatching" = "True"
    26.  
    27.         }
    28.  
    29.      
    30.  
    31.         Pass
    32.  
    33.         {
    34.  
    35.             Cull Off
    36.  
    37.             Lighting Off
    38.  
    39.             ZWrite On
    40.  
    41.             Fog { Mode Off }
    42.  
    43.             Offset -1, -1
    44.  
    45.             Blend SrcAlpha OneMinusSrcAlpha
    46.  
    47.  
    48.  
    49.             CGPROGRAM
    50.  
    51.             #pragma vertex vert
    52.  
    53.             #pragma fragment frag
    54.  
    55.             #include "UnityCG.cginc"
    56.  
    57.  
    58.  
    59.  
    60.  
    61.             sampler2D _MainTex;
    62.  
    63.             sampler2D _BlendTex;
    64.  
    65.          
    66.  
    67.             float4 _MainTex_ST;
    68.  
    69.  
    70.  
    71.             struct appdata_t
    72.  
    73.             {
    74.  
    75.                 float4 vertex : POSITION;
    76.  
    77.                 float2 texcoord : TEXCOORD0;
    78.  
    79.                 fixed4 color : COLOR;
    80.  
    81.      
    82.  
    83.             };
    84.  
    85.  
    86.  
    87.             struct v2f
    88.  
    89.             {
    90.  
    91.                 float4 vertex : SV_POSITION;
    92.  
    93.                 half2 texcoord : TEXCOORD0;
    94.  
    95.                 fixed4 color : COLOR;
    96.  
    97.          
    98.  
    99.             };
    100.  
    101.  
    102.  
    103.             v2f o;
    104.  
    105.  
    106.  
    107.             v2f vert (appdata_t v)
    108.  
    109.             {
    110.  
    111.                 o.vertex = UnityObjectToClipPos(v.vertex);
    112.  
    113.                 o.texcoord = v.texcoord;
    114.  
    115.                 o.color = v.color;
    116.  
    117.                 return o;
    118.  
    119.             }
    120.  
    121.              
    122.  
    123.             fixed4 frag (v2f IN) : SV_Target
    124.  
    125.             {
    126.  
    127.                 half4 color = tex2D(_MainTex, IN.texcoord);
    128.  
    129.                 half4 colorBlend = tex2D(_BlendTex, IN.texcoord);
    130.  
    131.                 color.a = color.a * colorBlend.a;
    132.  
    133.              
    134.  
    135.                 if(colorBlend.a != 0){
    136.  
    137.                     color.rgb = color.rgb * colorBlend.rgb;
    138.  
    139.                 }
    140.  
    141.                 return color;
    142.  
    143.             }
    144.  
    145.             ENDCG
    146.  
    147.         }
    148.  
    149.     }
    150.  
    151.  
    152.  
    153.     SubShader
    154.  
    155.     {
    156.  
    157.         LOD 100
    158.  
    159.  
    160.  
    161.         Tags
    162.  
    163.         {
    164.  
    165.             "Queue" = "Transparent"
    166.  
    167.             "IgnoreProjector" = "True"
    168.  
    169.             "RenderType" = "Transparent"
    170.  
    171.             "DisableBatching" = "True"
    172.  
    173.         }
    174.  
    175.      
    176.  
    177.         Pass
    178.  
    179.         {
    180.  
    181.             Cull Off
    182.  
    183.             Lighting Off
    184.  
    185.             ZWrite On
    186.  
    187.             Fog { Mode Off }
    188.  
    189.             Offset -1, -1
    190.  
    191.             //ColorMask RGB
    192.  
    193.             Blend SrcAlpha OneMinusSrcAlpha
    194.  
    195.             ColorMaterial AmbientAndDiffuse
    196.  
    197.          
    198.  
    199.             SetTexture [_MainTex]
    200.  
    201.             {
    202.  
    203.                 Combine Texture * Primary
    204.  
    205.             }
    206.  
    207.  
    208.  
    209.         }
    210.  
    211.     }
    212.  
    213.  
    214.  
    215. }
    216.  
    217.  


    Do you have any idea?


    EDIT:
    If I display the Texture2D I use when doing EncodeToPNG, it looks like that:

    This is correct, but then the png looks like that:


    Notice the white things at the bottom of the aircraft? That's transparency that was apparently not displayed when looking at the Texture2D in the Editor
     
    Last edited: Aug 25, 2017
  2. r-pedra

    r-pedra

    Joined:
    Dec 4, 2015
    Posts:
    104
    I solved it by changing Blend SrcAlpha OneMinusSrcAlpha to Blend SrcAlpha OneMinusSrcAlpha, OneMinusDstAlpha One