- Home /
Applying two colors to an object in a shader
Hi all,
I am currently trying to apply two colors to a single mesh/texture combination. I want the user to be able to customize two separate colors into a hair mesh. For example, the user could select black hair with ends that are red. It is also important to keep the height at which the color change occurs and the two colors variable. Right now, I have a shader that applies these colors to the mesh properly, but whenever the mesh rotates, the color line remains at the same position (the ends look like they grow/shorten when the head tilts upward/downward).
Is there a way to make it so the color stays in the same position on the mesh even when it rotates? This is what I have so far:
Shader "Hair" {
Properties {
_MainTex ("Diffuse Texture", 2D) = "white" {}
_BaseColor ("Base Color", Color) = (1,1,1,1)
_EndColor ("End Color", Color) = (.5,.5,.5,1)
_CHeight ("Color Change Height", Range(10.0, 50.0)) = 10.0
_Gradient ("Gradient", Range(0.5, 15)) = 1
}
SubShader {
Tags {"QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Opaque"}
Pass {
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
//pragmas
#pragma vertex vert
#pragma fragment frag
//user variables
uniform sampler2D _MainTex;
uniform sampler2D _DetailTex;
uniform half4 _MainTex_ST;
uniform half4 _DetailTex_ST;
uniform half4 _BaseColor;
uniform half4 _EndColor;
uniform half _CHeight; //Height where the color changes begins
uniform half _Gradient; //Range for the color gradient
//base input structs
struct vertexInput {
half4 vertex : POSITION;
half4 texcoord : TEXCOORD0;
};
struct vertexOutput {
half4 pos : SV_POSITION;
half4 tex : TEXCOORD0;
half4 vertPos : TEXCOORD1;
half vertBottom;
};
//vertex function
vertexOutput vert(vertexInput v) {
vertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.tex = v.texcoord;
o.vertPos = v.vertex;
o.vertBottom = _CHeight - _Gradient ;
return o;
}
//fragment function
half4 frag(vertexOutput i) : COLOR
{
fixed distanceFromNeutral = saturate( ( i.vertPos.y - i.vertBottom ) / _Gradient );
half4 colorGradient = lerp( half4( _EndColor.xyz, 1.0 ), half4( _BaseColor.xyz, 1.0 ), distanceFromNeutral );
half4 tex = half4( tex2D( _MainTex, _MainTex_ST.xy * i.tex.xy + _MainTex_ST.zw ) * colorGradient );
half alpha = tex.a;
return half4( tex.xyz , alpha);
}
ENDCG
}
}
//FallBack "Diffuse"
}
@Wisearn Thank you for the reference! It helped immensely!
$$anonymous$$oved my comment to an answer in case it solved your issue entirely, in which case you can mark your issue as resolved by accepting the answer.
Answer by Wisearn · Oct 27, 2014 at 03:49 PM
http://answers.unity3d.com/questions/51283/is-there-a-way-to-make-a-gradient-material-white-t.html
The answer to this question might help you.
$$anonymous$$odified shader code using reference from Wisearn. Texture code using vertex colors set via a script, with alpha applied.
Shader "Hair" {
Properties {
_$$anonymous$$ainTex ("Diffuse Texture", 2D) = "white" {}
}
SubShader {
Tags {"QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Opaque"}
Pass {
Blend SrcAlpha One$$anonymous$$inusSrcAlpha
ZWrite Off
CGPROGRA$$anonymous$$
//pragmas
#pragma vertex vert
#pragma fragment frag
//user variables
uniform sampler2D _$$anonymous$$ainTex;
uniform half4 _$$anonymous$$ainTex_ST;
//base input structs
struct vertexInput {
half4 vertex : POSITION;
half4 texcoord : TEXCOORD0;
half4 colors : COLOR;
};
struct vertexOutput {
half4 pos : SV_POSITION;
half4 tex : TEXCOORD0;
half3 vertexColor : Color;
};
//vertex function
vertexOutput vert(vertexInput v) {
vertexOutput o;
o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
o.tex = v.texcoord;
o.vertexColor = v.colors.xyz;
return o;
}
//fragment function
half4 frag(vertexOutput i) : COLOR
{
half4 tex = half4( tex2D( _$$anonymous$$ainTex, _$$anonymous$$ainTex_ST.xy * i.tex.xy + _$$anonymous$$ainTex_ST.zw ) * half4( i.vertexColor, 1.0 ) );
half alpha = tex.a;
return half4( tex.xyz , alpha);
}
ENDCG
}
}
//FallBack "Diffuse"
}