C#XNA 3D模型未渲染

C#XNA 3D模型未渲染,c#,3d,xna,rendering,C#,3d,Xna,Rendering,该模型是一个.fbx 我也尝试了很多模型,似乎没有什么工作,我想知道是否有人可以帮助我,下面的代码是我所有的谢谢 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna

该模型是一个.fbx 我也尝试了很多模型,似乎没有什么工作,我想知道是否有人可以帮助我,下面的代码是我所有的谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Model model;
        Matrix[] transforms;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 800;
            graphics.PreferredBackBufferWidth = 1280;
        }

        protected override void Initialize()
        {

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            model = Content.Load<Model>("3d/screwdriver");
            transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);
        }
        protected override void UnloadContent()
        {
        }


        protected override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            base.Update(gameTime);
        }    

        // The draw method is one of the reasons I think nothing is working,

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            Matrix view = Matrix.CreateLookAt(
                new Vector3(200, 300, 900),
                new Vector3(0, 50, 0),
                Vector3.Up);
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 10000.0f);
            Matrix baseWorld = Matrix.CreateScale(0.4f) * Matrix.CreateRotationY(MathHelper.ToRadians(180));
            foreach (ModelMesh mesh in model.Meshes)
            {
                Matrix localWorld = transforms[mesh.ParentBone.Index] * baseWorld;
                foreach (ModelMeshPart part in mesh.MeshParts)
                { 
                BasicEffect e = (BasicEffect)part.Effect;
                e.World = localWorld;
                e.View = view;
                e.Projection = projection;
                e.EnableDefaultLighting();

                }
                mesh.Draw();
            }
            base.Draw(gameTime);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
名称空间
{
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
模型;
矩阵[]变换;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferHeight=800;
graphics.PreferredBackBufferWidth=1280;
}
受保护的覆盖无效初始化()
{
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
模型=内容加载(“3d/螺丝刀”);
转换=新矩阵[model.Bones.Count];
model.copyAbsoluteBonetTransformsTo(转换);
}
受保护的覆盖无效UnloadContent()
{
}
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
更新(游戏时间);
}    
//抽签法是我认为什么都不管用的原因之一,
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
矩阵视图=矩阵.CreateLookAt(
新矢量3(200300900),
新矢量3(0,50,0),
向量3.向上);
矩阵投影=矩阵.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),GraphicsDevice.Viewport.AspectRatio,0.1f,10000.0f);
Matrix baseWorld=Matrix.CreateScale(0.4f)*Matrix.CreateRotationY(MathHelper.ToRadians(180));
foreach(model.mesh中的ModelMesh网格)
{
矩阵localWorld=变换[mesh.ParentBone.Index]*baseWorld;
foreach(网格中的ModelMeshPart零件。MeshParts)
{ 
BasicEffect e=(BasicEffect)part.Effect;
e、 世界=本地世界;
e、 视图=视图;
e、 投影=投影;
e、 启用DefaultLighting();
}
mesh.Draw();
}
基础。抽签(游戏时间);
}
}
}

我看不出您的代码有什么问题,因此问题可能出在型号上

这可能是由有问题的模型文件引起的。Blender上的库存FBX Exporter存在一个已知问题(我不知道您使用了哪种建模软件制作模型,但问题仍然相关),这使得模型的导出尺寸比Blender上的大100倍。结果是,模型非常大,它不仅将相机封闭在自身内部,而且非常大,以至于其多边形超出了观察平锥。结果:模型确实已渲染,但除非在意识到缩放问题之前移动摄影机,否则它永远不会出现在屏幕上

我这么说是因为我自己处理过这个问题。如果您确实使用Blender,则应使用本页中的导出器:

您需要修复格式设置-看起来您对某些代码的缩进不够……我看不出您的代码有什么真正的错误。(一个改进是只调用e.EnableDefaultLighting()一次)。可能您的相机方向不正确,您或您的模型太大或太小。我也有同样的问题,但我怀疑这是比例问题,尽管我仍在尝试缩放等操作,而且我还从3DS Max的学生版导出