-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
… to prevent artifacts with transparent geometry [closes #76]
- Loading branch information
There are no files selected for viewing
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.
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
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,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.