简体   繁体   English

C#XNA 3D模型无法渲染

[英]C# XNA 3D Model not rendering

The model is a .fbx I have tried many models also, Nothing seems to be working i was wondering if anyone could help me, The following code is all I have thanks! 该模型是.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);
        }
    }
}

I don't see anything really wrong with your code, so the problem may be on the model. 我没有发现您的代码有任何真正的问题,因此问题可能出在模型上。

It might be caused by the model file in question. 这可能是由于所讨论的模型文件引起的。 There is a know issue with the stock FBX Exporter on Blender (I don't know which modelling software you used to make the model, but the matter is still relevant) that makes the model be exported with a size 100x bigger than it is on Blender. Blender上的原始FBX导出器存在一个已知问题(我不知道您使用的建模软件是用来制作模型的,但问题仍然存在),导致导出的模型尺寸比实际尺寸大100倍。搅拌机。 The result is that the model is so big that it not only encloses the camera inside itself, but also is so big that it's polygons are beyond the viewing frustrum. 结果是该模型太大,以至于它不仅将相机包围在内部,而且太大,以至于多边形超出了视锥范围。 Result: the model is indeed rendered, but it'll never appear onscreen unless you move the camera around until you realise the scaling problem. 结果:该模型确实已渲染,但是除非您移动摄像机直到意识到缩放问题,否则它永远不会出现在屏幕上。

I'm saying this because I dealed with this problem myself. 我说这是因为我自己处理了这个问题。 If you are indeed using Blender, you should use the exporter found in this page: http://blog.diabolicalgame.co.uk/2011/07/exporting-animated-models-from-blender.html 如果您确实在使用Blender,则应使用在此页面中找到的导出器: http : //blog.diabolicalgame.co.uk/2011/07/exporting-animated-models-from-blender.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM