XNAを使用したシンプルな3Dゲームの作成/スカイスフィアの追加

SkySphereレンダラの作成 編集

スカイスフィアをレンダリングするために、魚をレンダリングしたものと似た新しいレンダリングクラスを作成していきますが、ただしアーマチュア構造なしでも所定の位置にモデルをレンダリングできるように修正を加えたものにします。 まず、新しいクラスファイルを作成するために、プロジェクトを右クリックし、 'Add'/'New Item' を選択し、 'Class'と選択します。 クラスとファイルの名前を'GenericModelRenderer'に変更します。

次の文を使用して、デフォルトを変更します。

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SkinnedModel;

そして確実にクラスがXNAの名前空間を使用するように、クラス名に以下のものを添付します

: Microsoft.Xna.Framework.Game

.

次のクラス変数とコンストラクタを追加します。

public Model currentModel;
public Vector3 Translation = new Vector3(0, 0, 0);
public Vector3 Rotation = new Vector3(0, 0, 0);
public float Scale = 1.0f;
public GenericModelRenderer(Model currentModelInput)
{
    currentModel = currentModelInput;
}

前のコードと違って、初期化が必要なものはかなり少なく、アニメーションプレーヤーを初期化する必要はありません。 あなたはこのクラスに 'ModelDraw' メソッドに対応する同様の傾向を見ることができるはずです。

public void ModelDraw(GraphicsDevice device, Vector3 cameraPosition, Vector3 cameraTarget, float farPlaneDistance)
{
    Matrix[] transforms = new Matrix[currentModel.Bones.Count];
    currentModel.CopyAbsoluteBoneTransformsTo(transforms);

    // Compute camera matrices.
    Matrix view = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Right);

    //Calculate the aspect ratio
    float aspectRatio = (float)device.Viewport.Width /
                                (float)device.Viewport.Height;

    Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio,
        1.0f, farPlaneDistance);

    // Draw the model. A model can have multiple meshes, so loop.
    foreach (ModelMesh mesh in currentModel.Meshes)
    {
        // This is where the mesh orientation is set, as well as our camera and projection.
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.World = transforms[mesh.ParentBone.Index] *
                Matrix.CreateRotationX(Rotation.X) *
                Matrix.CreateRotationY(Rotation.Y) *
                Matrix.CreateRotationZ(Rotation.Z) *
                Matrix.CreateScale(Scale) *
                Matrix.CreateWorld(Translation, Vector3.Forward, Vector3.Up);

            effect.View = view;
            effect.Projection = projection;
        }
        mesh.Draw();
    }
}

描画メソッドが他の魚コードと同様の引数を使用していたり、多かれ少なかれ同じような方法を使用していることに気づくはずです。

レンダラの使用 編集

モデルのフォルダに 'SkySphere'という名前の新しいフォルダを置き、あなたのモデルとテクスチャを追加しましょう。 メインの 'Game1'ファイルに戻って、新たなクラス変数を追加します

GenericModelRenderer SkySphere; //Your SkySphere model

.

LoadContentファイルに移動して、次の行を追加し、クラスの初期化と、レンダリング用モデルのセットアップをします。

currentModel = Content.Load<Model>("Models\\SkySphere\\SkyModel");
SkySphere = new GenericModelRenderer(currentModel);//Add the skysphere

そしてdrawメソッドで、魚モデルの描画メソッドと同じような場所に、次の行を追加します。

SkySphere.ModelDraw(GraphicsDevice, cameraPosition, cameraTarget, farPlaneDistance);

コー​​ドをレンダリングすると、あなたが私のものと同じ設定をしている場合なら、このような画面が表示されるはずです。

ファイル:XNA Bad sky sphere.gif

これは私たちが本当に必要としているものではないと気づくかもしれません。 SkySphereクラス内のパブリック変数を調整することで、位置とスケールを調整することができます。 私の場合、LoadContent()メソッド内のクラスの初期化の後に、次の変更を加えました。

SkySphere.Translation.X = 120.0f;
SkySphere.Translation.Z = 15.0f;
SkySphere.Scale = 30.0f;

お好みに調整すると、こちらの私のものと同じような画面が表示されるはずです。

ファイル:XNA Good sky sphere.gif