MonoGameで位置と色と法線とUV座標を持つカスタム頂点を使いたい
2018年2月13日 (火) 05:23時点におけるimported>Administratorによる版 (ページの作成:「テクスチャを貼らなければ頂点カラーで塗られ、テクスチャを貼ればテクスチャマッピングもバンプマッピングも...」)
テクスチャを貼らなければ頂点カラーで塗られ、テクスチャを貼ればテクスチャマッピングもバンプマッピングもされる。 そんな位置と頂点カラーと法線とUV座標を持つゴージャスなカスタム頂点フォーマット。 個人的にはゲームではなく細胞を3D表示するのにMonoGameを使っているので、動的に頂点を生成して、かつテクスチャはほぼ利用しないが、稀にマスク的にテクスチャマッピングとバンプマッピングを使いたいこともある。
MonoGameに標準搭載されている頂点フォーマット構造体たち。
VertexPosition | 位置 |
VertexPositionColor | 位置と頂点カラー |
VertexPositionColorTexture | 位置と頂点カラーとUV座標 |
VertexPositionNormalTexture | 位置と法線とUV座標 |
VertexPositionTexture | 位置とUV座標 |
無いんだが、これが。
そんなときはIVertexTypeインターフェイスを実装するだけでカスタム頂点フォーマット構造体を作れるそうだ。
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
public struct VertexPositionColorNormalTexture : IVertexType
{
public Vector3 Position;
public Color Color;
public Vector3 Normal;
public Vector2 TextureCoordinate;
public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration(
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement((sizeof(float) * 3), VertexElementFormat.Color, VertexElementUsage.Color, 0),
new VertexElement((sizeof(float) * 3) + (4), VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
new VertexElement((sizeof(float) * 3 * 2) + (4), VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
);
public VertexPositionColorNormalTexture(Vector3 position, Color color, Vector3 normal, Vector2 textureCoordinate)
{
Position = position;
Color = color;
normal.Normalize();
Normal = normal;
TextureCoordinate = textureCoordinate;
}
VertexDeclaration IVertexType.VertexDeclaration
{
get { return VertexDeclaration; }
}
}
}