这可能是一些新手问题,但我正在尝试用它的颜色(材料)渲染一个 fbx 模型。
问题是,当我尝试使用 BasicEffect 时,一切顺利,颜色渲染得很好——但现在我正在编写一个自定义 HLSL 效果文件,每当我尝试用它渲染模型时,它都会说模型没有颜色:
当前顶点声明不包括当前顶点着色器所需的所有元素。缺少颜色 0。
这可能是一些愚蠢的语义错误(或类似的错误),因为我对使用 HLSL 不是很有经验。无论如何,这是代码(最初取自 Riemers 教程):
float4x4 xWorldViewProjection;
float4x4 xWorld;
float3 xLightPos;
float xLightPower;
float xAmbient;
struct VertexToPixel
{
float4 Position : POSITION;
float3 Normal : TEXCOORD0;
float3 Position3D : TEXCOORD1;
float4 Color : COLOR0;
};
struct PixelToFrame
{
float4 Color : COLOR0;
};
float DotProduct(float3 lightPos, float3 pos3D, float3 normal)
{
float3 lightDir = normalize(pos3D - lightPos);
return dot(-lightDir, normal);
}
VertexToPixel SimplestVertexShader( float4 inPos : POSITION0, float3 inNormal: NORMAL0, float4 inColor : COLOR0)
{
VertexToPixel Output = (VertexToPixel)0;
Output.Position =mul(inPos, xWorldViewProjection);
Output.Normal = normalize(mul(inNormal, (float3x3)xWorld));
Output.Position3D = mul(inPos, xWorld);
Output.Color = inColor;
return Output;
}
PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
float diffuseLightingFactor = DotProduct(xLightPos, PSIn.Position3D, PSIn.Normal);
diffuseLightingFactor = saturate(diffuseLightingFactor);
diffuseLightingFactor *= xLightPower;
Output.Color = PSIn.Color* (diffuseLightingFactor + xAmbient);
return Output;
}
technique Simplest
{
pass Pass0
{
VertexShader = compile vs_3_0 SimplestVertexShader();
PixelShader = compile ps_3_0 OurFirstPixelShader();
}
}
和使用的 XNA 代码:
(加载后:)
foreach (ModelMesh mesh in MainRoom.Meshes)
foreach (ModelMeshPart meshPart in mesh.MeshParts)
meshPart.Effect = roomEffect.Clone();
...
private void DrawModel(Model model, Matrix world)
{
Matrix[] bones = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(bones);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (Effect currentEffect in mesh.Effects)
{
Matrix worldMatrix = bones[mesh.ParentBone.Index] * world;
roomEffect.CurrentTechnique = roomEffect.Techniques["Simplest"];
currentEffect.Parameters["xWorldViewProjection"].SetValue(worldMatrix * camera.view * camera.projection);
currentEffect.Parameters["xWorld"].SetValue(worldMatrix);
currentEffect.Parameters["xLightPos"].SetValue(lightPos);
currentEffect.Parameters["xLightPower"].SetValue(lightPower);
currentEffect.Parameters["xAmbient"].SetValue(ambientPower);
}
mesh.Draw();
}
}