ポイントスプライト

Page creation date :

The page you are currently viewing does not support the selected display language.

ポイントスプライトを使用すると非常に綺麗なエフェクトを表現することが出来ます。今回は上空から雪が舞い落ちてくるような神秘的なエフェクトを実現してみました。

ポイントスプライト

今回のメインコードファイルを載せます。

MainSample.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MDXSample
{
    /// <summary>
    /// メインサンプルクラス
    /// </summary>
    public partial class MainSample : IDisposable
    {
        /// <summary>
        /// 頂点の数
        /// </summary>
        private static readonly int VertexCount = 1000;

        /// <summary>
        /// ポイントスプライトの描画範囲(最小値)
        /// </summary>
        private static readonly Vector3 MinRange = new Vector3(-30.0f, -30.0f, -30.0f);

        /// <summary>
        /// ポイントスプライトの描画範囲(最大値)
        /// </summary>
        private static readonly Vector3 MaxRange = new Vector3(30.0f, 30.0f, 30.0f);

        /// <summary>
        /// 各頂点の位置
        /// </summary>
        private CustomVertex.PositionOnly[] _vertices = new CustomVertex.PositionOnly[VertexCount];

        /// <summary>
        /// ポイントスプライト用のテクスチャー
        /// </summary>
        private Texture _texture = null;


        /// <summary>
        /// アプリケーションの初期化
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        /// <returns>全ての初期化がOKなら true, ひとつでも失敗したら false を返すようにする</returns>
        /// <remarks>
        /// false を返した場合は、自動的にアプリケーションが終了するようになっている
        /// </remarks>
        public bool InitializeApplication(MainForm topLevelForm)
        {
            // フォームの参照を保持
            this._form = topLevelForm;

            // 入力イベント作成
            this.CreateInputEvent(topLevelForm);

            try
            {
                // Direct3D デバイス作成
                this.CreateDevice(topLevelForm);

                // フォントの作成
                this.CreateFont();

                // XYZライン作成
                this.CreateXYZLine();
            }
            catch (DirectXException ex)
            {
                // 例外発生
                MessageBox.Show(ex.ToString(), "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }


            // カメラの位置セット
            this.SetCameraPosition(5.0f, 270.0f, 0.0f);


            // 頂点の位置をランダムに設定 
            Random rand = new Random();
            Vector3 range = MaxRange - MinRange;
            for (int i = 0; i < VertexCount; i++)
            {
                this._vertices[i].X = (float)(rand.NextDouble() * range.X) + MinRange.X;
                this._vertices[i].Y = (float)(rand.NextDouble() * range.Y) + MinRange.Y;
                this._vertices[i].Z = (float)(rand.NextDouble() * range.Z) + MinRange.Z;
            }

            // ポイントスプライト用のテクスチャー作成
            this._texture = TextureLoader.FromFile(this._device, "Texture.bmp");


            // Zバッファ無効
            this._device.RenderState.ZBufferEnable = false;

            // アルファブレンディングを有効にする
            this._device.RenderState.AlphaBlendEnable = true;

            // アルファブレンディング方法を設定
            this._device.RenderState.SourceBlend = Blend.One;
            this._device.RenderState.DestinationBlend = Blend.One;

            // ポイントスプライト有効
            this._device.RenderState.PointSpriteEnable = true;

            // 点をカメラ空間でスケール有効化
            this._device.RenderState.PointScaleEnable = true;

            // ポイントサイズの設定
            this._device.RenderState.PointSize = 0.25f;

            // ポイントのスケール計算式
            this._device.RenderState.PointScaleA = 0.0f;
            this._device.RenderState.PointScaleB = 0.0f;
            this._device.RenderState.PointScaleC = 1.0f;

            // ライトを無効
            this._device.RenderState.Lighting = false;

            return true;
        }

        /// <summary>
        /// デバイスがロストしたとき
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void device_DeviceLost(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// デバイスがリセットしたとき
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void device_DeviceReset(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 更新処理
        /// </summary>
        public void Update()
        {
            // アプリケーションの終了操作
            if (this._keys[(int)Keys.Escape])
            {
                this._form.Close();
                return;
            }

            // カメラの設定
            this.SettingCamera();

            // 各頂点を下に移動
            for (int i = 0; i < VertexCount; i++)
            {
                this._vertices[i].Y -= 0.02f;

                // 一番下まで行ったら一番上に戻す
                if (this._vertices[i].Y < MinRange.Y)
                {
                    this._vertices[i].Y = MaxRange.Y;
                }
            }
        }

        /// <summary>
        /// 描画処理
        /// </summary>
        public void Draw()
        {
            // デバイスが使える状態か確認する
            if (!this.EnsureDevice())
            {
                return;
            }

            // 描画内容を単色でクリアし、Zバッファもクリア
            this._device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, Color.Black, 1.0f, 0);

            // 「BeginScene」と「EndScene」の間に描画内容を記述する
            this._device.BeginScene();


            // XYZライン描画
            this.RenderXYZLine();


            // テクスチャーセット
            this._device.SetTexture(0, this._texture);

            // 頂点フォーマットセット
            this._device.VertexFormat = CustomVertex.PositionOnly.Format;

            // ポイントスプライト描画
            this._device.DrawUserPrimitives(PrimitiveType.PointList, VertexCount, this._vertices);


            // 文字列の描画
            this.RenderText();

            // 描画はここまで
            this._device.EndScene();

            // 実際のディスプレイに描画
            this._device.Present();
        }

        /// <summary>
        /// テキストの描画
        /// </summary>
        private void RenderText()
        {
            this._font.DrawText(null, "[Escape]終了", 0, 0, Color.White);
            this._font.DrawText(null, "θ:" + this._lensPosTheta, 0, 12, Color.White);
            this._font.DrawText(null, "φ:" + this._lensPosPhi, 0, 24, Color.White);
        }

        /// <summary>
        /// リソースの破棄をするために呼ばれる
        /// </summary>
        public void Dispose()
        {
            // テクスチャー破棄
            this.SafeDispose(this._texture);
            
            // リソースの破棄
            this.DisposeResource();
        }
    }
}
MainSamplePartial.cs ファイルのコードはこちらです。

MainSamplePartial.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace MDXSample
{
    public partial class MainSample
    {
        /// <summary>
        /// メインフォーム
        /// </summary>
        private MainForm _form = null;

        /// <summary>
        /// Direct3D デバイス
        /// </summary>
        private Device _device = null;

        /// <summary>
        /// Direct3D 用フォント
        /// </summary>
        private Microsoft.DirectX.Direct3D.Font _font = null;

        /// <summary>
        /// キーのプレス判定
        /// </summary>
        private bool[] _keys = new bool[256];

        /// <summary>
        /// 1つ前のマウスの位置
        /// </summary>
        private Point _oldMousePoint = Point.Empty;

        /// <summary>
        /// カメラレンズの位置(R)
        /// </summary>
        private float _lensPosRadius = 5.0f;

        /// <summary>
        /// カメラレンズの位置(θ)
        /// </summary>
        private float _lensPosTheta = 300.0f;

        /// <summary>
        /// カメラレンズの位置(φ)
        /// </summary>
        private float _lensPosPhi = 30.0f;

        /// <summary>
        /// XYZライン用頂点バッファ
        /// </summary>
        private VertexBuffer _xyzLineVertexBuffer = null;


        /// <summary>
        /// 入力イベント作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        private void CreateInputEvent(MainForm topLevelForm)
        {
            // キーイベント作成
            topLevelForm.KeyDown += new KeyEventHandler(this.form_KeyDown);
            topLevelForm.KeyUp += new KeyEventHandler(this.form_KeyUp);

            // マウス移動イベント
            topLevelForm.MouseMove += new MouseEventHandler(this.form_MouseMove);
        }

        /// <summary>
        /// キーボードのキーを押した瞬間
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_KeyDown(object sender, KeyEventArgs e)
        {
            // 押されたキーコードのフラグを立てる
            if ((int)e.KeyCode < this._keys.Length)
            {
                this._keys[(int)e.KeyCode] = true;
            }
        }
        /// <summary>
        /// キーボードのキーを放した瞬間
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_KeyUp(object sender, KeyEventArgs e)
        {
            // 放したキーコードのフラグを下ろす
            if ((int)e.KeyCode < this._keys.Length)
            {
                this._keys[(int)e.KeyCode] = false;
            }
        }

        /// <summary>
        /// マウス移動イベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // 回転
                this._lensPosTheta -= e.Location.X - this._oldMousePoint.X;
                this._lensPosPhi += e.Location.Y - this._oldMousePoint.Y;

                // φに関しては制限をつける
                if (this._lensPosPhi >= 90.0f)
                {
                    this._lensPosPhi = 89.9999f;
                }
                else if (this._lensPosPhi <= -90.0f)
                {
                    this._lensPosPhi = -89.9999f;
                }
            }
            // マウスの位置を記??
            this._oldMousePoint = e.Location;
        }

        /// <summary>
        /// Direct3D デバイスの作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        private void CreateDevice(MainForm topLevelForm)
        {
            // PresentParameters。デバイスを作成する際に必須
            // どのような環境でデバイスを使用するかを設定する
            PresentParameters pp = new PresentParameters();

            // ウインドウモードなら true、フルスクリーンモードなら false を指定
            pp.Windowed = true;

            // スワップ効果。とりあえず「Discard」を指定。
            pp.SwapEffect = SwapEffect.Discard;

            // 深度ステンシルバッファ。3Dでは前後関係があるので通常 true
            pp.EnableAutoDepthStencil = true;

            // 自動深度ステンシル サーフェイスのフォーマット。
            // 「D16」に対応しているビデオカードは多いが、前後関係の精度があまりよくない。
            // できれば「D24S8」を指定したいところ。
            pp.AutoDepthStencilFormat = DepthFormat.D16;

            try
            {
                // デバイスの作成
                this.CreateDevice(topLevelForm, pp);
            }
            catch (DirectXException ex)
            {
                // 例外発生
                throw ex;
            }
        }
        /// <summary>
        /// Direct3D デバイスの作成
        /// </summary>
        /// <param name="topLevelForm">トップレベルウインドウ</param>
        /// <param name="presentationParameters">PresentParameters 構造体</param>
        private void CreateDevice(MainForm topLevelForm, PresentParameters presentationParameters)
        {
            // 実際にデバイスを作成します。
            // 常に最高のパフォーマンスで作成を試み、
            // 失敗したら下位パフォーマンスで作成するようにしている。
            try
            {
                // ハードウェアによる頂点処理、ラスタライズを行う
                // 最高のパフォーマンスで処理を行えます。
                // ビデオカードによっては実装できない処理が存在します。
                this._device = new Device(0, DeviceType.Hardware, topLevelForm.Handle,
                    CreateFlags.HardwareVertexProcessing, presentationParameters);
            }
            catch (DirectXException ex1)
            {
                // 作成に失敗
                Debug.WriteLine(ex1.ToString());
                try
                {
                    // ソフトウェアによる頂点処理、ハードウェアによるラスタライズを行う
                    this._device = new Device(0, DeviceType.Hardware, topLevelForm.Handle,
                        CreateFlags.SoftwareVertexProcessing, presentationParameters);
                }
                catch (DirectXException ex2)
                {
                    // 作成に失敗
                    Debug.WriteLine(ex2.ToString());
                    try
                    {
                        // ソフトウェアによる頂点処理、ラスタライズを行う
                        // パフォーマンスはとても低いです。
                        // その代わり、ほとんどの処理を制限なく行えます。
                        this._device = new Device(0, DeviceType.Reference, topLevelForm.Handle,
                            CreateFlags.SoftwareVertexProcessing, presentationParameters);
                    }
                    catch (DirectXException ex3)
                    {
                        // 作成に失敗
                        // 事実上デバイスは作成できません。
                        throw ex3;
                    }
                }
            }

            // デバイスの状態変更イベントを作成
            this._device.DeviceLost += new EventHandler(this.device_DeviceLost);
            this._device.DeviceReset += new EventHandler(this.device_DeviceReset);
        }

        /// <summary>
        /// フォントの作成
        /// </summary>
        private void CreateFont()
        {
            try
            {
                // フォントデータの構造体を作成
                FontDescription fd = new FontDescription();

                // 構造体に必要なデータをセット
                fd.Height = 12;
                fd.FaceName = "MS ゴシック";

                // フォントを作成
                this._font = new Microsoft.DirectX.Direct3D.Font(this._device, fd);
            }
            catch (DirectXException ex)
            {
                // 例外発生
                throw ex;
            }
        }

        /// <summary>
        /// XYZライン作成
        /// </summary>
        private void CreateXYZLine()
        {
            // 6つ分の頂点を作成
            this._xyzLineVertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
                6, this._device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);

            // 頂点バッファをロックして、位置、色情報を書き込む
            using (GraphicsStream data = this._xyzLineVertexBuffer.Lock(0, 0, LockFlags.None))
            {
                // 今回は各XYZのラインを原点(0.0f, 0.0f, 0.0f)からプラス方向に 10.0f 伸びた線を作成
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Red.ToArgb()));
                data.Write(new CustomVertex.PositionColored(10.0f, 0.0f, 0.0f, Color.Red.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Green.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 10.0f, 0.0f, Color.Green.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 0.0f, Color.Blue.ToArgb()));
                data.Write(new CustomVertex.PositionColored(0.0f, 0.0f, 10.0f, Color.Blue.ToArgb()));

                this._xyzLineVertexBuffer.Unlock();
            }
        }

        /// <summary>
        /// カメラの位置をセット
        /// </summary>
        /// <param name="radius">半径(degree)</param>
        /// <param name="theta">水平方向角度(degree)</param>
        /// <param name="phi">垂直方向角度(degree)</param>
        private void SetCameraPosition(float radius, float theta, float phi)
        {
            this._lensPosRadius = radius;
            this._lensPosTheta = theta;
            this._lensPosPhi = phi;
        }

        /// <summary>
        /// ライトの設定
        /// </summary>
        private void SettingLight()
        {
            // 平行光線を使用
            this._device.Lights[0].Type = LightType.Directional;

            // ライトの方向
            this._device.Lights[0].Direction = new Vector3(1.0f, -1.5f, 2.0f);

            // 光の色は白
            this._device.Lights[0].Diffuse = Color.White;

            // 環境光
            this._device.Lights[0].Ambient = Color.FromArgb(255, 128, 128, 128);

            // 0 番のライトを有効
            this._device.Lights[0].Enabled = true;

            // 0 番のライトを更新
            this._device.Lights[0].Update();
        }

        /// <summary>
        /// カメラの設定
        /// </summary>
        private void SettingCamera()
        {
            // レンズの位置を三次元極座標で変換
            float radius = this._lensPosRadius;
            float theta = Geometry.DegreeToRadian(this._lensPosTheta);
            float phi = Geometry.DegreeToRadian(this._lensPosPhi);
            Vector3 lensPosition = new Vector3(
                (float)(radius * Math.Cos(theta) * Math.Cos(phi)),
                (float)(radius * Math.Sin(phi)),
                (float)(radius * Math.Sin(theta) * Math.Cos(phi)));

            // ビュー変換行列を設定
            this._device.Transform.View = Matrix.LookAtLH(
                lensPosition, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));

            // 射影変換を設定
            this._device.Transform.Projection = Matrix.PerspectiveFovLH(
                Geometry.DegreeToRadian(60.0f),
                (float)this._device.Viewport.Width / (float)this._device.Viewport.Height,
                1.0f, 100.0f);
        }

        /// <summary>
        /// デバイスを保証する
        /// </summary>
        /// <returns></returns>
        private bool EnsureDevice()
        {
            // デバイスが動作可能かチェック
            int deviceResult;
            if (!this._device.CheckCooperativeLevel(out deviceResult))
            {
                switch ((ResultCode)deviceResult)
                {
                    case ResultCode.DeviceLost:
                        // まだリセットできる状態ではないので少し待つ
                        System.Threading.Thread.Sleep(10);
                        return false;
                    case ResultCode.DeviceNotReset:
                        // リセット可能状態

                        // デバイスをリセット
                        this._device.Reset(this._device.PresentationParameters);
                        return true;
                    default:
                        // 原因不明(正確には上記以外)
                        // まだリセットできる状態ではないので少し待つ
                        System.Threading.Thread.Sleep(10);
                        return false;
                }
            }
            return true;
        }

        /// <summary>
        /// XYZライン描画
        /// </summary>
        private void RenderXYZLine()
        {
            // ライトの状態を記憶
            bool oldLightEnable = this._device.RenderState.Lighting;

            // ライトを無効
            if (oldLightEnable)
            {
                this._device.RenderState.Lighting = false;
            }

            // テクスチャー無効
            this._device.SetTexture(0, null);

            // 原点に配置
            this._device.SetTransform(TransformType.World, Matrix.Identity);

            // ストリームセット
            this._device.SetStreamSource(0, this._xyzLineVertexBuffer, 0);

            // 頂点フォーマットセット
            this._device.VertexFormat = CustomVertex.PositionColored.Format;

            // 描画
            this._device.DrawPrimitives(PrimitiveType.LineList, 0, 3);

            // ライトを戻す
            if (oldLightEnable)
            {
                this._device.RenderState.Lighting = oldLightEnable;
            }
        }

        /// <summary>
        /// リソースの破棄
        /// </summary>
        private void DisposeResource()
        {
            // XYZラインの破棄
            this.SafeDispose(this._xyzLineVertexBuffer);

            // フォントのリソース解放
            this.SafeDispose(this._font);

            // Direct3D デバイスのリソース解放
            this.SafeDispose(this._device);
        }

        /// <summary>
        /// 安全なリソース破棄
        /// </summary>
        /// <param name="resource">破棄す???リソー???</param>
        private void SafeDispose(IDisposable resource)
        {
            if (resource != null)
            {
                resource.Dispose();
            }
        }
    }
}

