- Home /
Add Colour tint onto another shader?
Been messing with shaders recently combining a few things and thought I was getting the hang of this (simple things, like allowing bump maps to other shaders). What I want to do is colour materials at runtime. Looking at the unity documentation this is supposed to be the simplest of the simple.
Shader "Tutorial/Basic" {
Properties {
_Color ("Main Color", Color) = (1,0.5,0.5,1)
}
SubShader {
Pass {
Material {
Diffuse [_Color]
}
Lighting On
}
}
}
That is how colour is added. I have tried and tested over and over and I can't seem to get it to work in my shader. Looking at it the one I'm editing doesn't even have a pass that I can see. So my question is quite Simply how do I add this onto this script here?
Shader "Custom Shaders/PlanetBlend" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Texture (RGB)", 2D) = "white" {}
_SliceGuide ("_SliceGuide (RGB)", 2D) = "white" {}
_SliceAmount ("Slice Amount", Range(0.0, 1.0)) = 0.5
//New
_BumpMap ("Bumpmap", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
Cull Off
//Lighting Off
CGPROGRAM
#pragma surface surf Lambert addshadow
struct Input {
float2 uv_MainTex;
float2 uv_SliceGuide;
float2 uv_BumpMap;
float _SliceAmount;
};
sampler2D _MainTex;
sampler2D _SliceGuide;
sampler2D _BumpMap;
float _SliceAmount;
void surf (Input IN, inout SurfaceOutput o) {
clip(tex2D (_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback "Diffuse"
}
Many thanks. I don't need a direct answer, but a educational nod to where I'm going about this wrong would be nice. :)
Answer by syclamoth · Nov 01, 2011 at 03:14 PM
In this bit-
sampler2D _MainTex;
sampler2D _SliceGuide;
sampler2D _BumpMap;
float _SliceAmount;
You need to add the line
float4 _Color;
and from then on, you can use the Color value you input in the top of the shader inside your CG bit! In this case, it'd go something like this-
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
Which would multiply the main texture with the tint colour.
(Small disclaimer- I am hardly an expert on CG scripting, but I know how to hack a few things together)
Thanks for your help but I still can not seem to get it working. Played with both lines lots but nothing takes.
Would I need to add this in here? Or am I on the wrong track still.Have been messing with it regardless.
sampler2D _$$anonymous$$ainTex;
sampler2D _SliceGuide;
sampler2D _Bump$$anonymous$$ap;
float _Color; //This bit here? Inside the sample section?
float _SliceAmount;
Got anymore advice? I'm quite baffled.
No, it specifically has to be a float4. NOT a float. There's an important difference- a float only has one channel, where a float4 has 4 (since it represents a colour value).
You can use a half4 if you don't want to use as much memory (at the cost of accuracy, of course).
Thanks for explaining the float issue. Only problem is just this one line which doesn't take.
o.Albedo = tex2D ($$anonymous$$ainTex, IN.uv$$anonymous$$ainTex).rgb * _Color;
And thanks for sticking with me throughout these questions :)
I don't really know how to make those go together properly- just try a few permutations. Now that you've got your _Color value in there, you can do whatever operations you need to on it- so long as they fit within the syntax of the CG shader language (which I'm honestly not that familiar with).
Answer by jeffpk · Jun 26, 2012 at 04:58 PM
To multiply the rgb, use .rgb in both cases
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color.rgb;
This is called "swizzling' and the two operands must be swizzled compatibly (in this case three elements in each)
BUT I'm not sure multiplying is the right way to tint something. He'e a gedanken experiment to show why...
(r,g,b) * (0,1,0) = (0,g,0)
See? Rather then tinting its totally removes (sets to black) any color that isnt green.
What I think you are reaching for is something like an averaging of the hues, which would require color space conversion. (Im working on the same problem right now.)
Answer by BlackHunter · Oct 28, 2012 at 01:19 PM
eror in colorcorrection
Assets/Standard Assets/Image Effects (Pro Only)/ColorCorrectionEffect.cs(6,14): error CS0101: The namespace `global::' already contains a definition for `ColorCorrectionEffect'
Don't post answer with your own question.
moderators with enough karma please delete this useless and non-relevant post.
Your answer
Follow this Question
Related Questions
Colour tinting layered textures with transparencies. 1 Answer
single-pass particle render shader 1 Answer
How do you send output from one shader pass to another? 0 Answers
Animation clip and color curves 0 Answers
Multi materials for one mesh 0 Answers