当前位置: 首页 > news >正文

【URP】Unity 插入自定义RenderPass

【从UnityURP开始探索游戏渲染】专栏-直达

自定义渲染通道是一种改变通用渲染管道(URP)如何渲染场景或场景中的对象的方法。自定义呈现通道(RenderPass)包含自己的Render代码,可以在注入点将其添加到RenderPass中。

添加自定义呈现通道(RenderPass):

  • 使用Scriptable render pass API创建自定义render pass的代码。
  • 将自定的render pass注入到URP管线中的指定注入点中,有两种方式:
    • RenderPipelineManager API注入自定义渲染通道
    • 或者通过创建一个可脚本化的RendererFeature添加到URP渲染器中。

使用Scriptable render pass API创建自定义render pass

  • Example custom render pass

    using UnityEngine;
    using UnityEngine.Rendering;
    using UnityEngine.Rendering.Universal;internal class ColorBlitPass : ScriptableRenderPass
    {ProfilingSampler m_ProfilingSampler = new ProfilingSampler("ColorBlit");Material m_Material;RTHandle m_CameraColorTarget;float m_Intensity;public ColorBlitPass(Material material){m_Material = material;renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;}public void SetTarget(RTHandle colorHandle, float intensity){m_CameraColorTarget = colorHandle;m_Intensity = intensity;}public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData){ConfigureTarget(m_CameraColorTarget);}public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData){var cameraData = renderingData.cameraData;if (cameraData.camera.cameraType != CameraType.Game)return;if (m_Material == null)return;CommandBuffer cmd = CommandBufferPool.Get();using (new ProfilingScope(cmd, m_ProfilingSampler)){m_Material.SetFloat("_Intensity", m_Intensity);Blitter.BlitCameraTexture(cmd, m_CameraColorTarget, m_CameraColorTarget, m_Material, 0);}context.ExecuteCommandBuffer(cmd);cmd.Clear();CommandBufferPool.Release(cmd);}
    }

将自定的render pass注入到URP管线中的指定注入点中