ポイントスプライト

ポイントスプライトとはその名のとおり「点のスプライト」です。以前の Tips で普通のスプライトをやりましたが、あれは四角形ポリゴンにテクスチャーを貼ったものです。今回は「点にテクスチャーを貼る」という処理を行います。

しかし、実際「」には大きさというものが存在しないし、頂点データは1つなのでUV座標も複数登録することが出来ません。

そこでポイントスプライト独自の設定が用意されており、座標系大きさ計算方法などを設定することにより点の位置に画像を貼り付けることができるようになります。

また、基本的に向きという概念がないのでどの方向から見ても画像が正面を向く、「ビルボード」と呼ばれる表現方法も同時に実現することが可能です。


/// <summary>
/// 頂点の数
/// </summary>
private static readonly int VertexCount = 1000;

/// <summary>
/// ポイントスプライトの描画範囲(最小値)
/// </summary>
private static readonly Vector3 MinRange = new Vector3(-30.0f, -30.0f, -30.0f);

/// <summary>
/// ポイントスプライトの描画範囲(最大値)
/// </summary>
private static readonly Vector3 MaxRange = new Vector3(30.0f, 30.0f, 30.0f);

今回はポイントスプライトを1000個同時に描画することにします。静的フィールドとしてこの値を保持しておき、このフィールドを他のコード部分に使用すれば、描画個数を変更するときに楽になります。

