簡體   English   中英

C#XNA 3D模型無法渲染

[英]C# XNA 3D Model not 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.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);
        }
    }
}

我沒有發現您的代碼有任何真正的問題,因此問題可能出在模型上。

這可能是由於所討論的模型文件引起的。 Blender上的原始FBX導出器存在一個已知問題(我不知道您使用的建模軟件是用來制作模型的,但問題仍然存在),導致導出的模型尺寸比實際尺寸大100倍。攪拌機。 結果是該模型太大,以至於它不僅將相機包圍在內部,而且太大,以至於多邊形超出了視錐范圍。 結果:該模型確實已渲染,但是除非您移動攝像機直到意識到縮放問題,否則它永遠不會出現在屏幕上。

我說這是因為我自己處理了這個問題。 如果您確實在使用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