【问题标题】:C# XNA 3D Model not renderingC# XNA 3D 模型不渲染
【发布时间】:2013-01-28 21:10:21
【问题描述】:

模型是 .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);
        }
    }
}

【问题讨论】:

  • 你需要修正你的格式 - 看起来你没有足够缩进一些代码......
  • 我看不出你的代码有什么问题。 (一个改进是只调用一次e.EnableDefaultLighting())。可能您的相机没有朝向正确的方向,您或您的模型太大或太小。
  • 我遇到了同样的问题,但我怀疑它的规模,虽然我仍在尝试缩放等等,我还导出了 3DS Max 的学生版

标签: c# 3d xna rendering


【解决方案1】:

我看不出你的代码有什么问题,所以问题可能出在模型上。

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

我这样说是因为我自己处理过这个问题。如果您确实在使用 Blender,则应使用此页面中的导出器: http://blog.diabolicalgame.co.uk/2011/07/exporting-animated-models-from-blender.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-05
    • 2013-03-25
    • 2010-12-16
    • 2016-07-30
    • 2019-07-02
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    相关资源
    最近更新 更多