[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
Hardware Instancing |
Windows (shader 3.0) |
Shader Instancing |
Windows (shader 2.0) |
VFetch | Xbox360 |
解説 | |
GraphicsDevice.Vertices[0] | 普通のVertexBufferと同じ。モデルを構成する頂点を持っています。ここでは、C#で言うなら「クラス」の役割を果たします。 |
GraphicsDevice.Vertices[1] | 頂点の代わりに、モデルの各インスタンス固有の情報をもっています。C#で言うなら「メンバ変数」がいっぱい詰まっている感じ。構造体の配列みたい。 |
struct VertexPositionColor { float4 Position : POSITION0; float4 Color : COLOR; }; VertexPositionColor VertexShader( VertexPositionColor input, float3 position:POSITION1 ) { input.Position.xyz += position; return input; } float4 PixelShader(float4 color : COLOR) : COLOR0 { return color; } technique HardwareInstancing { pass Pass1 { VertexShader = compile vs_3_0 VertexShader(); PixelShader = compile ps_3_0 PixelShader(); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; class MyGame : Game { GraphicsDeviceManager graphics; ContentManager content; VertexPositionColor[] vertices = new VertexPositionColor[]{ new VertexPositionColor(new Vector3(-0.1f, 0.1f, 0), Color.Blue), new VertexPositionColor(new Vector3(0.1f, 0.1f, 0), Color.White), new VertexPositionColor(new Vector3(0.1f, -0.1f, 0), Color.Red) }; Vector3[] trianglePositions = new Vector3[] { new Vector3(), new Vector3(0.1f, 0.1f, 0) }; //Graphics Device Objects Effect effect; VertexBuffer triangleVertexBuffer; IndexBuffer indexBuffer; VertexBuffer positionVertexBuffer; public MyGame() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { effect = content.Load<Effect>("HardwareInstancing"); graphics.GraphicsDevice.VertexDeclaration = createVertexDeclaration(); triangleVertexBuffer = new VertexBuffer( graphics.GraphicsDevice, VertexPositionColor.SizeInBytes * vertices.Length, ResourceUsage.None ); triangleVertexBuffer.SetData<VertexPositionColor>(vertices); indexBuffer = new IndexBuffer( graphics.GraphicsDevice, sizeof(int) * 3, ResourceUsage.None, IndexElementSize.ThirtyTwoBits ); indexBuffer.SetData<int>(new int[] { 0, 1, 2 }); positionVertexBuffer = new VertexBuffer( graphics.GraphicsDevice, sizeof(float) * 3 * trianglePositions.Length, ResourceUsage.None ); positionVertexBuffer.SetData<Vector3>(trianglePositions); } } VertexDeclaration createVertexDeclaration() { System.Collections.Generic.List<VertexElement> elements = new System.Collections.Generic.List<VertexElement>(); elements.AddRange(VertexPositionColor.VertexElements); elements.Add( new VertexElement( 1, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 1) ); return new VertexDeclaration( graphics.GraphicsDevice, elements.ToArray() ); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); graphics.GraphicsDevice.Vertices[0].SetSource( triangleVertexBuffer, 0, VertexPositionColor.SizeInBytes ); graphics.GraphicsDevice.Vertices[0].SetFrequencyOfIndexData( trianglePositions.Length ); graphics.GraphicsDevice.Vertices[1].SetSource( positionVertexBuffer, 0, sizeof(float) * 3 ); graphics.GraphicsDevice.Vertices[1].SetFrequencyOfInstanceData(1); graphics.GraphicsDevice.Indices = indexBuffer; effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); graphics.GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, //baseVertex 0, //minVertexIndex vertices.Length, //numVertices 0, //startIndex vertices.Length/3 //primitiveCount ); pass.End(); } effect.End(); } }