当前位置:网站首页>运行时修改Universal Render Data
运行时修改Universal Render Data
2022-06-24 23:04:00 【ttod】
ScriptableRenderPipeline对象里面包含了UniversalRenderData对象,可以通过在运行时设置UniversalRenderData对象的内容来获得特定的显示结果。
using UnityEngine;
using UnityEngine.Rendering.Universal;
public class SetUniversalRenderData : MonoBehaviour
{
[SerializeField]
UniversalRendererData renderData;
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha0))
{
renderData.opaqueLayerMask = 0;
//设置renderData的不透明层为“Nothing”
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
renderData.opaqueLayerMask = ~0;
//设置renderData的不透明层为“Everything”
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
ScreenSpaceOutlines screenSpaceOutlines = renderData.rendererFeatures[0] as ScreenSpaceOutlines;
if (screenSpaceOutlines != null)screenSpaceOutlines.SetColor(Color.blue);
//通过执行第一个render feature提供的SetColor方法来修改显示效果。
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
ScreenSpaceOutlines screenSpaceOutlines = renderData.rendererFeatures[0] as ScreenSpaceOutlines;
if (screenSpaceOutlines != null) screenSpaceOutlines.SetColor(Color.yellow);
//通过执行第一个render feature提供的SetColor方法来修改显示效果。
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
renderData.rendererFeatures[0].SetActive(false);
//设置一个render feature为非激活状态
}
if (Input.GetKeyDown(KeyCode.Alpha5))
{
renderData.rendererFeatures[0].SetActive(true);
//设置一个render feature为激活状态
}
}
}以上代码演示了如何通过键盘数字来切换RenderData属性,执行RenderFeature方法以及改变RenderFeature状态。
下面的代码段贴出了上面提到的RenderFeature的内容,可以看到里面所包含的SetColor方法。
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class ScreenSpaceOutlines : ScriptableRendererFeature
{
class ViewSpaceNormalsTexturePass : ScriptableRenderPass
{
readonly Material normalsMaterials;
readonly List<ShaderTagId> shaderTagIdList;
FilteringSettings filteringSettings;
readonly RenderTargetHandle normals;
public RenderTargetIdentifier identifier { get { return normals.Identifier(); } }
LayerMask outlineLayerMask;
int msaaSamples;
public ViewSpaceNormalsTexturePass(LayerMask outlineLayerMask, int msaaSamples)
{
normalsMaterials = new Material(Shader.Find("Hidden/ViewSpaceNormals"));
shaderTagIdList = new List<ShaderTagId>
{
new ShaderTagId("UniversalForward"),
new ShaderTagId("UniversalForwardOnly"),
new ShaderTagId("LightWeightForward"),
new ShaderTagId("SRPDefaultUnlit")
};
renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
this.outlineLayerMask = outlineLayerMask;
this.msaaSamples = msaaSamples;
normals.Init("_SceneViewSpaceNormals");
}
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
RenderTextureDescriptor renderTextureDescriptor = cameraTextureDescriptor;
renderTextureDescriptor.msaaSamples = msaaSamples;
cmd.GetTemporaryRT(normals.id, renderTextureDescriptor, FilterMode.Point);
ConfigureTarget(identifier);
ConfigureClear(ClearFlag.All, Color.clear);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
if (!normalsMaterials) return;
DrawingSettings drawSettings = CreateDrawingSettings(shaderTagIdList, ref renderingData, renderingData.cameraData.defaultOpaqueSortFlags);
drawSettings.overrideMaterial = normalsMaterials;
filteringSettings = new FilteringSettings(RenderQueueRange.opaque, outlineLayerMask);
context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref filteringSettings);
}
}
class ScreenSpaceOutlinePass : ScriptableRenderPass
{
Material screenSpaceOutlineMaterial;
RenderTargetIdentifier cameraColorTarget;
readonly RenderTargetIdentifier normalsIdentifier;
Material CreateMaterial() { screenSpaceOutlineMaterial = new Material(Shader.Find("Hidden/ViewSpaceOutline")); return screenSpaceOutlineMaterial; }
public void SetColor(Color color)
{
if (!screenSpaceOutlineMaterial) screenSpaceOutlineMaterial = CreateMaterial();
screenSpaceOutlineMaterial.SetColor("_Color", color);
screenSpaceOutlineMaterial.SetFloat("_Intensity", color.a);
}
public ScreenSpaceOutlinePass(RenderTargetIdentifier normalsIdentifier, Color color)
{
renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
this.normalsIdentifier = normalsIdentifier;
screenSpaceOutlineMaterial = new Material(Shader.Find("Hidden/ViewSpaceOutline"));
SetColor(color);
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
cameraColorTarget = renderingData.cameraData.renderer.cameraColorTarget;
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
if (!screenSpaceOutlineMaterial) return;
CommandBuffer cmd = CommandBufferPool.Get();
Blit(cmd, normalsIdentifier, cameraColorTarget, screenSpaceOutlineMaterial, 0);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
ViewSpaceNormalsTexturePass viewSpaceNormalsTexturePass;
ScreenSpaceOutlinePass screenSpaceOutlinePass;
public override void Create()
{
viewSpaceNormalsTexturePass = new ViewSpaceNormalsTexturePass(outlineLayerMask, msaaSamples);
screenSpaceOutlinePass = new ScreenSpaceOutlinePass(viewSpaceNormalsTexturePass.identifier, color);
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(viewSpaceNormalsTexturePass);
renderer.EnqueuePass(screenSpaceOutlinePass);
}
[SerializeField]
LayerMask outlineLayerMask;
[SerializeField]
Color color = Color.yellow;
[SerializeField]
[Range(1, 16)]
int msaaSamples = 2;
public void SetColor(Color color)
{
if (screenSpaceOutlinePass != null) screenSpaceOutlinePass.SetColor(color);
}
public void SetMsaaSamples(int msaaSamples)
{
this.msaaSamples = Mathf.Clamp(msaaSamples, 1, 16);
}
public void SetLayerMask(LayerMask outlineLayerMask)
{
this.outlineLayerMask = outlineLayerMask;
}
}边栏推荐
- ACL access control of squid proxy server
- LeetCode 210:课程表 II (拓扑排序)
- [analysis of STL source code] functions and applications of six STL components (directory)
- After reciting the eight part essay, I won the hemp in June
- Practice and Thinking on process memory
- PSQL column to row
- It's 2022, and you still don't know what performance testing is?
- 探索C语言程序奥秘——C语言程序编译与预处理
- EasyCVR平台EHOME协议接入,视频播放出现断流是什么原因?
- 转行软件测试2年了,给还在犹豫的女生一点建议
猜你喜欢

計網 | 【四 網絡層】知識點及例題

分布式事务解决方案和代码落地

元宇宙的生态圈

EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
![Planification du réseau | [quatre couches de réseau] points de connaissance et exemples](/img/c3/d7f382409e99eeee4dcf4f50f1a259.png)
Planification du réseau | [quatre couches de réseau] points de connaissance et exemples

1-6搭建Win7虚拟机环境

DDD concept is complex and difficult to understand. How to design code implementation model in practice?

Please run IDA with elevated permissons for local debugging.

常用的软件测试工具清单,请查收。

数据库系统概论必背知识
随机推荐
E - Average and Median(二分)
Cusdis - lightweight, privacy first open source comment system | chain of the city
Jetson nano from introduction to practice (cases: opencv configuration, face detection, QR code detection)
算力服务网络:一场多元融合的系统革命
文件系统 -- 磁盘基础知识和FAT32文件系统详细介绍
psql 列转行
QT package the EXE file to solve the problem that "the program input point \u zdapvj cannot be located in the dynamic link library qt5cored.dll"
【FPGA】串口以命令控制温度采集
常用的软件测试工具清单,请查收。
Qt中使用QDomDocument操作XML文件
云原生数据库VS传统数据库
JS regular matching numbers, upper and lower case letters, underscores, midlines and dots [easy to understand]
Viewing MySQL password on Linux_ MySQL forgets password "suggestions collection" under Linux
Jetson Nano 从入门到实战(案例:Opencv配置、人脸检测、二维码检测)
中信证券手机开户是靠谱的吗?安全吗
What is the reason for the disconnection of video playback due to the EHOME protocol access of easycvr platform?
Charles 抓包工具
[I.MX6UL] U-Boot移植(六) 网络驱动修改 LAN8720A
数据库系统概论必背知识
|遇到bug怎么分析,专业总结分析来了