在Xna中我想用自定义效果绘制我的模型..所以我想从网格中获取纹理。
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
effect.Parameters["World"].SetValue(World );
effect.Parameters["View"].SetValue(View);
effect.Parameters["Projection"].SetValue(Projection);
effect.Parameters["TextureEnabled"].SetValue(true);
var basicEffect = part.Effect as BasicEffect;
if (basicEffect != null)
{
Texture2D texName = basicEffect.Texture;
effect.Parameters["Texture"].SetValue(texName);
}
part.Effect = effect;
}
mesh.Draw();
}

如果我运行此代码,模型是黑色的..但是如果我删除了if(basicEffect!= null),它会正确地显示1个fram但是会出现Null异常。
谢谢
答案 0 :(得分:0)
我终于能够通过将代码更改为:
来使其正确
Texture2D[] texts = new Texture2D[40000];
bool bol = true;
public void DrawModel( camera cam)
{
int i = 0;
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
effect.Parameters["World"].SetValue(World );
effect.Parameters["View"].SetValue(View);
effect.Parameters["Projection"].SetValue(Projection);
effect.Parameters["TextureEnabled"].SetValue(true);
var basicEffect = part.Effect as BasicEffect;
if (bol && basicEffect != null)
{
texts[i] = basicEffect.Texture;
}
effect.Parameters["Texture"].SetValue(texts[i]);
i++;
part.Effect = effect;
}
mesh.Draw();
}
bol = false;
}