Get an 20% OFF using the RELEASE code on your book purchase. For a limited time.

Introduction to the shader programming language

4.0.3. Adding transparency in Cg or HLSL

This post is also available in…

In this section, we will add Blending so that our shader has a defined Alpha channel. In the previous configuration of USB_simple_color, we added color to change the tint of the texture. Now, it is worth mentioning that the color property has four channels (RGBA), however, if we modify the Alpha channel of the color from the Inspector, we will notice that this does not generate any change, why is this? Mainly because it does not have Blending properties. For our shader to be affected by transparency we must: configure the RenderType as transparent, add the Queue with transparency and add the Blending type that we want to use.

Shader “USB / USB_simple_color”
{
    Properties
    {
        _MainTex (“Texture”, 2D) = “white” {}
        _Color (“Texture Color”, Color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Tags { “RenderType”=”Transparent”  “Queue”=”Transparent” }
        Blend SrcAlpha OneMinusSrcAlpha
        LOD 100

        Pass {} ...
    }
}

If we save our shader and go back to Unity, we can now change the Alpha channel color, consequently, this will affect the transparency of the texture in the material.

Follow us to stay informed about all the latest news, updates, and more.

Join the group to share your experiences with other developers.

Subscribe to our channel and keep learning game dev!

jettelly-logo

Jettelly Team

We are a team of indie developers with more than 9 years of experience in video games. As an independent studio, we have developed Nom Noms in which we published with Hyperbeard in 2019. We are currently developing The Unity Shader Bible.

Follow us on our social networks.