I’m using a custom effect to achieve some basic lighting and i’m having issues getting vertex colors to display.
When using the BasicEffect built in to monogame i can see the vertex colors of my test models which are fbx 2013 format.
However when using my custom shader the models vertex colors are not coming through:
Shader Code:
struct VertexShaderInput2
{
float4 Position : SV_Position0;
float4 Normal : NORMAL0;
float4 Color : COLOR0;
};
struct VertexShaderOutput2
{
float4 Position : SV_Position0;
float4 PositionWorld : TEXCOORD0;
float4 Color : COLOR0;
};
VertexShaderOutput2 VertexShaderFunction2(VertexShaderInput2 input)
{
VertexShaderOutput2 output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.PositionWorld = worldPosition;
output.Color = input.Color;
return output;
}
float4 PixelShaderFunction2(VertexShaderOutput2 input) : COLOR0
{
float3 Normal = cross(ddy(input.PositionWorld.xyz), ddx(input.PositionWorld.xyz));
Normal = normalize(Normal);
float lightIntensity = dot(Normal, DiffuseLightDirection);
float4 lightColor = lightIntensity * DiffuseColor * DiffuseIntensity + AmbientColor * AmbientIntensity;
lightColor.a = 1;
return saturate(input.Color * lightColor);
}
technique FlatNonTextured
{
pass Pass1
{
VertexShader = compile vs_4_0 VertexShaderFunction2();
PixelShader = compile ps_4_0 PixelShaderFunction2();
}
}
You can also find the fbx model im using here
What am i doing wrong?
I’m bumping this with more information.
I’ve tried copying line for line the BasicEffect shader that was being used to render the first image. I have had to replace some parts with my data that i am passing to the shader. This is producing an entirely white image with no vertex color information coming through.
Im sure that something in the source code for monogame is filling in the vertex color data through effect parameters. As you can see in VSBasicVcNoFog there is a variable called DiffuseColor which is an effect parameter. Having searched through the source code i cannot find where this is being assigned.
I may be entirely on the wrong track but i cannot be sure.
I’ve posted my copied BasicEffect shader from the source below.
struct VSInputVc
{
float4 Position : SV_Position;
float4 Color : COLOR;
};
struct VSOutputNoFog
{
float4 PositionPS : SV_Position;
float4 Diffuse : COLOR0;
};
float4 PSBasicNoFog(VSOutputNoFog pin) : SV_Target0
{
return pin.Diffuse;
}
VSOutputNoFog VSBasicVcNoFog(VSInputVc vin)
{
VSOutputNoFog vout;
float4 worldPosition = mul(vin.Position, World);
float4 viewPosition = mul(worldPosition, View);
vout.PositionPS = mul(viewPosition, Projection);
//vout.Diffuse = DiffuseColor;
vout.Diffuse *= vin.Color;
return vout;
}
technique Flat
{
pass Pass1
{
VertexShader = compile vs_4_0 VSBasicVcNoFog();
PixelShader = compile ps_4_0 PSBasicNoFog();
}
}
Also i will provide my draw method for drawing the object shown in the images:
foreach (ModelMesh mesh in modelobject.Model.Meshes) {
foreach (ModelMeshPart part in mesh.MeshParts) {
part.Effect = flatShadedEffect;
part.Effect.Parameters["View"].SetValue(camera.GetView());
part.Effect.Parameters["Projection"].SetValue(camera.GetProjection());
part.Effect.Parameters["World"].SetValue(modelobject.GetWorld());
}
//foreach (BasicEffect effect in mesh.Effects) {
// effect.World = modelobject.GetWorld();
// effect.View = camera.GetView();
// effect.Projection = camera.GetProjection();
//}
mesh.Draw();
}