次の描画範囲ですが、雪が降るようなエフェクトを使用する際、無限遠まで表示させるのは不可能なので下の図のようにボックス型の描画範囲を設定しておきます。

ボックス型描画範囲


/// <summary>
/// 各頂点の位置
/// </summary>
private CustomVertex.PositionOnly[] _vertices = new CustomVertex.PositionOnly[VertexCount];

/// <summary>
/// ポイントスプライト用のテクスチャー
/// </summary>
private Texture _texture = null;

今回は頂点の位置が常に変化するので頂点バッファは使用せず、直接描画時に頂点データを転送する形を取ります。

今回使用するデータは「位置」だけなので「CustomVertex.PositionOnly」構造体の配列を作成します。色は全てテクスチャーに任せるのでいりません。点なので法線は必要ありませんし、ポイントスプライトという特殊な表示方法をとるのでUV座標もいりません。

で、あらかじめ配列を作成していますが、今回頂点は 1000 個使うのでそのまま 1000 をいれてもいいのですが、先ほど宣言した「VertexCount」を指定すれば、数を変更したい場合に「VertexCount」の値だけを変えればいいことになります。

後はポイントスプライト用のテクスチャーを宣言しておきます。


// 頂点の位置をランダムに設定 
Random rand = new Random();
Vector3 range = MaxRange - MinRange;
for (int i = 0; i < VertexCount; i++)
{
    this._vertices[i].X = (float)(rand.NextDouble() * range.X) + MinRange.X;
    this._vertices[i].Y = (float)(rand.NextDouble() * range.Y) + MinRange.Y;
    this._vertices[i].Z = (float)(rand.NextDouble() * range.Z) + MinRange.Z;
}

