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-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-10-01 19:46:25 +00:00
|
|
|
|
private const int MAX_SHADOW_CASCADES = 4;
|
|
|
|
|
private int ShadowMapSize { get; }
|
|
|
|
|
|
2020-08-07 00:58:50 +00:00
|
|
|
|
private GraphicsDevice GraphicsDevice { get; }
|
|
|
|
|
private int RenderDimensionsX { get; }
|
|
|
|
|
private int RenderDimensionsY { get; }
|
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
private VertexBuffer FullscreenTriangle { get; }
|
|
|
|
|
private int NumShadowCascades { get; }
|
|
|
|
|
|
|
|
|
|
private RenderTarget2D ColorRenderTarget { get; }
|
|
|
|
|
private RenderTarget2D DirectionalRenderTarget { get; }
|
|
|
|
|
private RenderTarget2D[] ShadowRenderTargets { 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-10-19 10:01:37 +00:00
|
|
|
|
private LinearDepthEffect LinearDepthEffect { 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-04 23:39:29 +00:00
|
|
|
|
private BasicEffect BasicEffect { get; }
|
2020-08-07 00:58:50 +00:00
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
|
private RenderTarget2D gPosition { get; }
|
|
|
|
|
private RenderTarget2D gNormal { get; }
|
|
|
|
|
private RenderTarget2D gAlbedo { get; }
|
|
|
|
|
private RenderTarget2D gMetallicRoughness { get; }
|
2020-10-19 10:01:37 +00:00
|
|
|
|
private RenderTargetCube PointShadowCubeMap { get; }
|
2020-08-27 18:14:17 +00:00
|
|
|
|
|
|
|
|
|
private RenderTargetBinding[] GBuffer { get; }
|
|
|
|
|
|
2020-10-17 20:53:26 +00:00
|
|
|
|
private Kav.Model UnitCube { get; }
|
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
|
private SpriteBatch SpriteBatch { get; }
|
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
public Renderer(
|
|
|
|
|
GraphicsDevice graphicsDevice,
|
|
|
|
|
int renderDimensionsX,
|
|
|
|
|
int renderDimensionsY,
|
|
|
|
|
int numShadowCascades,
|
|
|
|
|
int shadowMapSize
|
|
|
|
|
) {
|
2020-08-07 00:58:50 +00:00
|
|
|
|
GraphicsDevice = graphicsDevice;
|
|
|
|
|
RenderDimensionsX = renderDimensionsX;
|
|
|
|
|
RenderDimensionsY = renderDimensionsY;
|
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
ShadowMapSize = shadowMapSize;
|
|
|
|
|
|
|
|
|
|
NumShadowCascades = (int)MathHelper.Clamp(numShadowCascades, 1, MAX_SHADOW_CASCADES);
|
|
|
|
|
ShadowRenderTargets = new RenderTarget2D[numShadowCascades];
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < numShadowCascades; i++)
|
|
|
|
|
{
|
|
|
|
|
ShadowRenderTargets[i] = new RenderTarget2D(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
ShadowMapSize,
|
|
|
|
|
ShadowMapSize,
|
|
|
|
|
false,
|
|
|
|
|
SurfaceFormat.Single,
|
|
|
|
|
DepthFormat.Depth24
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ColorRenderTarget = new RenderTarget2D(
|
|
|
|
|
graphicsDevice,
|
2020-08-07 00:58:50 +00:00
|
|
|
|
renderDimensionsX,
|
|
|
|
|
renderDimensionsY,
|
|
|
|
|
false,
|
2020-10-01 19:46:25 +00:00
|
|
|
|
SurfaceFormat.Color,
|
2020-10-17 20:53:26 +00:00
|
|
|
|
DepthFormat.Depth24,
|
2020-10-01 19:46:25 +00:00
|
|
|
|
0,
|
|
|
|
|
RenderTargetUsage.PreserveContents
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
DirectionalRenderTarget = new RenderTarget2D(
|
|
|
|
|
graphicsDevice,
|
|
|
|
|
renderDimensionsX,
|
|
|
|
|
renderDimensionsY,
|
|
|
|
|
false,
|
|
|
|
|
SurfaceFormat.Color,
|
|
|
|
|
DepthFormat.None,
|
|
|
|
|
0,
|
|
|
|
|
RenderTargetUsage.PreserveContents
|
2020-08-07 00:58:50 +00:00
|
|
|
|
);
|
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
|
gPosition = new RenderTarget2D(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
renderDimensionsX,
|
|
|
|
|
renderDimensionsY,
|
|
|
|
|
false,
|
|
|
|
|
SurfaceFormat.Vector4,
|
|
|
|
|
DepthFormat.Depth24
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
gNormal = new RenderTarget2D(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
renderDimensionsX,
|
|
|
|
|
renderDimensionsY,
|
|
|
|
|
false,
|
|
|
|
|
SurfaceFormat.Vector4,
|
|
|
|
|
DepthFormat.None
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
gAlbedo = new RenderTarget2D(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
renderDimensionsX,
|
|
|
|
|
renderDimensionsY,
|
|
|
|
|
false,
|
|
|
|
|
SurfaceFormat.Color,
|
|
|
|
|
DepthFormat.None
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
gMetallicRoughness = new RenderTarget2D(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
renderDimensionsX,
|
|
|
|
|
renderDimensionsY,
|
|
|
|
|
false,
|
|
|
|
|
SurfaceFormat.HalfVector2,
|
|
|
|
|
DepthFormat.None
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
GBuffer = new RenderTargetBinding[4] {
|
|
|
|
|
new RenderTargetBinding(gPosition),
|
|
|
|
|
new RenderTargetBinding(gNormal),
|
|
|
|
|
new RenderTargetBinding(gAlbedo),
|
|
|
|
|
new RenderTargetBinding(gMetallicRoughness)
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-19 10:01:37 +00:00
|
|
|
|
PointShadowCubeMap = new RenderTargetCube(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
shadowMapSize,
|
|
|
|
|
false,
|
|
|
|
|
SurfaceFormat.Single,
|
|
|
|
|
DepthFormat.Depth24
|
|
|
|
|
);
|
|
|
|
|
|
2020-08-07 00:58:50 +00:00
|
|
|
|
SimpleDepthEffect = new SimpleDepthEffect(GraphicsDevice);
|
2020-10-19 10:01:37 +00:00
|
|
|
|
LinearDepthEffect = new LinearDepthEffect(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);
|
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapSize = ShadowMapSize;
|
|
|
|
|
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-04 23:39:29 +00:00
|
|
|
|
BasicEffect = new BasicEffect(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-10-01 19:46:25 +00:00
|
|
|
|
SpriteBatch = new SpriteBatch(graphicsDevice);
|
2020-08-27 18:14:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeferredRender(
|
2020-10-01 19:46:25 +00:00
|
|
|
|
PerspectiveCamera camera,
|
2020-08-27 18:14:17 +00:00
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
2020-10-01 21:52:19 +00:00
|
|
|
|
AmbientLight ambientLight,
|
2020-08-27 18:14:17 +00:00
|
|
|
|
IEnumerable<PointLight> pointLights,
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DirectionalLight directionalLight
|
2020-08-27 18:14:17 +00:00
|
|
|
|
) {
|
2020-10-01 21:52:19 +00:00
|
|
|
|
GBufferRender(camera, modelTransforms);
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
|
|
|
|
|
GraphicsDevice.Clear(Color.Black);
|
|
|
|
|
|
|
|
|
|
AmbientLightRender(ambientLight);
|
|
|
|
|
|
|
|
|
|
DeferredPointLightEffect.EyePosition = camera.Position;
|
|
|
|
|
|
|
|
|
|
foreach (var pointLight in pointLights)
|
|
|
|
|
{
|
2020-10-19 10:01:37 +00:00
|
|
|
|
PointLightRender(camera, modelTransforms, pointLight);
|
2020-10-01 21:52:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirectionalLightRender(camera, modelTransforms, directionalLight);
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.SetRenderTarget(null);
|
|
|
|
|
GraphicsDevice.Clear(Color.Black);
|
|
|
|
|
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, ToneMapEffect);
|
|
|
|
|
SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White);
|
|
|
|
|
SpriteBatch.End();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeferredToonRender(
|
2020-12-04 23:39:29 +00:00
|
|
|
|
RenderTarget2D renderTarget,
|
2020-10-01 21:52:19 +00:00
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
|
|
|
|
AmbientLight ambientLight,
|
2020-10-19 10:01:37 +00:00
|
|
|
|
IEnumerable<PointLight> pointLights,
|
2020-10-17 20:53:26 +00:00
|
|
|
|
DirectionalLight directionalLight,
|
|
|
|
|
TextureCube skybox
|
2020-12-04 23:39:29 +00:00
|
|
|
|
) {
|
2020-10-01 21:52:19 +00:00
|
|
|
|
GBufferRender(camera, modelTransforms);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
|
|
|
|
|
DepthRender(camera, modelTransforms);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
2020-10-01 21:52:19 +00:00
|
|
|
|
|
|
|
|
|
AmbientLightRender(ambientLight);
|
2020-10-19 10:01:37 +00:00
|
|
|
|
foreach (var pointLight in pointLights)
|
|
|
|
|
{
|
|
|
|
|
PointLightRender(camera, modelTransforms, pointLight);
|
|
|
|
|
}
|
2020-10-19 21:00:11 +00:00
|
|
|
|
DirectionalLightToonRender(camera, modelTransforms, directionalLight);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
SkyboxRender(camera, skybox);
|
2020-10-01 21:52:19 +00:00
|
|
|
|
|
2020-12-04 23:39:29 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2020-10-02 05:26:40 +00:00
|
|
|
|
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, null);
|
2020-10-01 21:52:19 +00:00
|
|
|
|
SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White);
|
|
|
|
|
SpriteBatch.End();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 23:39:29 +00:00
|
|
|
|
// 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 * Matrix.CreateRotationX(sprite.Rotation) * 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();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 20:53:26 +00:00
|
|
|
|
private void DepthRender(
|
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms
|
|
|
|
|
) {
|
2020-10-20 01:22:54 +00:00
|
|
|
|
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
|
|
|
|
|
|
|
|
|
|
foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms))
|
2020-10-17 20:53:26 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var modelMesh in model.Meshes)
|
|
|
|
|
{
|
|
|
|
|
foreach (var meshPart in modelMesh.MeshParts)
|
|
|
|
|
{
|
|
|
|
|
SimpleDepthEffect.Model = transform;
|
|
|
|
|
SimpleDepthEffect.View = camera.View;
|
|
|
|
|
SimpleDepthEffect.Projection = camera.Projection;
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
|
|
|
|
|
GraphicsDevice.Indices = meshPart.IndexBuffer;
|
|
|
|
|
|
|
|
|
|
foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.Triangles.Length
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SkyboxRender(
|
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
TextureCube skybox
|
|
|
|
|
) {
|
|
|
|
|
GraphicsDevice.RasterizerState.CullMode = CullMode.CullClockwiseFace;
|
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;
|
|
|
|
|
SkyboxEffect.View = view;
|
|
|
|
|
|
|
|
|
|
SkyboxEffect.Projection = camera.Projection;
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(UnitCube.Meshes[0].MeshParts[0].VertexBuffer);
|
|
|
|
|
GraphicsDevice.Indices = UnitCube.Meshes[0].MeshParts[0].IndexBuffer;
|
|
|
|
|
|
|
|
|
|
foreach (var pass in SkyboxEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
UnitCube.Meshes[0].MeshParts[0].VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
UnitCube.Meshes[0].MeshParts[0].Triangles.Length
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.RasterizerState.CullMode = CullMode.CullCounterClockwiseFace;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
private void GBufferRender(
|
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms
|
|
|
|
|
) {
|
2020-08-27 18:14:17 +00:00
|
|
|
|
GraphicsDevice.SetRenderTargets(GBuffer);
|
|
|
|
|
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
|
|
|
|
|
2020-10-20 01:22:54 +00:00
|
|
|
|
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
|
|
|
|
|
|
|
|
|
|
foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms))
|
2020-08-27 18:14:17 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var modelMesh in model.Meshes)
|
|
|
|
|
{
|
|
|
|
|
foreach (var meshPart in modelMesh.MeshParts)
|
|
|
|
|
{
|
2020-10-02 05:26:40 +00:00
|
|
|
|
Deferred_GBufferEffect.World = transform;
|
|
|
|
|
Deferred_GBufferEffect.View = camera.View;
|
|
|
|
|
Deferred_GBufferEffect.Projection = camera.Projection;
|
|
|
|
|
|
|
|
|
|
Deferred_GBufferEffect.Albedo = meshPart.Albedo;
|
|
|
|
|
Deferred_GBufferEffect.Metallic = meshPart.Metallic;
|
|
|
|
|
Deferred_GBufferEffect.Roughness = meshPart.Roughness;
|
|
|
|
|
|
|
|
|
|
Deferred_GBufferEffect.AlbedoTexture = meshPart.AlbedoTexture;
|
|
|
|
|
Deferred_GBufferEffect.NormalTexture = meshPart.NormalTexture;
|
|
|
|
|
Deferred_GBufferEffect.MetallicRoughnessTexture = meshPart.MetallicRoughnessTexture;
|
|
|
|
|
|
2020-08-27 18:14:17 +00:00
|
|
|
|
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
|
|
|
|
|
GraphicsDevice.Indices = meshPart.IndexBuffer;
|
|
|
|
|
|
2020-10-02 05:26:40 +00:00
|
|
|
|
foreach (var pass in Deferred_GBufferEffect.CurrentTechnique.Passes)
|
2020-08-27 18:14:17 +00:00
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.Triangles.Length
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-01 21:52:19 +00:00
|
|
|
|
}
|
2020-08-27 18:14:17 +00:00
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
private void AmbientLightRender(AmbientLight ambientLight)
|
|
|
|
|
{
|
2020-10-01 19:46:25 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
|
2020-10-17 20:53:26 +00:00
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
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-10-01 19:46:25 +00:00
|
|
|
|
foreach (var pass in DeferredAmbientLightEffect.CurrentTechnique.Passes)
|
2020-08-27 18:14:17 +00:00
|
|
|
|
{
|
2020-10-01 19:46:25 +00:00
|
|
|
|
pass.Apply();
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(FullscreenTriangle);
|
|
|
|
|
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
|
2020-08-27 18:14:17 +00:00
|
|
|
|
}
|
2020-08-07 00:58:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 10:01:37 +00:00
|
|
|
|
private void PointLightRender(
|
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
|
|
|
|
PointLight pointLight
|
|
|
|
|
) {
|
|
|
|
|
RenderPointShadows(camera, modelTransforms, pointLight);
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
|
|
|
|
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-10-19 10:01:37 +00:00
|
|
|
|
DeferredPointLightEffect.ShadowMap = PointShadowCubeMap;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
|
|
|
|
DeferredPointLightEffect.PointLightPosition = pointLight.Position;
|
2020-12-04 23:39:29 +00:00
|
|
|
|
DeferredPointLightEffect.PointLightColor =
|
2020-10-01 19:46:25 +00:00
|
|
|
|
pointLight.Color.ToVector3() * pointLight.Intensity;
|
|
|
|
|
|
2020-10-19 10:01:37 +00:00
|
|
|
|
DeferredPointLightEffect.FarPlane = 25f; // FIXME: magic value
|
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
foreach (var pass in DeferredPointLightEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(FullscreenTriangle);
|
|
|
|
|
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DirectionalLightRender(
|
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
|
|
|
|
DirectionalLight directionalLight
|
2020-08-07 08:12:46 +00:00
|
|
|
|
) {
|
2020-10-19 10:01:37 +00:00
|
|
|
|
RenderDirectionalShadows(camera, modelTransforms, directionalLight, DeferredDirectionalLightEffect);
|
2020-10-01 19:46:25 +00:00
|
|
|
|
|
|
|
|
|
DeferredDirectionalLightEffect.GPosition = gPosition;
|
|
|
|
|
DeferredDirectionalLightEffect.GAlbedo = gAlbedo;
|
|
|
|
|
DeferredDirectionalLightEffect.GNormal = gNormal;
|
|
|
|
|
DeferredDirectionalLightEffect.GMetallicRoughness = gMetallicRoughness;
|
|
|
|
|
|
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapOne = ShadowRenderTargets[0];
|
|
|
|
|
if (NumShadowCascades > 1)
|
|
|
|
|
{
|
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapTwo = ShadowRenderTargets[1];
|
|
|
|
|
}
|
|
|
|
|
if (NumShadowCascades > 2)
|
|
|
|
|
{
|
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapThree = ShadowRenderTargets[2];
|
|
|
|
|
}
|
|
|
|
|
if (NumShadowCascades > 3)
|
|
|
|
|
{
|
|
|
|
|
DeferredDirectionalLightEffect.ShadowMapFour = ShadowRenderTargets[3];
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Additive;
|
|
|
|
|
|
|
|
|
|
foreach (EffectPass pass in DeferredDirectionalLightEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(FullscreenTriangle);
|
|
|
|
|
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
|
|
|
|
|
}
|
2020-08-07 00:58:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
private void DirectionalLightToonRender(
|
|
|
|
|
PerspectiveCamera camera,
|
2020-10-02 19:28:28 +00:00
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
2020-10-01 21:52:19 +00:00
|
|
|
|
DirectionalLight directionalLight
|
|
|
|
|
) {
|
2020-10-19 10:01:37 +00:00
|
|
|
|
RenderDirectionalShadows(camera, modelTransforms, directionalLight, Deferred_ToonEffect);
|
2020-10-02 19:28:28 +00:00
|
|
|
|
|
2020-10-02 05:26:40 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(ColorRenderTarget);
|
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-10-19 21:00:11 +00:00
|
|
|
|
Deferred_ToonEffect.DitheredShadows = false;
|
2020-10-16 01:19:43 +00:00
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
Deferred_ToonEffect.EyePosition = camera.Position;
|
|
|
|
|
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-10-02 19:28:28 +00:00
|
|
|
|
Deferred_ToonEffect.ShadowMapOne = ShadowRenderTargets[0];
|
|
|
|
|
if (NumShadowCascades > 1)
|
|
|
|
|
{
|
|
|
|
|
Deferred_ToonEffect.ShadowMapTwo = ShadowRenderTargets[1];
|
|
|
|
|
}
|
|
|
|
|
if (NumShadowCascades > 2)
|
|
|
|
|
{
|
|
|
|
|
Deferred_ToonEffect.ShadowMapThree = ShadowRenderTargets[2];
|
|
|
|
|
}
|
|
|
|
|
if (NumShadowCascades > 3)
|
|
|
|
|
{
|
|
|
|
|
Deferred_ToonEffect.ShadowMapFour = ShadowRenderTargets[3];
|
|
|
|
|
}
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-10-02 19:28:28 +00:00
|
|
|
|
Deferred_ToonEffect.ViewMatrix = camera.View;
|
|
|
|
|
|
2020-10-01 21:52:19 +00:00
|
|
|
|
foreach (EffectPass pass in Deferred_ToonEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(FullscreenTriangle);
|
|
|
|
|
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 10:01:37 +00:00
|
|
|
|
private void RenderDirectionalShadows(
|
2020-10-02 19:28:28 +00:00
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
|
|
|
|
DirectionalLight directionalLight,
|
|
|
|
|
ShadowCascadeEffect effect
|
|
|
|
|
) {
|
|
|
|
|
// render the individual shadow cascades
|
|
|
|
|
var previousFarPlane = camera.NearPlane;
|
|
|
|
|
for (var i = 0; i < NumShadowCascades; i++)
|
|
|
|
|
{
|
|
|
|
|
var farPlane = camera.FarPlane / (MathHelper.Max((NumShadowCascades - i - 1) * 2f, 1f));
|
|
|
|
|
|
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-10-02 19:28:28 +00:00
|
|
|
|
// TODO: This is tightly coupled to the effect and it sucks
|
2020-10-19 10:01:37 +00:00
|
|
|
|
RenderDirectionalShadowMap(shadowCamera, modelTransforms, directionalLight, effect, i);
|
2020-10-02 19:28:28 +00:00
|
|
|
|
|
|
|
|
|
effect.CascadeFarPlanes[i] = farPlane;
|
|
|
|
|
previousFarPlane = farPlane;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 10:01:37 +00:00
|
|
|
|
private void RenderDirectionalShadowMap(
|
2020-12-04 23:39:29 +00:00
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
2020-10-01 19:46:25 +00:00
|
|
|
|
DirectionalLight directionalLight,
|
2020-10-02 19:28:28 +00:00
|
|
|
|
ShadowCascadeEffect effect,
|
2020-10-01 19:46:25 +00:00
|
|
|
|
int shadowCascadeIndex
|
|
|
|
|
) {
|
|
|
|
|
GraphicsDevice.SetRenderTarget(ShadowRenderTargets[shadowCascadeIndex]);
|
|
|
|
|
GraphicsDevice.Clear(Color.White);
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-10-01 19:46:25 +00:00
|
|
|
|
var cameraBoundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
|
|
|
|
|
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-10-01 19:46:25 +00:00
|
|
|
|
SimpleDepthEffect.View = lightView;
|
|
|
|
|
SimpleDepthEffect.Projection = Matrix.CreateOrthographicOffCenter(
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var lightSpaceMatrix = SimpleDepthEffect.View * SimpleDepthEffect.Projection;
|
|
|
|
|
|
|
|
|
|
if (shadowCascadeIndex == 0)
|
|
|
|
|
{
|
2020-10-02 19:28:28 +00:00
|
|
|
|
effect.LightSpaceMatrixOne = lightSpaceMatrix;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
|
|
|
|
else if (shadowCascadeIndex == 1)
|
|
|
|
|
{
|
2020-10-02 19:28:28 +00:00
|
|
|
|
effect.LightSpaceMatrixTwo = lightSpaceMatrix;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
|
|
|
|
else if (shadowCascadeIndex == 2)
|
|
|
|
|
{
|
2020-10-02 19:28:28 +00:00
|
|
|
|
effect.LightSpaceMatrixThree = lightSpaceMatrix;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
|
|
|
|
else if (shadowCascadeIndex == 3)
|
|
|
|
|
{
|
2020-10-02 19:28:28 +00:00
|
|
|
|
effect.LightSpaceMatrixFour = lightSpaceMatrix;
|
2020-10-01 19:46:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-20 01:22:54 +00:00
|
|
|
|
var boundingFrustum = new BoundingFrustum(lightSpaceMatrix);
|
2020-12-04 23:39:29 +00:00
|
|
|
|
|
2020-10-20 01:22:54 +00:00
|
|
|
|
foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms))
|
2020-08-07 00:58:50 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var modelMesh in model.Meshes)
|
|
|
|
|
{
|
|
|
|
|
foreach (var meshPart in modelMesh.MeshParts)
|
|
|
|
|
{
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
|
|
|
|
|
GraphicsDevice.Indices = meshPart.IndexBuffer;
|
|
|
|
|
|
|
|
|
|
SimpleDepthEffect.Model = transform;
|
2020-08-04 09:32:02 +00:00
|
|
|
|
|
2020-08-07 00:58:50 +00:00
|
|
|
|
foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.Triangles.Length
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-19 10:01:37 +00:00
|
|
|
|
|
|
|
|
|
private void RenderPointShadows(
|
|
|
|
|
PerspectiveCamera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
|
|
|
|
PointLight pointLight
|
|
|
|
|
) {
|
|
|
|
|
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
|
|
|
|
GraphicsDevice.BlendState = BlendState.Opaque;
|
|
|
|
|
|
|
|
|
|
LinearDepthEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
|
|
|
|
|
MathHelper.PiOver2,
|
|
|
|
|
1,
|
|
|
|
|
0.1f,
|
|
|
|
|
25f // FIXME: magic value
|
|
|
|
|
);
|
|
|
|
|
LinearDepthEffect.FarPlane = 25f;
|
|
|
|
|
LinearDepthEffect.LightPosition = pointLight.Position;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinearDepthEffect.View = Matrix.CreateLookAt(
|
|
|
|
|
pointLight.Position,
|
|
|
|
|
pointLight.Position + targetDirection,
|
|
|
|
|
targetUpDirection
|
|
|
|
|
);
|
|
|
|
|
|
2020-10-20 01:22:54 +00:00
|
|
|
|
var boundingFrustum = new BoundingFrustum(LinearDepthEffect.View * LinearDepthEffect.Projection);
|
|
|
|
|
|
|
|
|
|
foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms))
|
2020-10-19 10:01:37 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var modelMesh in model.Meshes)
|
|
|
|
|
{
|
|
|
|
|
foreach (var meshPart in modelMesh.MeshParts)
|
|
|
|
|
{
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
|
|
|
|
|
GraphicsDevice.Indices = meshPart.IndexBuffer;
|
|
|
|
|
|
|
|
|
|
LinearDepthEffect.Model = transform;
|
|
|
|
|
|
|
|
|
|
foreach (var pass in LinearDepthEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.Triangles.Length
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-20 01:22:54 +00:00
|
|
|
|
|
|
|
|
|
private static IEnumerable<(Model, Matrix)> FrustumCull(
|
|
|
|
|
BoundingFrustum boundingFrustum,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms
|
|
|
|
|
) {
|
|
|
|
|
foreach (var modelTransform in modelTransforms)
|
|
|
|
|
{
|
|
|
|
|
var boundingBox = TransformedBoundingBox(modelTransform.Item1.BoundingBox, modelTransform.Item2);
|
|
|
|
|
var containment = boundingFrustum.Contains(boundingBox);
|
|
|
|
|
if (containment != ContainmentType.Disjoint)
|
|
|
|
|
{
|
|
|
|
|
yield return modelTransform;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|