Ceață BasicEffect

Pagina actualizată :
Data creării paginii :

rezumat

Manipulați parametrii legați de porțiunea de ceață a BasicEffect pentru a vedea cum este afișat modelul.

BasicEffect のフォグ

Mediu de operare

Cerințe preliminare

Versiuni XNA acceptate
  • 4.0
Platforme acceptate
  • Windows (XP SP2 sau o versiune ulterioară, Vista, 7)
  • Xbox 360
  • Windows Phone 7
Versiunea Windows necesară Vertex Shader 2.0
Versiunea Windows Pixel Shader necesară 2.0

Mediu de operare

peron
  • Ferestre 7
  • Xbox 360
  • Windows Phone 7

Cum se lucrează cu eșantionul

Funcționează tastaturăcontroler Xbox 360mouse tactil
Selectați parametrii pe care doriți să îi modificați ↑、↓ Baston stâng ↑, ↓ Butonul din stânga -
Modificarea parametrilor ←、→ Bastonul stâng ←, → ←→ Drag -

substanță

ceață

Ceața se referă la semnificația ceții și poate fi exprimată astfel încât cu cât obiectul este mai departe de poziția punctului de vedere, ceața îl face dificil de văzut. Ceața nu este o setare comună ca un parametru de mediu, ci este setată pentru fiecare efect.

Parametrii de ceață includ:

FogEnabled Specificați dacă utilizați ceață sau nu.
FogColor Specifică culoarea ceții.
FogStart Specifică distanța față de poziția punctului de vedere înainte de începerea ceții.
FogEnd Specificați distanța față de punctul de vedere la care culoarea ceții este reflectată 100%.

Imagine a schimbării ceții

Imaginea de mai jos arată fiecare dintre valorile de ceață variate.

Fără ceață

Starea în care ceața este îndepărtată.

フォグの変化イメージ

Activat Fals
Culoare (roșu) 1
Culoare (verde) 1
Culoare (albastru) 1
Porni 60
Capăt 100

Starea inițială

Aceasta este starea în care este lansat eșantionul.

初期状態

Activat Adevărat
Culoare (roșu) 1
Culoare (verde) 1
Culoare (albastru) 1
Porni 60
Capăt 100

Schimbarea culorii

Culoarea ceții este setată la roșu.

Color 変更

Activat Adevărat
Culoare (roșu) 1
Culoare (verde) 0
Culoare (albastru) 0
Porni 60
Capăt 100

Modificarea FogStart

Modificarea valorii FogStart. Deoarece ceața începe de la 0, puteți vedea că ceața este interpolată de la punctul de vedere la distanța FogEnd.

FogStart 変更

Activat Adevărat
Culoare (roșu) 1
Culoare (verde) 0
Culoare (albastru) 0
Porni 0
Capăt 100

Modificare FogEnd

Modificarea valorii FogEnd. Puteți vedea că distanța FogEnd este complet albă.

FogEnd 変更

Activat Adevărat
Culoare (roșu) 1
Culoare (verde) 1
Culoare (albastru) 1
Porni 34.33
Capăt 61.66

câmp

Câmpul are informații despre ceață pentru a seta la BasicEffect. În plus, are parametri pentru selectarea meniurilor, dar din moment ce este doar un parametru pentru funcționare, voi omite detaliile.

/// <summary>
/// フォグの有効フラグ
/// </summary>
private bool fogEnabled = true;

/// <summary>
/// フォグの色
/// </summary>
private Vector3 fogColor = Vector3.One;

/// <summary>
/// フォグの開始距離
/// </summary>
private float fogStart = 60.0f;

/// <summary>
/// フォグの終了距離
/// </summary>
private float fogEnd = 100.0f;

Setări ceață

Valoarea modificată este setată la parametrul de ceață al efectului.

// フォグを設定
foreach (ModelMesh mesh in this.model.Meshes)
{
    foreach (BasicEffect effect in mesh.Effects)
    {
        // フォグの有効フラグ
        effect.FogEnabled = this.fogEnabled;

        // フォグの色
        effect.FogColor = this.fogColor;

        // フォグの開始距離
        effect.FogStart = this.fogStart;

        // フォグの終了距離
        effect.FogEnd = this.fogEnd;
    }
}