RenderPipelineManager API注入自定义渲染通道

  • 通过RenderPipelineManager的注入点委托提供执行时机,加上Camera的EnqueuePass方法注入自定义RenderPass。

    public class EnqueuePass : MonoBehaviour
    {[SerializeField] private BlurSettings settings;    private BlurRenderPass blurRenderPass;private void OnEnable(){...blurRenderPass = new BlurRenderPass(settings);// Subscribe the OnBeginCamera method to the beginCameraRendering event.RenderPipelineManager.beginCameraRendering += OnBeginCamera;}private void OnDisable(){RenderPipelineManager.beginCameraRendering -= OnBeginCamera;blurRenderPass.Dispose();...}private void OnBeginCamera(ScriptableRenderContext context, Camera cam){...// Use the EnqueuePass method to inject a custom render passcam.GetUniversalAdditionalCameraData().scriptableRenderer.EnqueuePass(blurRenderPass);}
    }

创建一个可脚本化的RendererFeature

此示例执行将屏幕染成绿色的全屏blit。

  • 要创建自定义渲染通道,创建一个名为ColorBlitPass.cs的新c#脚本,然后从示例自定义渲染通道部分粘贴代码。

    • 注意:这个例子使用了Blitter API。不要使用CommandBuffer。URP中的Blit API。更多信息请参考Blit。
    • 使用上面定义好的定制Render Pass
  • 要创建Scriptable RendererFeature,将自定义渲染通道添加到渲染循环中,请创建一个名为ColorBlitRendererFeature.cs的新c#脚本,然后将示例Scriptable RendererFeature部分中的代码粘贴进来。

    • Example Scriptable Renderer Feature Scriptable Renderer Feature 添加 render pass 到渲染循环.

      using UnityEngine;
      using UnityEngine.Rendering;
      using UnityEngine.Rendering.Universal;internal class ColorBlitRendererFeature : ScriptableRendererFeature
      {public Shader m_Shader;public float m_Intensity;Material m_Material;ColorBlitPass m_RenderPass = null;public override void AddRenderPasses(ScriptableRenderer renderer,ref RenderingData renderingData){if (renderingData.cameraData.cameraType == CameraType.Game)renderer.EnqueuePass(m_RenderPass);}public override void SetupRenderPasses(ScriptableRenderer renderer,in RenderingData renderingData){if (renderingData.cameraData.cameraType == CameraType.Game){// Calling ConfigureInput with the ScriptableRenderPassInput.Color argument// ensures that the opaque texture is available to the Render Pass.m_RenderPass.ConfigureInput(ScriptableRenderPassInput.Color);m_RenderPass.SetTarget(renderer.cameraColorTargetHandle, m_Intensity);}}public override void Create(){m_Material = CoreUtils.CreateEngineMaterial(m_Shader);m_RenderPass = new ColorBlitPass(m_Material);}protected override void Dispose(bool disposing){CoreUtils.Destroy(m_Material);}
      }
  • 要创建将像素染成绿色的着色器代码,请创建一个着色器文件,然后从示例着色器部分粘贴代码。

  • Example shader

    • 着色器执行渲染的GPU端。它从相机中采样颜色纹理,然后输出绿色值设置为所选强度的颜色。
      注意:与Blitter API一起使用的着色器必须是手工编码的着色器。图形着色器与Blitter API不兼容。

      Shader "ColorBlit"
      {SubShader{Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline"}LOD 100ZWrite Off Cull OffPass{Name "ColorBlitPass"HLSLPROGRAM#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"// The Blit.hlsl file provides the vertex shader (Vert),// the input structure (Attributes) and the output structure (Varyings)#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"#pragma vertex Vert#pragma fragment frag// Set the color texture from the camera as the input textureTEXTURE2D_X(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);// Set up an intensity parameterfloat _Intensity;half4 frag (Varyings input) : SV_Target{UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);// Sample the color from the input texturefloat4 color = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, input.texcoord);// Output the color from the texture, with the green value set to the chosen intensityreturn color * float4(0, _Intensity, 0, 1);}ENDHLSL}}
      }
      
  • 将ColorBlitRendererFeature添加到当前URP Renderer资源中。有关更多信息,请参阅向URP渲染器添加渲染器功能。

  • 要更改亮度,请调整Color Blit Renderer Feature组件中的Intensity属性。

注意:如果项目使用XR,为了使示例可视化,在项目中安装MockHMD XR插件包,然后将渲染模式属性设置为单通道实例化。


https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@14.0/manual/renderer-features/custom-rendering-pass-workflow-in-urp.html


【从UnityURP开始探索游戏渲染】专栏-直达
(欢迎点赞留言探讨,更多人加入进来能更加完善这个探索的过程,🙏)

http://www.dtcms.com/a/350253.html

相关文章:

  • 【学习记录】CSS: clamp、@scope
  • C++ extern 关键字面试深度解析
  • 大模型的思考方式
  • 引脚电平异常?以下或许是原因
  • Java 高可用实现方式
  • 基于MATLAB长时间序列遥感数据处理及在全球变化、物候提取、植被变绿与固碳分析等领域中的实践技术应用
  • 面试常考算法题汇总
  • Java设计模式-观察者模式
  • MATLAB函数文件编写规范
  • imx6ull-驱动开发篇41——Linux RTC 驱动实验
  • 详解flink SQL基础(四)
  • 使用Docker+WordPress部署个人博客
  • 无人机和无人系统的计算机视觉-人工智能无人机
  • k8s的etcd备份脚本
  • 4G模块 EC200通过MQTT协议连接到阿里云
  • Java-面试八股文-Java高级篇
  • Springboot 集成 TraceID
  • 在react里使用路由,手动跳转
  • C++ 内存安全与智能指针深度解析
  • 【flutter对屏幕底部有手势区域(如:一条横杠)导致出现重叠遮挡】
  • YOLOv7:重新定义实时目标检测的技术突破
  • 浅聊RLVR
  • 绿色循环经济下的旧物回收App:重构闲置资源的价值链条
  • 设计仿真 | 从物理扫描到虚拟检具:Simufact Welding革新汽车零部件检测
  • 汽车零部件工厂ESOP系统工业一体机如何选型
  • 基于51单片机红外避障车辆高速汽车测速仪表设计
  • AEB 强制来临,东软睿驰Next-Cube-Lite有望成为汽车安全普惠“破局器”
  • kubeadm join 命令无法加入node节点,ip_forward 内核参数没有被正确设置
  • IIS 安装了.netcore运行时 还是报错 HTTP 错误 500.19
  • k8s笔记03-常用操作命令