Top > XNA

XNA

このページの情報は古いです。
2008年頃の情報です。
XNAのバージョン2.0くらいです。
Wikipedia:XNA


初期設定

XNAのバージョン2.0くらい。

  • プロジェクトの作成
    Visual C# を起動。
    [ファイル]タブ -> [新しいプロジェクト] -> 左側の[プロジェクトの種類]をXNA Game Studio 2.0に -> 右側の[Windows Game(2.0)]を選択 -> OK
     
    あとは普通にビルドすれば青い画面が表示されるはずです。

Vsync同期をOFFにする

GraphicsDeviceManager.SynchronizeWithVerticalRetrace = false;

パフォーマンス計測

System.Diagnostics.Stopwatch Stopwatch = new System.Diagnostics.Stopwatch();
Stopwatch.Start();
/// コード
Stopwatch.End();

float time = Stopwatch.ElapsedTicks / (float)(System.Diagnostics.Stopwatch.Frequency * (1.0f / 60.0f)) * 100.0f;

カリング面の変更

GraphicsDevice.RenderState.CullMode = ;

// モードの種類
CullMode.CullCounterClockwiseFace;
CullMode.CullClockwiseFace;
CullMode.None;

深度バッファ(Zバッファ)をONにする

GraphicsDevice.RenderState.DepthBufferEnable = true;

ポリゴンの描画

  • 変数
    VertexDeclaration   m_VertexDeclaration = null; // 頂点データ定義
    VertexPositionColor[] m_Vertices = null; // 頂点データ
    BasicEffect      m_BasicEffect = null; // シェーダ
  • コンストラクタ
    // 頂点データ定義
    m_VertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionColor.VertexElements);
     
    // 頂点データ
    m_Vertices = new VertexPositionColor[3];
     
    m_Vertices[0] = new VertexPositionColor(new Vector3(0.0f, 3.0f, 0.0f), Color.Red);
    m_Vertices[1] = new VertexPositionColor(new Vector3(3.0f, -2.0f, 0.0f), Color.Blue);
    m_Vertices[2] = new VertexPositionColor(new Vector3(-3.0f, -2.0f, 0.0f), Color.Green);
     
    // 基本シェーダ
    m_BasicEffect = new BasicEffect(GraphicsDevice, null);
    m_BasicEffect.VertexColorEnabled = true;
  • 描画
    // 頂点データの定義
    GraphicsDevice.VertexDeclaration = m_VertexDeclaration;
     
    // ViewMatrix
    m_BasicEffect.View = Matrix.CreateLookAt(
      new Vector3(0.0f, 0.0f, 10.0f), // pos
      Vector3.Zero, // view
      Vector3.Up // upper
    );
     
    // Projection
    m_BasicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
      MathHelper.ToRadians(45.0f), // pers
      (float)GraphicsDevice.Viewport.Width / (float)GraphicsDevice.Viewport.Height, // aspect
      1.0f, // near
      100.0f // far
    );
     
    // begin effect
    m_BasicEffect.Begin();
     
    // パスの数だけ描画
    foreach (EffectPass pass in m_BasicEffect.CurrentTechnique.Passes)
    {
      pass.Begin();
    
      // ポリゴンを描画する
      GraphicsDevice.DrawUserPrimitives(
        PrimitiveType.TriangleList, // 描画モード
        m_Vertices, // 頂点データ
        0, // 
        1 // ポリゴン枚数
      );
      pass.End();
    }
    m_BasicEffect.End();

シェーダ

Contentで生成

Effect effect = i_Content.Load("shader/test"); // ./Shader/test.fxをロード
effect.CurrentTechnique = effect.Techniques["main"]; // Technique:mainを使用
EffectParameter uniform = effect.Parameters["WVP"]; // Uniform値を取得

文字列から生成

string src = "..." // ここにソース文字列
CompiledEffect compiledEffect = Effect.CompileEffectFromSource(src, null, null, CompilerOptions.Debug, TargetPlatform.Windows);
Effect effect = new Effect(GraphicsDevice,
  compiledEffect.GetEffectCode(),
  CompilerOptions.Debug,
  null);
effect.CurrentTechnique = effect.Techniques["main"];
EffectParameter uniform = effect.Parameters["WVP"];

使用

uniform.SetValue(Matrix);
effect.Begin();
foreach(EffectPass pass in effect.CurrentTechnique.Passes)
{
  pass.Begin();
  // 描画
  pass.End();
}
effect.End();

生成時のエラー:X3000

なにやら文字コードの関係で、日本語を使用すると発生する可能性有り。
Asciiに戻してからロードするか、コメントも英語でやるか。

CONTENT

外部リソースデータをコンパイルして使用できるようにする物です。

一番簡単な生のtxtをstringに落とし込む事を。

1.ソリューション エクスプローラ

  • >ソリューションを右クリック
  • >追加
  • >新しいプロジェクト
  • >Content Pipeline Extension Library

2.できた.csに以下の記述を。

//========================================
// インポーター
//========================================
// Attributeで拡張子と、説明文を設定する
[ContentImporter(".txt", DisplayName = "TextFileImporter")]
public class TextContentImporter : ContentImporter
{
  // 読み込みメソッドのオーバーライド
  public override string Import(string hfilename, ContentImporterContext hcontext)
  {
    string ret = "";
    using (StreamReader reader = File.OpenText(hfilename))
    {
      ret = reader.ReadToEnd();
    }
    return ret;
  }
}

3.ビルド

4.実行プロジェクトの方へ戻る
Contentに"test.txt"を追加

  • >右クリック
  • >プロパティ
  • >パラメータを以下のように
    ContentImporter:TextFileImporter(先ほどライブラリとして作ったクラスのAttribute名)
    ContentProcessor:No Processing Required
    ビルドアクション:コンパイル
  • >Microsoft.Xna.Framework.Gameを継承したクラスのLoadContentで
    string txt = Content.Load("test.txt");

で読み込めているはず。

RenderTexture

初期化

GraphicsDevice device      = getDevice();  // デバイス
RenderTarget2D target      = null;
RenderTarget   last_target = null;
target = new RenderTarget2D(device,
  i_Width,
  i_Height,
  1,    // mip level
  SurfaceFormat.Bgr565); // format

設定

last_target = device.GetRenderTarget(0);
device.SetRenderTarget(0, target);  // 0番のターゲットを
device.Clear(ClearOptions.Target, Color.CornflowerBlue, 0, 0);

設定した後は普通にモデル描画

解除

device.SetRenderTarget(0, last_target as RenderTarget2D);
last_target = null;

テクスチャとして、描画したものを取得

Texture2D tex = target.GetTexture();

Tips


リロード   新規 編集 凍結 差分 添付 複製 名前変更   ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2011-07-29 (金) 02:26:09 (4655d)