Skip to content

Commit

Permalink
Implemented all of transparencies except shadowing and the actual pee…
Browse files Browse the repository at this point in the history
…ling algorithm

Signed-off-by: Martin Evans <martindevans@gmail.com>
  • Loading branch information
martindevans committed Oct 6, 2015
1 parent 830a7bd commit 6346359
Show file tree
Hide file tree
Showing 27 changed files with 1,042 additions and 314 deletions.
41 changes: 28 additions & 13 deletions Myre/Myre.Graphics.Content/ClearGBuffer.fx
@@ -1,21 +1,36 @@
#include "FullScreenQuad.fxh"

void PixelShaderFunction(out float4 out_Depth : COLOR0,
out float4 out_Normals : COLOR1,
out float4 out_Diffuse : COLOR2)
float4 defaultDepth = float4(1, 1, 1, 1);
float4 defaultNormals = float4(0, 0, 0, 0);
float4 defaultDiffuse = float4(0, 0, 0, 1);

void Pixel_DepthNormalsDiffuse(out float4 out_Depth : COLOR0, out float4 out_Normals : COLOR1, out float4 out_Diffuse : COLOR2)
{
out_Depth = defaultDepth;
out_Normals = defaultNormals;
out_Diffuse = defaultDiffuse;
}

void Pixel_NormalsDiffuse(out float4 out_Normals : COLOR0, out float4 out_Diffuse : COLOR1)
{
out_Depth = float4(1, 1, 1, 1);
out_Normals = float4(0, 0, 0, 1);
out_Diffuse = float4(0, 0, 0, 1);
out_Normals = defaultNormals;
out_Diffuse = defaultDiffuse;
}

technique Technique1
technique Clear_DepthNormalsDiffuse
{
pass Pass1
{
// TODO: set renderstates here.
pass Pass1
{
VertexShader = compile vs_2_0 FullScreenQuadVS();
PixelShader = compile ps_2_0 Pixel_DepthNormalsDiffuse();
}
}

VertexShader = compile vs_2_0 FullScreenQuadVS();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
technique Clear_NormalsDiffuse
{
pass Pass1
{
VertexShader = compile vs_2_0 FullScreenQuadVS();
PixelShader = compile ps_2_0 Pixel_NormalsDiffuse();
}
}
36 changes: 32 additions & 4 deletions Myre/Myre.Graphics.Content/CopyTexture.fx
Expand Up @@ -11,17 +11,45 @@ sampler textureSampler = sampler_state
MipFilter = None;
};

void PixelShaderFunction(in float2 in_TexCoord: TEXCOORD0,
out float4 out_Colour: COLOR0)
texture Stencil;
sampler stencilSample = sampler_state
{
Texture = (Stencil);
AddressU = Clamp;
AddressV = Clamp;
MinFilter = Point;
MagFilter = Point;
MipFilter = None;
};

void CopyFunction(uniform bool readStencil,
in float2 in_TexCoord: TEXCOORD0,
out float4 out_Colour : COLOR0)
{
if (readStencil)
{
float4 sampledStencil = tex2D(stencilSample, in_TexCoord);
if (sampledStencil.a == 0)
clip(-1);
}

out_Colour = float4(tex2D(textureSampler, in_TexCoord).rgb, 1);
}

technique Technique1
technique Copy
{
pass Pass1
{
VertexShader = compile vs_3_0 FullScreenQuadVS();
PixelShader = compile ps_3_0 CopyFunction(false);
}
}

