Skip to content

Commit

Permalink
Implement alternative (After Opaque) version of Screen-Space Outlines…
Browse files Browse the repository at this point in the history
… to prevent artifacts with transparent geometry [closes #76]
  • Loading branch information
Delt06 committed May 21, 2023
1 parent 19175b2 commit 76b3eca
Show file tree
Hide file tree
Showing 29 changed files with 451 additions and 106 deletions.
29 changes: 29 additions & 0 deletions Assets/Samples/Pipeline/Screen-Space Outline (Opaque).asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: bd0e7677b9b54d16b59337851cd19737, type: 3}
m_Name: Screen-Space Outline (Opaque)
m_EditorClassIdentifier:
_forceIncludedShaders:
- {fileID: 4800000, guid: e9b4a97aaeee49ce9093d98b22f74188, type: 3}
Settings:
Color: {r: 0, g: 0, b: 0, a: 0.83137256}
DepthFilter:
Enabled: 1
Threshold: 0.29
Smoothness: 0.15
NormalsFilter:
Enabled: 1
Threshold: 4.86
Smoothness: 2
UseFog: 1
MaxDistance: 9
DistanceFade: 0.1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Samples/Pipeline/Toon Render Pipeline Asset.asset
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ MonoBehaviour:
Extensions:
- {fileID: 11400000, guid: 91c04dff8aa908e4e951dac087bdf2ad, type: 2}
- {fileID: 11400000, guid: 1646ef03c10aee74d9d024dabb976ff3, type: 2}
- {fileID: 11400000, guid: 30041836c268b614db8f6cfc9f36024b, type: 2}
PostProcessing:
Enabled: 1
Passes:
- {fileID: 11400000, guid: a2b9b9eff21d681439a54dd24d18aa73, type: 2}
- {fileID: 11400000, guid: 444443e987af20941be3a8f1b36ba921, type: 2}
- {fileID: 11400000, guid: b9995397bc4c75a4c8099a1bc0829d3a, type: 2}
72 changes: 72 additions & 0 deletions Packages/com.deltation.toon-rp/Runtime/CustomBlitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using UnityEngine;
using UnityEngine.Rendering;

