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. :)
This is Perfect, what I was looking for.
ReplyDeleteThank you.
thanks for sharing, plz add lightmap support too...
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI'm making a point-click adventure game and your shader has been really helpful. Thanks a lot for sharing!
ReplyDeleteI have a problem though, and I'd appreciate your help on that: I want to read the initial color of a mesh whose material I'm using your shader on.
The command I'm using in the Start() function is:
Color intialColor = getComponent().material.color;
However, I always get the value of (1,1,1,1), which is the "Main Color" value you have defined in your shader.
Is it possible to read the actual initial color of a mesh?
Thanks in advance,
Vasilis
Unfortunately, HTML doesn't let me use the "less than" and "greater than" symbols... GetComponent is followed by "Renderer".
DeleteSorry for spamming your thread :)
I found a way out, eventually. It was a Unity bug having nothing to do with your shader after all. The solution came through this block of assignments:
ReplyDeleteRenderer rend = GetComponent[Renderer] ();
Color initialColor = rend.material.color;
The Color assignment must take place immediately after the Renderer one, otherwise Color is set to (1,1,1,1).
Thank you so much! I modified your code to have transparency too!
ReplyDeleteThank you so much! I modified your code to have transparency too!
ReplyDeleteI am very much pleased with the contents you have mentioned. I wanted to thank you for this great article. I enjoyed every little bit part of it and I will be waiting for the new updates.
ReplyDeletemobile game developers
Thank you so much, That's all I needed.
ReplyDelete