当前位置:网站首页>运行时修改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;
}
}边栏推荐
- 把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
- 疫情防控,居家办公,网上授课之心得 | 社区征文
- 3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
- Resolution of cross reference in IDA
- It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
- 元宇宙的生态圈
- 計網 | 【四 網絡層】知識點及例題
- Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (2) -- convert database to cluster mode
- Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
- Smartctl opens the device and encounters permission denied problem troubleshooting process record
猜你喜欢

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

The ecosystem of the yuan universe

Left hand dreams right hand responsibilities GAC Honda not only pays attention to sales but also children's safety

LeetCode 210:课程表 II (拓扑排序)

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"

I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating

Distributed transaction solutions and code implementation

Random list random generation of non repeating numbers

Use of hashcat

DDD concept is complex and difficult to understand. How to design code implementation model in practice?
随机推荐
How to quickly familiarize yourself with the code when you join a new company?
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
E - average and median
After reciting the eight part essay, I won the hemp in June
Convert string array to list collection
2022年云计算应用关键威胁调查
Sumati gamefi ecological overview, element design in the magical world
[day 26] given the ascending array nums of n elements, find a function to find the subscript of target in nums | learn binary search
計網 | 【四 網絡層】知識點及例題
Viewing MySQL password on Linux_ MySQL forgets password "suggestions collection" under Linux
vim的Dirvish中文文档
When they are in private, they have a sense of propriety
Is the compass reliable? Is it safe to open a securities account?
Folding screen will become an important weapon for domestic mobile phones to share the apple market
Mall project pc--- product details page
Qt中使用QDomDocument操作XML文件
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
How do the TMUX color palette work?
Rod and Schwartz cooperated with ZhongGuanCun pan Lianyuan Institute to carry out 6G technology research and early verification
当一个接口出现异常时候,你是如何分析异常的?