加入很多新内容,这波一块交了
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.RenderGraphModule;
|
||||
using UnityEngine.Rendering.RenderGraphModule.Util;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
public class GlobalMonochromeRendererFeature : ScriptableRendererFeature
|
||||
{
|
||||
[Tooltip("When the custom monochrome pass should execute.")]
|
||||
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
|
||||
|
||||
private Material material;
|
||||
private GlobalMonochromePass pass;
|
||||
|
||||
public override void Create()
|
||||
{
|
||||
if (pass == null)
|
||||
pass = new GlobalMonochromePass();
|
||||
|
||||
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;
|
||||
|
||||
GlobalMonochromeVolume settings = VolumeManager.instance.stack.GetComponent<GlobalMonochromeVolume>();
|
||||
bool hasVolume = settings != null && settings.active;
|
||||
float runtimeIntensity = GlobalMonochromeRuntimeController.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);
|
||||
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/GlobalMonochrome");
|
||||
if (shader == null)
|
||||
return false;
|
||||
|
||||
material = CoreUtils.CreateEngineMaterial(shader);
|
||||
return material != null;
|
||||
}
|
||||
|
||||
private sealed class GlobalMonochromePass : ScriptableRenderPass
|
||||
{
|
||||
private static readonly int IntensityId = Shader.PropertyToID("_MonochromeIntensity");
|
||||
|
||||
private Material passMaterial;
|
||||
private float intensity;
|
||||
private RTHandle compatibilityCopy;
|
||||
|
||||
public void Setup(Material material, float passIntensity)
|
||||
{
|
||||
passMaterial = material;
|
||||
intensity = passIntensity;
|
||||
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;
|
||||
|
||||
passMaterial.SetFloat(IntensityId, intensity);
|
||||
|
||||
TextureHandle source = resourceData.activeColorTexture;
|
||||
TextureDesc tempDesc = renderGraph.GetTextureDesc(source);
|
||||
tempDesc.name = "CameraColor-GlobalMonochromeTemp";
|
||||
tempDesc.clearBuffer = false;
|
||||
|
||||
TextureHandle tempTexture = renderGraph.CreateTexture(tempDesc);
|
||||
RenderGraphUtils.BlitMaterialParameters effectParameters = new(source, tempTexture, passMaterial, 0);
|
||||
renderGraph.AddBlitPass(effectParameters, passName: "Global Monochrome Effect");
|
||||
renderGraph.AddBlitPass(tempTexture, resourceData.activeColorTexture, Vector2.one, Vector2.zero, passName: "Global Monochrome Restore");
|
||||
}
|
||||
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
if (passMaterial == null)
|
||||
return;
|
||||
|
||||
passMaterial.SetFloat(IntensityId, intensity);
|
||||
|
||||
RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor;
|
||||
descriptor.msaaSamples = 1;
|
||||
descriptor.depthBufferBits = 0;
|
||||
RenderingUtils.ReAllocateHandleIfNeeded(ref compatibilityCopy, descriptor, FilterMode.Bilinear, TextureWrapMode.Clamp, name: "_GlobalMonochromeCompatibilityCopy");
|
||||
|
||||
CommandBuffer cmd = CommandBufferPool.Get("Global Monochrome Pass");
|
||||
Blitter.BlitCameraTexture(cmd, renderingData.cameraData.renderer.cameraColorTargetHandle, compatibilityCopy);
|
||||
Blitter.BlitCameraTexture(cmd, compatibilityCopy, renderingData.cameraData.renderer.cameraColorTargetHandle, passMaterial, 0);
|
||||
context.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user