c# - 将相机放在球体中以产生空间效果

标签 c# visual-studio xna

我想像标题所说的那样,在一个球体中放置一个相机,我在 Autodesk 中将其创建为带有星星的 2 面 Material ,然后将该球体放置在 Visual Studio 的世界中,并在其中放置一个相机。执行此操作的自然方法是将球体置于 vector.zero 中,并将相机置于 vector 中。零以及任何方向的 View 。但是一旦相机进入我的球体,我就什么都看不到了……只能看到背景……这是代码,不胜感激。

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 _3dGame
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        GameObject starBubble; // represents the outter star texture
        Camera gameCamera;//represents the camera.
        SpriteBatch spriteBatch;

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


        protected override void Initialize()
        {

            starBubble = new GameObject();
            gameCamera = new Camera();
            base.Initialize();
        }


        Model myModel;

        // The aspect ratio determines how to scale 3d to 2d projection.
        float aspectRatio;

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            myModel = Content.Load<Model>("Models\\star_bubble2");
            aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio; 
        }


        protected override void UnloadContent()
        {

        }


        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
                ButtonState.Pressed)
                this.Exit();

            modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds *
                MathHelper.ToRadians(0.1f);

            base.Update(gameTime);
        }


        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        // Set the position of the model in world space, and set the rotation.
        Vector3 modelPosition = Vector3.Zero;
        float modelRotation = 0.0f;

        // Set the position of the camera in world space, for our view matrix.
        Vector3 cameraPosition = new Vector3(0,0,0);

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.White);

            // Copy any parent transforms.
            Matrix[] transforms = new Matrix[myModel.Bones.Count];
            myModel.CopyAbsoluteBoneTransformsTo(transforms);

            // Draw the model. A model can have multiple meshes, so loop.
            foreach (ModelMesh mesh in myModel.Meshes)
            {
                // This is where the mesh orientation is set, as well 
                // as our camera and projection.
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] *
                        Matrix.CreateRotationY(modelRotation)
                        * Matrix.CreateTranslation(modelPosition);
                    effect.View = Matrix.CreateLookAt(cameraPosition,
                        Vector3.Zero, Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                        MathHelper.ToRadians(45.0f), aspectRatio,
                        1.0f, 10000.0f);
                }
                // Draw the mesh, using the effects set above.
                mesh.Draw();
            }
            base.Draw(gameTime);
        }
    }
}

最佳答案

(确实:修复法线很重要)

额外提示:

不要用相机旋转球体。

先绘制球体,同时关闭 z 缓冲区。

See this article (它使用了一个天空盒,但这在这里并不重要)

关于c# - 将相机放在球体中以产生空间效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5642814/

相关文章:

c# - 从委托(delegate)检索调用方法和元数据

visual-studio - 在 Cordova 中打开预填充的 SQLite 数据库

c# - 类型或命名空间名称 'Properties' 不存在

c# - Galaxian-like 敌人运动

sql - XNA Windows 游戏简单数据库 Access - .mdb 不起作用 [非常基本]

c# - 在 C# 中使用德语小数分隔符对双值进行 XML 反序列化

c# - 比较 C#/Unity 中的枚举值?

c++ - 为什么当我输入三月或七月时,我的代码没有给出正确的输入?

c# - XNA 4.0 寻找房间算法问题

c# - 单元测试资源管理器不显示 metro 应用程序的异步单元测试