[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
float4x4 View; float4x4 Projection; float ViewportHeight; float3 Center; float Age; float StartLife; float PointSize; texture ExplosionTexture; sampler explosionSampler = sampler_state { Texture = <ExplosionTexture>; }; struct ExplosionVertexShaderOutput { float4 Position : POSITION; float4 Color : COLOR; float PointSize : PSIZE; }; float4 ComputeColor(float normalizedAge) { float4 color = 1; color.a *= normalizedAge * (1 - normalizedAge) * (1 - normalizedAge) * 6.7; return color; } ExplosionVertexShaderOutput ExplosionVertexShader(float3 velocity:COLOR) { ExplosionVertexShaderOutput output; float4 worldPosition = float4(velocity * Age + Center, 1); output.Position = mul(worldPosition, mul(View, Projection)); output.Color = ComputeColor(Age / StartLife); output.PointSize = PointSize * Projection._m11 / output.Position.w * ViewportHeight / 2; return output; } float4 ExplosionPixelShader( float2 textureCoordinate:TEXCOORD, float4 color:COLOR ):COLOR { return tex2D(explosionSampler, textureCoordinate) * color; } technique ExplosionShaderTechnique { pass P0 { VertexShader = compile vs_2_0 ExplosionVertexShader(); PixelShader = compile ps_2_0 ExplosionPixelShader(); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using System.Collections.Generic; public class ExplosionRenderer : Microsoft.Xna.Framework.DrawableGameComponent { private Effect effect; private VertexDeclaration vertexDeclaration; private VertexVelocity[] vertices; private ContentManager content; private static System.Random random = new System.Random(); private Matrix view; private Matrix projection; private List<Explosion> explosions = new List<Explosion>(); public ExplosionRenderer(Game game) : base(game) { content = new ContentManager(game.Services); } public override void Initialize() { vertices = new VertexVelocity[30]; for (int i = 0; i < vertices.Length; i++) { vertices[i].Velocity = randomVector3()/10; } base.Initialize(); } Vector3 randomVector3() { return new Vector3( (float)(random.NextDouble() * 2 - 1), (float)(random.NextDouble() * 2 - 1), (float)(random.NextDouble() * 2 - 1) ); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { effect = content.Load<Effect>("Content/ExplosionShader"); effect.Parameters["ExplosionTexture"].SetValue( content.Load<Texture2D>("Content/explosion") ); vertexDeclaration = new VertexDeclaration( GraphicsDevice, VertexVelocity.VertexElements ); } } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { content.Unload(); } } public override void Update(GameTime gameTime) { foreach (Explosion explosion in explosions) { explosion.Update(gameTime); } explosions.RemoveAll( delegate(Explosion explosion) { return explosion.Life < 0; } ); } public override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.RenderState.PointSize = 50; GraphicsDevice.RenderState.PointSpriteEnable = true; GraphicsDevice.RenderState.AlphaBlendEnable = true; GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha; GraphicsDevice.RenderState.DestinationBlend = Blend.One; GraphicsDevice.RenderState.DepthBufferWriteEnable = false; GraphicsDevice.VertexDeclaration = vertexDeclaration; effect.Parameters["View"].SetValue(view); effect.Parameters["Projection"].SetValue(projection); effect.Parameters["ViewportHeight"].SetValue( GraphicsDevice.Viewport.Height ); effect.Parameters["PointSize"].SetValue(0.3f); foreach (Explosion explosion in explosions) { drawExplosion(explosion); } GraphicsDevice.RenderState.AlphaBlendEnable = false; GraphicsDevice.RenderState.DepthBufferWriteEnable = true; } private void drawExplosion(Explosion explosion) { effect.Parameters["Center"].SetValue(explosion.Center); effect.Parameters["Age"].SetValue(explosion.Age); effect.Parameters["StartLife"].SetValue(explosion.StartLife); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); GraphicsDevice.DrawUserPrimitives<VertexVelocity>( PrimitiveType.PointList, vertices, 0, vertices.Length ); pass.End(); } effect.End(); } public void SetCamera(Matrix view, Matrix projection) { this.view = view; this.projection = projection; } public void AddExplosion(Explosion explosion) { this.explosions.Add(explosion); } }
エフェクトファイルのコードを見返していて気付いたところがあったので
簡略化します。
リファクタリング前
ExplosionVertexShaderOutput ExplosionVertexShader(float4 velocity:COLOR)
{
ExplosionVertexShaderOutput output;
float4 worldPosition = float4(velocity.xyz * Age + Center, 1);
リファクタリング後
ExplosionVertexShaderOutput ExplosionVertexShader(float3 velocity:COLOR)
{
ExplosionVertexShaderOutput output;
float4 worldPosition = float4(velocity * Age + Center, 1);
このほうがずっと簡単ですね。
using Microsoft.Xna.Framework; public class ExplosionTest : Game { private GraphicsDeviceManager graphics; private ExplosionRenderer explosion; private float life; public ExplosionTest() { graphics = new GraphicsDeviceManager(this); explosion = new ExplosionRenderer(this); Matrix view = Matrix.CreateLookAt( new Vector3(0, 0, 4), new Vector3(), new Vector3(0, 1, 0) ); Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), (float)Window.ClientBounds.Width/Window.ClientBounds.Height, 0.1f, 1000 ); explosion.SetCamera(view, projection); this.Components.Add(explosion); } protected override void Update(GameTime gameTime) { life -= (float)gameTime.ElapsedGameTime.TotalSeconds; if (life < 0) { life = 2; explosion.AddExplosion(new Explosion(new Vector3(), life)); explosion.AddExplosion(new Explosion(new Vector3(1, 0, 0), life)); } base.Update(gameTime); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using System.Collections.Generic; public class ExplosionRenderer : Microsoft.Xna.Framework.DrawableGameComponent { private Effect effect; private VertexDeclaration vertexDeclaration; private VertexVelocity[] vertices; private ContentManager content; private static System.Random random = new System.Random(); private Matrix view; private Matrix projection; private List<Explosion> explosions = new List<Explosion>(); public ExplosionRenderer(Game game) : base(game) { content = new ContentManager(game.Services); } public override void Initialize() { vertices = new VertexVelocity[30]; for (int i = 0; i < vertices.Length; i++) { vertices[i].Velocity = randomVector3()/10; } base.Initialize(); } Vector3 randomVector3() { return new Vector3( (float)(random.NextDouble() * 2 - 1), (float)(random.NextDouble() * 2 - 1), (float)(random.NextDouble() * 2 - 1) ); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { effect = content.Load<Effect>("Content/ExplosionShader"); effect.Parameters["ExplosionTexture"].SetValue( content.Load<Texture2D>("Content/explosion") ); vertexDeclaration = new VertexDeclaration( GraphicsDevice, VertexVelocity.VertexElements ); } } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { content.Unload(); } } public override void Update(GameTime gameTime) { for (int i = 0; i < explosions.Count; i++) { Explosion explosion = explosions[i]; explosion.Update(gameTime); explosions[i] = explosion; } explosions.RemoveAll( delegate(Explosion explosion) { return explosion.Life < 0; } ); } public override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.RenderState.PointSize = 50; GraphicsDevice.RenderState.PointSpriteEnable = true; GraphicsDevice.RenderState.AlphaBlendEnable = true; GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha; GraphicsDevice.RenderState.DestinationBlend = Blend.One; GraphicsDevice.RenderState.DepthBufferWriteEnable = false; GraphicsDevice.VertexDeclaration = vertexDeclaration; effect.Parameters["View"].SetValue(view); effect.Parameters["Projection"].SetValue(projection); foreach (Explosion explosion in explosions) { drawExplosion(explosion); } GraphicsDevice.RenderState.AlphaBlendEnable = false; GraphicsDevice.RenderState.DepthBufferWriteEnable = true; } private void drawExplosion(Explosion explosion) { effect.Parameters["Center"].SetValue(explosion.Center); effect.Parameters["Age"].SetValue(explosion.Age); effect.Parameters["StartLife"].SetValue(explosion.StartLife); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); GraphicsDevice.DrawUserPrimitives<VertexVelocity>( PrimitiveType.PointList, vertices, 0, vertices.Length ); pass.End(); } effect.End(); } public void SetCamera(Matrix view, Matrix projection) { this.view = view; this.projection = projection; } public void AddExplosion(Explosion explosion) { this.explosions.Add(explosion); } }
public override void Update(GameTime gameTime) { foreach (Explosion explosion in explosions) { explosion.Update(gameTime); } explosions.RemoveAll( delegate(Explosion explosion) { return explosion.Life < 0; } ); }
using Microsoft.Xna.Framework; public class Explosion { private float life; private float startLife; public float Life { get { return life; } } public float StartLife { get { return startLife; } } public float Age { get { return startLife - life; } } public Vector3 Center; public Explosion(Vector3 center, float life) { this.Center = center; this.life = life; this.startLife = life; } public void Update(GameTime gameTime) { life -= (float)gameTime.ElapsedGameTime.TotalSeconds; } }
using Microsoft.Xna.Framework; public class ExplosionTest : Game { private GraphicsDeviceManager graphics; private ExplosionRenderer explosion; private float life; public ExplosionTest() { graphics = new GraphicsDeviceManager(this); explosion = new ExplosionRenderer(this); Matrix view = Matrix.CreateLookAt( new Vector3(0, 0, 4), new Vector3(), new Vector3(0, 1, 0) ); Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), (float)Window.ClientBounds.Width/Window.ClientBounds.Height, 0.1f, 1000 ); explosion.SetCamera(view, projection); this.Components.Add(explosion); } protected override void Update(GameTime gameTime) { life -= (float)gameTime.ElapsedGameTime.TotalSeconds; if (life < 0) { life = 2; explosion.SetExplosion(new Explosion(new Vector3(), life)); } base.Update(gameTime); } }
using Microsoft.Xna.Framework; public struct Explosion { private float life; private float startLife; public float Life { get { return life; } } public float StartLife { get { return startLife; } } public float Age { get { return startLife - life; } } public Vector3 Center; public Explosion(Vector3 center, float life) { this.Center = center; this.life = life; this.startLife = life; } public void Update(GameTime gameTime) { life -= (float)gameTime.ElapsedGameTime.TotalSeconds; } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; public class ExplosionRenderer : Microsoft.Xna.Framework.DrawableGameComponent { private Effect effect; private VertexDeclaration vertexDeclaration; private VertexVelocity[] vertices; private ContentManager content; private static System.Random random = new System.Random(); private Matrix view; private Matrix projection; private Explosion explosion; public ExplosionRenderer(Game game) : base(game) { content = new ContentManager(game.Services); } public override void Initialize() { vertices = new VertexVelocity[30]; for (int i = 0; i < vertices.Length; i++) { vertices[i].Velocity = randomVector3()/10; } base.Initialize(); } Vector3 randomVector3() { return new Vector3( (float)(random.NextDouble() * 2 - 1), (float)(random.NextDouble() * 2 - 1), (float)(random.NextDouble() * 2 - 1) ); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { effect = content.Load<Effect>("Content/ExplosionShader"); effect.Parameters["ExplosionTexture"].SetValue( content.Load<Texture2D>("Content/explosion") ); vertexDeclaration = new VertexDeclaration( GraphicsDevice, VertexVelocity.VertexElements ); } } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { content.Unload(); } } public override void Update(GameTime gameTime) { explosion.Update(gameTime); } public override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.RenderState.PointSize = 50; GraphicsDevice.RenderState.PointSpriteEnable = true; GraphicsDevice.RenderState.AlphaBlendEnable = true; GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha; GraphicsDevice.RenderState.DestinationBlend = Blend.One; GraphicsDevice.RenderState.DepthBufferWriteEnable = false; GraphicsDevice.VertexDeclaration = vertexDeclaration; effect.Parameters["View"].SetValue(view); effect.Parameters["Projection"].SetValue(projection); drawExplosion(explosion); GraphicsDevice.RenderState.AlphaBlendEnable = false; GraphicsDevice.RenderState.DepthBufferWriteEnable = true; } private void drawExplosion(Explosion explosion) { effect.Parameters["Center"].SetValue(explosion.Center); effect.Parameters["Age"].SetValue(explosion.Age); effect.Parameters["StartLife"].SetValue(explosion.StartLife); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); GraphicsDevice.DrawUserPrimitives<VertexVelocity>( PrimitiveType.PointList, vertices, 0, vertices.Length ); pass.End(); } effect.End(); } public void SetCamera(Matrix view, Matrix projection) { this.view = view; this.projection = projection; } public void SetExplosion(Explosion explosion) { this.explosion = explosion; } }
float4x4 View; float4x4 Projection; float3 Center; float Age; float StartLife; texture ExplosionTexture; sampler explosionSampler = sampler_state { Texture = <ExplosionTexture>; }; struct ExplosionVertexShaderOutput { float4 Position : POSITION; float4 Color : COLOR; }; float4 ComputeColor(float normalizedAge) { float4 color = 1; color.a *= normalizedAge * (1 - normalizedAge) * (1 - normalizedAge) * 6.7; return color; } ExplosionVertexShaderOutput ExplosionVertexShader(float4 velocity:COLOR) { ExplosionVertexShaderOutput output; float4 worldPosition = float4(velocity.xyz * Age + Center, 1); output.Position = mul(worldPosition, mul(View, Projection)); output.Color = ComputeColor(Age / StartLife); return output; } float4 ExplosionPixelShader( float2 textureCoordinate:TEXCOORD, float4 color:COLOR ):COLOR { return tex2D(explosionSampler, textureCoordinate) * color; } technique ExplosionShaderTechnique { pass P0 { VertexShader = compile vs_2_0 ExplosionVertexShader(); PixelShader = compile ps_2_0 ExplosionPixelShader(); } }
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; public class ExplosionRenderer : Microsoft.Xna.Framework.DrawableGameComponent { private Effect effect; private VertexDeclaration vertexDeclaration; private VertexVelocity[] vertices; private ContentManager content; private static System.Random random = new System.Random(); private Matrix view; private Matrix projection; private Explosion explosion; public ExplosionRenderer(Game game) : base(game) { content = new ContentManager(game.Services); } public override void Initialize() { vertices = new VertexVelocity[30]; for (int i = 0; i < vertices.Length; i++) { vertices[i].Velocity = randomVector3()/10; } base.Initialize(); } Vector3 randomVector3() { return new Vector3( (float)(random.NextDouble() * 2 - 1), (float)(random.NextDouble() * 2 - 1), (float)(random.NextDouble() * 2 - 1) ); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { effect = content.Load<Effect>("Content/ExplosionShader"); effect.Parameters["ExplosionTexture"].SetValue( content.Load<Texture2D>("Content/explosion") ); vertexDeclaration = new VertexDeclaration( GraphicsDevice, VertexVelocity.VertexElements ); } } protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { content.Unload(); } } public override void Update(GameTime gameTime) { explosion.Update(gameTime); if (explosion.Life < 0) { explosion = new Explosion(2); } } public override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.RenderState.PointSize = 50; GraphicsDevice.RenderState.PointSpriteEnable = true; GraphicsDevice.RenderState.AlphaBlendEnable = true; GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha; GraphicsDevice.RenderState.DestinationBlend = Blend.One; GraphicsDevice.RenderState.DepthBufferWriteEnable = false; GraphicsDevice.VertexDeclaration = vertexDeclaration; effect.Parameters["View"].SetValue(view); effect.Parameters["Projection"].SetValue(projection); drawExplosion(explosion); GraphicsDevice.RenderState.AlphaBlendEnable = false; GraphicsDevice.RenderState.DepthBufferWriteEnable = true; } private void drawExplosion(Explosion explosion) { effect.Parameters["Age"].SetValue(explosion.Age); effect.Parameters["StartLife"].SetValue(explosion.StartLife); effect.Begin(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Begin(); GraphicsDevice.DrawUserPrimitives<VertexVelocity>( PrimitiveType.PointList, vertices, 0, vertices.Length ); pass.End(); } effect.End(); } public void SetCamera(Matrix view, Matrix projection) { this.view = view; this.projection = projection; } }
using Microsoft.Xna.Framework; struct Explosion { private float life; private float startLife; public float Life { get { return life; } } public float StartLife { get { return startLife; } } public float Age { get { return startLife - life; } } public Explosion(float life) { this.life = life; this.startLife = life; } public void Update(GameTime gameTime) { life -= (float)gameTime.ElapsedGameTime.TotalSeconds; } }
float4x4 View; float4x4 Projection; float Age; float StartLife; texture ExplosionTexture; sampler explosionSampler = sampler_state { Texture = <ExplosionTexture>; }; struct ExplosionVertexShaderOutput { float4 Position : POSITION; float4 Color : COLOR; }; float4 ComputeColor(float normalizedAge) { float4 color = 1; color.a *= normalizedAge * (1 - normalizedAge) * (1 - normalizedAge) * 6.7; return color; } ExplosionVertexShaderOutput ExplosionVertexShader(float4 velocity:COLOR) { ExplosionVertexShaderOutput output; float4 worldPosition = float4(velocity.xyz * Age, 1); output.Position = mul(worldPosition, mul(View, Projection)); output.Color = ComputeColor(Age / StartLife); return output; } float4 ExplosionPixelShader( float2 textureCoordinate:TEXCOORD, float4 color:COLOR ):COLOR { return tex2D(explosionSampler, textureCoordinate) * color; } technique ExplosionShaderTechnique { pass P0 { VertexShader = compile vs_2_0 ExplosionVertexShader(); PixelShader = compile ps_2_0 ExplosionPixelShader(); } }