[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
struct VertexPositionColor { float4 Position : POSITION0; float4 Color : COLOR; }; VertexPositionColor MyVertexShader( VertexPositionColor input, float3 position : POSITION1 ) { input.Position.xyz += position; return input; } float4 MyPixelShader(float4 color : COLOR) : COLOR0 { return color; } technique HardwareInstancing { pass Pass1 { VertexShader = compile vs_3_0 MyVertexShader(); PixelShader = compile ps_3_0 MyPixelShader(); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; class MyGame : Game { 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[] instances = new Vector3[] { new Vector3(), new Vector3(0.1f, 0.1f, 0), new Vector3(0.2f, 0.2f, 0) }; //Graphics Device Objects Effect effect; VertexBuffer triangleVertexBuffer; IndexBuffer indexBuffer; DynamicVertexBuffer instanceVertexBuffer; public MyGame() { new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void LoadContent() { effect = Content.Load<Effect>("HardwareInstancing"); triangleVertexBuffer = new VertexBuffer( GraphicsDevice, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly ); triangleVertexBuffer.SetData<VertexPositionColor>(vertices); var index = new int[] { 0, 1, 2 }; indexBuffer = new IndexBuffer( GraphicsDevice, IndexElementSize.ThirtyTwoBits, index.Length, BufferUsage.WriteOnly ); indexBuffer.SetData<int>(index); instanceVertexBuffer = new DynamicVertexBuffer( GraphicsDevice, new VertexDeclaration( new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 1) ), instances.Length, BufferUsage.WriteOnly); instanceVertexBuffer.SetData(instances); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.SetVertexBuffers( new VertexBufferBinding(triangleVertexBuffer, 0, 0), new VertexBufferBinding(instanceVertexBuffer, 0, 1) ); GraphicsDevice.Indices = indexBuffer; instanceVertexBuffer.SetData(instances, 0, instances.Length, SetDataOptions.Discard); foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawInstancedPrimitives( PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, vertices.Length / 3, instances.Length ); } } }
using Microsoft.Xna.Framework; using JigLibX.Physics; using JigLibX.Collision; public class BasicWorldGame : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; PhysicsSystem world; BoxActor fallingBox; BoxActor immovableBox; BoxRenderer boxRenderer; public BasicWorldGame() { graphics = new GraphicsDeviceManager(this); InitializePhysics(); } private void InitializePhysics() { world = new PhysicsSystem { }; world.CollisionSystem = new CollisionSystemSAP(); fallingBox = new BoxActor(new Vector3(0, 20, 0), new Vector3(1, 1, 1)); immovableBox = new BoxActor(new Vector3(0, -5, 0), new Vector3(5, 5, 5)); immovableBox.Body.Immovable = true; world.AddBody(fallingBox.Body); world.AddBody(immovableBox.Body); } protected override void LoadContent() { boxRenderer = new BoxRenderer(GraphicsDevice); } protected override void UnloadContent() { boxRenderer.Dispose(); } protected override void Update(GameTime gameTime) { world.Integrate(1f / 60); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); boxRenderer.SetCamera( Matrix.CreateLookAt( new Vector3(5, 5, 40), new Vector3(), Vector3.Up ), Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 1000f ) ); drawBox(fallingBox); drawBox(immovableBox); base.Draw(gameTime); } private void drawBox(BoxActor box) { boxRenderer.Draw(box.GetWorldTransform()); } }BoxActor.cs
using Microsoft.Xna.Framework; using JigLibX.Physics; using JigLibX.Collision; using JigLibX.Geometry; using JigLibX.Math; public class BoxActor { private Vector3 scale; public Body Body { get; private set; } private CollisionSkin skin; public BoxActor(Vector3 position, Vector3 scale) { this.scale = scale; this.Body = new Body(); this.skin = new CollisionSkin(this.Body); this.Body.CollisionSkin = this.skin; this.skin.AddPrimitive( new Box(Vector3.Zero, Matrix.Identity, scale), new MaterialProperties( 0.8f, // elasticity 0.8f, // static roughness 0.7f // dynamic roughness )); SetMass(1.0f); this.Body.MoveTo(position, Matrix.Identity); } private void SetMass(float mass) { PrimitiveProperties primitiveProperties = new PrimitiveProperties( PrimitiveProperties.MassDistributionEnum.Solid, PrimitiveProperties.MassTypeEnum.Mass, mass ); float junk; Vector3 centerOfMass; Matrix inertiaTensor, inertiaTensorCenterOfMass; this.skin.GetMassProperties( primitiveProperties, out junk, out centerOfMass, out inertiaTensor, out inertiaTensorCenterOfMass ); this.Body.BodyInertia = inertiaTensorCenterOfMass; this.Body.Mass = mass; this.skin.ApplyLocalTransform(new Transform(-centerOfMass, Matrix.Identity)); } public Matrix GetWorldTransform() { return Matrix.CreateScale(scale) * skin.GetPrimitiveLocal(0).Transform.Orientation * Body.Orientation * Matrix.CreateTranslation(Body.Position); } }
using Microsoft.Xna.Framework; using JigLibX.Physics; using JigLibX.Collision; public class BasicWorldGame : Game { GraphicsDeviceManager graphics; PhysicsSystem world; BoxRenderer boxRenderer; public BasicWorldGame() { graphics = new GraphicsDeviceManager(this); InitializePhysics(); } private void InitializePhysics() { world = new PhysicsSystem(); world.CollisionSystem = new CollisionSystemSAP(); } protected override void LoadContent() { boxRenderer = new BoxRenderer(GraphicsDevice); } protected override void UnloadContent() { boxRenderer.Dispose(); } protected override void Update(GameTime gameTime) { world.Integrate(1 / 60f); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); boxRenderer.SetCamera( Matrix.CreateLookAt( new Vector3(2, 1, 5), new Vector3(), Vector3.Up ), Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 1000 ) ); boxRenderer.Draw(Matrix.Identity); base.Draw(gameTime); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class BoxRenderer:System.IDisposable { public GraphicsDevice GraphicsDevice { get; private set; } VertexBuffer vertexBuffer; BasicEffect basicEffect; public BoxRenderer(GraphicsDevice graphicsDevice) { GraphicsDevice = graphicsDevice; basicEffect = new BasicEffect(GraphicsDevice) { VertexColorEnabled = true}; Vector3[] positions = new [] { new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(-1, -1,1), new Vector3(-1, 1, 1), new Vector3(1, 1, -1), new Vector3(1, -1, -1), new Vector3(-1, -1,-1), new Vector3(-1, 1, -1), }; VertexPositionColor[] vertices = new[] { new VertexPositionColor(positions[0], Color.Blue), new VertexPositionColor(positions[1], Color.Blue), new VertexPositionColor(positions[2], Color.Blue), new VertexPositionColor(positions[3], Color.Blue), new VertexPositionColor(positions[0], Color.Blue), new VertexPositionColor(positions[2], Color.Blue), new VertexPositionColor(positions[3], Color.Red), new VertexPositionColor(positions[2], Color.Red), new VertexPositionColor(positions[7], Color.Red), new VertexPositionColor(positions[7], Color.Red), new VertexPositionColor(positions[2], Color.Red), new VertexPositionColor(positions[6], Color.Red), new VertexPositionColor(positions[0], Color.Green), new VertexPositionColor(positions[3], Color.Green), new VertexPositionColor(positions[7], Color.Green), new VertexPositionColor(positions[0], Color.Green), new VertexPositionColor(positions[7], Color.Green), new VertexPositionColor(positions[4], Color.Green), new VertexPositionColor(positions[4], Color.Cyan), new VertexPositionColor(positions[6], Color.Cyan), new VertexPositionColor(positions[5], Color.Cyan), new VertexPositionColor(positions[4], Color.Cyan), new VertexPositionColor(positions[7], Color.Cyan), new VertexPositionColor(positions[6], Color.Cyan), new VertexPositionColor(positions[5], Color.Magenta), new VertexPositionColor(positions[2], Color.Magenta), new VertexPositionColor(positions[1], Color.Magenta), new VertexPositionColor(positions[5], Color.Magenta), new VertexPositionColor(positions[6], Color.Magenta), new VertexPositionColor(positions[2], Color.Magenta), new VertexPositionColor(positions[0], Color.Yellow), new VertexPositionColor(positions[5], Color.Yellow), new VertexPositionColor(positions[1], Color.Yellow), new VertexPositionColor(positions[0], Color.Yellow), new VertexPositionColor(positions[4], Color.Yellow), new VertexPositionColor(positions[5], Color.Yellow), }; vertexBuffer = new VertexBuffer( graphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly ); vertexBuffer.SetData<VertexPositionColor>(vertices); } public void SetCamera(Matrix view, Matrix projection) { basicEffect.View = view; basicEffect.Projection = projection; } public void Draw(Matrix world) { basicEffect.World = Matrix.CreateScale(1 / 2f) * world; basicEffect.CurrentTechnique.Passes[0].Apply(); GraphicsDevice.SetVertexBuffer(vertexBuffer); GraphicsDevice.DrawPrimitives( PrimitiveType.TriangleList, 0, vertexBuffer.VertexCount / 3 ); } public void Dispose() { basicEffect.Dispose(); vertexBuffer.Dispose(); } }
using Microsoft.Xna.Framework; using JigLibX.Physics; using JigLibX.Collision; public class BasicWorldGame : Game { GraphicsDeviceManager graphics; PhysicsSystem world; public BasicWorldGame() { graphics = new GraphicsDeviceManager(this); InitializePhysics(); } private void InitializePhysics() { world = new PhysicsSystem(); world.CollisionSystem = new CollisionSystemSAP(); } protected override void Update(GameTime gameTime) { world.Integrate(1 / 60f); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); base.Draw(gameTime); } }
クラス名 | 解説 |
CollisionSystemBrute | すべての物体を、全ての他の物体に対して衝突をチェックします。一見遅そうですが、小さなシーンではこれは速度が出て、CollisionSystemGridより速いでしょう。 |
CollisionSystemGrid | 世界をあるサイズのグリッドに分割したCollisionSystemです。物体が均等に散らばっていると、チェックの回数が減ります(速度が出ます)。 |
CollisionSystemSAP | sweep-and-pruneアルゴリズムを使ったCollisionSystemです。 |