Update custom pass to global custom pass

This commit is contained in:
Misaki
2024-10-23 20:15:07 +09:00
parent e441bb7911
commit d0554a73bb
64 changed files with 949 additions and 46128 deletions

View File

@@ -0,0 +1,106 @@
using Unity.Toonshader;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering.RendererUtils;
[HideInInspector]
public class UTSHairShadowPass : DrawRenderersCustomPass
{
private const string Hair_Shadow_Distance_Prop_Name = "_HairShadowDistance";
private const string Hair_Shadow_Distance_Scale_Prop_Name = "_HairShadowDistanceScaleFactor";
private const string Hair_Shadow_Depth_Bias_Prop_Name = "_HairShadowDepthBias";
private const string Hair_Shadow_FadeIn_Prop_Name = "_HairShadowFadeInDistance";
private const string Hair_Shadow_Fade_Out_Prop_Name = "_HairShadowFadeOutDistance";
private const string Output_RT_Name = "_HairShadowTex";
private RTHandle _outputRTHandle;
private bool _isEnable = false;
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
_outputRTHandle?.Release();
_outputRTHandle = RTHandles.Alloc(Screen.width, Screen.height, colorFormat: GraphicsFormat.D32_SFloat, filterMode: FilterMode.Bilinear, wrapMode: TextureWrapMode.Clamp, name: Output_RT_Name);
SetEnable(true);
}
protected override void Execute(CustomPassContext ctx)
{
var utsRenderer = ctx.hdCamera.volumeStack.GetComponent<UTSRenderer>();
if (utsRenderer == null || !utsRenderer.enableHairShadow.value || !utsRenderer.enable.value)
{
SetEnable(false);
return;
}
if (!_isEnable)
{
return;
}
SetShadowProperty(utsRenderer);
if (_outputRTHandle != null)
{
Shader.SetGlobalTexture(Output_RT_Name, _outputRTHandle);
CoreUtils.SetRenderTarget(ctx.cmd, _outputRTHandle, ClearFlag.DepthStencil);
}
var mask = RenderStateMask.Nothing;
var stateBlock = new RenderStateBlock(mask)
{
depthState = new DepthState(depthWrite, depthCompareFunction),
};
var result = new RendererListDesc(HDShaderPassNames.s_DepthForwardOnlyName, ctx.cullingResults, ctx.hdCamera.camera)
{
renderQueueRange = GetRenderQueueRange(renderQueueType),
sortingCriteria = sortingCriteria,
excludeObjectMotionVectors = false,
stateBlock = stateBlock,
layerMask = layerMask,
};
CoreUtils.DrawRendererList(ctx.renderContext, ctx.cmd, ctx.renderContext.CreateRendererList(result));
}
private static void SetShadowProperty(UTSRenderer utsRenderer)
{
Shader.SetGlobalFloat(Hair_Shadow_Distance_Prop_Name, utsRenderer.shadowDistance.value);
Shader.SetGlobalFloat(Hair_Shadow_Distance_Scale_Prop_Name, utsRenderer.shadowDistanceScale.value);
Shader.SetGlobalFloat(Hair_Shadow_Depth_Bias_Prop_Name, utsRenderer.shadowDepthBias.value);
Shader.SetGlobalFloat(Hair_Shadow_FadeIn_Prop_Name, utsRenderer.shadowFadeIn.value);
Shader.SetGlobalFloat(Hair_Shadow_Fade_Out_Prop_Name, utsRenderer.shadowFadeOut.value);
}
protected override void Cleanup()
{
Release();
}
public void Release()
{
SetEnable(false);
_outputRTHandle?.Release();
}
public void SetEnable(bool isEnable)
{
if (_isEnable != isEnable)
{
if (isEnable)
{
Shader.EnableKeyword("_HAIR_SHADOWS");
}
else
{
Shader.DisableKeyword("_HAIR_SHADOWS");
}
_isEnable = isEnable;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7157c6dc91cee0d4aae565d1c8d66807

View File

@@ -0,0 +1,45 @@
using Unity.Toonshader;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
[HideInInspector]
public class UTSOutlinePass : DrawRenderersCustomPass
{
protected override void Execute(CustomPassContext ctx)
{
var utsRenderer = ctx.hdCamera.volumeStack.GetComponent<UTSRenderer>();
if (utsRenderer == null)
return;
if (!utsRenderer.enableOutline.value || !utsRenderer.enable.value)
return;
Shader.SetGlobalFloat("_Outline_MaxWidth", utsRenderer.outlineMaxWidth.value * 0.01f);
var outlineTag = new ShaderTagId("Outline");
var mask = RenderStateMask.Nothing;
var stateBlock = new RenderStateBlock(mask)
{
depthState = new DepthState(depthWrite, depthCompareFunction),
// We disable the stencil when the depth is overwritten but we don't write to it, to prevent writing to the stencil.
stencilState = new StencilState(false),
};
var renderConfig = HDUtils.GetRendererConfiguration(false, false);
var result = new UnityEngine.Rendering.RendererUtils.RendererListDesc(outlineTag, ctx.cullingResults, ctx.hdCamera.camera)
{
rendererConfiguration = renderConfig,
renderQueueRange = GetRenderQueueRange(renderQueueType),
sortingCriteria = sortingCriteria,
excludeObjectMotionVectors = false,
overrideMaterial = overrideMaterial,
overrideMaterialPassIndex = (overrideMaterial != null) ? overrideMaterial.FindPass(overrideMaterialPassName) : 0,
stateBlock = stateBlock,
layerMask = layerMask,
};
CoreUtils.DrawRendererList(ctx.renderContext, ctx.cmd, ctx.renderContext.CreateRendererList(result));
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f4a6d55d5a6ea0f4b9da8e1ff2c98aa0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,70 @@
using System.IO;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
namespace Unity.Toonshader
{
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoad]
#endif
public static class RegisterUTSRenderPass
{
private static readonly UTSRenderPassSetting _renderSetting;
private static readonly UTSOutlinePass _outlinePass;
private static readonly UTSHairShadowPass _hairShadowPass;
static RegisterUTSRenderPass()
{
var assetPath = Path.GetFileNameWithoutExtension(UTSRenderPassSetting.RENDERER_SETTING_ASSET_PATH);
_renderSetting = Resources.Load<UTSRenderPassSetting>(assetPath);
if (_renderSetting == null)
{
return;
}
_outlinePass = new()
{
name = "UTS Outline",
layerMask = _renderSetting.outlineSetting.layerMask
};
_hairShadowPass = new()
{
name = "UTS Hair Shadow Map",
layerMask = _renderSetting.hairShadowSetting.casterLayerMask,
targetColorBuffer = CustomPass.TargetBuffer.None,
targetDepthBuffer = CustomPass.TargetBuffer.None,
};
RegisterCustomPasses();
}
[RuntimeInitializeOnLoadMethod]
public static void RegisterCustomPasses()
{
CustomPassVolume.RegisterGlobalCustomPass(CustomPassInjectionPoint.AfterOpaqueAndSky, _outlinePass);
CustomPassVolume.RegisterGlobalCustomPass(CustomPassInjectionPoint.AfterOpaqueDepthAndNormal, _hairShadowPass);
_outlinePass.enabled = _renderSetting.outlineSetting.enable;
_hairShadowPass.SetEnable(_renderSetting.hairShadowSetting.enable);
}
public static void UnregisterGlobalCustomPass()
{
CustomPassVolume.UnregisterGlobalCustomPass(_outlinePass);
CustomPassVolume.UnregisterGlobalCustomPass(_hairShadowPass);
_hairShadowPass.Release();
}
public static void NotifyRendererSettingChanged()
{
_outlinePass.enabled = _renderSetting.outlineSetting.enable;
_outlinePass.layerMask = _renderSetting.outlineSetting.layerMask;
_hairShadowPass.SetEnable(_renderSetting.hairShadowSetting.enable);
_hairShadowPass.layerMask = _renderSetting.hairShadowSetting.casterLayerMask;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 651a4bd94acf45c459468b2d4db62b29

View File

@@ -2,7 +2,6 @@ using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
namespace Unity.Toonshader
{
@@ -10,8 +9,7 @@ namespace Unity.Toonshader
public class UTSRenderer : VolumeComponent
{
// flags
bool m_initialized = false;
bool m_srpCallbackInitialized = false;
bool _initialized = false;
const int kAdjustmentCurvePrecision = 128;
@@ -24,12 +22,12 @@ namespace Unity.Toonshader
const string kIgnoreVolumeExposurePropName = "_ToonIgnoreExposureMultiplier";
[SerializeField]
internal float[] m_ExposureArray;
internal float[] _ExposureArray;
[SerializeField]
internal float m_Max, m_Min;
internal float _Max, _Min;
CustomPassVolume customPassVolume = new();
UTSOutlinePass outlinePass = new();
//CustomPassVolume customPassVolume = new();
//UTSOutlinePass outlinePass = new();
#if UNITY_EDITOR
#pragma warning restore CS0414
@@ -89,7 +87,7 @@ namespace Unity.Toonshader
Initialize();
if (!m_initialized)
if (!_initialized)
{
return;
}
@@ -100,20 +98,20 @@ namespace Unity.Toonshader
if (curve == null || curve.length == 0)
{
m_Min = 0f;
m_Max = 0f;
_Min = 0f;
_Max = 0f;
for (var i = 0; i < kAdjustmentCurvePrecision; i++)
m_ExposureArray[i] = 0.0f;
_ExposureArray[i] = 0.0f;
}
else
{
m_Min = curve[0].time;
m_Max = curve[curve.length - 1].time;
var step = (m_Max - m_Min) / (kAdjustmentCurvePrecision - 1f);
_Min = curve[0].time;
_Max = curve[curve.length - 1].time;
var step = (_Max - _Min) / (kAdjustmentCurvePrecision - 1f);
for (var i = 0; i < kAdjustmentCurvePrecision; i++)
m_ExposureArray[i] = curve.Evaluate(m_Min + step * i);
_ExposureArray[i] = curve.Evaluate(_Min + step * i);
}
@@ -132,9 +130,9 @@ namespace Unity.Toonshader
m_isCompiling = false;
}
#endif
Shader.SetGlobalFloatArray(kExposureArrayPropName, m_ExposureArray);
Shader.SetGlobalFloat(kExposureMinPropName, m_Min);
Shader.SetGlobalFloat(kExposureMaxPropName, m_Max);
Shader.SetGlobalFloatArray(kExposureArrayPropName, _ExposureArray);
Shader.SetGlobalFloat(kExposureMinPropName, _Min);
Shader.SetGlobalFloat(kExposureMaxPropName, _Max);
Shader.SetGlobalInt(kExposureAdjustmentPropName, toonEVAdjustment.value ? 1 : 0);
Shader.SetGlobalInt(kToonLightFilterPropName, lightIntensityLimiter.value ? 1 : 0);
Shader.SetGlobalInt(kIgnoreVolumeExposurePropName, ignoreVolumeExposure.value ? 1 : 0);
@@ -148,7 +146,7 @@ namespace Unity.Toonshader
void Initialize()
{
if (m_initialized)
if (_initialized)
{
return;
}
@@ -159,12 +157,12 @@ namespace Unity.Toonshader
return;
#endif
if (m_ExposureArray == null || m_ExposureArray.Length != kAdjustmentCurvePrecision)
if (_ExposureArray == null || _ExposureArray.Length != kAdjustmentCurvePrecision)
{
m_ExposureArray = new float[kAdjustmentCurvePrecision];
_ExposureArray = new float[kAdjustmentCurvePrecision];
}
m_initialized = true;
_initialized = true;
}
protected override void OnDestroy()
@@ -173,11 +171,11 @@ namespace Unity.Toonshader
Release();
}
void Release()
new void Release()
{
if (m_initialized)
if (_initialized)
{
m_ExposureArray = null;
_ExposureArray = null;
Shader.SetGlobalFloat(kExposureMinPropName, 0);
Shader.SetGlobalFloat(kExposureMaxPropName, 0);
Shader.SetGlobalInt(kExposureAdjustmentPropName, 0);
@@ -186,8 +184,8 @@ namespace Unity.Toonshader
Shader.SetGlobalFloat(kCompensationPropName, 0);
}
m_initialized = false;
_initialized = false;
base.Release();
}
private void Reset()