[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
| 名前 | 座標 | 色 | テクスチャ座標 | 法線 |
| VertexPositionColor | ○ | ○ | × | × |
| VertexPositionColorTexture | ○ | ○ | ○ | × |
| VertexPositionNormalTexture | ○ | × | ○ | ○ |
| VertexPositionTexture | ○ | × | ○ | × |
| メンバ名 | 説明 |
| Single | floatです。(32bit) |
| Vector2 | Vector2構造体です。 |
| Vector3 | Vector3構造体です。 |
| Vector4 | Vector4構造体です。 |
| HalfVector2 | 16bitの浮動少数点数が2つです。(VertexShaderのバージョンが2.0以上でなければなりません) |
| HalfVector4 | 16bitの浮動小数点数が4つです。(VertexShaderのバージョンが2.0以上でなければなりません) |
| Color | Color構造体です。 |
| NormalizedShort2 | 符号付き16bit整数が2つです。それぞれ小数に展開されて0~1の範囲内の大きさになります。 |
| NormalizedShort4 | 符号付き16bit整数が4つです。それぞれ小数に展開されて0~1の範囲内の大きさになります。 |
| Short2 | 符号付き16bit整数が2つです。 |
| Short4 | 符号付き16bit整数が4つです。 |
| Byte4 | unsinged byteが4つです。 |
| メンバ名 | 説明 |
| Position | 頂点の位置です。 |
| Normal | 法線です。 |
| Color | 色です。UsageIndexが0の時にはディフューズの色を表し、UsageIndexが1の時にはスペキュラの色を表します。 |
| TextureCoordinate | テクスチャの座標です。 |
| PointSize | ポイントスプライトの大きさです。 |
| Depth | 深度です。 |
| Sample | サンプラーデータです。 |
| Fog | フォグに使うデータです。 |
| Binormal | 従法線ベクトル(接ベクトル×法線ベクトル、接線と法線の両方に対して垂直です)データです。 |
| BlendIndices | ブレンディングのインデックスのデータです。 |
| BlendWeight | ブレンディングに使われる重みのデータです。 |
| Tangent | 接線ベクトルです。 |
| TessellateFactor | テセラレーションに使われる浮動小数点数です。 |
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; public struct VertexPositionNormalColor:IVertexType { public Vector3 Position; public Vector3 Normal; public Color Color; public VertexPositionNormalColor(Vector3 position, Vector3 normal, Color color) { this.Position = position; this.Normal = normal; this.Color = color; } public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration ( new VertexElement( 0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0 ), new VertexElement( sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0 ), new VertexElement( sizeof(float) * (3 + 3), VertexElementFormat.Color, VertexElementUsage.Color, 0 ) ); VertexDeclaration IVertexType.VertexDeclaration { get { return VertexDeclaration; } } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect basicEffect; VertexPositionNormalColor[] vertices = { new VertexPositionNormalColor(new Vector3(0, 1, 0),new Vector3(0, 0, 1), Color.Red), new VertexPositionNormalColor(new Vector3(1, 0, 0),new Vector3(0, 0, 1), Color.Red), new VertexPositionNormalColor(new Vector3(-1, 0, 0),new Vector3(0, 0, 1), Color.Red) }; public MyGame() { graphics = new GraphicsDeviceManager(this); } protected override void LoadContent() { basicEffect = new BasicEffect(GraphicsDevice) { VertexColorEnabled = true, View = Matrix.CreateLookAt ( new Vector3(0, 0, 3), //カメラの位置 new Vector3(0, 0, 0), //カメラの見る点 new Vector3(0, 1, 0) //カメラの上向きベクトル ), Projection = Matrix.CreatePerspectiveFieldOfView ( MathHelper.ToRadians(45), //視野の角度。ここでは45° GraphicsDevice.Viewport.AspectRatio,//画面のアスペクト比(=横/縦) 1, //カメラからこれより近い物体は画面に映らない 100 //カメラからこれより遠い物体は画面に映らない ) }; basicEffect.EnableDefaultLighting(); } protected override void UnloadContent() { basicEffect.Dispose(); } protected override void Update(GameTime gameTime) { basicEffect.World *= Matrix.CreateRotationY(MathHelper.ToRadians(1)); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); foreach (var pass in basicEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionNormalColor> ( PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3 ); } } }
今まではウィンドウの画面にだけポリゴンを描画していましたが、
XNAでは実はテクスチャに対して描画することも出来ます。
これによりテクスチャを動的に作り出すことができ、
例えばゲームの中のアイテムとしてパソコンを作って、その画面を動かしたり出来るでしょう。
テクスチャに対していろいろ描画して、
それをゲームの中のパソコン画面に貼り付ければいいのです。
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect basicEffect; VertexPositionColor[] vertices = { new VertexPositionColor(new Vector3(0, 1, 0), Color.White), new VertexPositionColor(new Vector3(1, 0, 0), Color.Blue), new VertexPositionColor(new Vector3(-1, 0, 0), Color.Red) }; RenderTarget2D renderTarget; SpriteBatch spriteBatch; public MyGame() { graphics = new GraphicsDeviceManager(this); } protected override void LoadContent() { basicEffect = new BasicEffect(GraphicsDevice) { VertexColorEnabled = true, View = Matrix.CreateLookAt ( new Vector3(0, 0, 5), //カメラの位置 new Vector3(0, 0, 0), //カメラの見る点 new Vector3(0, 1, 0) //カメラの上向きベクトル ), Projection = Matrix.CreatePerspectiveFieldOfView ( MathHelper.ToRadians(45), //視野の角度。ここでは45° 400/200,//画面のアスペクト比(=横/縦) 1, //カメラからこれより近い物体は画面に映らない 100 //カメラからこれより遠い物体は画面に映らない ) }; spriteBatch = new SpriteBatch(GraphicsDevice); renderTarget = new RenderTarget2D(GraphicsDevice, 400, 200); } protected override void UnloadContent() { basicEffect.Dispose(); } protected override void Draw(GameTime gameTime) { renderToRenderTarget(); GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(renderTarget, new Rectangle(0, 0, 400, 200), Color.White); spriteBatch.End(); } private void renderToRenderTarget() { GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.Clear(Color.Gray); foreach (var pass in basicEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionColor> ( PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3 ); } GraphicsDevice.SetRenderTarget(null); } }
| 左スクリーン | 右スクリーン | |
| X | 0 | 左スクリーンの横幅 |
| Width | 画面の横幅の半分 | 画面の横幅の半分 |
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect basicEffect; VertexPositionColor[] vertices = { new VertexPositionColor(new Vector3(0, 1, 0), Color.White), new VertexPositionColor(new Vector3(1, 0, 0), Color.Blue), new VertexPositionColor(new Vector3(-1, 0, 0), Color.Red) }; public MyGame() { graphics = new GraphicsDeviceManager(this); } protected override void LoadContent() { basicEffect = new BasicEffect(GraphicsDevice) { VertexColorEnabled = true, View = Matrix.CreateLookAt ( new Vector3(0, 0, 2), //カメラの位置 new Vector3(0, 0, 0), //カメラの見る点 new Vector3(0, 1, 0) //カメラの上向きベクトル ), Projection = Matrix.CreatePerspectiveFieldOfView ( MathHelper.ToRadians(45), //視野の角度。ここでは45° GraphicsDevice.Viewport.AspectRatio / 2,//画面のアスペクト比(=横/縦) 1, //カメラからこれより近い物体は画面に映らない 100 //カメラからこれより遠い物体は画面に映らない ) }; } protected override void UnloadContent() { basicEffect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Viewport fullViewport = graphics.GraphicsDevice.Viewport; Viewport leftViewport = fullViewport; leftViewport.Width /= 2; graphics.GraphicsDevice.Viewport = leftViewport; drawTriangle(); Viewport rightViewport = leftViewport; rightViewport.X = leftViewport.Width; graphics.GraphicsDevice.Viewport = rightViewport; drawTriangle(); graphics.GraphicsDevice.Viewport = fullViewport; } private void drawTriangle() { foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionColor>( PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3 ); } } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class Triangle { VertexPositionColor[] vertices = new VertexPositionColor[3]; public Triangle(Vector3 p1, Vector3 p2, Vector3 p3, Color color) { vertices[0] = new VertexPositionColor(p1, color); vertices[1] = new VertexPositionColor(p2, color); vertices[2] = new VertexPositionColor(p3, color); } public void Draw(GraphicsDevice graphicsDevice, BasicEffect effect) { effect.VertexColorEnabled = true; foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); graphicsDevice.DrawUserPrimitives<VertexPositionColor>( PrimitiveType.TriangleList, vertices, 0, 1 ); } } } class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; Triangle whiteTriangle = new Triangle( new Vector3(0, 1, 1), new Vector3(1, 0, 1), new Vector3(-1, 0, 1), Color.White ); Triangle blueTriangle = new Triangle( new Vector3(1, 1, 0), new Vector3(0, 0, 0), new Vector3(-1, 1, 0), Color.Blue ); DepthStencilState stencilTemplateCreator = new DepthStencilState { StencilEnable = true, StencilFunction = CompareFunction.Always, StencilPass = StencilOperation.Replace, ReferenceStencil = 1, DepthBufferWriteEnable = false, }; BlendState noColorWriter = new BlendState { ColorWriteChannels = ColorWriteChannels.None }; DepthStencilState sprayer = new DepthStencilState { StencilEnable = true, ReferenceStencil = 0, StencilFunction = CompareFunction.Less }; public MyGame() { graphics = new GraphicsDeviceManager(this); graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8; } protected override void LoadContent() { effect = new BasicEffect(GraphicsDevice) { View = Matrix.CreateLookAt ( new Vector3(0, 0, 3), //カメラの位置 new Vector3(0, 0, 0), //カメラの見る点 new Vector3(0, 1, 0) //カメラの上向きベクトル ), Projection = Matrix.CreatePerspectiveFieldOfView ( MathHelper.ToRadians(45), //視野の角度。ここでは45° GraphicsDevice.Viewport.AspectRatio,//画面のアスペクト比(=横/縦) 1, //カメラからこれより近い物体は画面に映らない 100 //カメラからこれより遠い物体は画面に映らない ) }; } protected override void UnloadContent() { effect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.DepthStencilState = stencilTemplateCreator; GraphicsDevice.BlendState = noColorWriter; whiteTriangle.Draw(GraphicsDevice, effect); GraphicsDevice.DepthStencilState = sprayer; GraphicsDevice.BlendState = BlendState.Opaque; blueTriangle.Draw(GraphicsDevice, effect); } }
| メンバ名 | 説明 |
| Depth16 | 深度バッファは16bitです。 |
| Depth24 | 深度バッファは24bitです。ただし、実際には32bit確保されます。そのうち使える分が24bitということです。 |
| Depth24Stencil8 | 深度バッファは24bitで、ステンシルバッファは8bitです。 |
| None | 深度バッファを作りません。 |
| メンバ名 | 説明 |
| Decrement | ステンシル・バッファの値をデクリメントします。0より小さくなったらMaxの値になります。 |
| DecrementSaturation | ステンシル・バッファの値をデクリメントします。ただし、0になるまでです。 |
| Increment | ステンシル・バッファの値をインクリメントします。Maxの値を超えたら0に戻ります。 |
| IncrementSaturation | ステンシル・バッファの値をインクリメントします。ただし、Maxの値になるまでです。 |
| Invert | ステンシル・バッファのビットを反転します。 |
| Keep | ステンシル・バッファの値を書き換えません。デフォルトの値です。 |
| Replace | ステンシル・バッファをRenderState.ReferenceStencilプロパティの値に書き換えます。 |
| Zero | ステンシル・バッファの値を0にセットします。 |
物を半透明にするアルファ・ブレンディングですが、
ブレンド関数を使うことによってもっと別のことも出来ます。
アルファ・ブレンディングとはもう少し一般的にいうと、
色を混ぜる(ブレンドする)ということです。
ある混ぜ方をすると物が半透明になりますが、
別の混ぜ方をすれば別の効果が得られます。
前回では2つの色は常に足されましたが、(変わったのは係数だけ)
これは逆に引いたり、あるいは最大値をとったり、
最小値をとったりするようにも変更できます。
(そのため、アルファは単なる不透明度ではない場合だってたくさんあり、
むしろそれがほとんどなのです。)
このような、混ぜ方の関数を設定するのが
BlendState.ColorBlendFunctionプロパティです。
| メンバ名 | 説明 |
| Add | "最終的な色" = ("これから描画するポリゴンの色" × SourceBlend) + ("描画する前の色" × DestinationBlend) |
| Max | "最終的な色" = max( ("これから描画するポリゴンの色" × SourceBlend), ("描画する前の色" × DestinationBlend) ) |
| Min | "最終的な色" = min( ("これから描画するポリゴンの色" × SourceBlend), ("描画する前の色" × DestinationBlend) ) |
| ReverseSubtract | "最終的な色" = ("描画する前の色" × DestinationBlend) - ("これから描画するポリゴンの色" × SourceBlend) |
| Subtract | "最終的な色" = ("これから描画するポリゴンの色" × SourceBlend) - ("描画する前の色" × DestinationBlend) |
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class Triangle { VertexPositionColor[] vertices = new VertexPositionColor[3]; public Triangle(Vector3 p1, Vector3 p2, Vector3 p3, Color color) { vertices[0] = new VertexPositionColor(p1, color); vertices[1] = new VertexPositionColor(p2, color); vertices[2] = new VertexPositionColor(p3, color); } public void Draw(GraphicsDevice graphicsDevice, BasicEffect effect) { effect.VertexColorEnabled = true; foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); graphicsDevice.DrawUserPrimitives<VertexPositionColor>( PrimitiveType.TriangleList, vertices, 0, 1 ); } } } class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; Triangle redTriangle = new Triangle ( new Vector3(1, 0.5f, -1), new Vector3(0, -1, -1), new Vector3(-1, 0.5f, -1), Color.Red ); public MyGame() { graphics = new GraphicsDeviceManager(this); } protected override void LoadContent() { effect = new BasicEffect(GraphicsDevice) { View = Matrix.CreateLookAt ( new Vector3(0, 0, 3), //カメラの位置 new Vector3(0, 0, 0), //カメラの見る点 new Vector3(0, 1, 0) //カメラの上向きベクトル ), Projection = Matrix.CreatePerspectiveFieldOfView ( MathHelper.ToRadians(45), //視野の角度。ここでは45° GraphicsDevice.Viewport.AspectRatio,//画面のアスペクト比(=横/縦) 1, //カメラからこれより近い物体は画面に映らない 100 //カメラからこれより遠い物体は画面に映らない ) }; GraphicsDevice.BlendState = new BlendState { ColorSourceBlend = Blend.SourceAlpha, ColorDestinationBlend = Blend.One, ColorBlendFunction = BlendFunction.ReverseSubtract, }; } protected override void UnloadContent() { effect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); redTriangle.Draw(GraphicsDevice, effect); } }