各頂点の位置を描画範囲内でランダムに配置するようにしています。全部原点にあったらエフェクトも何も無いです。

ランダムな値を取得するには「Random」クラスを作成します。コンストラクタでは「シード」と呼ばれる値を指定できますが、このシードによってどの値から計算を始めるかを指定することが出来ます。何も指定しない場合はPC起動経過時間が使われ。アプリケーション起動ごとに毎回違う位置に配置を行うことが出来ます。

ためしに 0 を入れると、見た目はランダムに配置されているように見えますが、実は毎回同じ位置に配置されていることが分かるかと思います。

後は描画範囲内に配置するように計算していますが、次のランダム値を取得するには「Random.NextDouble」メソッドを使用します。これはランダムで 0.0~1.0 の値を返してくれます。他には「Random.Next」「Random.NextBytes」メソッドがあります。


// ポイントスプライト用のテクスチャー作成
this._texture = TextureLoader.FromFile(this._device, "Texture.bmp");

ポイントスプライト用の画像を読み込み、テクスチャーを作成します。


// Zバッファ無効
this._device.RenderState.ZBufferEnable = false;

Zバッファは、本来は有効でいいのですが、たまに前後関係の問題でアルファブレンディングがかからないことがあるので無効にしています。


// アルファブレンディングを有効にする
this._device.RenderState.AlphaBlendEnable = true;

