2020-10-19 10:01:37 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-10-17 20:53:26 +00:00
|
|
|
|
using System.IO;
|
2020-12-10 08:34:06 +00:00
|
|
|
|
using Kav.Data;
|
2020-08-07 00:58:50 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
2020-08-04 09:32:02 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace Kav
|
|
|
|
|
{
|
2020-08-07 00:58:50 +00:00
|
|
|
|
public class Renderer
|
2020-08-04 09:32:02 +00:00
|
|
|
|
{
|
2020-08-07 00:58:50 +00:00
|
|
|
|
private GraphicsDevice GraphicsDevice { get; }
|
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
private VertexBuffer FullscreenTriangle { get; }
|
|
|
|
|
|
|
|
|
|
private DeferredPBREffect DeferredPBREffect { get; }
|
2020-10-02 05:26:40 +00:00
|
|
|
|
/* FIXME: these next two dont actually have anything to do with PBR */
|
|
|
|
|
private DeferredPBR_GBufferEffect Deferred_GBufferEffect { get; }
|
2020-10-01 19:46:25 +00:00
|
|
|
|
private DeferredPBR_AmbientLightEffect DeferredAmbientLightEffect { get; }
|
|
|
|
|
private DeferredPBR_PointLightEffect DeferredPointLightEffect { get; }
|
|
|
|
|
private DeferredPBR_DirectionalLightEffect DeferredDirectionalLightEffect { get; }
|
2020-10-01 21:52:19 +00:00
|
|
|
|
private Deferred_ToonEffect Deferred_ToonEffect { get; }
|
2020-08-07 00:58:50 +00:00
|
|
|
|
private SimpleDepthEffect SimpleDepthEffect { get; }
|
2020-12-08 10:49:18 +00:00
|
|
|
|
private SimpleDepthEffectInstanced SimpleDepthEffectInstanced { get; }
|
2020-10-19 10:01:37 +00:00
|
|
|
|
private LinearDepthEffect LinearDepthEffect { get; }
|
2020-12-09 00:37:22 +00:00
|
|
|
|
private LinearDepthEffectInstanced LinearDepthEffectInstanced { get; }
|
2020-10-01 19:46:25 +00:00
|
|
|
|
private Effect ToneMapEffect { get; }
|
2020-10-17 20:53:26 +00:00
|
|
|
|
private SkyboxEffect SkyboxEffect { get; }
|
2020-12-06 03:47:01 +00:00
|
|
|
|
private DiffuseLitSpriteEffect DiffuseLitSpriteEffect { get; }
|
2020-12-10 00:10:53 +00:00
|
|
|
|
|
2020-10-17 20:53:26 +00:00
|
|
|
|
private Kav.Model UnitCube { get; }
|
2020-12-10 00:10:53 +00:00
|
|
|
|
private Kav.Model UnitSphere { get; }
|
2020-10-17 20:53:26 +00:00
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
public Renderer(
|
2020-12-08 05:36:16 +00:00
|
|
|
|
GraphicsDevice graphicsDevice
|
2020-10-01 19:46:25 +00:00
|
|
|
|
) {
|
2020-08-07 00:58:50 +00:00
|
|
|
|
GraphicsDevice = graphicsDevice;
|
|
|
|
|
|
|
|
|
|
SimpleDepthEffect = new SimpleDepthEffect(GraphicsDevice);
|
2020-12-08 10:49:18 +00:00
|
|
|
|
SimpleDepthEffectInstanced = new SimpleDepthEffectInstanced(GraphicsDevice);
|
2020-10-19 10:01:37 +00:00
|
|
|
|
LinearDepthEffect = new LinearDepthEffect(GraphicsDevice);
|
2020-12-09 00:37:22 +00:00
|
|
|
|
LinearDepthEffectInstanced = new LinearDepthEffectInstanced(GraphicsDevice);
|
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
|
DeferredPBREffect = new DeferredPBREffect(GraphicsDevice);
|
2020-10-02 05:26:40 +00:00
|
|
|
|
|
|
|
|
|
Deferred_GBufferEffect = new DeferredPBR_GBufferEffect(GraphicsDevice);
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DeferredAmbientLightEffect = new DeferredPBR_AmbientLightEffect(GraphicsDevice);
|
|
|
|
|
DeferredPointLightEffect = new DeferredPBR_PointLightEffect(GraphicsDevice);
|
|
|
|
|
DeferredDirectionalLightEffect = new DeferredPBR_DirectionalLightEffect(GraphicsDevice);
|
|
|
|
|
ToneMapEffect = new Effect(graphicsDevice, Resources.ToneMapEffect);
|
2020-10-01 21:52:19 +00:00
|
|
|
|
Deferred_ToonEffect = new Deferred_ToonEffect(GraphicsDevice);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
SkyboxEffect = new SkyboxEffect(GraphicsDevice);
|
2020-12-06 03:47:01 +00:00
|
|
|
|
DiffuseLitSpriteEffect = new DiffuseLitSpriteEffect(GraphicsDevice);
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
|
|
|
|
FullscreenTriangle = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 3, BufferUsage.WriteOnly);
|
|
|
|
|
FullscreenTriangle.SetData(new VertexPositionTexture[3] {
|
|
|
|
|
new VertexPositionTexture(new Vector3(-1, -3, 0), new Vector2(0, 2)),
|
|
|
|
|
new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)),
|
|
|
|
|
new VertexPositionTexture(new Vector3(3, 1, 0), new Vector2(2, 0))
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-17 20:53:26 +00:00
|
|
|
|
UnitCube = Kav.ModelLoader.Load(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
Smuggler.Importer.ImportGLB(GraphicsDevice, new MemoryStream(Resources.UnitCubeModel))
|
|
|
|
|
);
|
2020-12-10 00:10:53 +00:00
|
|
|
|
|
|
|
|
|
UnitSphere = Kav.ModelLoader.Load(
|
|
|
|
|
graphicsDevice,
|
|
|
|
|
Smuggler.Importer.ImportGLB(graphicsDevice, new MemoryStream(Resources.UnitSphereModel))
|
|
|
|
|
);
|
2020-12-09 00:37:22 +00:00
|
|
|
|
}
|
2020-10-17 20:53:26 +00:00
|
|
|
|
|
2020-12-09 06:20:54 +00:00
|
|
|
|
public static (T[], DynamicVertexBuffer) CreateInstanceVertexBuffer<T>(
|
2020-12-09 00:37:22 +00:00
|
|
|
|
GraphicsDevice graphicsDevice,
|
|
|
|
|
int instanceVertexCount
|
2020-12-09 06:20:54 +00:00
|
|
|
|
) where T : IVertexType {
|
|
|
|
|
var positionData = new T[instanceVertexCount];
|
|
|
|
|
|
2020-12-09 00:37:22 +00:00
|
|
|
|
var vertexBuffer = new DynamicVertexBuffer(
|
|
|
|
|
graphicsDevice,
|
2020-12-09 06:20:54 +00:00
|
|
|
|
typeof(T),
|
2020-12-09 00:37:22 +00:00
|
|
|
|
instanceVertexCount,
|
2020-12-07 09:30:09 +00:00
|
|
|
|
BufferUsage.WriteOnly
|
|
|
|
|
);
|
2020-12-09 00:37:22 +00:00
|
|
|
|
|
|
|
|
|
return (positionData, vertexBuffer);
|
2020-08-27 18:14:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 05:36:16 +00:00
|
|
|
|
public static RenderTargetCube CreateShadowCubeMap(
|
|
|
|
|
GraphicsDevice graphicsDevice,
|
|
|
|
|
int shadowMapSize
|
|
|
|
|
) {
|
|
|
|
|
return new RenderTargetCube(
|
|
|
|
|
graphicsDevice,
|
|
|
|
|
shadowMapSize,
|
|
|
|
|
false,
|
|
|
|
|
SurfaceFormat.Single,
|
|
|
|
|
DepthFormat.Depth24,
|
|
|
|
|
0,
|
|
|
|
|
RenderTargetUsage.PreserveContents
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DirectionalShadowMapData CreateDirectionalShadowMaps(
|
|
|
|
|
GraphicsDevice graphicsDevice,
|
|
|
|
|
int shadowMapSize,
|
|
|
|
|
int numCascades
|
|
|
|
|
) {
|
|
|
|
|
return new DirectionalShadowMapData(
|
|
|
|
|
graphicsDevice,
|
|
|
|
|
shadowMapSize,
|
|
|
|
|
numCascades
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 02:52:14 +00:00
|
|
|
|
// TODO: we could make this a lot more efficient probably
|
|
|
|
|
// draws mesh sprites with a forward rendered diffuse lighting technique
|
|
|
|
|
public void MeshSpriteRender(
|
2020-12-04 23:39:29 +00:00
|
|
|
|
RenderTarget2D renderTarget,
|
|
|
|
|
PerspectiveCamera camera,
|
2020-12-10 08:34:06 +00:00
|
|
|
|
IEnumerable<MeshSpriteDrawData> meshSpriteDrawDatas,
|
2020-12-06 03:47:01 +00:00
|
|
|
|
AmbientLight ambientLight,
|
|
|
|
|
IEnumerable<PointLight> pointLights,
|
2020-12-07 02:52:14 +00:00
|
|
|
|
DirectionalLight? directionalLight
|
2020-12-04 23:39:29 +00:00
|
|
|
|
) {
|
2020-12-08 02:46:53 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-12-07 02:52:14 +00:00
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
|
|
|
|
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
|
|
|
|
|
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
|
|
|
|
|
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.AlphaBlend;
|
|
|
|
|
|
2020-12-06 03:47:01 +00:00
|
|
|
|
DiffuseLitSpriteEffect.View = camera.View;
|
|
|
|
|
DiffuseLitSpriteEffect.Projection = camera.Projection;
|
|
|
|
|
|
2020-12-06 03:58:29 +00:00
|
|
|
|
DiffuseLitSpriteEffect.AmbientColor =
|
|
|
|
|
ambientLight.Color.ToVector3();
|
|
|
|
|
|
2020-12-07 02:52:14 +00:00
|
|
|
|
if (directionalLight.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DiffuseLitSpriteEffect.DirectionalLightDirection =
|
|
|
|
|
directionalLight.Value.Direction;
|
|
|
|
|
DiffuseLitSpriteEffect.DirectionalLightColor =
|
|
|
|
|
directionalLight.Value.Color.ToVector3() * directionalLight.Value.Intensity;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DiffuseLitSpriteEffect.DirectionalLightColor = Vector3.Zero;
|
|
|
|
|
}
|
2020-12-06 03:47:01 +00:00
|
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
|
foreach (var pointLight in pointLights)
|
|
|
|
|
{
|
|
|
|
|
if (i > DiffuseLitSpriteEffect.MaxPointLights) { break; }
|
|
|
|
|
DiffuseLitSpriteEffect.PointLights[i] = pointLight;
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-12-07 03:27:46 +00:00
|
|
|
|
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
|
|
|
|
|
|
2020-12-11 00:02:13 +00:00
|
|
|
|
foreach (var data in FrustumCull(boundingFrustum, meshSpriteDrawDatas))
|
2020-12-04 23:39:29 +00:00
|
|
|
|
{
|
2020-12-10 08:34:06 +00:00
|
|
|
|
var matrix = BillboardTransforms(camera, data.TransformMatrix, data.BillboardConstraint);
|
2020-12-07 02:52:14 +00:00
|
|
|
|
|
2020-12-10 08:34:06 +00:00
|
|
|
|
DiffuseLitSpriteEffect.NormalMapEnabled = data.MeshSprite.Normal != null;
|
|
|
|
|
|
|
|
|
|
DiffuseLitSpriteEffect.World = matrix;
|
2020-12-11 00:02:13 +00:00
|
|
|
|
DiffuseLitSpriteEffect.UVData = data.UVData.ToVector4();
|
2020-12-05 02:51:59 +00:00
|
|
|
|
|
2020-12-10 08:34:06 +00:00
|
|
|
|
GraphicsDevice.Textures[0] = data.MeshSprite.Texture;
|
|
|
|
|
GraphicsDevice.Textures[1] = data.MeshSprite.Normal;
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-12-10 08:34:06 +00:00
|
|
|
|
GraphicsDevice.SetVertexBuffer(data.MeshSprite.VertexBuffer);
|
|
|
|
|
GraphicsDevice.Indices = data.MeshSprite.IndexBuffer;
|
2020-12-07 02:52:14 +00:00
|
|
|
|
|
|
|
|
|
foreach (var pass in DiffuseLitSpriteEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
2020-12-10 08:34:06 +00:00
|
|
|
|
data.MeshSprite.VertexBuffer.VertexCount,
|
2020-12-07 02:52:14 +00:00
|
|
|
|
0,
|
|
|
|
|
2
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-04 23:39:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 08:34:06 +00:00
|
|
|
|
private static Matrix BillboardTransforms(
|
2020-12-08 23:29:28 +00:00
|
|
|
|
PerspectiveCamera camera,
|
2020-12-10 08:34:06 +00:00
|
|
|
|
Matrix transform,
|
|
|
|
|
SpriteBillboardConstraint billboardConstraint
|
2020-12-08 23:29:28 +00:00
|
|
|
|
) {
|
2020-12-10 08:34:06 +00:00
|
|
|
|
if (billboardConstraint == SpriteBillboardConstraint.None)
|
2020-12-08 23:29:28 +00:00
|
|
|
|
{
|
2020-12-10 08:34:06 +00:00
|
|
|
|
return transform;
|
|
|
|
|
}
|
|
|
|
|
else if (billboardConstraint == SpriteBillboardConstraint.Horizontal)
|
|
|
|
|
{
|
|
|
|
|
return Matrix.CreateConstrainedBillboard(
|
|
|
|
|
transform.Translation,
|
|
|
|
|
camera.Position,
|
|
|
|
|
Vector3.Up,
|
|
|
|
|
camera.Forward,
|
|
|
|
|
camera.Position - transform.Translation
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Matrix.CreateConstrainedBillboard(
|
|
|
|
|
transform.Translation,
|
|
|
|
|
camera.Position,
|
|
|
|
|
Vector3.Up,
|
|
|
|
|
null,
|
|
|
|
|
null
|
|
|
|
|
);
|
2020-12-08 23:29:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
// Renders a series of drawable-transform pairs using an effect that has a World matrix.
|
|
|
|
|
// Effect must be pre-configured!!
|
|
|
|
|
public static void CullAndRenderIndexed<T, U>(
|
|
|
|
|
GraphicsDevice graphicsDevice,
|
2020-12-08 10:49:18 +00:00
|
|
|
|
BoundingFrustum boundingFrustum,
|
2020-12-11 00:02:13 +00:00
|
|
|
|
IEnumerable<T> drawables,
|
2020-12-08 04:10:27 +00:00
|
|
|
|
U effect
|
2020-12-10 21:53:55 +00:00
|
|
|
|
) where T : IIndexDrawable, ICullable where U : Effect, IHasWorldMatrix
|
2020-12-08 04:10:27 +00:00
|
|
|
|
{
|
2020-12-11 00:02:13 +00:00
|
|
|
|
foreach (var drawable in FrustumCull(boundingFrustum, drawables))
|
2020-12-08 04:10:27 +00:00
|
|
|
|
{
|
2020-12-11 00:02:13 +00:00
|
|
|
|
effect.World = drawable.TransformMatrix;
|
2020-12-08 04:10:27 +00:00
|
|
|
|
|
|
|
|
|
RenderIndexed(
|
|
|
|
|
graphicsDevice,
|
|
|
|
|
drawable,
|
|
|
|
|
effect
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RenderIndexed<T, U>(
|
|
|
|
|
GraphicsDevice graphicsDevice,
|
|
|
|
|
T drawable,
|
|
|
|
|
U effect
|
|
|
|
|
) where T : IIndexDrawable where U : Effect
|
|
|
|
|
{
|
|
|
|
|
graphicsDevice.SetVertexBuffer(drawable.VertexBuffer);
|
|
|
|
|
graphicsDevice.Indices = drawable.IndexBuffer;
|
|
|
|
|
|
|
|
|
|
foreach (var pass in effect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
graphicsDevice.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
drawable.VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
drawable.IndexBuffer.IndexCount / 3
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-07 09:57:57 +00:00
|
|
|
|
|
2020-12-09 00:37:22 +00:00
|
|
|
|
public static void RenderInstanced<T>(
|
2020-12-08 04:10:27 +00:00
|
|
|
|
GraphicsDevice graphicsDevice,
|
|
|
|
|
T drawable,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
VertexBuffer instanceVertexBuffer,
|
|
|
|
|
int numInstances,
|
|
|
|
|
Effect effect
|
|
|
|
|
) where T : IIndexDrawable {
|
|
|
|
|
graphicsDevice.SetVertexBuffers(
|
|
|
|
|
drawable.VertexBuffer,
|
|
|
|
|
new VertexBufferBinding(instanceVertexBuffer, 0, 1)
|
|
|
|
|
);
|
|
|
|
|
graphicsDevice.Indices = drawable.IndexBuffer;
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
foreach (var pass in effect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
graphicsDevice.DrawInstancedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
drawable.VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
drawable.IndexBuffer.IndexCount / 3,
|
|
|
|
|
numInstances
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: can probably make this static somehow
|
|
|
|
|
public void RenderFullscreenEffect(
|
|
|
|
|
Effect effect
|
|
|
|
|
) {
|
|
|
|
|
foreach (var pass in effect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(FullscreenTriangle);
|
|
|
|
|
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 05:36:16 +00:00
|
|
|
|
public void RenderDepthIndexed<T>(
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderTarget2D renderTarget,
|
|
|
|
|
PerspectiveCamera camera,
|
2020-12-11 00:02:13 +00:00
|
|
|
|
IEnumerable<T> drawables
|
2020-12-10 21:53:55 +00:00
|
|
|
|
) where T : ICullable, IIndexDrawable
|
2020-12-08 04:10:27 +00:00
|
|
|
|
{
|
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
|
|
|
|
|
SimpleDepthEffect.View = camera.View;
|
|
|
|
|
SimpleDepthEffect.Projection = camera.Projection;
|
|
|
|
|
|
|
|
|
|
CullAndRenderIndexed(
|
|
|
|
|
GraphicsDevice,
|
2020-12-08 10:49:18 +00:00
|
|
|
|
new BoundingFrustum(camera.View * camera.Projection),
|
2020-12-11 00:02:13 +00:00
|
|
|
|
drawables,
|
2020-12-08 04:10:27 +00:00
|
|
|
|
SimpleDepthEffect
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RenderSkybox(
|
2020-12-07 09:30:09 +00:00
|
|
|
|
RenderTarget2D renderTarget,
|
2020-10-17 20:53:26 +00:00
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
TextureCube skybox
|
|
|
|
|
) {
|
2020-12-07 09:30:09 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
GraphicsDevice.RasterizerState.CullMode = CullMode.CullClockwiseFace;
|
2020-12-08 02:46:53 +00:00
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
|
|
|
|
|
2020-10-19 21:00:11 +00:00
|
|
|
|
SkyboxEffect.Skybox = skybox;
|
2020-10-17 20:53:26 +00:00
|
|
|
|
|
|
|
|
|
var view = camera.View;
|
|
|
|
|
view.Translation = Vector3.Zero;
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
SkyboxEffect.View = view;
|
2020-10-17 20:53:26 +00:00
|
|
|
|
SkyboxEffect.Projection = camera.Projection;
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderIndexed(
|
|
|
|
|
GraphicsDevice,
|
2020-12-10 21:53:55 +00:00
|
|
|
|
UnitCube.Meshes[0].MeshParts[0],
|
2020-12-08 04:10:27 +00:00
|
|
|
|
SkyboxEffect
|
|
|
|
|
);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
|
|
|
|
|
GraphicsDevice.RasterizerState.CullMode = CullMode.CullCounterClockwiseFace;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 09:30:09 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// GBuffer binding must have 4 render targets.
|
2020-12-09 00:37:22 +00:00
|
|
|
|
/// Assumes that vertex buffer has been filled.
|
2020-12-07 09:30:09 +00:00
|
|
|
|
/// </summary>
|
2020-12-08 04:10:27 +00:00
|
|
|
|
public void RenderGBufferInstanced<T>(
|
2020-12-07 09:30:09 +00:00
|
|
|
|
RenderTargetBinding[] gBuffer,
|
2020-12-08 02:46:53 +00:00
|
|
|
|
RenderTarget2D depthBuffer,
|
2020-12-07 09:30:09 +00:00
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
T drawable,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
VertexBuffer instanceVertexBuffer,
|
|
|
|
|
int numInstances
|
|
|
|
|
) where T : IGBufferDrawable, IIndexDrawable {
|
2020-12-07 09:30:09 +00:00
|
|
|
|
GraphicsDevice.SetRenderTargets(gBuffer);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
|
|
|
|
|
2020-12-07 21:50:32 +00:00
|
|
|
|
Deferred_GBufferEffect.HardwareInstancingEnabled = true;
|
|
|
|
|
|
2020-12-07 09:30:09 +00:00
|
|
|
|
Deferred_GBufferEffect.Albedo = drawable.Albedo;
|
|
|
|
|
Deferred_GBufferEffect.Metallic = drawable.Metallic;
|
|
|
|
|
Deferred_GBufferEffect.Roughness = drawable.Roughness;
|
|
|
|
|
|
|
|
|
|
Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture;
|
|
|
|
|
Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture;
|
|
|
|
|
Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture;
|
|
|
|
|
|
2020-12-07 21:50:32 +00:00
|
|
|
|
Deferred_GBufferEffect.View = camera.View;
|
|
|
|
|
Deferred_GBufferEffect.Projection = camera.Projection;
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderInstanced(
|
|
|
|
|
GraphicsDevice,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
drawable,
|
|
|
|
|
instanceVertexBuffer,
|
|
|
|
|
numInstances,
|
|
|
|
|
Deferred_GBufferEffect
|
2020-12-07 09:30:09 +00:00
|
|
|
|
);
|
2020-12-08 02:46:53 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
// re-render to get depth
|
2020-12-08 10:49:18 +00:00
|
|
|
|
GraphicsDevice.SetRenderTargets(depthBuffer);
|
|
|
|
|
|
|
|
|
|
SimpleDepthEffectInstanced.View = camera.View;
|
|
|
|
|
SimpleDepthEffectInstanced.Projection = camera.Projection;
|
2020-12-08 02:46:53 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderInstanced(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
drawable,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
instanceVertexBuffer,
|
|
|
|
|
numInstances,
|
|
|
|
|
SimpleDepthEffectInstanced
|
2020-12-08 04:10:27 +00:00
|
|
|
|
);
|
2020-12-07 09:30:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
public void RenderGBufferIndexed<T>(
|
2020-12-07 09:30:09 +00:00
|
|
|
|
RenderTargetBinding[] gBuffer,
|
2020-10-01 21:52:19 +00:00
|
|
|
|
PerspectiveCamera camera,
|
2020-12-11 00:02:13 +00:00
|
|
|
|
IEnumerable<T> drawables
|
|
|
|
|
) where T : ICullable, IIndexDrawable, IUVDrawable, IGBufferDrawable {
|
2020-12-07 09:30:09 +00:00
|
|
|
|
GraphicsDevice.SetRenderTargets(gBuffer);
|
2020-08-27 18:14:17 +00:00
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
Deferred_GBufferEffect.HardwareInstancingEnabled = false;
|
|
|
|
|
Deferred_GBufferEffect.View = camera.View;
|
|
|
|
|
Deferred_GBufferEffect.Projection = camera.Projection;
|
|
|
|
|
|
2020-10-20 01:22:54 +00:00
|
|
|
|
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
|
|
|
|
|
|
2020-12-11 00:02:13 +00:00
|
|
|
|
foreach (var drawable in FrustumCull(boundingFrustum, drawables))
|
2020-08-27 18:14:17 +00:00
|
|
|
|
{
|
2020-12-11 00:02:13 +00:00
|
|
|
|
Deferred_GBufferEffect.World = drawable.TransformMatrix;
|
2020-10-02 05:26:40 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
Deferred_GBufferEffect.HardwareInstancingEnabled = false;
|
2020-12-07 21:50:32 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
Deferred_GBufferEffect.Albedo = drawable.Albedo;
|
|
|
|
|
Deferred_GBufferEffect.Metallic = drawable.Metallic;
|
|
|
|
|
Deferred_GBufferEffect.Roughness = drawable.Roughness;
|
2020-10-02 05:26:40 +00:00
|
|
|
|
|
2020-12-11 00:02:13 +00:00
|
|
|
|
Deferred_GBufferEffect.UVData = drawable.UVData.ToVector4();
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture;
|
|
|
|
|
Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture;
|
|
|
|
|
Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture;
|
2020-08-27 18:14:17 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderIndexed(GraphicsDevice, drawable, Deferred_GBufferEffect);
|
2020-08-27 18:14:17 +00:00
|
|
|
|
}
|
2020-10-01 21:52:19 +00:00
|
|
|
|
}
|
2020-08-27 18:14:17 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
public void RenderAmbientLight(
|
2020-12-07 09:30:09 +00:00
|
|
|
|
RenderTarget2D renderTarget,
|
|
|
|
|
Texture2D gPosition,
|
|
|
|
|
Texture2D gAlbedo,
|
|
|
|
|
AmbientLight ambientLight
|
|
|
|
|
) {
|
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
2020-12-07 09:57:57 +00:00
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
2020-08-27 18:14:17 +00:00
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DeferredAmbientLightEffect.GPosition = gPosition;
|
|
|
|
|
DeferredAmbientLightEffect.GAlbedo = gAlbedo;
|
2020-10-01 21:52:19 +00:00
|
|
|
|
DeferredAmbientLightEffect.AmbientColor = ambientLight.Color.ToVector3();
|
2020-08-27 18:14:17 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderFullscreenEffect(DeferredAmbientLightEffect);
|
2020-08-07 00:58:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 05:36:16 +00:00
|
|
|
|
public void RenderPointLight(
|
2020-12-07 09:30:09 +00:00
|
|
|
|
RenderTarget2D renderTarget,
|
|
|
|
|
Texture2D gPosition,
|
|
|
|
|
Texture2D gAlbedo,
|
|
|
|
|
Texture2D gNormal,
|
|
|
|
|
Texture2D gMetallicRoughness,
|
2020-12-08 05:36:16 +00:00
|
|
|
|
TextureCube shadowMap,
|
2020-10-19 10:01:37 +00:00
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
PointLight pointLight
|
2020-12-08 05:36:16 +00:00
|
|
|
|
) {
|
2020-12-07 09:30:09 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2020-12-10 00:28:40 +00:00
|
|
|
|
GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
|
2020-12-10 00:10:53 +00:00
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.None;
|
2020-10-19 10:01:37 +00:00
|
|
|
|
GraphicsDevice.BlendState = BlendState.Additive;
|
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DeferredPointLightEffect.GPosition = gPosition;
|
|
|
|
|
DeferredPointLightEffect.GAlbedo = gAlbedo;
|
|
|
|
|
DeferredPointLightEffect.GNormal = gNormal;
|
2020-12-04 23:39:29 +00:00
|
|
|
|
DeferredPointLightEffect.GMetallicRoughness = gMetallicRoughness;
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DeferredPointLightEffect.ShadowMap = shadowMap;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
2020-12-07 09:57:57 +00:00
|
|
|
|
DeferredPointLightEffect.EyePosition = camera.Position;
|
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DeferredPointLightEffect.PointLightPosition = pointLight.Position;
|
2020-12-04 23:39:29 +00:00
|
|
|
|
DeferredPointLightEffect.PointLightColor =
|
2020-12-10 00:28:40 +00:00
|
|
|
|
pointLight.Color.ToVector3() * pointLight.Radius;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
2020-10-19 10:01:37 +00:00
|
|
|
|
DeferredPointLightEffect.FarPlane = 25f; // FIXME: magic value
|
|
|
|
|
|
2020-12-10 00:10:53 +00:00
|
|
|
|
DeferredPointLightEffect.World =
|
2020-12-10 00:28:40 +00:00
|
|
|
|
Matrix.CreateScale(pointLight.Radius) *
|
2020-12-10 00:10:53 +00:00
|
|
|
|
Matrix.CreateTranslation(pointLight.Position);
|
|
|
|
|
DeferredPointLightEffect.View = camera.View;
|
|
|
|
|
DeferredPointLightEffect.Projection = camera.Projection;
|
|
|
|
|
|
|
|
|
|
RenderIndexed(
|
2020-12-10 21:53:55 +00:00
|
|
|
|
GraphicsDevice,
|
2020-12-10 00:10:53 +00:00
|
|
|
|
UnitSphere.Meshes[0].MeshParts[0],
|
|
|
|
|
DeferredPointLightEffect
|
|
|
|
|
);
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 10:49:18 +00:00
|
|
|
|
public void RenderDirectionalLight(
|
2020-12-07 09:30:09 +00:00
|
|
|
|
RenderTarget2D renderTarget,
|
|
|
|
|
Texture2D gPosition,
|
|
|
|
|
Texture2D gAlbedo,
|
|
|
|
|
Texture2D gNormal,
|
|
|
|
|
Texture2D gMetallicRoughness,
|
2020-12-08 10:49:18 +00:00
|
|
|
|
DirectionalShadowMapData shadowMapData,
|
2020-10-01 19:46:25 +00:00
|
|
|
|
PerspectiveCamera camera,
|
2020-12-08 10:49:18 +00:00
|
|
|
|
DirectionalLight directionalLight
|
|
|
|
|
) {
|
2020-12-07 09:30:09 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2020-12-10 00:10:53 +00:00
|
|
|
|
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
|
2020-12-07 09:30:09 +00:00
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Additive;
|
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DeferredDirectionalLightEffect.GPosition = gPosition;
|
|
|
|
|
DeferredDirectionalLightEffect.GAlbedo = gAlbedo;
|
|
|
|
|
DeferredDirectionalLightEffect.GNormal = gNormal;
|
|
|
|
|
DeferredDirectionalLightEffect.GMetallicRoughness = gMetallicRoughness;
|
|
|
|
|
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapSize = shadowMapData.ShadowMapSize;
|
|
|
|
|
|
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapOne = shadowMapData.ShadowMaps[0];
|
2020-12-08 11:07:41 +00:00
|
|
|
|
DeferredDirectionalLightEffect.LightSpaceMatrixOne = shadowMapData.LightSpaceViews[0] * shadowMapData.LightSpaceProjections[0];
|
2020-12-08 10:49:18 +00:00
|
|
|
|
DeferredDirectionalLightEffect.CascadeFarPlanes[0] = shadowMapData.CascadeFarPlanes[0];
|
2020-12-08 05:36:16 +00:00
|
|
|
|
|
|
|
|
|
if (shadowMapData.NumShadowCascades > 1)
|
2020-10-01 19:46:25 +00:00
|
|
|
|
{
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapTwo = shadowMapData.ShadowMaps[1];
|
2020-12-08 11:07:41 +00:00
|
|
|
|
DeferredDirectionalLightEffect.LightSpaceMatrixTwo = shadowMapData.LightSpaceViews[1] * shadowMapData.LightSpaceProjections[1];
|
2020-12-08 10:49:18 +00:00
|
|
|
|
DeferredDirectionalLightEffect.CascadeFarPlanes[1] = shadowMapData.CascadeFarPlanes[1];
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
2020-12-08 05:36:16 +00:00
|
|
|
|
if (shadowMapData.NumShadowCascades > 2)
|
2020-10-01 19:46:25 +00:00
|
|
|
|
{
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapThree = shadowMapData.ShadowMaps[2];
|
2020-12-08 11:07:41 +00:00
|
|
|
|
DeferredDirectionalLightEffect.LightSpaceMatrixThree = shadowMapData.LightSpaceViews[2] * shadowMapData.LightSpaceProjections[2];
|
2020-12-08 10:49:18 +00:00
|
|
|
|
DeferredDirectionalLightEffect.CascadeFarPlanes[2] = shadowMapData.CascadeFarPlanes[2];
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
2020-12-08 05:36:16 +00:00
|
|
|
|
if (shadowMapData.NumShadowCascades > 3)
|
2020-10-01 19:46:25 +00:00
|
|
|
|
{
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapFour = shadowMapData.ShadowMaps[3];
|
2020-12-08 11:07:41 +00:00
|
|
|
|
DeferredDirectionalLightEffect.LightSpaceMatrixFour = shadowMapData.LightSpaceViews[3] * shadowMapData.LightSpaceProjections[3];
|
2020-12-08 10:49:18 +00:00
|
|
|
|
DeferredDirectionalLightEffect.CascadeFarPlanes[3] = shadowMapData.CascadeFarPlanes[3];
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DeferredDirectionalLightEffect.DirectionalLightDirection = directionalLight.Direction;
|
2020-12-04 23:39:29 +00:00
|
|
|
|
DeferredDirectionalLightEffect.DirectionalLightColor =
|
2020-10-01 19:46:25 +00:00
|
|
|
|
directionalLight.Color.ToVector3() * directionalLight.Intensity;
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DeferredDirectionalLightEffect.ViewMatrix = camera.View;
|
2020-10-02 19:28:28 +00:00
|
|
|
|
DeferredDirectionalLightEffect.EyePosition = camera.Position;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderFullscreenEffect(DeferredDirectionalLightEffect);
|
2020-08-07 00:58:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 10:49:18 +00:00
|
|
|
|
public void RenderDirectionalLightToon(
|
2020-12-07 09:30:09 +00:00
|
|
|
|
RenderTarget2D renderTarget,
|
|
|
|
|
Texture2D gPosition,
|
|
|
|
|
Texture2D gAlbedo,
|
|
|
|
|
Texture2D gNormal,
|
|
|
|
|
Texture2D gMetallicRoughness,
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DirectionalShadowMapData shadowMapData,
|
2020-10-01 21:52:19 +00:00
|
|
|
|
PerspectiveCamera camera,
|
2020-12-07 09:30:09 +00:00
|
|
|
|
DirectionalLight directionalLight,
|
|
|
|
|
bool ditheredShadows
|
2020-12-08 10:49:18 +00:00
|
|
|
|
) {
|
2020-12-07 09:30:09 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
2020-10-02 05:26:40 +00:00
|
|
|
|
GraphicsDevice.BlendState = BlendState.Additive;
|
|
|
|
|
|
2020-10-02 19:28:28 +00:00
|
|
|
|
Deferred_ToonEffect.GPosition = gPosition;
|
|
|
|
|
Deferred_ToonEffect.GAlbedo = gAlbedo;
|
|
|
|
|
Deferred_ToonEffect.GNormal = gNormal;
|
2020-10-05 22:45:10 +00:00
|
|
|
|
Deferred_ToonEffect.GMetallicRoughness = gMetallicRoughness;
|
2020-10-02 19:28:28 +00:00
|
|
|
|
|
2020-12-07 09:30:09 +00:00
|
|
|
|
Deferred_ToonEffect.DitheredShadows = ditheredShadows;
|
2020-10-16 01:19:43 +00:00
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
Deferred_ToonEffect.EyePosition = camera.Position;
|
2020-12-07 02:52:14 +00:00
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
Deferred_ToonEffect.DirectionalLightDirection = directionalLight.Direction;
|
2020-12-04 23:39:29 +00:00
|
|
|
|
Deferred_ToonEffect.DirectionalLightColor =
|
2020-10-02 05:26:40 +00:00
|
|
|
|
directionalLight.Color.ToVector3() * directionalLight.Intensity;
|
2020-10-01 21:52:19 +00:00
|
|
|
|
|
2020-12-08 05:36:16 +00:00
|
|
|
|
Deferred_ToonEffect.ShadowMapOne = shadowMapData.ShadowMaps[0];
|
2020-12-08 11:07:41 +00:00
|
|
|
|
Deferred_ToonEffect.LightSpaceMatrixOne = shadowMapData.LightSpaceViews[0] * shadowMapData.LightSpaceProjections[0];
|
2020-12-08 05:36:16 +00:00
|
|
|
|
|
|
|
|
|
if (shadowMapData.NumShadowCascades > 1)
|
2020-10-02 19:28:28 +00:00
|
|
|
|
{
|
2020-12-08 05:36:16 +00:00
|
|
|
|
Deferred_ToonEffect.ShadowMapTwo = shadowMapData.ShadowMaps[1];
|
2020-12-08 11:07:41 +00:00
|
|
|
|
Deferred_ToonEffect.LightSpaceMatrixTwo = shadowMapData.LightSpaceViews[1] * shadowMapData.LightSpaceProjections[1];
|
2020-10-02 19:28:28 +00:00
|
|
|
|
}
|
2020-12-08 05:36:16 +00:00
|
|
|
|
if (shadowMapData.NumShadowCascades > 2)
|
2020-10-02 19:28:28 +00:00
|
|
|
|
{
|
2020-12-08 05:36:16 +00:00
|
|
|
|
Deferred_ToonEffect.ShadowMapThree = shadowMapData.ShadowMaps[2];
|
2020-12-08 11:07:41 +00:00
|
|
|
|
Deferred_ToonEffect.LightSpaceMatrixThree = shadowMapData.LightSpaceViews[1] * shadowMapData.LightSpaceProjections[2];
|
2020-10-02 19:28:28 +00:00
|
|
|
|
}
|
2020-12-08 05:36:16 +00:00
|
|
|
|
if (shadowMapData.NumShadowCascades > 3)
|
2020-10-02 19:28:28 +00:00
|
|
|
|
{
|
2020-12-08 05:36:16 +00:00
|
|
|
|
Deferred_ToonEffect.ShadowMapFour = shadowMapData.ShadowMaps[3];
|
2020-12-08 11:07:41 +00:00
|
|
|
|
Deferred_ToonEffect.LightSpaceMatrixFour = shadowMapData.LightSpaceViews[2] * shadowMapData.LightSpaceProjections[3];
|
2020-10-02 19:28:28 +00:00
|
|
|
|
}
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-10-02 19:28:28 +00:00
|
|
|
|
Deferred_ToonEffect.ViewMatrix = camera.View;
|
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderFullscreenEffect(Deferred_ToonEffect);
|
2020-10-01 21:52:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 11:07:41 +00:00
|
|
|
|
public void PrepareDirectionalShadowData(
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DirectionalShadowMapData shadowMapData,
|
2020-10-02 19:28:28 +00:00
|
|
|
|
PerspectiveCamera camera,
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DirectionalLight directionalLight
|
2020-12-08 11:07:41 +00:00
|
|
|
|
) {
|
2020-10-02 19:28:28 +00:00
|
|
|
|
var previousFarPlane = camera.NearPlane;
|
2020-12-08 05:36:16 +00:00
|
|
|
|
for (var i = 0; i < shadowMapData.NumShadowCascades; i++)
|
2020-10-02 19:28:28 +00:00
|
|
|
|
{
|
2020-12-08 05:36:16 +00:00
|
|
|
|
var farPlane = camera.FarPlane / (MathHelper.Max((shadowMapData.NumShadowCascades - i - 1) * 2f, 1f));
|
2020-10-02 19:28:28 +00:00
|
|
|
|
|
2020-12-04 23:39:29 +00:00
|
|
|
|
// divide the view frustum
|
2020-10-02 19:28:28 +00:00
|
|
|
|
var shadowCamera = new PerspectiveCamera(
|
|
|
|
|
camera.Position,
|
|
|
|
|
camera.Forward,
|
|
|
|
|
camera.Up,
|
|
|
|
|
camera.FieldOfView,
|
|
|
|
|
camera.AspectRatio,
|
|
|
|
|
previousFarPlane,
|
|
|
|
|
farPlane
|
|
|
|
|
);
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-12-08 11:07:41 +00:00
|
|
|
|
PrepareDirectionalShadowCascade(
|
|
|
|
|
shadowMapData,
|
|
|
|
|
i,
|
|
|
|
|
shadowCamera,
|
2020-12-08 05:36:16 +00:00
|
|
|
|
directionalLight
|
|
|
|
|
);
|
2020-10-02 19:28:28 +00:00
|
|
|
|
|
2020-12-08 05:36:16 +00:00
|
|
|
|
shadowMapData.CascadeFarPlanes[i] = farPlane;
|
2020-10-02 19:28:28 +00:00
|
|
|
|
previousFarPlane = farPlane;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 11:07:41 +00:00
|
|
|
|
private void PrepareDirectionalShadowCascade(
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DirectionalShadowMapData shadowMapData,
|
|
|
|
|
int shadowCascadeIndex,
|
2020-12-08 11:07:41 +00:00
|
|
|
|
PerspectiveCamera shadowCamera,
|
2020-12-08 05:36:16 +00:00
|
|
|
|
DirectionalLight directionalLight
|
2020-12-08 11:07:41 +00:00
|
|
|
|
) {
|
|
|
|
|
var cameraBoundingFrustum = new BoundingFrustum(shadowCamera.View * shadowCamera.Projection);
|
2020-10-01 19:46:25 +00:00
|
|
|
|
Vector3[] frustumCorners = cameraBoundingFrustum.GetCorners();
|
|
|
|
|
|
|
|
|
|
Vector3 frustumCenter = Vector3.Zero;
|
|
|
|
|
for (var i = 0; i < frustumCorners.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
frustumCenter += frustumCorners[i];
|
|
|
|
|
}
|
|
|
|
|
frustumCenter /= 8f;
|
2020-08-07 00:58:50 +00:00
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
var lightView = Matrix.CreateLookAt(frustumCenter + directionalLight.Direction, frustumCenter, Vector3.Backward);
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < frustumCorners.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners);
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-12-08 11:07:41 +00:00
|
|
|
|
shadowMapData.LightSpaceViews[shadowCascadeIndex] = lightView;
|
|
|
|
|
shadowMapData.LightSpaceProjections[shadowCascadeIndex] = Matrix.CreateOrthographicOffCenter(
|
2020-10-01 19:46:25 +00:00
|
|
|
|
lightBox.Min.X,
|
|
|
|
|
lightBox.Max.X,
|
|
|
|
|
lightBox.Min.Y,
|
|
|
|
|
lightBox.Max.Y,
|
|
|
|
|
-lightBox.Max.Z - 10f, // TODO: near clip plane needs scene AABB info to get rid of this magic value
|
|
|
|
|
-lightBox.Min.Z
|
|
|
|
|
);
|
2020-12-08 11:07:41 +00:00
|
|
|
|
}
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
2020-12-08 11:07:41 +00:00
|
|
|
|
public void RenderDirectionalShadowsIndexed<T>(
|
|
|
|
|
DirectionalShadowMapData shadowMapData,
|
2020-12-11 00:02:13 +00:00
|
|
|
|
IEnumerable<T> drawableTransforms
|
2020-12-08 11:07:41 +00:00
|
|
|
|
) where T : ICullable, IIndexDrawable {
|
|
|
|
|
// render the individual shadow cascades
|
|
|
|
|
for (var i = 0; i < shadowMapData.NumShadowCascades; i++)
|
2020-10-01 19:46:25 +00:00
|
|
|
|
{
|
2020-12-08 11:07:41 +00:00
|
|
|
|
RenderDirectionalShadowMapIndexed(
|
2020-12-10 21:53:55 +00:00
|
|
|
|
shadowMapData,
|
|
|
|
|
i,
|
2020-12-08 11:07:41 +00:00
|
|
|
|
drawableTransforms
|
|
|
|
|
);
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
2020-12-08 11:07:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 11:35:06 +00:00
|
|
|
|
private void RenderDirectionalShadowMapIndexed<T>(
|
2020-12-08 11:07:41 +00:00
|
|
|
|
DirectionalShadowMapData shadowMapData,
|
|
|
|
|
int shadowCascadeIndex,
|
2020-12-11 00:02:13 +00:00
|
|
|
|
IEnumerable<T> drawables
|
2020-12-08 11:07:41 +00:00
|
|
|
|
) where T : ICullable, IIndexDrawable {
|
|
|
|
|
GraphicsDevice.SetRenderTarget(shadowMapData.ShadowMaps[shadowCascadeIndex]);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
|
|
|
|
|
|
|
|
|
SimpleDepthEffect.View = shadowMapData.LightSpaceViews[shadowCascadeIndex];
|
|
|
|
|
SimpleDepthEffect.Projection = shadowMapData.LightSpaceProjections[shadowCascadeIndex];
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
2020-12-08 10:49:18 +00:00
|
|
|
|
CullAndRenderIndexed(
|
2020-12-10 21:53:55 +00:00
|
|
|
|
GraphicsDevice,
|
|
|
|
|
new BoundingFrustum(SimpleDepthEffect.View * SimpleDepthEffect.Projection),
|
2020-12-11 00:02:13 +00:00
|
|
|
|
drawables,
|
2020-12-08 10:49:18 +00:00
|
|
|
|
SimpleDepthEffect
|
|
|
|
|
);
|
2020-08-07 00:58:50 +00:00
|
|
|
|
}
|
2020-10-19 10:01:37 +00:00
|
|
|
|
|
2020-12-08 11:35:06 +00:00
|
|
|
|
public void RenderDirectionalShadowsInstanced<T>(
|
|
|
|
|
DirectionalShadowMapData shadowMapData,
|
|
|
|
|
T drawable,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
VertexBuffer instanceVertexBuffer,
|
|
|
|
|
int numInstances
|
|
|
|
|
) where T : IIndexDrawable
|
2020-12-08 11:35:06 +00:00
|
|
|
|
{
|
|
|
|
|
// render the individual shadow cascades
|
|
|
|
|
for (var i = 0; i < shadowMapData.NumShadowCascades; i++)
|
|
|
|
|
{
|
|
|
|
|
RenderDirectionalShadowMapInstanced(
|
|
|
|
|
shadowMapData,
|
|
|
|
|
i,
|
|
|
|
|
drawable,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
instanceVertexBuffer,
|
|
|
|
|
numInstances
|
2020-12-08 11:35:06 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RenderDirectionalShadowMapInstanced<T>(
|
|
|
|
|
DirectionalShadowMapData shadowMapData,
|
|
|
|
|
int shadowCascadeIndex,
|
|
|
|
|
T drawable,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
VertexBuffer instanceVertexBuffer,
|
|
|
|
|
int numInstances
|
|
|
|
|
) where T : IIndexDrawable
|
2020-12-08 11:35:06 +00:00
|
|
|
|
{
|
|
|
|
|
GraphicsDevice.SetRenderTarget(shadowMapData.ShadowMaps[shadowCascadeIndex]);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
|
|
|
|
|
|
|
|
|
SimpleDepthEffectInstanced.View = shadowMapData.LightSpaceViews[shadowCascadeIndex];
|
|
|
|
|
SimpleDepthEffectInstanced.Projection = shadowMapData.LightSpaceProjections[shadowCascadeIndex];
|
|
|
|
|
|
|
|
|
|
RenderInstanced(
|
2020-12-10 21:53:55 +00:00
|
|
|
|
GraphicsDevice,
|
2020-12-08 11:35:06 +00:00
|
|
|
|
drawable,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
instanceVertexBuffer,
|
|
|
|
|
numInstances,
|
|
|
|
|
SimpleDepthEffectInstanced
|
2020-12-08 11:35:06 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 10:49:18 +00:00
|
|
|
|
public void RenderPointShadowMapIndexed<T>(
|
2020-12-08 04:10:27 +00:00
|
|
|
|
RenderTargetCube pointShadowCubeMap,
|
2020-12-11 00:02:13 +00:00
|
|
|
|
IEnumerable<T> drawables,
|
2020-10-19 10:01:37 +00:00
|
|
|
|
PointLight pointLight
|
2020-12-08 04:10:27 +00:00
|
|
|
|
) where T : ICullable, IIndexDrawable {
|
2020-10-19 10:01:37 +00:00
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
|
|
|
|
|
|
|
|
|
foreach (CubeMapFace face in Enum.GetValues(typeof(CubeMapFace)))
|
|
|
|
|
{
|
2020-12-08 04:10:27 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(pointShadowCubeMap, face);
|
2020-10-19 10:01:37 +00:00
|
|
|
|
|
|
|
|
|
Vector3 targetDirection;
|
|
|
|
|
Vector3 targetUpDirection;
|
|
|
|
|
switch(face)
|
|
|
|
|
{
|
|
|
|
|
case CubeMapFace.PositiveX:
|
|
|
|
|
targetDirection = Vector3.Right;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.NegativeX:
|
|
|
|
|
targetDirection = Vector3.Left;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.PositiveY:
|
|
|
|
|
targetDirection = Vector3.Up;
|
|
|
|
|
targetUpDirection = Vector3.Forward;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.NegativeY:
|
|
|
|
|
targetDirection = Vector3.Down;
|
|
|
|
|
targetUpDirection = Vector3.Backward;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.PositiveZ:
|
|
|
|
|
targetDirection = Vector3.Backward;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.NegativeZ:
|
|
|
|
|
targetDirection = Vector3.Forward;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
targetDirection = Vector3.Right;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinearDepthEffect.View = Matrix.CreateLookAt(
|
|
|
|
|
pointLight.Position,
|
|
|
|
|
pointLight.Position + targetDirection,
|
|
|
|
|
targetUpDirection
|
|
|
|
|
);
|
2020-12-08 04:10:27 +00:00
|
|
|
|
LinearDepthEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
|
|
|
|
|
MathHelper.PiOver2,
|
|
|
|
|
1,
|
|
|
|
|
0.1f,
|
|
|
|
|
25f // FIXME: magic value
|
|
|
|
|
);
|
|
|
|
|
LinearDepthEffect.FarPlane = 25f;
|
2020-10-19 10:01:37 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
LinearDepthEffect.LightPosition = pointLight.Position;
|
2020-10-20 01:22:54 +00:00
|
|
|
|
|
2020-12-08 04:10:27 +00:00
|
|
|
|
CullAndRenderIndexed(
|
|
|
|
|
GraphicsDevice,
|
2020-12-10 21:53:55 +00:00
|
|
|
|
new BoundingFrustum(LinearDepthEffect.View * LinearDepthEffect.Projection),
|
2020-12-11 00:02:13 +00:00
|
|
|
|
drawables,
|
2020-12-08 04:10:27 +00:00
|
|
|
|
LinearDepthEffect
|
|
|
|
|
);
|
2020-10-19 10:01:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-20 01:22:54 +00:00
|
|
|
|
|
2020-12-08 10:49:18 +00:00
|
|
|
|
public void RenderPointShadowMapInstanced<T>(
|
|
|
|
|
RenderTargetCube pointShadowCubeMap,
|
|
|
|
|
T drawable,
|
2020-12-09 00:37:22 +00:00
|
|
|
|
VertexBuffer instanceVertexBuffer,
|
|
|
|
|
int numInstances,
|
2020-12-08 10:49:18 +00:00
|
|
|
|
PointLight pointLight
|
2020-12-11 00:02:13 +00:00
|
|
|
|
) where T : IIndexDrawable
|
2020-12-08 10:49:18 +00:00
|
|
|
|
{
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
|
|
|
|
|
|
|
|
|
foreach (CubeMapFace face in Enum.GetValues(typeof(CubeMapFace)))
|
|
|
|
|
{
|
|
|
|
|
GraphicsDevice.SetRenderTarget(pointShadowCubeMap, face);
|
|
|
|
|
|
|
|
|
|
Vector3 targetDirection;
|
|
|
|
|
Vector3 targetUpDirection;
|
|
|
|
|
switch(face)
|
|
|
|
|
{
|
|
|
|
|
case CubeMapFace.PositiveX:
|
|
|
|
|
targetDirection = Vector3.Right;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.NegativeX:
|
|
|
|
|
targetDirection = Vector3.Left;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.PositiveY:
|
|
|
|
|
targetDirection = Vector3.Up;
|
|
|
|
|
targetUpDirection = Vector3.Forward;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.NegativeY:
|
|
|
|
|
targetDirection = Vector3.Down;
|
|
|
|
|
targetUpDirection = Vector3.Backward;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.PositiveZ:
|
|
|
|
|
targetDirection = Vector3.Backward;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CubeMapFace.NegativeZ:
|
|
|
|
|
targetDirection = Vector3.Forward;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
targetDirection = Vector3.Right;
|
|
|
|
|
targetUpDirection = Vector3.Up;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 00:37:22 +00:00
|
|
|
|
LinearDepthEffectInstanced.View = Matrix.CreateLookAt(
|
2020-12-08 10:49:18 +00:00
|
|
|
|
pointLight.Position,
|
|
|
|
|
pointLight.Position + targetDirection,
|
|
|
|
|
targetUpDirection
|
|
|
|
|
);
|
2020-12-09 00:37:22 +00:00
|
|
|
|
LinearDepthEffectInstanced.Projection = Matrix.CreatePerspectiveFieldOfView(
|
2020-12-08 10:49:18 +00:00
|
|
|
|
MathHelper.PiOver2,
|
|
|
|
|
1,
|
|
|
|
|
0.1f,
|
|
|
|
|
25f // FIXME: magic value
|
|
|
|
|
);
|
|
|
|
|
|
2020-12-09 00:37:22 +00:00
|
|
|
|
LinearDepthEffectInstanced.FarPlane = 25f;
|
|
|
|
|
LinearDepthEffectInstanced.LightPosition = pointLight.Position;
|
2020-12-08 10:49:18 +00:00
|
|
|
|
|
2020-12-09 00:37:22 +00:00
|
|
|
|
RenderInstanced(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
drawable,
|
|
|
|
|
instanceVertexBuffer,
|
|
|
|
|
numInstances,
|
|
|
|
|
LinearDepthEffectInstanced
|
|
|
|
|
);
|
2020-12-08 10:49:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 00:02:13 +00:00
|
|
|
|
private static IEnumerable<T> FrustumCull<T>(
|
2020-10-20 01:22:54 +00:00
|
|
|
|
BoundingFrustum boundingFrustum,
|
2020-12-11 00:02:13 +00:00
|
|
|
|
IEnumerable<T> cullables
|
2020-12-07 03:36:00 +00:00
|
|
|
|
) where T : ICullable {
|
2020-12-11 00:02:13 +00:00
|
|
|
|
foreach (var cullable in cullables)
|
2020-12-07 03:27:46 +00:00
|
|
|
|
{
|
2020-12-11 00:02:13 +00:00
|
|
|
|
var boundingBox = TransformedBoundingBox(cullable.BoundingBox, cullable.TransformMatrix);
|
2020-12-07 03:27:46 +00:00
|
|
|
|
var containment = boundingFrustum.Contains(boundingBox);
|
|
|
|
|
if (containment != ContainmentType.Disjoint)
|
|
|
|
|
{
|
2020-12-11 00:02:13 +00:00
|
|
|
|
yield return cullable;
|
2020-12-07 03:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 01:22:54 +00:00
|
|
|
|
private static BoundingBox TransformedBoundingBox(BoundingBox boundingBox, Matrix matrix)
|
|
|
|
|
{
|
|
|
|
|
var center = (boundingBox.Min + boundingBox.Max) / 2f;
|
|
|
|
|
var extent = (boundingBox.Max - boundingBox.Min) / 2f;
|
|
|
|
|
|
|
|
|
|
var newCenter = Vector3.Transform(center, matrix);
|
|
|
|
|
var newExtent = Vector3.TransformNormal(extent, AbsoluteMatrix(matrix));
|
|
|
|
|
|
|
|
|
|
return new BoundingBox(newCenter - newExtent, newCenter + newExtent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Matrix AbsoluteMatrix(Matrix matrix)
|
|
|
|
|
{
|
|
|
|
|
return new Matrix(
|
|
|
|
|
Math.Abs(matrix.M11), Math.Abs(matrix.M12), Math.Abs(matrix.M13), Math.Abs(matrix.M14),
|
|
|
|
|
Math.Abs(matrix.M21), Math.Abs(matrix.M22), Math.Abs(matrix.M23), Math.Abs(matrix.M24),
|
|
|
|
|
Math.Abs(matrix.M31), Math.Abs(matrix.M32), Math.Abs(matrix.M33), Math.Abs(matrix.M34),
|
|
|
|
|
Math.Abs(matrix.M41), Math.Abs(matrix.M42), Math.Abs(matrix.M43), Math.Abs(matrix.M44)
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-08-04 09:32:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|