Showing posts with label combiner. Show all posts
Showing posts with label combiner. Show all posts

Monday, November 19, 2012

Creating a Colored Unlit Alpha Texture Shader for Unity

Last week I wrote about Creating a Colored Unlit Texture Shader for Unity.  I've used the shader from that post quite a lot on my new game Match'n Flip; however I recently realized it didn't work with alpha textures (textures with transparency).  So I decided to make a short post about creating the same kind of colored shader for a texture with alpha.

Applying a color combine with an alpha texture opens up some interesting possibilities, because you can control the opacity of the texture by modifying the color multiplier's alpha value.  Below is a picture of the default Unity Unlit Transparent shader, and on the right are 2 examples of my modified colored unlit transparent shader.  The opaque section of the object on the top right is semi-transparent because I set the color multiplier's alpha value to 50%(0.5), which the object on the bottom left is full opacity but has a red tint applied.


The shader itself is pretty simple, I modified Unity's Unlit-Alpha shader to support a Color property and handle the color combine operation.  The shader will appear in the shader dropdown menu under NAKAI/Unlit/Transparent Colored.  In case you're wondering, the Unlit-Alpha shader was written using Unity's ShaderLab Shader Syntax, I highly recommend reading up on the operations involved.  As before, you are free to copy and use this shader as you wish, Enjoy!

// Copied from Unlit-Alpha.shader, but adds color combine(tinting)
// Unlit alpha-blended shader.
// - no lighting
// - no lightmap support
// - Added per-material color

// Change this string to move shader to new location
Shader "NAKAI/Unlit/Transparent Colored" {
    Properties {
        // Adds Color field we can modify
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    
    SubShader {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        LOD 100
    
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha 

        Pass {
            Lighting Off
            SetTexture [_MainTex] { 
                // Sets _Color as the 'constant' variable
                constantColor[_Color]
                
                // Multiplies color (in constant) with texture
                combine constant * texture
            } 
        }
    }
}


Wednesday, November 14, 2012

Creating a Colored Unlit Texture Shader for Unity

When dealing with Unity materials, one easy and convenient feature is the ability to change the Main Color of a shader to tint the color of the texture it uses.  This can be used for a lot of different situations, like changing the color of a tile on a selection grid or changing the background color of a 2d game, to name a few.

Unfortunately I found the ability to specify a shader color missing in Unity's Unlit Texture shader.  The Unlit Texture shader isn't affected by lights, which is very convenient when you don't want to include lights in a scene or want to manually control the brightness of a texture.  It makes sense why the Unlit Texture shader doesn't include the color combiner support, the Unlit Texture shader is the simplest and fastest shader they have, and adding a color combiner would add extra instructions which would rarely be used.  Since I needed the ability to color tint an Unlit Texture for my new project, I decided to extend the existing Unlit Texture shader to support a color multiply operation.

The source for the new shader is below, you are free to copy it for your own needs*.  Use the 'Create--Shader' command in unity, then paste this code into the new file.  The shader will appear in the shader dropdown menu under NAKAI/Unlit/Texture Colored.  In case you're wondering, the Unlit Texture shader was written using Unity's ShaderLab Shader Syntax and I added a few extra lines to support the color combine operation.  Here's a picture of it in action, Enjoy!




// Unlit color shader. Very simple textured and colored shader.
// - no lighting
// - no lightmap support
// - per-material color

// Change this string to move shader to new location
Shader "NAKAI/Unlit/Texture Colored" {
    Properties {
        // Adds Color field we can modify
        _Color ("Main Color", Color) = (1, 1, 1, 1)        
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 100
        
        Pass {
            Lighting Off
            
            SetTexture [_MainTex] { 
                // Sets our color as the 'constant' variable
                constantColor [_Color]
                
                // Multiplies color (in constant) with texture
                combine constant * texture
            } 
        }
    }
}


*You are free to use this for your own games.  Please do not sell it on the Unity asset store or anywhere else. :)