1

I'm trying to make a simple diffuse shader in Unity, though with a global alpha value. So you add a texture (without alpha), and then define an alpha value in the shader (float between 0 and 1.) The entire texture then is transparent.

This is what I already have, it's just the most basic diffuse shader you can image, though I'm stuck at adding the alpha.

Properties
{
    _Alpha ("Alpha", Range (0.0,1.0)) = 0.0 
    _MainTex ("Base (RGB) Transparency (A)", 2D) = "" { }
}

SubShader
{
    Pass
    {
        SetTexture [_MainTex] { combine texture }
    }
} 

Thanks,

1 Answer 1

1

It's been a while since I've done any of this, but I think you want something like:

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

    Pass
    {
        Blend SrcAlpha OneMinusSrcAlpha

        SetTexture [_MainTex] {
           constantColor (1, 1, 1, [_Alpha])
           combine texture * constant
        }
    }
} 

Note that you can download the built-in shaders for unity from here, they're pretty useful to learn from.

Sign up to request clarification or add additional context in comments.

3 Comments

this almost works! it has to be "constantColor (1, 1, 1, [_Alpha])" otherwise the texture is black. Thanks!
Awesomeness, I'm starting to understand this shaderlab stuff, one of the hardest things in Unity I think :P
That isn't semantically the best; I'd either multiply the entire color, or just do a calculation for A and leave RGB alone: Combine texture, texture * constant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.