// アルファブレンディング方法を設定
this._device.RenderState.SourceBlend = Blend.One;
this._device.RenderState.DestinationBlend = Blend.One;

綺麗なエフェクトを表現する場合は大抵アルファブレンディングを使用します。さらに重なったときにより光って見えるようにBlend.One」を指定して色を加算合成するようにします。


// ポイントスプライト有効
this._device.RenderState.PointSpriteEnable = true;

// 点をカメラ空間でスケール有効化
this._device.RenderState.PointScaleEnable = true;

// ポイントサイズの設定
this._device.RenderState.PointSize = 0.25f;

さて、ここからがポイントスプライト独自の設定になります。

まず「PointSpriteEnable」を true に設定することにより、各頂点に画像を表示させることが出来ます。

次の「PointScaleEnable」を true にするとスプライトのサイズがカメラ空間で計算されるので、近くだと大きく、遠くだと小さく見えるようになります。 false を指定するとスクリーン座標での計算になるので距離に関係なく大きさが一定になります。

PointSize」はそのままポイントのサイズになります。ただこれは上の「PointScaleEnable」の設定によって基準が異なり、true の場合はカメラ空間での大きさになり、false だとスクリーン座標での大きさになります。


// ポイントのスケール計算式
this._device.RenderState.PointScaleA = 0.0f;
this._device.RenderState.PointScaleB = 0.0f;
this._device.RenderState.PointScaleC = 1.0f;