namespace DELTation.ToonRP
{
public static class CustomBlitter
{
private static Mesh _triangleMesh;

public static void Blit(CommandBuffer cmd, Material material, int shaderPass = 0)
{
EnsureInitialized();
cmd.DrawMesh(_triangleMesh, Matrix4x4.identity, material, 0, shaderPass);
}

private static void EnsureInitialized()
{
if (_triangleMesh != null)
{
return;
}

{
float nearClipZ = -1;
if (SystemInfo.usesReversedZBuffer)
{
nearClipZ = 1;
}

if (_triangleMesh == null)
{
_triangleMesh = new Mesh
{
vertices = GetFullScreenTriangleVertexPosition(nearClipZ),
uv = GetFullScreenTriangleTexCoord(),
triangles = new[] { 0, 1, 2 },
};
}

static Vector3[] GetFullScreenTriangleVertexPosition(float z /*= UNITY_NEAR_CLIP_VALUE*/)
{
var r = new Vector3[3];
for (int i = 0; i < 3; i++)
{
var uv = new Vector2((i << 1) & 2, i & 2);
r[i] = new Vector3(uv.x * 2.0f - 1.0f, uv.y * 2.0f - 1.0f, z);
}

return r;
}

static Vector2[] GetFullScreenTriangleTexCoord()
{
var r = new Vector2[3];
for (int i = 0; i < 3; i++)
{
if (SystemInfo.graphicsUVStartsAtTop)
{
r[i] = new Vector2((i << 1) & 2, 1.0f - (i & 2));
}
else
{
r[i] = new Vector2((i << 1) & 2, i & 2);
}
}

return r;
}
}
}
}
}
3 changes: 3 additions & 0 deletions Packages/com.deltation.toon-rp/Runtime/CustomBlitter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Packages/com.deltation.toon-rp/Runtime/DepthPrePassMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DELTation.ToonRP
{
public enum DepthPrePassMode
{
Off,
Depth,
DepthNormals,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DELTation.ToonRP
{
public static class DepthPrePassModeUtils
{
public static DepthPrePassMode CombineDepthPrePassModes(DepthPrePassMode mode1, DepthPrePassMode mode2) =>
mode1 > mode2 ? mode1 : mode2;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using DELTation.ToonRP.PostProcessing.BuiltIn;
using UnityEngine.Rendering;

namespace DELTation.ToonRP.Extensions.BuiltIn
{
public class ToonScreenSpaceOutlineAfterOpaque : ToonRenderingExtensionBase
{
private readonly ToonScreenSpaceOutlineImpl _impl = new();
private ScriptableRenderContext _context;
private int _rtHeight;
private int _rtWidth;
private ToonScreenSpaceOutlineSettings _settings;

public override void Setup(in ToonRenderingExtensionContext context,
IToonRenderingExtensionSettingsStorage settingsStorage)
{
base.Setup(context, settingsStorage);
_context = context.ScriptableRenderContext;
_settings = ToonScreenSpaceOutlineAfterOpaqueSettings.ConvertToCommonSettings(
settingsStorage.GetSettings<ToonScreenSpaceOutlineAfterOpaqueSettings>(this)
);
_rtWidth = context.RtWidth;
_rtHeight = context.RtHeight;
}

public override void Render()
{
CommandBuffer cmd = CommandBufferPool.Get();

using (new ProfilingScope(cmd, NamedProfilingSampler.Get(ToonRpPassId.ScreenSpaceOutlinesAfterOpaque)))
{
_impl.EnableAlphaBlending(true);
_impl.SetRtSize(_rtWidth, _rtHeight);
_impl.RenderViaCustomBlit(cmd, _settings);
}

_context.ExecuteCommandBuffer(cmd);
cmd.Clear();
CommandBufferPool.Release(cmd);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using DELTation.ToonRP.PostProcessing.BuiltIn;
using UnityEngine;
using static DELTation.ToonRP.Extensions.BuiltIn.ToonScreenSpaceOutlineAfterOpaqueSettings;

namespace DELTation.ToonRP.Extensions.BuiltIn
{
[CreateAssetMenu(menuName = Path + "Screen-Space Outline (After Opaque)")]
public class
ToonScreenSpaceOutlineAfterOpaqueAsset : ToonRenderingExtensionAsset<ToonScreenSpaceOutlineAfterOpaqueSettings>
{
public override ToonRenderingEvent Event => ToonRenderingEvent.AfterOpaque;

private void Reset()
{
Settings = new ToonScreenSpaceOutlineAfterOpaqueSettings
{
Color = Color.black,
DepthFilter = new ToonScreenSpaceOutlineSettings.OutlineFilter
{
Enabled = true,
Threshold = 1.0f,
Smoothness = 0.5f,
},
NormalsFilter = new ToonScreenSpaceOutlineSettings.OutlineFilter
{
Enabled = true,
Smoothness = 5.0f,
Threshold = 0.5f,
},
UseFog = true,
MaxDistance = 100.0f,
DistanceFade = 0.1f,
};
}

public override IToonRenderingExtension CreateExtension() => new ToonScreenSpaceOutlineAfterOpaque();

protected override string[] ForceIncludedShaderNames() => new[]
{
ToonScreenSpaceOutlineImpl.ShaderName,
};

public override DepthPrePassMode RequiredDepthPrePassMode() =>
ToonScreenSpaceOutlineAsset.RequiredDepthPrePassMode(ConvertToCommonSettings(Settings));
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using DELTation.ToonRP.PostProcessing.BuiltIn;
using UnityEngine;

namespace DELTation.ToonRP.Extensions.BuiltIn
{
[Serializable]
public struct ToonScreenSpaceOutlineAfterOpaqueSettings
{
[ColorUsage(true, true)]
public Color Color;

public ToonScreenSpaceOutlineSettings.OutlineFilter DepthFilter;
public ToonScreenSpaceOutlineSettings.OutlineFilter NormalsFilter;

public bool UseFog;
[Min(0f)]
public float MaxDistance;
[Range(0.001f, 1f)]
public float DistanceFade;

public static ToonScreenSpaceOutlineSettings ConvertToCommonSettings(
in ToonScreenSpaceOutlineAfterOpaqueSettings settings) =>
new()
{
Color = settings.Color,
DepthFilter = settings.DepthFilter,
NormalsFilter = settings.NormalsFilter,
UseFog = settings.UseFog,
DistanceFade = settings.DistanceFade,
MaxDistance = settings.MaxDistance,
};
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ private void Reset()

public override IToonRenderingExtension CreateExtension() => new ToonSsao();

public override ToonCameraRendererSettings.DepthPrePassMode RequiredDepthPrePassMode() =>
ToonCameraRendererSettings.DepthPrePassMode.DepthNormals;
public override DepthPrePassMode RequiredDepthPrePassMode() =>
DepthPrePassMode.DepthNormals;

protected override string[] ForceIncludedShaderNames() => new[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ protected virtual void OnValidate()

public virtual bool RequiresStencil() => false;

public virtual ToonCameraRendererSettings.DepthPrePassMode RequiredDepthPrePassMode() =>
ToonCameraRendererSettings.DepthPrePassMode.Off;
public virtual DepthPrePassMode RequiredDepthPrePassMode() =>
DepthPrePassMode.Off;

public abstract IToonRenderingExtension CreateExtension();

Expand Down
Loading

0 comments on commit 76b3eca

Please sign in to comment.