【Unity URP后处理/Post Process】尝试RTHandler直接生成相机的Temporary RenderTexture(填坑中)

急急急,吉吉国王。先把一部分源码贴上去。其余的之后再说
文档: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);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值