差分

ナビゲーションに移動 検索に移動

MonoGame/クラスライブラリ/VertexPositionColor構造体

5,320 バイト追加, 2017年11月6日 (月) 11:23
ページの作成:「VertexPositionColor構造体は、MonoGameに標準搭載されている頂点構造体のひとつで、位置と色を持つ。 == 名前空間 == * MonoGame/クラ...」
VertexPositionColor構造体は、MonoGameに標準搭載されている頂点構造体のひとつで、位置と色を持つ。

== 名前空間 ==
* [[MonoGame/クラスライブラリ/Microsoft.Xna.Framework.Graphics名前空間|Microsoft.Xna.Framework.Graphics名前空間]]

== 構文 ==
[DataContractAttribute]
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct VertexPositionColor : IVertexType

== コンストラクタ ==
{| class="wikitable"
|+
!
! 名称
! 概要
|-
|
| VertexPositionColor
|
|}

== フィールド ==
{| class="wikitable"
|+
!
! 名称
! 概要
|-
|
| Color
| 頂点の色
|-
|
| Position
| XYZ座標
|-
| static
| VertexDeclaration
| 頂点の構造
|}

== メソッド ==
{| class="wikitable"
|+
!
! 名称
! 概要
|-
|
| Equals
|
|-
|
| GetHashCode
|
|-
|
| ToString
|
|}

== オペレータ ==
{| class="wikitable"
|+
!
! 名称
! 概要
|-
| static
| Equality
|
|-
| static
| Inequality
|
|}

== 実装例 ==
この例ではVertexBufferを作らずにDrawUserPrimitivesメソッドで描画している。
VertexBufferを作りDrawPrimitivesメソッドで描画しても同じ結果になる。
どっちが良いのかは知らん。

BasicEffectのVertexColorEnabledプロパティで色付けを有効にしている。これを設定しないと真っ白になり、設定すると頂点カラー間でグラデーションが掛かる。

<source lang="csharp">
using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DrawVertex
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager _graphics;
VertexPositionColor[] _vertices;
Vector3 _cameraPosition = new Vector3(30, 0, 20);
BasicEffect _effect;

public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
// ウインドウサイズを変更する
_graphics.PreferredBackBufferWidth = 800;
_graphics.PreferredBackBufferHeight = 600;
_graphics.ApplyChanges();

// MonoGame標準搭載の固定シェーダーを生成する
_effect = new BasicEffect(_graphics.GraphicsDevice);
// 色を有効にする
_effect.VertexColorEnabled = true;

// 頂点データを作成する
_vertices = new VertexPositionColor[6];
_vertices[0] = new VertexPositionColor(new Vector3(-10, -10, 0), Color.Red);
_vertices[1] = new VertexPositionColor(new Vector3(-10, +10, 0), Color.Green);
_vertices[2] = new VertexPositionColor(new Vector3(+10, -10, 0), Color.Blue);
_vertices[3] = new VertexPositionColor(new Vector3(-10, +10, 0), Color.Green);
_vertices[4] = new VertexPositionColor(new Vector3(+10, +10, 0), Color.White);
_vertices[5] = new VertexPositionColor(new Vector3(+10, -10, 0), Color.Blue);

base.Initialize();
}

protected override void LoadContent()
{
}

protected override void Update(GameTime gameTime)
{
// For Mobile devices, this logic will close the Game when the Back button is pressed
// Exit() is obsolete on iOS
#if !__IOS__ && !__TVOS__
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
#endif
base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
_graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// カメラ
var cameraLookAtVector = Vector3.Zero;
var cameraUpVector = Vector3.UnitZ;
_effect.View = Matrix.CreateLookAt(_cameraPosition, cameraLookAtVector, cameraUpVector);

float aspectRatio = _graphics.PreferredBackBufferWidth / (float)_graphics.PreferredBackBufferHeight;
float fieldOfView = Microsoft.Xna.Framework.MathHelper.PiOver4;
float nearClipPlane = 1;
float farClipPlane = 200;
_effect.Projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearClipPlane, farClipPlane);

// 描画
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();

_graphics.GraphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleList,
_vertices,
vertexOffset: 0,
primitiveCount: 2);
};

base.Draw(gameTime);
}
}
}
</source>

== 関連項目 ==
* [[MonoGame/クラスライブラリ/IVertexTypeインターフェイス]]
** [[MonoGame/クラスライブラリ/VertexPositionColor構造体]]
** [[MonoGame/クラスライブラリ/VertexPositionTexture構造体]]
** [[MonoGame/クラスライブラリ/VertexPositionColorTexture構造体]]
** [[MonoGame/クラスライブラリ/VertexPositionNormalTexture構造体]]

[[category: MonoGame]]
匿名利用者

案内メニュー