Brouillard BasicEffect
résumé
Manipulez les paramètres liés à la partie brouillard du BasicEffect pour voir comment le modèle est affiché.
Environnement d’exploitation
Conditions préalables
| Versions XNA prises en charge | 
 | 
| Plates-formes prises en charge | 
 | 
| Version du nuanceur de vertex requise par Windows | 2.0 | 
| Version de Pixel Shader requise par Windows | 2.0 | 
Environnement d’exploitation
| plateforme | 
 | 
Comment utiliser l’échantillon
| Fonctionne avec le clavierManette Xbox | 360Souris | tactile | ||
|---|---|---|---|---|
| Sélectionnez les paramètres que vous souhaitez modifier | ↑、↓ | Joystick gauche ↑, ↓ | Bouton gauche | - | 
| Modification des paramètres | ←、→ | Joystick gauche ←, → | ←→ Glisser | - | 
substance
brouillard
Le brouillard fait référence à la signification du brouillard, et il peut être exprimé de telle manière que plus l’objet est éloigné de la position du point de vue, plus le brouillard le rend difficile à voir. Le brouillard n’est pas un paramètre courant comme un paramètre d’environnement, mais il est défini pour chaque effet.
Les paramètres du brouillard sont les suivants :
| Brouillard activé | Indiquez si vous souhaitez utiliser le brouillard ou non. | 
| Couleur du brouillard | Spécifie la couleur du brouillard. | 
| FogStart (en anglais seulement) | Spécifie la distance par rapport à la position du point de vue avant le début du brouillard. | 
| Embout de brouillard | Spécifiez la distance par rapport au point de vue à laquelle la couleur du brouillard est réfléchie à 100 %. | 
Image du changement de brouillard
L’image ci-dessous montre que chacune des valeurs de brouillard varie.
Pas de brouillard
État dans lequel le brouillard est levé.
| Activé | Faux | 
| Couleur (Rouge) | 1 | 
| Couleur (Vert) | 1 | 
| Couleur (Bleu) | 1 | 
| Commencer | 60 | 
| Fin | 100 | 
État initial
Il s’agit de l’état dans lequel l’exemple est lancé.
| Activé | Vrai | 
| Couleur (Rouge) | 1 | 
| Couleur (Vert) | 1 | 
| Couleur (Bleu) | 1 | 
| Commencer | 60 | 
| Fin | 100 | 
Changement de couleur
La couleur du brouillard est définie sur le rouge.
| Activé | Vrai | 
| Couleur (Rouge) | 1 | 
| Couleur (Vert) | 0 | 
| Couleur (Bleu) | 0 | 
| Commencer | 60 | 
| Fin | 100 | 
FogStart Modification
Modification de la valeur de FogStart. Étant donné que le brouillard commence à 0, vous pouvez voir que le brouillard est interpolé du point de vue à la distance FogEnd.
| Activé | Vrai | 
| Couleur (Rouge) | 1 | 
| Couleur (Vert) | 0 | 
| Couleur (Bleu) | 0 | 
| Commencer | 0 | 
| Fin | 100 | 
FogEnd Modification
Modification de la valeur de FogEnd. Vous pouvez voir que la distance du FogEnd est complètement blanche.
| Activé | Vrai | 
| Couleur (Rouge) | 1 | 
| Couleur (Vert) | 1 | 
| Couleur (Bleu) | 1 | 
| Commencer | 34.33 | 
| Fin | 61.66 | 
champ
Le champ contient des informations de brouillard à définir sur BasicEffect. De plus, il a des paramètres pour sélectionner les menus, mais comme il ne s’agit que d’un paramètre de fonctionnement, je vais omettre les détails.
 <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;
Paramètres de brouillard
La valeur modifiée est définie sur le paramètre fog de l’effet.
// フォグを設定
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 propriété
| Spécifie s’il faut utiliser le brouillard. | Bool | obtenir, définir | 
BasicEffect.FogColor propriété
| Spécifie la couleur du brouillard. | Vecteur 3 | obtenir, définir | 
BasicEffect.FogStart propriété
| Spécifie la distance de départ du brouillard. Il s’agira de la distance par rapport au point de vue. | flotter | obtenir, définir | 
BasicEffect.FogEnd propriété
| Spécifie la distance de fin à laquelle la couleur du brouillard est complète. Il s’agira de la distance par rapport au point de vue. | flotter | obtenir, définir | 
Tous les codes
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);
        }
    }
}