具有多重纹理的绘制效果 XNA

Draw effect XNA with multiple texture

在 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 异常。

谢谢

我终于能够通过将代码更改为:

        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;
        }