- Home /
Can vertex colours be SET in a surface shader? Or must it be done through code?
As i said, I would very much like to know if this is at all possible, I have researched thoroughly and have only found shader examples which READ vertex colours and do something like multiply them with the diffuse.
I want to do the opposite, and SET vertex colours in my surface shader after some appropriate math has been done, then read the new value to multiply it with my diffuse.
Specify do you want to save the new colors to be used elsewhere and where. Or do you just want to calculate the vertex color every time?
Edit: Re-read the question a couple of times. Doesn't make much sense to me as the both paragraphs essentially mean the same thing.
Essentially, I'm trying to calculate a vertex colour for each vertex by sampling the world pos of each vert and doing a dot product calculation against a predefined VectorPos which is sent to the shader via scripting. I will be mulitplying a colour (also sent to the shader via scripting with the result of this dot product. I then want to apply the resulting colour to that vertex.
The resulting colour will most likely need a dot clamped at 0-1 at least. I intend to then blend the pixels using the vert colours. I could also use Saturate() here ins$$anonymous$$d of a clamped dot product if I'm not mistaken...
It's simple really....(in pseudo)
Ins$$anonymous$$d of...(which there are examples of everywhere)
col = vertex color;
pixel = diffuse * col;
I'm wondering whether you can do this...
_SomeColour (1, 1, 1, 1);
newCol = some fancy math * _SomeColour;
vertex color = newCol;
pixel = diffuse * newCol;
All I wanna know is whether I can do something like this...
v.color.rgb = newCol;
Answer by Dep · Mar 14, 2014 at 03:11 AM
Now there is no reason to use surface shader to set vertex colors, but here's a surface + vertex shader that does the job.
Shader "Custom/PositionShade" {
Properties {
_MainTex ("Base (RGB)", 2D) = "black" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert vertex:vert
void vert (inout appdata_full v)
{
float3 worldPos = mul (_Object2World, v.vertex).xyz;
v.color.r = worldPos.x;
v.color.g = worldPos.y;
v.color.b = worldPos.z;
}
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float4 color : COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Albedo.r += IN.color.r;
o.Albedo.g += IN.color.g;
o.Albedo.b += IN.color.b;
o.Alpha = c.a;
}
ENDCG
}
}
The vertex color is modified in the vertex shader "vert" based on it's world position. Then later in surface shader "surf" I add the vertex color to the pixel color. Hope this get's you started.
I'm not having a problem writing the surface shader with vert colours, it doesn't matter, I'm using another tactic to achieve what I need. Thanks anyway...
Your answer
