[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
3Dグラフィックスでは、奥にある物体は手前にある物体に
隠されなければなりません。
例えば、壁の向こうに敵がいる場合、敵は壁に隠れて見えません。
(当たり前です!)
XNAではこのことを特に気にする必要はありません。
ふつうに描画するだけで自動的に奥のものが隠れてくれます。
(奥に赤い三角形、手前に青い三角形があります。
奥の三角形は手前の三角形に隠れています。)
using System; 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; VertexPositionNormalTexture[] vertices; Triangle blueTriangle = new Triangle ( new Vector3(0, 1, 0), new Vector3(1, -0.5f, 0), new Vector3(-1, -0.5f, 0), Color.Blue ); 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); } Vector3 getPosition(int index, int count) { return new Vector3( (float)Math.Sin(2 * Math.PI * index / count), 0, (float)Math.Cos(2 * Math.PI * index / count) ); } 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 = DepthStencilState.None; blueTriangle.Draw(GraphicsDevice, effect); redTriangle.Draw(GraphicsDevice, effect); } }
using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; VertexPositionNormalTexture[] vertices; public MyGame() { graphics = new GraphicsDeviceManager(this); int planeCount = 30; vertices = new VertexPositionNormalTexture[planeCount * 6]; for (int i = 0; i < planeCount; i++) { VertexPositionNormalTexture[] plane = new VertexPositionNormalTexture[4]; plane[0] = new VertexPositionNormalTexture( getPosition(i, planeCount) + new Vector3(0, 1, 0), getPosition(i, planeCount), new Vector2((float)i / planeCount, 0) ); plane[1] = new VertexPositionNormalTexture( getPosition(i + 1, planeCount) + new Vector3(0, 1, 0), getPosition(i + 1, planeCount), new Vector2((float)(i + 1) / planeCount, 0) ); plane[2] = new VertexPositionNormalTexture( getPosition(i, planeCount) + new Vector3(0, -1, 0), getPosition(i, planeCount), new Vector2((float)i / planeCount, 1) ); plane[3] = new VertexPositionNormalTexture( getPosition(i + 1, planeCount) + new Vector3(0, -1, 0), getPosition(i + 1, planeCount), new Vector2((float)(i + 1) / planeCount, 1) ); vertices[i * 6] = plane[0]; vertices[i * 6 + 1] = plane[1]; vertices[i * 6 + 2] = plane[2]; vertices[i * 6 + 3] = plane[1]; vertices[i * 6 + 4] = plane[3]; vertices[i * 6 + 5] = plane[2]; } } Vector3 getPosition(int index, int count) { return new Vector3( (float)Math.Sin(2 * Math.PI * index / count), 0, (float)Math.Cos(2 * Math.PI * index / count) ); } protected override void LoadContent() { effect = new BasicEffect(GraphicsDevice) { LightingEnabled = true, View = Matrix.CreateLookAt ( new Vector3(0, 0, 8), //カメラの位置 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); effect.DirectionalLight0.Direction = new Vector3(1, 0, -1); effect.DirectionalLight0.DiffuseColor = Color.Gray.ToVector3(); effect.DirectionalLight0.SpecularColor = Color.White.ToVector3(); effect.DiffuseColor = Color.Green.ToVector3(); effect.SpecularColor = Color.Blue.ToVector3(); effect.SpecularPower = 100; foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>( PrimitiveType.TriangleList, vertices, 0,//vertexOffset vertices.Length / 3 //primitiveCount ); } } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; VertexPositionNormalTexture[] vertices = new[] { new VertexPositionNormalTexture { Position = new Vector3(0, 1, 0), Normal = new Vector3(0, 0, 1), }, new VertexPositionNormalTexture { Position = new Vector3(1, 0, 0), Normal = new Vector3(0, 0, 1), }, new VertexPositionNormalTexture { Position = new Vector3(-1, 0, 0), Normal = new Vector3(0, 0, 1), }, }; public MyGame() { graphics = new GraphicsDeviceManager(this); } protected override void LoadContent() { effect = new BasicEffect(GraphicsDevice) { LightingEnabled = 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 //カメラからこれより遠い物体は画面に映らない ) }; } protected override void UnloadContent() { effect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); effect.DirectionalLight0.Direction = new Vector3(1, 0, -1); effect.DirectionalLight0.DiffuseColor = Color.Gray.ToVector3(); effect.DirectionalLight0.SpecularColor = Color.White.ToVector3(); foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>( PrimitiveType.TriangleList, vertices, 0,//vertexOffset 1 //primitiveCount ); } } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; VertexPositionTexture[] vertices = new[] { new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)), }; Texture2D texture; public MyGame() { graphics = new GraphicsDeviceManager(this); } protected override void LoadContent() { effect = new BasicEffect(GraphicsDevice) { TextureEnabled = 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 //カメラからこれより遠い物体は画面に映らない ) }; texture = Content.Load<Texture2D>("Content/explosion"); } protected override void UnloadContent() { effect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); effect.Texture = texture; foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>( PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3 ); } } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; VertexPositionTexture[] vertices = new[] { new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)), new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)), }; Texture2D texture; public MyGame() { graphics = new GraphicsDeviceManager(this); } protected override void LoadContent() { effect = new BasicEffect(GraphicsDevice) { TextureEnabled = 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 //カメラからこれより遠い物体は画面に映らない ) }; texture = Content.Load<Texture2D>("Content/explosion"); } protected override void UnloadContent() { effect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.BlendState = BlendState.Additive; effect.Texture = texture; foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>( PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3 ); } } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; VertexPositionTexture[] vertices; Texture2D texture; public MyGame() { graphics = new GraphicsDeviceManager(this); const int particleCount = 20; vertices = new VertexPositionTexture[particleCount * 6]; for (int i = 0; i < particleCount; i++) { vertices[i * 6 + 0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)); vertices[i * 6 + 1] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)); vertices[i * 6 + 2] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)); vertices[i * 6 + 3] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)); vertices[i * 6 + 4] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)); vertices[i * 6 + 5] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)); for (int j = 0; j < 6; j++) { vertices[i * 6 + j].Position *= 0.5f; vertices[i * 6 + j].Position += new Vector3( (float)System.Math.Cos(2 * System.Math.PI * i / particleCount), 0, (float)System.Math.Sin(2 * System.Math.PI * i / particleCount) ); } } } protected override void LoadContent() { effect = new BasicEffect(GraphicsDevice) { TextureEnabled = true, View = Matrix.CreateLookAt ( new Vector3(0, 3, 3), //カメラの位置 new Vector3(0, 0, 0), //カメラの見る点 new Vector3(0, 1, 0) //カメラの上向きベクトル ), Projection = Matrix.CreatePerspectiveFieldOfView ( MathHelper.ToRadians(45), //視野の角度。ここでは45° GraphicsDevice.Viewport.AspectRatio,//画面のアスペクト比(=横/縦) 1, //カメラからこれより近い物体は画面に映らない 100 //カメラからこれより遠い物体は画面に映らない ) }; texture = Content.Load<Texture2D>("Content/explosion"); } protected override void UnloadContent() { effect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.BlendState = BlendState.Additive; effect.Texture = texture; foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>( PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3 ); } } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; VertexPositionTexture[] vertices; Texture2D texture; public MyGame() { graphics = new GraphicsDeviceManager(this); const int particleCount = 20; vertices = new VertexPositionTexture[particleCount * 6]; for (int i = 0; i < particleCount; i++) { vertices[i * 6 + 0] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)); vertices[i * 6 + 1] = new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)); vertices[i * 6 + 2] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)); vertices[i * 6 + 3] = new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)); vertices[i * 6 + 4] = new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)); vertices[i * 6 + 5] = new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)); for (int j = 0; j < 6; j++) { vertices[i * 6 + j].Position *= 0.5f; vertices[i * 6 + j].Position += new Vector3( (float)System.Math.Cos(2 * System.Math.PI * i / particleCount), 0, (float)System.Math.Sin(2 * System.Math.PI * i / particleCount) ); } } } protected override void LoadContent() { effect = new BasicEffect(GraphicsDevice) { TextureEnabled = true, View = Matrix.CreateLookAt ( new Vector3(0, 3, 3), //カメラの位置 new Vector3(0, 0, 0), //カメラの見る点 new Vector3(0, 1, 0) //カメラの上向きベクトル ), Projection = Matrix.CreatePerspectiveFieldOfView ( MathHelper.ToRadians(45), //視野の角度。ここでは45° GraphicsDevice.Viewport.AspectRatio,//画面のアスペクト比(=横/縦) 1, //カメラからこれより近い物体は画面に映らない 100 //カメラからこれより遠い物体は画面に映らない ) }; texture = Content.Load<Texture2D>("Content/explosion"); } protected override void UnloadContent() { effect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.BlendState = BlendState.Additive; GraphicsDevice.DepthStencilState = DepthStencilState.None; effect.Texture = texture; foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>( PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3 ); } } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class MyGame : Game { GraphicsDeviceManager graphics; BasicEffect effect; VertexPositionColor[] vertices = new[] { new VertexPositionColor(new Vector3(0, 1, 0), Color.White), new VertexPositionColor(new Vector3(1, 0, 0), Color.Red), new VertexPositionColor(new Vector3(-1, 0, 0), Color.Blue), new VertexPositionColor(new Vector3(0, 1, -1), Color.White), new VertexPositionColor(new Vector3(1, 0, -1), Color.Red), new VertexPositionColor(new Vector3(-1, 0, -1), Color.Blue), }; public MyGame() { graphics = new GraphicsDeviceManager(this); } protected override void LoadContent() { effect = 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 //カメラからこれより遠い物体は画面に映らない ) }; GraphicsDevice.RasterizerState = new RasterizerState { FillMode = FillMode.WireFrame }; } protected override void UnloadContent() { effect.Dispose(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionColor> ( PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3 ); } } }