technique CopyWithStencil
{
pass Pass1
{
VertexShader = compile vs_3_0 FullScreenQuadVS();
PixelShader = compile ps_3_0 PixelShaderFunction();
PixelShader = compile ps_3_0 CopyFunction(true);
}
}
20 changes: 10 additions & 10 deletions Myre/Myre.Graphics.Content/Myre.Graphics.Content.contentproj
Expand Up @@ -53,16 +53,6 @@
<Importer>EffectImporter</Importer>
<Processor>EffectProcessor</Processor>
</Compile>
<Compile Include="ClearGBuffer.fx">
<Name>ClearGBuffer</Name>
<Importer>EffectImporter</Importer>
<Processor>EffectProcessor</Processor>
</Compile>
<Compile Include="CopyTexture.fx">
<Name>CopyTexture</Name>
<Importer>EffectImporter</Importer>
<Processor>EffectProcessor</Processor>
</Compile>
<Compile Include="DirectionalLight.fx">
<Name>DirectionalLight</Name>
<Importer>EffectImporter</Importer>
Expand All @@ -83,6 +73,16 @@
<Importer>EffectImporter</Importer>
<Processor>EffectProcessor</Processor>
</Compile>
<Compile Include="ClearGBuffer.fx">
<Name>ClearGBuffer</Name>
<Importer>EffectImporter</Importer>
<Processor>EffectProcessor</Processor>
</Compile>
<Compile Include="CopyTexture.fx">
<Name>CopyTexture</Name>
<Importer>EffectImporter</Importer>
<Processor>EffectProcessor</Processor>
</Compile>
<None Include="DepthHeader.fxh">
<Name>DepthHeader</Name>
</None>
Expand Down
4 changes: 2 additions & 2 deletions Myre/Myre.Graphics.Content/RestoreDepth.fx
Expand Up @@ -24,7 +24,7 @@ void PixelShaderFunction(in float2 in_TexCoord : TEXCOORD0,

float4 projectedPosition = mul(float4(viewPosition, 1), Projection);

out_Colour = float4(0, 0, 0, 1);
out_Colour = float4(0, 0, 0, 0);
out_Depth = projectedPosition.z / projectedPosition.w;
//projectedPosition /= projectedPosition.w;
//out_Colour = float4(projectedPosition.xy / 2 + 0.5, projectedPosition.z, 1); //float4(viewPosition.xy, -viewPosition.z / farClip, 1);
Expand All @@ -36,7 +36,7 @@ technique Technique1
{
// TODO: set renderstates here.

VertexShader = compile vs_2_0 FullScreenQuadFrustumCornerVS();//VertexShaderFunction();
VertexShader = compile vs_2_0 FullScreenQuadFrustumCornerVS();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
2 changes: 1 addition & 1 deletion Myre/Myre.Graphics/Deferred/LightingComponent.cs
Expand Up @@ -82,7 +82,7 @@ internal static void PerformLightingPass(Renderer renderer, bool ssao, Quad quad
var width = (int)resolution.X;
var height = (int)resolution.Y;

//Enable or disbale SSAO
//Enable or disable SSAO
renderer.Data.Set("ssao", ssao);

// prepare direct lights
Expand Down
11 changes: 8 additions & 3 deletions Myre/Myre.Graphics/Deferred/RestoreDepthPhase.cs
Expand Up @@ -19,7 +19,7 @@ public class RestoreDepthPhase
public RestoreDepthPhase(GraphicsDevice device)
{
_quad = new Quad(device);
_restoreDepth = new Material(Content.Load<Effect>("RestoreDepth").Clone());
_restoreDepth = new Material(Content.Load<Effect>("RestoreDepth"));
ClearDepth = true;
}

Expand All @@ -32,17 +32,22 @@ public override void Initialise(Renderer renderer, ResourceContext context)
}

public override void Draw(Renderer renderer)
{
RestoreDepth(renderer, _quad, _restoreDepth, ClearDepth);
}

public static void RestoreDepth(Renderer renderer, Quad quad, Material restoreDepth, bool clearDepth = true)
{
// work arround for a bug in xna 4.0
renderer.Device.SamplerStates[0] = SamplerState.LinearClamp;
renderer.Device.SamplerStates[0] = SamplerState.PointClamp;

if (ClearDepth)
if (clearDepth)
renderer.Device.Clear(ClearOptions.DepthBuffer, Color.Transparent, 1, 0);

renderer.Device.DepthStencilState = DepthStencilState.Default;
renderer.Device.BlendState = BlendState.Additive;
_quad.Draw(_restoreDepth, renderer.Data);
quad.Draw(restoreDepth, renderer.Data);
}
}
}
12 changes: 12 additions & 0 deletions Myre/Myre.Graphics/Geometry/DepthSort.cs
@@ -0,0 +1,12 @@

namespace Myre.Graphics.Geometry
{
public enum DepthSort
{
None,

BackToFront,

FrontToBack
}
}
44 changes: 30 additions & 14 deletions Myre/Myre.Graphics/Geometry/IGeometryProvider.cs
@@ -1,6 +1,8 @@
using Myre.Collections;
using System;
using Myre.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Myre.Graphics.Materials;

namespace Myre.Graphics.Geometry
{
Expand All @@ -22,32 +24,46 @@ public GeometryRenderer(ReadOnlyCollection<IGeometryProvider> geometryProviders)
_geometryProviders = geometryProviders;
}

public void Draw(string phase, Renderer renderer)
public List<IGeometry> Query(string phase, Renderer renderer)
{
//Find geometry to draw in this phase
_geometry.Clear();
foreach (var geometryProvider in _geometryProviders)
geometryProvider.Query(phase, renderer.Data, _geometry);

//Return the raw list. Risky, as it could be externally mutated, but we don't want to incur a copy
return _geometry;
}

public void Draw(string phase, Renderer renderer)
{
//Draw the geometry
Draw(_geometry, BackToFront, phase, renderer);
Draw(Query(phase, renderer), DepthSort.FrontToBack, phase, renderer);
}

