- Home /
Vertex color blending shader
Hi,
i'm trying to create a 3 layer terrain shader that uses the vertex colors in the mesh painted by the vertex painter modifier in 3ds max
Now i tried to make this shader but it gives me errors that doesn't make any sense to me.
So here's the shader:
Shader "Debug/Vertex color" {
Properties {
_Color ("Color", Color) = (0,0,0,1)
_Layer1 ("Layer1", 2D) = "white" {}
_Layer2 ("Layer2", 2D) = "white" {}
_Layer3 ("Layer3", 2D) = "white" {}
}
SubShader {
Pass {
Fog { Mode Off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _Layer1;
sampler2D _Layer2;
sampler2D _Layer3;
float _Color;
// vertex input: position, color
struct appdata {
float4 vertex : POSITION;
fixed4 color : COLOR;
};
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : COLOR;
float2 uv_Layer1 : TEXCOORD0;
float2 uv_Layer2 : TEXCOORD0;
float2 uv_Layer3 : TEXCOORD0;
};
v2f vert (appdata v) {
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.color = v.color;
return o;
}
float4 frag (v2f i) : COLOR0
{
half4 l1 = tex2D (_Layer1, i.uv_Layer1);
half4 l2 = tex2D (_Layer2, i.uv_Layer2);
half4 l3 = tex2D (_Layer3, i.uv_Layer3);
float4 out;
out = (l1.rgb * i.color.r) + (l2.rgb * i.color.g) + (l3.rgb * i.color.b);
return out;
}
ENDCG
}
}
}
and this is the error log:
Shader error in 'Debug/Vertex color': Shader program had errors at line 12
Shader error in 'Debug/Vertex color': Program 'frag', cast not allowed at line 50
Shader error in 'Debug/Vertex color': Program 'frag', syntax error, unexpected '=' at token "=" at line 50
Shader error in 'Debug/Vertex color': Program 'frag', type name expected at token "=" at line 50
Shader error in 'Debug/Vertex color': Program 'vert', cast not allowed at line 50
Shader error in 'Debug/Vertex color': Program 'vert', syntax error, unexpected '=' at token "=" at line 50
Shader error in 'Debug/Vertex color': Program 'vert', type name expected at token "=" at line 50
Shader error in 'Debug/Vertex color': Program 'frag', syntax error, unexpected ';' at token ";" at line 52
Shader error in 'Debug/Vertex color': Program 'frag', type name expected at token ";" at line 52
Shader error in 'Debug/Vertex color': Program 'vert', syntax error, unexpected ';' at token ";" at line 52
Shader error in 'Debug/Vertex color': Program 'vert', type name expected at token ";" at line 52
I would appreciate some corrections here.
Thanks!
Answer by MaT227 · Jul 17, 2014 at 08:32 AM
Here you have no more errors: As @Bunny83 said the out keyword was causing the error.
Shader "Debug/Vertex color" {
Properties
{
_Color ("Color", Color) = (0,0,0,1)
_Layer1 ("Layer1", 2D) = "white" {}
_Layer2 ("Layer2", 2D) = "white" {}
_Layer3 ("Layer3", 2D) = "white" {}
}
SubShader
{
Pass
{
Fog { Mode Off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _Layer1;
sampler2D _Layer2;
sampler2D _Layer3;
float _Color;
struct appdata
{
float4 vertex : POSITION;
fixed4 color : COLOR;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed4 color : COLOR;
float2 uv_Layer1 : TEXCOORD0;
float2 uv_Layer2 : TEXCOORD1;
float2 uv_Layer3 : TEXCOORD2;
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
return o;
}
float4 frag (v2f i) : COLOR
{
half4 l1 = tex2D (_Layer1, i.uv_Layer1);
half4 l2 = tex2D (_Layer2, i.uv_Layer2);
half4 l3 = tex2D (_Layer3, i.uv_Layer3);
float4 color = float4(1, 1, 1, 1);
color.rgb = (l1.rgb * i.color.r) + (l2.rgb * i.color.g) + (l3.rgb * i.color.b);
return color;
}
ENDCG
}
}
}
thanks man it helped to fix the errors , but 3ds max does not correctly save the vertex colors in de fbx file so i have to look into that also , but thanks :D
You're welcome, there are several example of vertex color blending shaders. You should also take a look at them.
Also when i apply the shader only the colors are shown on the mesh , the textures are not applied to it , something weird have a look at this pic. link text
@kilian277: Just in case you still don't know what the problem was: "out" is a keyword like in C#.
@Bunny83: oh i didn't know that , anyways the texturing per vertex color is now working so everyone thanks for the input !
Your answer
Follow this Question
Related Questions
how can i catch the sky box and do a reflection in object 0 Answers
Why does my shader only shade objects based on their normals in the X axis? 1 Answer
Problem with texture blending and vertex color shader for iOS 1 Answer
Additive shader visible in viewport but not in-game 3 Answers
CG - Using Matrix as texcoord 1 Answer