ここで設定する A,B,C の値はポイントスプライトの最終的な大きさを導き出すための計算式の値になります。その計算式は下のようになります。

Sz = Vh * Si * sqrt(1 / (A + B * Di + C * Di * Di))

Sz 最終的な表示サイズ
Vh ビューポートの高さ
Si PointSize で設定した値
A PointScaleA で設定した値
B PointScaleB で設定した値
C PointScaleC で設定した値
Di 視点から頂点の位置までの距離

詳しくはヘルプなどを参照してください。


// ライトを無効
this._device.RenderState.Lighting = false;

ポイントスプライトでは通常ライトは使用しないので無効にしておきます。


// 各頂点を下に移動
for (int i = 0; i < VertexCount; i++)
{
    this._vertices[i].Y -= 0.02f;

    // 一番下まで行ったら一番上に戻す
    if (this._vertices[i].Y < MinRange.Y)
    {
        this._vertices[i].Y = MaxRange.Y;
    }
}

毎フレームごとに各頂点を -Y 方向に移動させています。

一番下まで行ったら、また一番上に戻すようにしてループさせています。


// テクスチャーセット
this._device.SetTexture(0, this._texture);

// 頂点フォーマットセット
this._device.VertexFormat = CustomVertex.PositionOnly.Format;

// ポイントスプライト描画
this._device.DrawUserPrimitives(PrimitiveType.PointList, VertexCount, this._vertices);

ポイントスプライトの描画コードです。ポイントスプライトのテクスチャーをセットし、頂点のフォーマットを設定します。

描画は直接頂点データを送る「Device.DrawUserPrimitives」メソッドを使用します。

第1引数にはを描画するので「PrimitiveType.PointList」を渡します。

第2引数は「点の数=プリミティブの数」なので頂点数をそのまま渡します。

第3引数に頂点データを渡します。


// テクスチャー破棄
this.SafeDispose(this._texture);

最後に使用したテクスチャーを破棄します。