public static void Draw(List<IGeometry> geometry, bool backToFront, string phase, Renderer renderer)
public static void Draw(List<IGeometry> geometry, DepthSort sort, string phase, Renderer renderer)
{
//Depth sort geometry
DepthSortGeometry(geometry);
//Depth sort geometry (always sort front-to-back, we'll render in reverse order for back-to-front)
if (sort != DepthSort.None)
DepthSortGeometry(geometry);

//Draw geometry
if (backToFront)
{
for (int i = geometry.Count - 1; i >= 0; i--)
geometry[i].Draw(phase, renderer);
}
else
switch (sort)
{
foreach (IGeometry g in geometry)
g.Draw(phase, renderer);
case DepthSort.BackToFront: {
for (int i = geometry.Count - 1; i >= 0; i--)
geometry[i].Draw(phase, renderer);
break;
}
case DepthSort.None:
case DepthSort.FrontToBack: {
foreach (IGeometry g in geometry)
g.Draw(phase, renderer);

break;
}
default:
throw new ArgumentOutOfRangeException("sort");
}
}

Expand Down
24 changes: 24 additions & 0 deletions Myre/Myre.Graphics/Geometry/ModelInstance.cs
Expand Up @@ -25,6 +25,8 @@ public class ModelInstance
public static readonly TypedName<bool> IsStaticName = new TypedName<bool>("is_static");
public static readonly TypedName<bool> IsInvisibleName = new TypedName<bool>("is_invisible");
public static readonly TypedName<float> OpacityName = new TypedName<float>("opacity");
public static readonly TypedName<float> AttenuationName = new TypedName<float>("transmittance");
public static readonly TypedName<float> SubSurfaceScatteringName = new TypedName<float>("subsurface_scattering");
public static readonly TypedName<Matrix4x4?> CustomViewMatrixName = new TypedName<Matrix4x4?>("custom_view_matrix");
public static readonly TypedName<Matrix4x4?> CustomProjectionMatrixName = new TypedName<Matrix4x4?>("custom_projection_matrix");

Expand All @@ -33,6 +35,8 @@ public class ModelInstance
private Property<bool> _isStatic;
private Property<bool> _isInvisible;
private Property<float> _opacity;
private Property<float> _attenuation;
private Property<float> _scattering;
private Property<Matrix4x4?> _customViewMatrix;
private Property<Matrix4x4?> _customProjectionMatrix;

Expand Down Expand Up @@ -68,6 +72,18 @@ public float Opacity
set { _opacity.Value = value; }
}

public float Attenuation
{
get { return _attenuation.Value; }
set { _attenuation.Value = value; }
}

public float SubSurfaceScattering
{
get { return _scattering.Value; }
set { _scattering.Value = value; }
}


internal event Action<ModelInstance> ModelDataChanged;
internal event Action<ModelInstance, Mesh> ModelMeshAdded;
Expand Down Expand Up @@ -106,6 +122,8 @@ public override void CreateProperties(Entity.ConstructionContext context)
_isStatic = context.CreateProperty(IsStaticName);
_isInvisible = context.CreateProperty(IsInvisibleName);
_opacity = context.CreateProperty(OpacityName, 1);
_attenuation = context.CreateProperty(AttenuationName, 1);
_scattering = context.CreateProperty(SubSurfaceScatteringName, 0);
_customViewMatrix = context.CreateProperty(CustomViewMatrixName);
_customProjectionMatrix = context.CreateProperty(CustomProjectionMatrixName);

Expand Down Expand Up @@ -175,6 +193,10 @@ public void Draw(string phase, Renderer renderer)
if (!Mesh.Materials.TryGetValue(phase, out material))
return;

var renderTransparent = renderer.Data.Get<bool>("render_translucent").Value;
if (!renderTransparent && Instance.Opacity < 1)
return;

Draw(material, renderer);
}

Expand Down Expand Up @@ -202,6 +224,8 @@ public void Draw(Material material, Renderer renderer)
//Allow the instance to apply any old data that it likes into the renderer
Instance.ApplyRendererData(renderer.Data);
renderer.Data.Set("opacity", Instance.Opacity);
renderer.Data.Set("attenuation", Instance.Attenuation);
renderer.Data.Set("scattering", Instance.SubSurfaceScattering);

//Calculate transform matrices
world.Value = Instance.Transform;
Expand Down
1 change: 1 addition & 0 deletions Myre/Myre.Graphics/Myre.Graphics.csproj
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Deferred\ToneMapComponent.cs" />
<Compile Include="Extensions\MatrixExtensions.cs" />
<Compile Include="Extensions\RenderTarget2DExtensions.cs" />
<Compile Include="Geometry\DepthSort.cs" />
<Compile Include="Geometry\BoundingVolume.cs" />
<Compile Include="Geometry\IGeometry.cs" />
<Compile Include="Geometry\IGeometryProvider.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Myre/Myre.Graphics/Properties/AssemblyInfo.cs
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("6.4.0")]
[assembly: AssemblyVersion("7.0.0")]
Binary file modified Myre/Myre.Graphics/Resources/ClearGBuffer.xnb
Binary file not shown.
Binary file modified Myre/Myre.Graphics/Resources/CopyTexture.xnb
Binary file not shown.
Binary file modified Myre/Myre.Graphics/Resources/RestoreDepth.xnb
Binary file not shown.

0 comments on commit 6346359

Please sign in to comment.