user5790809
user5790809

Reputation:

Unity 2D - deactivate addition of A values

how can i deactivate addition of A values (ARGB) in Unity? I have a fugurine from multipleparts. When the parts overlap, opacity is added like on pictures. Thanksenter image description here

Upvotes: 1

Views: 61

Answers (1)

Kleber
Kleber

Reputation: 1065

I found a Shader that kida do the job:

Shader "Custom/FlatTransparent" {
    Properties {
        _Color("Main Color", Color) = (1,1,1,1)
        _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
    }

    SubShader {
        Tags{ "Queue" = "Transparent" "IgnoreProjector" = "False" "RenderType" = "Transparent" }

        Pass {
            ColorMask A
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Color;

            float4 vert(float4 vertex : POSITION) : SV_POSITION {
                return UnityObjectToClipPos(vertex);
            }

            fixed4 frag() : SV_Target {
                return _Color;
            }
            ENDCG
        }

        Pass {
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            sampler2D _MainTex;
            fixed4 _Color;

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata v) {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target{
                fixed4 col = _Color * tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }

    Fallback "Diffuse"
}

To work properly, three conditions need to be fulfilled:

  • the Sprites have to have their Material set to a Material with this shader
  • the Sprites need to be in different z coordinates
  • and the alpha is set inside the shader, not in the sprite, so every sprite have the same alpha

I'm not very good at shaders, so I couldn't figure out how to not have those two final restraints. If someone can improve it, please share.

Sources: Transparent Solid Color Shader, ShaderLab: Blending

Upvotes: 1

Related Questions