[CLOSED] Custom shader : Transparency on a Mesh renderer

Hello unity fellow developers.

Here is my question : how to make a custom shader displaying some transparency on a mesh renderer ?
I have to do this in a complex shader, but the point is I don’t even succeed doing the basic step of rendering transparent pixels.

Let’s try to do a very simple shader so we will be able to find out what I’m doing wrong :

**Shader "Custom/Gauge" {

    Properties {
        DisplayValue ("Value", Float) = 0
    }

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

        Pass {
            CGPROGRAM
            #pragma target 3.0
            #pragma vertex vert_img
            #pragma fragment frag
            #include "UnityCG.cginc"

            uniform float DisplayValue;

            float4 frag(v2f_img input) : COLOR {

                if (input.uv.x < DisplayValue)
                {
                    return half4(0.0, 1.0, 0.0, 1.0);
                }
                else
                {
                    return half4(0.0, 0.0, 0.0, 0.0);
                }

            }
			
            ENDCG
        }
    }
}**

This is supposed to diplay green until uv.x reach the “display value” (it could be used as a gauge or a loading bar for exemple), but the mesh (a quad) is part green and part black depending the value used, it should be part green and part transparent.

Any idea about why it doesn’t work has I wish it to ?

PS : Forgive my mistakes, english is not my native language.

2 Answers

2

Without more data

input.uv.x < DisplayValue

can never be < 0
So you should probably set DisplayValue initially to 1.0 instead of 0.0 and check if your input to the shader works.

If you replace
if (input.uv.x < DisplayValue)
with
if (input.uv.x < 0.5)

you should be able to locate the problem. Cause at if you don`t set DisplayValue to higher than 0.0 things will never change.

Thanks but that's not the problem. DisplayValue works fine. I've just say something wrong about the shader behavior, my bad : the mesh isn't full green with these color values, of course ; it is part green and part black, depending on the DisplayValue, it should be part transparent instead of part black. I don't see any more data to add to the question, there is no more data : a quad, a material, a shader and that's all. DisplayValue range is of course [0,1], and I set it in the inspector to check the shader. EDIT : I've edited the question to correct it.

Answer provided on the forum :
I need to add the following line after the Tag listing :

Blend SrcAlpha OneMinusSrcAlpha

Now it works perfectly well.