URP学习汇总
新建URP项目,Unity默认测试场景查看变化
Camera
Camera 基础设置解释
- 相机堆叠:RenderType
Camera 特殊功能列举
- 分屏相机两个Camera,RenderType都设置成Base,设置ViewportRect即可
相机分屏
Post-process Volume后处理盒子
- 添加Volume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15namespace UnityEngine.Rendering.Universal
{
[SerializeField, VolumeComponentMenu("Mypost/ScreenSpacePlaneReflect")]
public class ScreenSpacePlaneReflection : VolumeComponent
{
public BoolParameter on = new BoolParameter(false);
public ClampedIntParameter RTsize = new ClampedIntParameter(512, 128, 720, false);
public FloatParameter ReflectHeight = new FloatParameter(0.2f, false);
public ClampedFloatParameter fadeOutRange = new ClampedFloatParameter(0.3f, 0.0f, 1.0f, false);
public bool IsActive() => on.value;
public bool IsTileCompatible() => false;
}
} - 获得组件: VolumeManager.instance.stack.GetComponent
(); //获取自定义的volume组件 - 目前这种方式还是扩展后处理还是比较困难,由于流程最终走的uber shader,
加新效果都需要再里面加一个处理,相当于改源码,因此使用RenderFeather添加后处理比较方便
Forward Renderer Data设置说明
Renderer Features
基础Features:Render Objects
- 初次理解,在渲染管线的某个时刻,过滤到需要的Object,并覆盖一些特殊设置,如遮挡显示等等
URP手册基础Features:SSAO
环境遮挡Ambient Occlusion自定义Renderer Features
- 如何创建:
- Create / Rendering / URP / RenderFeature
- 创建脚本继承自:ScriptableRendererFeature, ScriptableRenderPass
/ Create / Rendering / URP / RenderFeature创建 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class CustomRenderPassFeature : ScriptableRendererFeature
{
class CustomRenderPass : ScriptableRenderPass
{
// This method is called before executing the render pass.
// It can be used to configure render targets and their clear state. Also to create temporary render target textures.
// When empty this render pass will render to the active camera render target.
// You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
// The render pipeline will ensure target setup and clearing happens in a performant manner.
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
}
// Here you can implement the rendering logic.
// Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers
// https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html
// You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
}
// Cleanup any allocated resources that were created during the execution of this render pass.
public override void OnCameraCleanup(CommandBuffer cmd)
{
}
}
CustomRenderPass m_ScriptablePass;
/// <inheritdoc/>
public override void Create()
{
m_ScriptablePass = new CustomRenderPass();
// Configures where the render pass should be injected.
m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
}
// Here you can inject one or multiple render passes in the renderer.
// This method is called when setting up the renderer once per-camera.
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(m_ScriptablePass);
}
}
- 用途:
- 实现URP没有提供的后处理效果
- 很多效果都可以放在RenderFeather中实现
//贴花 URP Decal Projector
ShaderGraph使用
解析URP默认ShaderGraph
一些URP特殊用法
SAMPLER
- 官网关于SAMPLER的使用说明
“Point”, “Linear” or “Trilinear” (required) set up texture filtering mode.
“Clamp”, “Repeat”, “Mirror” or “MirrorOnce” (required) set up texture wrap mode.Wrap modes can be specified per-axis (UVW), e.g. “ClampU_RepeatV”.
“Compare” (optional) set up sampler for depth comparison; use with HLSL SamplerComparisonState type and SampleCmp / SampleCmpLevelZero functions.
- 使用方式
sampler_<过滤><UV处理> 形成一个变量名,用这个变量作为参数形成采样
例如:sampler_LinearClamp 这个表示采样过滤是linear,超过(0,1)用clamp方式采样
SAMPLER(sampler_LinearClamp);在shader里这样声明变量
采样是这样使用:
float4 cloud = SAMPLE_TEXTURE2D_X(_CloudTex, sampler_LinearClamp, cloud_uv);
也可以这样
_CloudTex.Sample(sampler_LinearClamp, cloud_uv);
- 定义纹理和纹理采样器
TEXTURE2D(textureName);
SAMPLER(sampler_textureName);
TEXTURE2D
- TEXTURE2D_X(_MainTex);
- TEXTURE2D_X_FLOAT(_CameraDepthTexture);
CBuffer
Constant Buffer 是GPU中的一处常量缓冲区。 Unity Shader中使用CBUFFER_START和CBUFFER_END来定义缓冲区变量。
当前Unity内部使用的缓冲区有
UnityPerCamera
UnityLighting
UnityShadows
UnityPerDraw
UnityPerFrame
UnityPerMaterial
UnityPerObject
这些缓冲区是根据各自数据的刷新频率来定义的。 例如UnityPerCamera中的数据,仅在渲染的Camera发生变化时刷新。里面存的即是与Camera相关的数据。UnityPerMaterial则在材质球发生变化的时候刷新。
pragma multi_compile
URP 支持变体 ,你可能需要某些功能 #pragma multi_compile 添加以下关键字
_MAIN_LIGHT_SHADOWS
_MAIN_LIGHT_SHADOWS_CASCADE
_ADDITIONAL_LIGHTS_VERTEX
_ADDITIONAL_LIGHTS
_ADDITIONAL_LIGHT_SHADOWS
_SHADOWS_SOFT
_MIXED_LIGHTING_SUBTRACTIVE