Unity Render Feature示例
Posted 五百零一
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity Render Feature示例相关的知识,希望对你有一定的参考价值。
参考教程:Scriptable Render Feature
Unity 版本:2019.4
搭建URP场景
Render Feature作用是支持SRP扩展自定义渲染pass,因此需要在SRP下使用,这里选择URP,在package manager中找到Universal RP并install。
创建一个前向渲染管线资产,会生成一个UniversalRenderPipelineAsset和UniversalRenderPipelineAsset_Renderer文件。
在Edit-Project Settings-Graphics中将渲染管线设置为刚设置好的URP Asset
场景就自由发挥搭建吧
Render Feature脚本
MyBlitFeature.cs
using UnityEngine;
using UnityEngine.Rendering.Universal;
public class MyBlitFeature : ScriptableRendererFeature
[System.Serializable]
public class MyFeatureSettings
// 在inspector面板中设置开关,插入pass的位置,材质
public bool IsEnabled = true;
public RenderPassEvent WhenToInsert = RenderPassEvent.AfterRendering;
public Material MaterialToBlit;
// 命名为"settings" (小写) 才能在inspector面板中正确显示
public MyFeatureSettings settings = new MyFeatureSettings();
RenderTargetHandle renderTextureHandle;
MyBlitRenderPass myRenderPass;
// 创建pass,参数为pass名称、pass插入位置、渲染用到的材质
public override void Create()
myRenderPass = new MyBlitRenderPass(
"My custom pass",
settings.WhenToInsert,
settings.MaterialToBlit
);
// 每帧调用一次这个方法将自定义pass加入渲染管线中
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
if (!settings.IsEnabled)
// 关闭则不添加
return;
// 获取一些渲染所需信息,此处是摄像机的color buffer
var cameraColorTargetIdent = renderer.cameraColorTarget;
myRenderPass.Setup(cameraColorTargetIdent);
// 添加pass.
// Could queue up multiple passes and/or pick passes to use
renderer.EnqueuePass(myRenderPass);
MyBlitRenderPass.cs
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
class MyBlitRenderPass : ScriptableRenderPass
// 用来在Frame Debug工具中标识pass名
string profilerTag;
Material materialToBlit;
RenderTargetIdentifier cameraColorTargetIdent;
RenderTargetHandle tempTexture;
public MyBlitRenderPass(string profilerTag,
RenderPassEvent renderPassEvent, Material materialToBlit)
this.profilerTag = profilerTag;
this.renderPassEvent = renderPassEvent;
this.materialToBlit = materialToBlit;
// 用来被Feature类调用初始化一些需要的数据,例如本例中的相机Color buffer
public void Setup(RenderTargetIdentifier cameraColorTargetIdent)
this.cameraColorTargetIdent = cameraColorTargetIdent;
// 该方法每帧Execute方法执行前被调用, 用作渲染前准备工作
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
// 使用相机渲染目标参数创建临时纹理
cmd.GetTemporaryRT(tempTexture.id, cameraTextureDescriptor);
// 每帧每个符合条件的相机会执行一次,渲染发生时不会再调用。所以只能通过cmd执行渲染,不能在这里写渲染命令。
// RenderingData(没有清晰的文档)提供了一系列场景还有被渲染的信息 provides a bunch of (not very well documented)
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
// 获取cmd
CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
cmd.Clear();
// 实际渲染pass
// 将相机纹理通过材质生成到临时纹理中
cmd.Blit(cameraColorTargetIdent, tempTexture.Identifier(), materialToBlit, 0);
// 再将临时纹理内容复制回相机颜色缓冲中
cmd.Blit(tempTexture.Identifier(), cameraColorTargetIdent);
// 执行cmd
context.ExecuteCommandBuffer(cmd);
// 释放cmd
cmd.Clear();
CommandBufferPool.Release(cmd);
// Excute方法后执行,在这里释放Configure中分配的内存
public override void FrameCleanup(CommandBuffer cmd)
cmd.ReleaseTemporaryRT(tempTexture.id);
创建材质
原文用Shader Graph创建的一个Invert shader我不知道为什么不行,有试过的朋友如果有同样问题最后解决了可以告诉我下为啥?
我换了个其他后处理shader,创建一个Material,使用这个shader
Shader "Custom/BrightnessSaturationAndContrast"
Properties
_MainTex ("Texture", 2D) = "white"
_Brightness("Brightness", Float) = 1
_Saturation("Saturation", Float) = 1
_Contrast("_Contrast", Float) = 1
SubShader
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
half _Brightness;
half _Saturation;
half _Contrast;
struct v2f
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
;
v2f vert (appdata_img v)
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
fixed4 frag (v2f i) : SV_Target
fixed4 renderTex = tex2D(_MainTex, i.uv);
fixed3 finalColor = renderTex.rgb * _Brightness;
fixed luminance = 0.2125 * renderTex.r + 0.7154 * renderTex.g + 0.0721 * renderTex.b;
fixed3 luminanceColor = fixed3(luminance, luminance, luminance);
finalColor = lerp(luminanceColor, finalColor, _Saturation);
fixed avgColor = fixed3(0.5,0.5, 0.5);
finalColor = lerp(avgColor, finalColor, _Contrast);
return fixed4(finalColor, renderTex.a);
ENDCG
添加Render Feature
添加后效果如下,并在Material中设置刚加入好的材质
效果
最终调整材质球中的参数就可以看到屏幕中的变化。
Unity Materials Render
以上是关于Unity Render Feature示例的主要内容,如果未能解决你的问题,请参考以下文章
Unity WebView 插件⭐️(十七)超核心模块 高级功能接口—Advanced feature interfaces