I have a fbx model that I want to render, but the Vertex Colors do not work. It was created in Blender 2.77a and then exported to fbx.
When I try to render the model with my custom effect like this:
foreach (ModelMesh mesh in model.Meshes)
foreach (ModelMeshPart part in mesh.MeshParts)
{
part.Effect = effect;
}
foreach (ModelMesh mesh in model.Meshes)
{
foreach (Effect e in mesh.Effects)
{
e.Parameters["World"].SetValue(world);
e.Parameters["View"].SetValue(cam.View);
e.Parameters["Projection"].SetValue(cam.Projection);
e.Parameters["AmbientColor"].SetValue(Color.White.ToVector3());
e.Parameters["AmbientIntensity"].SetValue(0.3f);
e.Parameters["DiffuseColor"].SetValue(Color.White.ToVector3());
e.Parameters["DiffuseIntensity"].SetValue(0.5f);
e.Parameters["DiffuseDirection"].SetValue(new Vector3(1.0f, 0.8f, 0.3f));
}
mesh.Draw();
}
the model appears just black (since I multiply by the vertex color in my shader).
However, if I render it like this (with BasicEffect):
foreach(ModelMesh mesh in model.Meshes)
foreach(BasicEffect e in mesh.Effects)
{
e.EnableDefaultLighting();
}
model.Draw(world, cam.View, cam.Projection);
everything works just fine. Also using the first variant with BasicEffect instead of my own effect works.
So it is not my fbx file that is faulty (trying it with the default Blender cube also did not work).
This is my Shader code:
// Matrix
float4x4 World;
float4x4 View;
float4x4 Projection;
// Light
float4 AmbientColor;
float AmbientIntensity;
float3 DiffuseDirection;
float4 DiffuseColor;
float DiffuseIntensity;
// The input for the VertexShader
struct VertexShaderInput
{
float4 Position : POSITION;
float4 Color : COLOR0;
float3 Normal : NORMAL0;
float2 TexCoord : TEXCOORD0;
};
// The output from the vertex shader
struct VertexShaderOutput
{
float4 Position : POSITION;
float3 Normal : TEXCOORD0;
float4 Color : COLOR0;
float2 TexCoord : TEXCOORD1;
};
// The VertexShader.
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
float3 normal = normalize(mul(input.Normal, World));
output.Normal = normal;
output.Color = input.Color;
output.TexCoord = input.TexCoord;
return output;
}
// The Pixel Shader
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float4 norm = float4(input.Normal, 1.0);
float diffuse = saturate(dot(-DiffuseDirection, norm));
return float4(AmbientColor.rgb*AmbientIntensity + DiffuseIntensity*DiffuseColor.rgb*diffuse, 1) * input.Color;
}
//the Techinique
technique Technique1
{
pass Pass1
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}
I really do not know what the problem is. It could have to do with VertexDeclarations.
Just for clarity: If I am not multiplying by color in the shader i get the lighting, but without colors.
With Google I found this question that is similar to mine. The question was also posted on Stackexchange, but the solution he found is not really good. There has to be something better that is not a “hack”.
So my final question is: How do I get vertex colors right with custom shaders?
Thanks in advance