185 lines
6.9 KiB
C#
185 lines
6.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.RenderGraphModule;
|
|
using UnityEngine.Rendering.RenderGraphModule.Util;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
public class GlobalGlitchRendererFeature : ScriptableRendererFeature
|
|
{
|
|
[Tooltip("When the custom glitch pass should execute.")]
|
|
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
|
|
|
|
private Material material;
|
|
private GlobalGlitchPass pass;
|
|
|
|
public override void Create()
|
|
{
|
|
if (pass == null)
|
|
pass = new GlobalGlitchPass();
|
|
|
|
pass.renderPassEvent = renderPassEvent;
|
|
EnsureMaterial();
|
|
}
|
|
|
|
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
|
{
|
|
if (!EnsureMaterial())
|
|
return;
|
|
|
|
Camera camera = renderingData.cameraData.camera;
|
|
if (camera == null)
|
|
return;
|
|
|
|
if (renderingData.cameraData.cameraType == CameraType.Preview || renderingData.cameraData.cameraType == CameraType.Reflection)
|
|
return;
|
|
|
|
if (!renderingData.cameraData.postProcessEnabled)
|
|
return;
|
|
|
|
GlobalGlitchVolume settings = VolumeManager.instance.stack.GetComponent<GlobalGlitchVolume>();
|
|
bool hasVolume = settings != null && settings.active;
|
|
float runtimeIntensity = GlobalGlitchRuntimeController.RuntimeIntensity;
|
|
float baseIntensity = hasVolume ? settings.intensity.value : 0f;
|
|
float finalIntensity = Mathf.Clamp01(Mathf.Max(baseIntensity, runtimeIntensity));
|
|
if (finalIntensity <= 0.0001f)
|
|
return;
|
|
|
|
if (camera.cameraType == CameraType.SceneView && hasVolume && !settings.affectSceneView.value)
|
|
return;
|
|
|
|
pass.Setup(
|
|
material,
|
|
finalIntensity,
|
|
hasVolume ? settings.blockStrength.value : 0.45f,
|
|
hasVolume ? settings.colorSplit.value : 0.01f,
|
|
hasVolume ? settings.jitterAmount.value : 0.4f,
|
|
hasVolume ? settings.stripFrequency.value : 48f,
|
|
hasVolume ? settings.scanlineStrength.value : 0.18f
|
|
);
|
|
renderer.EnqueuePass(pass);
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (pass != null)
|
|
{
|
|
pass.Dispose();
|
|
pass = null;
|
|
}
|
|
|
|
if (material != null)
|
|
{
|
|
CoreUtils.Destroy(material);
|
|
material = null;
|
|
}
|
|
}
|
|
|
|
private bool EnsureMaterial()
|
|
{
|
|
if (material != null)
|
|
return true;
|
|
|
|
Shader shader = Shader.Find("Hidden/Bansonic/GlobalGlitch");
|
|
if (shader == null)
|
|
return false;
|
|
|
|
material = CoreUtils.CreateEngineMaterial(shader);
|
|
return material != null;
|
|
}
|
|
|
|
private sealed class GlobalGlitchPass : ScriptableRenderPass
|
|
{
|
|
private static readonly int IntensityId = Shader.PropertyToID("_GlitchIntensity");
|
|
private static readonly int BlockStrengthId = Shader.PropertyToID("_BlockStrength");
|
|
private static readonly int ColorSplitId = Shader.PropertyToID("_ColorSplit");
|
|
private static readonly int JitterAmountId = Shader.PropertyToID("_JitterAmount");
|
|
private static readonly int StripFrequencyId = Shader.PropertyToID("_StripFrequency");
|
|
private static readonly int ScanlineStrengthId = Shader.PropertyToID("_ScanlineStrength");
|
|
|
|
private Material passMaterial;
|
|
private float intensity;
|
|
private float blockStrength;
|
|
private float colorSplit;
|
|
private float jitterAmount;
|
|
private float stripFrequency;
|
|
private float scanlineStrength;
|
|
private RTHandle compatibilityCopy;
|
|
|
|
public void Setup(Material material, float passIntensity, float passBlockStrength, float passColorSplit, float passJitterAmount, float passStripFrequency, float passScanlineStrength)
|
|
{
|
|
passMaterial = material;
|
|
intensity = passIntensity;
|
|
blockStrength = passBlockStrength;
|
|
colorSplit = passColorSplit;
|
|
jitterAmount = passJitterAmount;
|
|
stripFrequency = passStripFrequency;
|
|
scanlineStrength = passScanlineStrength;
|
|
requiresIntermediateTexture = true;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
compatibilityCopy?.Release();
|
|
compatibilityCopy = null;
|
|
}
|
|
|
|
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
|
|
{
|
|
if (passMaterial == null)
|
|
return;
|
|
|
|
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
|
|
if (resourceData.isActiveTargetBackBuffer)
|
|
return;
|
|
|
|
ApplyMaterialProperties();
|
|
|
|
TextureHandle source = resourceData.activeColorTexture;
|
|
TextureDesc tempDesc = renderGraph.GetTextureDesc(source);
|
|
tempDesc.name = "CameraColor-GlobalGlitchTemp";
|
|
tempDesc.clearBuffer = false;
|
|
|
|
TextureHandle tempTexture = renderGraph.CreateTexture(tempDesc);
|
|
RenderGraphUtils.BlitMaterialParameters effectParameters = new(source, tempTexture, passMaterial, 0);
|
|
renderGraph.AddBlitPass(effectParameters, passName: "Global Glitch Effect");
|
|
renderGraph.AddBlitPass(tempTexture, resourceData.activeColorTexture, Vector2.one, Vector2.zero, passName: "Global Glitch Restore");
|
|
}
|
|
|
|
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
|
{
|
|
if (passMaterial == null)
|
|
return;
|
|
|
|
ApplyMaterialProperties();
|
|
|
|
RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
|
|
descriptor.msaaSamples = 1;
|
|
descriptor.depthBufferBits = 0;
|
|
RenderingUtils.ReAllocateHandleIfNeeded(ref compatibilityCopy, descriptor, FilterMode.Bilinear, TextureWrapMode.Clamp, name: "_GlobalGlitchCompatibilityCopy");
|
|
|
|
CommandBuffer cmd = CommandBufferPool.Get("Global Glitch Pass");
|
|
try
|
|
{
|
|
Blitter.BlitCameraTexture(cmd, renderingData.cameraData.renderer.cameraColorTargetHandle, compatibilityCopy);
|
|
Blitter.BlitCameraTexture(cmd, compatibilityCopy, renderingData.cameraData.renderer.cameraColorTargetHandle, passMaterial, 0);
|
|
context.ExecuteCommandBuffer(cmd);
|
|
}
|
|
finally
|
|
{
|
|
cmd.Clear();
|
|
CommandBufferPool.Release(cmd);
|
|
}
|
|
}
|
|
|
|
private void ApplyMaterialProperties()
|
|
{
|
|
passMaterial.SetFloat(IntensityId, intensity);
|
|
passMaterial.SetFloat(BlockStrengthId, blockStrength);
|
|
passMaterial.SetFloat(ColorSplitId, colorSplit);
|
|
passMaterial.SetFloat(JitterAmountId, jitterAmount);
|
|
passMaterial.SetFloat(StripFrequencyId, stripFrequency);
|
|
passMaterial.SetFloat(ScanlineStrengthId, scanlineStrength);
|
|
}
|
|
}
|
|
}
|