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
            } 
        }
    }
}


Friday, November 16, 2012

OpenFeint servers shutting down Dec. 14, 2012

Today GREE announced OpenFeint servers will be shutting down on December 14, 2012.  This very truncated transition period gives developers less than 1 month to replace or remove OpenFeint functionality to their existing games.  While this would be short notice in the best of times, giving 1 months notice during the December time period is, the hands down, worst possible time they could make this move.  Right now most mobile developers are hurriedly working to get new games and updates finished and submitted in time for the big holiday rush and before the App Store locks down for the winter holidays.

If you're wondering what OpenFeint is, in short it's a predecessor to Game Center.  Most notably developers used it for leaderboards, achievements and adding other social aspects to their games before Game Center came along.  While OpenFeint adoption has dropped since Game Center was introduced, many older (but potentially popular) apps still use OpenFeint because their developers haven't felt the need to switch it over to Game Center.  Developers must now make the choice to drop their new projects to update their older games, or face the specter of allowing their older games to use a service that no longer exists.  To really twist the knife in the whole experience, GREE informed developers that failure to remove the OpenFeint SDK from your games by December 14, 2012, "violates the terms of the OpenFeint Developer Agreement".  GREE is pushing developers to migrate to their OpenFeint games to the GREE Platform however I'm very suspect on how many developers will choose to migrate to that service given this experience.

While I removed OpenFeint from my iOS games awhile ago, I'm still using it for the current version of Ninja Hamster Rescue on Android.  This raises a new question, are they any other services out there that provide leaderboard/achievement type functionality for Android, or was OpenFeint the only one?

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. :)