BasicEffect.FogEnabled proprietate

Specifică dacă se utilizează ceață. Bool obțineți, setați

BasicEffect.FogColor proprietate

Specifică culoarea ceții. Vector3 obțineți, setați

BasicEffect.FogStart proprietate

Specifică distanța de pornire a ceții. Aceasta va fi distanța față de punctul de vedere. pluti obțineți, setați

BasicEffect.FogEnd proprietate

Specifică distanța finală la care culoarea de ceață este completă. Aceasta va fi distanța față de punctul de vedere. pluti obțineți, setați

Toate codurile

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;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace BasicEffectFog
{
    /// <summary>
    /// ゲームメインクラス
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// グラフィックデバイス管理クラス
        /// </summary>
        private GraphicsDeviceManager graphics = null;

        /// <summary>
        /// スプライトのバッチ化クラス
        /// </summary>
        private SpriteBatch spriteBatch = null;

        /// <summary>
        /// スプライトでテキストを描画するためのフォント
        /// </summary>
        private SpriteFont font = null;

        /// <summary>
        /// 直前のキーボード入力の状態
        /// </summary>
        private KeyboardState oldKeyboardState = new KeyboardState();

        /// <summary>
        /// 直前のマウスの状態
        /// </summary>
        private MouseState oldMouseState = new MouseState();

        /// <summary>
        /// 直前のゲームパッド入力の状態
        /// </summary>
        private GamePadState oldGamePadState = new GamePadState();

        /// <summary>
        /// モデル
        /// </summary>
        private Model model = null;

        /// <summary>
        /// フォグの有効フラグ
        /// </summary>
        private bool fogEnabled = true;

        /// <summary>
        /// フォグの色
        /// </summary>
        private Vector3 fogColor = Vector3.One;

        /// <summary>
        /// フォグの開始距離
        /// </summary>
        private float fogStart = 60.0f;

        /// <summary>
        /// フォグの終了距離
        /// </summary>
        private float fogEnd = 100.0f;

        /// <summary>
        /// 選択しているメニューのインデックス
        /// </summary>
        private int selectedMenuIndex = 0;

        /// <summary>
        /// メニューリスト
        /// </summary>
        private static string[] MenuNameList = new string[]
            {
                "Enabled",
                "Color (Red)",
                "Color (Green)",
                "Color (Blue)",
                "Start",
                "End"
            };

        /// <summary>
        /// パラメータテキストリスト
        /// </summary>
        private string[] parameters = new string[6];


        /// <summary>
        /// GameMain コンストラクタ
        /// </summary>
        public GameMain()
        {
            // グラフィックデバイス管理クラスの作成
            this.graphics = new GraphicsDeviceManager(this);

            // ゲームコンテンツのルートディレクトリを設定
            this.Content.RootDirectory = "Content";

#if WINDOWS_PHONE
            // Windows Phone のデフォルトのフレームレートは 30 FPS
            this.TargetElapsedTime = TimeSpan.FromTicks(333333);

            // バックバッファサイズの設定
            this.graphics.PreferredBackBufferWidth = 480;
            this.graphics.PreferredBackBufferHeight = 800;

            // フルスクリーン表示
            this.graphics.IsFullScreen = true;
#endif

            // ウインドウ上でマウスのポインタを表示するようにする
            this.IsMouseVisible = true;
        }

        /// <summary>
        /// ゲームが始まる前の初期化処理を行うメソッド
        /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
        /// </summary>
        protected override void Initialize()
        {
            // TODO: ここに初期化ロジックを書いてください

            // コンポーネントの初期化などを行います
            base.Initialize();
        }

        /// <summary>
        /// ゲームが始まるときに一回だけ呼ばれ
        /// すべてのゲームコンテンツを読み込みます
        /// </summary>
        protected override void LoadContent()
        {
            // テクスチャーを描画するためのスプライトバッチクラスを作成します
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);

            // フォントをコンテンツパイプラインから読み込む
            this.font = this.Content.Load<SpriteFont>("Font");

            // モデルを作成
            this.model = this.Content.Load<Model>("Model");

            // ライトとビュー、プロジェクションはあらかじめ設定しておく
            foreach (ModelMesh mesh in this.model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    // デフォルトのライト適用
                    effect.EnableDefaultLighting();

                    // ビューマトリックスをあらかじめ設定
                    effect.View = Matrix.CreateLookAt(
                        new Vector3(0.0f, 30.0f, 50.0f),
                        new Vector3(0.0f, -10.0f, 0.0f),
                        Vector3.Up
                    );

                    // プロジェクションマトリックスをあらかじめ設定
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                        MathHelper.ToRadians(45.0f),
                        (float)this.GraphicsDevice.Viewport.Width /
                            (float)this.GraphicsDevice.Viewport.Height,
                        1.0f,
                        100.0f
                    );
                }
            }
        }

        /// <summary>
        /// ゲームが終了するときに一回だけ呼ばれ
        /// すべてのゲームコンテンツをアンロードします
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: ContentManager で管理されていないコンテンツを
            //       ここでアンロードしてください
        }

        /// <summary>
        /// 描画以外のデータ更新等の処理を行うメソッド
        /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Update(GameTime gameTime)
        {
            // 入力デバイスの状態取得
            KeyboardState keyboardState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();
            GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

            // Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
            // ゲームを終了させます
            if (gamePadState.Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // メニューの選択
            if ((keyboardState.IsKeyDown(Keys.Up) && this.oldKeyboardState.IsKeyUp(Keys.Up)) ||
                (gamePadState.ThumbSticks.Left.Y >= 0.5f &&
                    this.oldGamePadState.ThumbSticks.Left.Y < 0.5f))
            {
                // 選択メニューをひとつ上に移動
                this.selectedMenuIndex =
                    (this.selectedMenuIndex + this.parameters.Length - 1) % this.parameters.Length;
            }
            if ((keyboardState.IsKeyDown(Keys.Down) && this.oldKeyboardState.IsKeyUp(Keys.Down)) ||
                (gamePadState.ThumbSticks.Left.Y <= -0.5f &&
                    this.oldGamePadState.ThumbSticks.Left.Y > -0.5f) ||
                (this.oldMouseState.LeftButton == ButtonState.Pressed &&
                 mouseState.LeftButton == ButtonState.Released))
            {
                // 選択メニューをひとつ下に移動
                this.selectedMenuIndex =
                    (this.selectedMenuIndex + this.parameters.Length + 1) % this.parameters.Length;
            }

            // 各マテリアルの値を操作
            float moveValue = 0.0f;
            if (keyboardState.IsKeyDown(Keys.Left))
            {
                moveValue -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                moveValue += (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                moveValue += (mouseState.X - this.oldMouseState.X) * 0.005f;
            }
            if (gamePadState.IsConnected)
            {
                moveValue += gamePadState.ThumbSticks.Left.X *
                             (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            if (moveValue != 0.0f)
            {
                switch (this.selectedMenuIndex)
                {
                    case 0: // フォグの有効フラグ
                        this.fogEnabled = (moveValue > 0.0f);
                        break;
                    case 1: // フォグの色 (赤)
                        this.fogColor.X = MathHelper.Clamp(this.fogColor.X + moveValue,
                                                           0.0f,
                                                           1.0f);
                        break;
                    case 2: // フォグの色 (緑)
                        this.fogColor.Y = MathHelper.Clamp(this.fogColor.Y + moveValue,
                                                           0.0f,
                                                           1.0f);
                        break;
                    case 3: // フォグの色 (青)
                        this.fogColor.Z = MathHelper.Clamp(this.fogColor.Z + moveValue,
                                                           0.0f,
                                                           1.0f);
                        break;
                    case 4: // フォグの開始距離
                        this.fogStart = MathHelper.Clamp(this.fogStart + moveValue * 10.0f,
                                                         0.0f,
                                                         100.0f);
                        break;
                    case 5: // フォグの終了距離
                        this.fogEnd = MathHelper.Clamp(this.fogEnd + moveValue * 10.0f,
                                                       0.0f,
                                                       100.0f);
                        break;
                }
            }

            // フォグを設定
            foreach (ModelMesh mesh in this.model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    // フォグの有効フラグ
                    effect.FogEnabled = this.fogEnabled;

                    // フォグの色
                    effect.FogColor = this.fogColor;

                    // フォグの開始距離
                    effect.FogStart = this.fogStart;

                    // フォグの終了距離
                    effect.FogEnd = this.fogEnd;
                }
            }

            // 入力情報を記憶
            this.oldKeyboardState = keyboardState;
            this.oldMouseState = mouseState;
            this.oldGamePadState = gamePadState;

            // 登録された GameComponent を更新する
            base.Update(gameTime);
        }

        /// <summary>
        /// 描画処理を行うメソッド
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Draw(GameTime gameTime)
        {
            // 画面を指定した色でクリアします
            this.GraphicsDevice.Clear(Color.CornflowerBlue);

            // 深度バッファを有効にする
            this.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            // モデルを描画
            foreach (ModelMesh mesh in this.model.Meshes)
            {
#if WINDOWS_PHONE
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = Matrix.CreateScale(new Vector3(0.5f, 1, 1));
                }
#endif
                mesh.Draw();
            }

            // スプライトの描画準備
            this.spriteBatch.Begin();

            // 操作
            this.spriteBatch.DrawString(this.font,
                "Up, Down : Select Menu",
                new Vector2(20.0f, 20.0f), Color.Black);
            this.spriteBatch.DrawString(this.font,
                "Left, right : Change Value",
                new Vector2(20.0f, 45.0f), Color.Black);
            this.spriteBatch.DrawString(this.font,
                "MouseClick & Drag :",
                new Vector2(20.0f, 70.0f), Color.Black);
            this.spriteBatch.DrawString(this.font,
                "    Select Menu & Change Value",
                new Vector2(20.0f, 95.0f), Color.Black);

            this.spriteBatch.DrawString(this.font,
                "Up, Down : Select Menu",
                new Vector2(19.0f, 19.0f), Color.White);
            this.spriteBatch.DrawString(this.font,
                "Left, right : Change Value",
                new Vector2(19.0f, 44.0f), Color.White);
            this.spriteBatch.DrawString(this.font,
                "MouseClick & Drag :",
                new Vector2(19.0f, 69.0f), Color.White);
            this.spriteBatch.DrawString(this.font,
                "    Select Menu & Change Value",
                new Vector2(19.0f, 94.0f), Color.White);

            // 各メニュー //
            for (int i = 0; i < MenuNameList.Length; i++)
            {
                this.spriteBatch.DrawString(this.font,
                    MenuNameList[i],
                    new Vector2(40.0f, 120.0f + i * 20.0f), Color.Black);
                this.spriteBatch.DrawString(this.font,
                    MenuNameList[i],
                    new Vector2(39.0f, 119.0f + i * 20.0f), Color.White);
            }

            // 各パラメータ //

            // フォグの有効フラグ
            this.parameters[0] = this.fogEnabled.ToString();

            // フォグの色 (赤)
            this.parameters[1] = this.fogColor.X.ToString();

            // フォグの色 (緑)
            this.parameters[2] = this.fogColor.Y.ToString();

            // フォグの色 (青)
            this.parameters[3] = this.fogColor.Z.ToString();

            // フォグの開始距離
            this.parameters[4] = this.fogStart.ToString();

            // フォグの終了距離
            this.parameters[5] = this.fogEnd.ToString();

            for (int i = 0; i < this.parameters.Length; i++)
            {
                this.spriteBatch.DrawString(this.font,
                    this.parameters[i],
                    new Vector2(220.0f, 120.0f + i * 20.0f), Color.Black);
                this.spriteBatch.DrawString(this.font,
                    this.parameters[i],
                    new Vector2(219.0f, 119.0f + i * 20.0f), Color.White);
            }

            // 選択インデックス
            this.spriteBatch.DrawString(this.font, "*",
                new Vector2(20.0f, 120.0f + this.selectedMenuIndex * 20.0f), Color.Black);
            this.spriteBatch.DrawString(this.font, "*",
                new Vector2(19.0f, 119.0f + this.selectedMenuIndex * 20.0f), Color.White);

            // スプライトの一括描画
            this.spriteBatch.End();

            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}