急急急,吉吉国王。先把一部分源码贴上去。其余的之后再说
文档:https://docs.unity3d.com/Packages/com.unity.render-pipelines.core@12.1/api/UnityEngine.Rendering.RTHandle.html
下面的例子是使用RTHandler直接生成:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class VolLightPass : ScriptableRenderPass, IDisposable
{
// get RT
public RenderTargetIdentifier _cameraColorTargetID;
//private RenderTargetHandle _tempTargetHandle;
private Material mat;
private RTHandle _tempTarget;
// Configures where the render pass should be injected.
public VolLightPass()
{
this.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
this.mat = new Material(Shader.Find("Hidden/testPP"));
this._tempTarget = RTHandles.Alloc(
Mathf.Max(1, (int)(Screen.width*0.5)),
Mathf.Max(1, (int)(Screen.height*0.5)),
depthBufferBits: DepthBits.None
);
//RenderTargetIdentifier id = _tempTarget.nameID;
}
public void Dispose()
{
_tempTarget.Release();
}
// 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)
{
// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget.
//This method should be called inside Configure.
ConfigureTarget(this._tempTarget);
}
// 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();
if(mat==null) {
Debug.Log("No PP Mat");
return;
}
Debug.Log(_tempTarget.referenceSize.x);
// test the pp
//cmd.Blit(_cameraColorTarget,_tempTarget, mat);
//cmd.Blit(_tempTarget, _cameraColorTarget);
Blit(cmd, _cameraColorTargetID,_tempTarget.nameID, mat);
Blit(cmd,_tempTarget.nameID, _cameraColorTargetID);
//cmd.ReleaseTemporaryRT(Shader.PropertyToID(_tempTarget.name));
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
// Cleanup any allocated resources that were created during the execution of this render pass.
public override void OnCameraCleanup(CommandBuffer cmd)
{
base.OnCameraCleanup(cmd);
cmd.ReleaseTemporaryRT(Shader.PropertyToID(_tempTarget.name));
}
}
或者使用RenderTargetHandler结构体
如果使用这种方式,需要多走几步路子。
cmd.GetTemporaryRT(temp.id,cameraTextureDescriptor);