- Home /
Adding tint color variable to shader
hey
I'm a bit confused on shaders and programming them. I've looked at some other questions like this on unity Q&A but I still didn't totally understand what to do.
I'm trying to add a tint colour variable to the shader Particles/Additive (soft) but don't know how to do this. I have the source code here:
Shader "Particles/Additive (Soft)" {
Properties {
_MainTex ("Particle Texture", 2D) = "white" {}
_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
}
Category {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend One OneMinusSrcColor
ColorMask RGB
Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}
// ---- Fragment program cards
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_particles
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 _TintColor;
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
#ifdef SOFTPARTICLES_ON
float4 projPos : TEXCOORD1;
#endif
};
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#ifdef SOFTPARTICLES_ON
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
#endif
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
sampler2D _CameraDepthTexture;
float _InvFade;
fixed4 frag (v2f i) : COLOR
{
#ifdef SOFTPARTICLES_ON
float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
float partZ = i.projPos.z;
float fade = saturate (_InvFade * (sceneZ-partZ));
i.color.a *= fade;
#endif
half4 prev = i.color * tex2D(_MainTex, i.texcoord);
prev.rgb *= prev.a;
return prev;
}
ENDCG
}
}
// ---- Dual texture cards
SubShader {
Pass {
SetTexture [_MainTex] {
combine texture * primary
}
SetTexture [_MainTex] {
combine previous * previous alpha, previous
}
}
}
// ---- Single texture cards (does not do particle colors)
SubShader {
Pass {
SetTexture [_MainTex] {
combine texture * texture alpha, texture
}
}
}
}
}
So I'm trying to add a tint colour variable to that. I'm not sure if its a large or small job to add it to this shader. Thanks for all replies in advance.
EDIT:
Thanks for your reply cgcookie, your upcoming shader vids look good too, unfortunately the code your gave me produced these errors: see pic
Answer by cgcookie · Jan 07, 2013 at 08:30 PM
Hi, we will be covering how to do this soon in our new series: http://forum.unity3d.com/threads/165056-Noob-to-Pro-Shader-writing-in-Unity-4
To answer your question now: You need to have a color property for the tinting, a definition to get that property to the CG section, and a multiplier to do the actual tinting.
Not a lot of work actually ;)
Here is your ammended code:
Shader "Particles/Additive (Soft)" {
Properties {
_MainTex ("Particle Texture", 2D) = "white" {}
_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
_TintColor("Color Tint", Color) = (1,1,1,1)
}
Category {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend One OneMinusSrcColor
ColorMask RGB
Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}
// ---- Fragment program cards
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_particles
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 _TintColor;
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
#ifdef SOFTPARTICLES_ON
float4 projPos : TEXCOORD1;
#endif
};
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#ifdef SOFTPARTICLES_ON
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
#endif
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
sampler2D _CameraDepthTexture;
float _InvFade;
fixed4 frag (v2f i) : COLOR
{
#ifdef SOFTPARTICLES_ON
float sceneZ = LinearEyeDepth (UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos))));
float partZ = i.projPos.z;
float fade = saturate (_InvFade * (sceneZ-partZ));
i.color.a *= fade;
#endif
half4 prev = i.color _ColorTint * tex2D(_MainTex, i.texcoord);
prev.rgb *= prev.a;
return prev;
}
ENDCG
}
}
// ---- Dual texture cards
SubShader {
Pass {
SetTexture [_MainTex] {
combine texture * primary
}
SetTexture [_MainTex] {
combine previous * previous alpha, previous
}
}
}
// ---- Single texture cards (does not do particle colors)
SubShader {
Pass {
SetTexture [_MainTex] {
combine texture * texture alpha, texture
}
}
}
}
}
I found out what the problem was, on line 76 it should be;
half4 prev = i.color _TintColor tex2D(_$$anonymous$$ainTex, i.texcoord);