Batched Billboard Implementation #5
			
				
			
		
		
		
	|  | @ -0,0 +1,39 @@ | ||||||
|  | using Microsoft.Xna.Framework; | ||||||
|  | using Microsoft.Xna.Framework.Graphics; | ||||||
|  | 
 | ||||||
|  | namespace Kav | ||||||
|  | { | ||||||
|  |     public struct Sprite | ||||||
|  |     { | ||||||
|  |         public Texture2D Texture { get; } | ||||||
|  |         public Vector3 Position { get; } | ||||||
|  |         public Vector2 Origin { get; } | ||||||
|  |         public float Rotation { get; } | ||||||
|  |         public Vector2 Scale { get; } | ||||||
|  | 
 | ||||||
|  |         public Sprite( | ||||||
|  |             Texture2D texture, | ||||||
|  |             Vector3 position, | ||||||
|  |             Vector2 origin, | ||||||
|  |             float rotation, | ||||||
|  |             Vector2 scale | ||||||
|  |         ) { | ||||||
|  |             Texture = texture; | ||||||
|  |             Position = position; | ||||||
|  |             Origin = origin; | ||||||
|  |             Rotation = rotation; | ||||||
|  |             Scale = scale; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public Sprite( | ||||||
|  |             Texture2D texture, | ||||||
|  |             Vector3 position | ||||||
|  |         ) { | ||||||
|  |             Texture = texture; | ||||||
|  |             Position = position; | ||||||
|  |             Origin = Vector2.Zero; | ||||||
|  |             Rotation = 0f; | ||||||
|  |             Scale = Vector2.One; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										61
									
								
								Renderer.cs
								
								
								
								
							
							
						
						
									
										61
									
								
								Renderer.cs
								
								
								
								
							|  | @ -33,6 +33,7 @@ namespace Kav | ||||||
|         private LinearDepthEffect LinearDepthEffect { get; } |         private LinearDepthEffect LinearDepthEffect { get; } | ||||||
|         private Effect ToneMapEffect { get; } |         private Effect ToneMapEffect { get; } | ||||||
|         private SkyboxEffect SkyboxEffect { get; } |         private SkyboxEffect SkyboxEffect { get; } | ||||||
|  |         private BasicEffect BasicEffect { get; } | ||||||
| 
 | 
 | ||||||
|         private RenderTarget2D gPosition { get; } |         private RenderTarget2D gPosition { get; } | ||||||
|         private RenderTarget2D gNormal { get; } |         private RenderTarget2D gNormal { get; } | ||||||
|  | @ -159,6 +160,7 @@ namespace Kav | ||||||
|             ToneMapEffect = new Effect(graphicsDevice, Resources.ToneMapEffect); |             ToneMapEffect = new Effect(graphicsDevice, Resources.ToneMapEffect); | ||||||
|             Deferred_ToonEffect = new Deferred_ToonEffect(GraphicsDevice); |             Deferred_ToonEffect = new Deferred_ToonEffect(GraphicsDevice); | ||||||
|             SkyboxEffect = new SkyboxEffect(GraphicsDevice); |             SkyboxEffect = new SkyboxEffect(GraphicsDevice); | ||||||
|  |             BasicEffect = new BasicEffect(GraphicsDevice); | ||||||
| 
 | 
 | ||||||
|             FullscreenTriangle = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 3, BufferUsage.WriteOnly); |             FullscreenTriangle = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 3, BufferUsage.WriteOnly); | ||||||
|             FullscreenTriangle.SetData(new VertexPositionTexture[3] { |             FullscreenTriangle.SetData(new VertexPositionTexture[3] { | ||||||
|  | @ -176,6 +178,7 @@ namespace Kav | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public void DeferredRender( |         public void DeferredRender( | ||||||
|  |             RenderTarget2D renderTarget, | ||||||
|             PerspectiveCamera camera, |             PerspectiveCamera camera, | ||||||
|             IEnumerable<(Model, Matrix)> modelTransforms, |             IEnumerable<(Model, Matrix)> modelTransforms, | ||||||
|             AmbientLight ambientLight, |             AmbientLight ambientLight, | ||||||
|  | @ -198,14 +201,14 @@ namespace Kav | ||||||
| 
 | 
 | ||||||
|             DirectionalLightRender(camera, modelTransforms, directionalLight); |             DirectionalLightRender(camera, modelTransforms, directionalLight); | ||||||
| 
 | 
 | ||||||
|             GraphicsDevice.SetRenderTarget(null); |             GraphicsDevice.SetRenderTarget(renderTarget); | ||||||
|             GraphicsDevice.Clear(Color.Black); |  | ||||||
|             SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, ToneMapEffect); |             SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, ToneMapEffect); | ||||||
|             SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White); |             SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White); | ||||||
|             SpriteBatch.End(); |             SpriteBatch.End(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public void DeferredToonRender( |         public void DeferredToonRender( | ||||||
|  |             RenderTarget2D renderTarget, | ||||||
|             PerspectiveCamera camera, |             PerspectiveCamera camera, | ||||||
|             IEnumerable<(Model, Matrix)> modelTransforms, |             IEnumerable<(Model, Matrix)> modelTransforms, | ||||||
|             AmbientLight ambientLight, |             AmbientLight ambientLight, | ||||||
|  | @ -230,12 +233,64 @@ namespace Kav | ||||||
|             DirectionalLightToonRender(camera, modelTransforms, directionalLight); |             DirectionalLightToonRender(camera, modelTransforms, directionalLight); | ||||||
|             SkyboxRender(camera, skybox); |             SkyboxRender(camera, skybox); | ||||||
| 
 | 
 | ||||||
|             GraphicsDevice.SetRenderTarget(null); |             GraphicsDevice.SetRenderTarget(renderTarget); | ||||||
|             SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, null); |             SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, null); | ||||||
|             SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White); |             SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White); | ||||||
|             SpriteBatch.End(); |             SpriteBatch.End(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         // billboards sprites into the scene | ||||||
|  |         // FIXME: we can frustum cull the sprites probably | ||||||
|  |         public void BillboardSpriteRender( | ||||||
|  |             RenderTarget2D renderTarget, | ||||||
|  |             PerspectiveCamera camera, | ||||||
|  |             IEnumerable<(Model, Matrix)> modelTransforms, | ||||||
|  |             IEnumerable<Sprite> sprites | ||||||
|  |         ) { | ||||||
|  |             GraphicsDevice.SetRenderTarget(ColorRenderTarget); | ||||||
|  |             GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0); | ||||||
|  |             GraphicsDevice.DepthStencilState = DepthStencilState.Default; | ||||||
|  | 
 | ||||||
|  |             DepthRender(camera, modelTransforms); | ||||||
|  |             GraphicsDevice.Clear(ClearOptions.Target, new Color(0, 0, 0, 0), 1f, 0); | ||||||
|  | 
 | ||||||
|  |             Matrix invertY = Matrix.CreateScale(1, -1, 1); | ||||||
|  | 
 | ||||||
|  |             BasicEffect.World = invertY; | ||||||
|  |             BasicEffect.View = Matrix.Identity; | ||||||
|  |             BasicEffect.Projection = camera.Projection; | ||||||
|  |             BasicEffect.TextureEnabled = true; | ||||||
|  |             BasicEffect.VertexColorEnabled = true; | ||||||
|  | 
 | ||||||
|  |             SpriteBatch.Begin(0, null, null, DepthStencilState.DepthRead, RasterizerState.CullNone, BasicEffect); | ||||||
|  | 
 | ||||||
|  |             foreach (var sprite in sprites) | ||||||
|  |             { | ||||||
|  |                 // transform view space on CPU so we don't have to break the batch | ||||||
|  |                 Vector3 viewSpacePosition = Vector3.Transform(sprite.Position, camera.View * invertY); | ||||||
|  | 
 | ||||||
|  |                 SpriteBatch.Draw( | ||||||
|  |                     sprite.Texture, | ||||||
|  |                     new Vector2(viewSpacePosition.X, viewSpacePosition.Y), | ||||||
|  |                     null, | ||||||
|  |                     Color.White, | ||||||
|  |                     0, | ||||||
|  |                     sprite.Origin, | ||||||
|  |                     sprite.Scale / new Vector2(sprite.Texture.Width, sprite.Texture.Height), | ||||||
|  |                     0, | ||||||
|  |                     viewSpacePosition.Z | ||||||
|  |                 ); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             SpriteBatch.End(); | ||||||
|  | 
 | ||||||
|  |             GraphicsDevice.SetRenderTarget(renderTarget); | ||||||
|  |             GraphicsDevice.Clear(new Color(0, 0, 0, 0)); | ||||||
|  |             SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null); | ||||||
|  |             SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White); | ||||||
|  |             SpriteBatch.End(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         private void DepthRender( |         private void DepthRender( | ||||||
|             PerspectiveCamera camera, |             PerspectiveCamera camera, | ||||||
|             IEnumerable<(Model, Matrix)> modelTransforms |             IEnumerable<(Model, Matrix)> modelTransforms | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue