97 lines
3.8 KiB
C#

using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class BurRenderPassFeature : ScriptableRendererFeature
{
public float Value=1;
class BurRenderPass : ScriptableRenderPass
{
public float Value;
private readonly int BurTexture = Shader.PropertyToID("_BurTexture");
private readonly int BurTemp = Shader.PropertyToID("BurTemp");
private Material mat;
private Vector4 H = new Vector4(1, 0, 0, 0);
private Vector4 V = new Vector4(0, 1, 0, 0);
// This method is called before executing the render pass.
// It can be used to configure render targets and their clear state. Also to create temporary render target textures.
// When empty this render pass will render to the active camera render target.
// You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
// The render pipeline will ensure target setup and clearing happens in a performant manner.
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
if (mat == null) mat = CoreUtils.CreateEngineMaterial("Unlit URP Shader/Bur");
//if(bur==null)
//var dp = renderingData.cameraData.cameraTargetDescriptor;
//dp.msaaSamples = 1;
//cmd.GetTemporaryRT(BurTexture, dp);
//bur = RTHandles.Alloc(BurTexture);
}
// Here you can implement the rendering logic.
// Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers
// https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html
// You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get("Bur");
var dp = renderingData.cameraData.cameraTargetDescriptor;
dp.msaaSamples = 1;
cmd.GetTemporaryRT(BurTemp, dp);
cmd.GetTemporaryRT(BurTexture, dp);
//mat.SetFloat("_Value",Value);
cmd.SetGlobalVector("HorV",H);
cmd.Blit(renderingData.cameraData.renderer.cameraColorTarget,BurTexture,mat,0);
cmd.SetGlobalVector("HorV",V);
cmd.Blit(BurTexture,BurTemp,mat,0);
cmd.Blit(BurTemp,BurTexture,mat,1);
cmd.SetRenderTarget(renderingData.cameraData.renderer.cameraColorTarget);
cmd.ReleaseTemporaryRT(BurTemp);
cmd.ReleaseTemporaryRT(BurTexture);
context.ExecuteCommandBuffer(cmd);
cmd.Release();
}
// Cleanup any allocated resources that were created during the execution of this render pass.
public override void OnCameraCleanup(CommandBuffer cmd)
{
}
public void Disop()
{
if(mat!=null)
CoreUtils.Destroy(mat);
}
}
BurRenderPass m_ScriptablePass;
/// <inheritdoc/>
public override void Create()
{
m_ScriptablePass = new BurRenderPass();
// Configures where the render pass should be injected.
m_ScriptablePass.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
}
// Here you can inject one or multiple render passes in the renderer.
// This method is called when setting up the renderer once per-camera.
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
m_ScriptablePass.Value = Value;
renderer.EnqueuePass(m_ScriptablePass);
}
protected override void Dispose(bool disposing)
{
m_ScriptablePass.Disop();
}
}