Unity URP RenderFeature 自定义后效

关于自定义Pass 我们可以在Create-Rendering-renderPiplineAsset创建我们的管理文件,RenderFeature中添加内置feature pass管理启用我们自定义pass关键字
Unity URP RenderFeature 自定义后效_第1张图片
或者我们完全自定义我们的feature Pass

using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class ArtsEffectFeature : ScriptableRendererFeature
{
	//属性暴露在RenderFeature界面
    public static bool GlobalActive = false;
    [Serializable]
    public class Setting
    {
        public Material material;
        public Texture texture;
        public float _RotateSpeed;
      ...
        [Range(0, 10f)] public float _CenterPower;
    }
    public bool enable = false;
    public Setting setting = new Setting();
    private ArtsEffectPass pass;

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        if(!enable || !GlobalActive) return;
        //这一效果是特写相机使用,屏蔽BaseCamera 防止LUT等被修改
        if (renderingData.cameraData.renderType != CameraRenderType.Base) return;
        //传递colorTexture到Pass
        pass.SetUp(setting, renderer.cameraColorTarget);
        renderer.EnqueuePass(pass);
    }

    public override void Create()
    {
        pass = new ArtsEffectPass();
    }
}

public class ArtsEffectPass : ScriptableRenderPass
{
    private ArtsEffectFeature.Setting setting;
    private RenderTargetIdentifier colorRt;

    public ArtsEffectPass()
    {
    	//在半透明队列之后
        renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
    }

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        if (renderingData.cameraData.renderType != CameraRenderType.Base) return;
        var material = setting.material;
        if (material == null || material.shader == null || !material.shader.isSupported)  return;
        //命名并创建CommandBuffer
        CommandBuffer cmd = CommandBufferPool.Get("ArtsEffect");
        cmd.Clear();
        //Texture属性设置
        var descriptor = renderingData.cameraData.cameraTargetDescriptor;
        descriptor.msaaSamples = 1;
        descriptor.depthBufferBits = 0;
        //图片名称
        var texId = Shader.PropertyToID("ArtsEffectTex");
        //创建图片
        cmd.GetTemporaryRT(texId, descriptor);
        material.SetTexture("_BaseMap", setting.texture);
        material.SetFloat("_Brightness", setting._Brightness);
        ...
        material.SetFloat("_CenterPower", setting._CenterPower);

		//ColorTexture 根据后效shader绘制到TempTexture上
        cmd.Blit(colorRt, texId, material);
        //拷贝回colorTexture
        cmd.Blit(texId, colorRt);
        cmd.ReleaseTemporaryRT(texId);
        //执行
        context.ExecuteCommandBuffer(cmd);
        CommandBufferPool.Release(cmd);
    }

    internal void SetUp(FinishArtsEffectFeature.Setting setting, RenderTargetIdentifier colorRt)
    {
        this.setting = setting;
        this.colorRt = colorRt;
    }
}
//shader中我们采样_MainTex

你可能感兴趣的:(Unity)