- Home /
Question by
vijaykiran · Jan 26, 2017 at 11:23 AM ·
color changeshader writingfragment
Portray scalar value as color using shader
Hi,
How do I portray a scalar value computed per vertex in a shader as a color in fragment shader? For example, if scalar value computed is between 0 and 1, then it should color the vertex as blue and if it is between 1 and 2, color it green, etc.
I have the following code and it seems to work fine. However, in addition to displaying the colors based on the scalar, they are also changing if the camera position moves. The colors should not vary based on the camera position, but purely on the scalar values computed in the vertex shader.
I hope I posed the question properly. Any advice on what I might be doing wrong?
Thanks!
v2f vert( u2v v )
{
u2v o;
o.uv = v.uv;
float dist = distance(CoilPos, v.pos);
float fieldval = blah-equation-that-uses-'dist'-above
float3 lightval = lerp(0.0, 0.125, fieldval);
o.pos = mul(UNITY_MATRIX_MVP, v.pos);
o.col = float4 (lightval, 1.0);
return o;
}
half4 frag(v2f i) : COLOR
{
half4 src = tex2D(_MainTex, i.uv);
half4 dst = tex2D(_VolumeTex, float2(i.uv.x, 1.0-i.uv.y));
half p = floor(i.col*4);
half i2 = i.col*4 - p;
half4 c = p == 0 ? half4(0, i2, 1, 1) :
p == 1 ? half4(0, 1, 1-i2, 1) :
p == 2 ? half4(i2, 1, 0, 1) :
p == 3 ? half4(1, 1-i2, 0, 1) :
half4(1, 0, 0, 1);
return ((1.0f - dst.a) * src) + 4.0f*dst*c;
}
Comment