Due to work reasons, the project needs to be upgraded, from the build-in rendering pipeline to the urp rendering pipeline. My own version of unity is 2018. The version is upgraded to version 2021.3.2. Since the upgrade of the unity version is relatively fast in recent years, the individual version The differences are different, please understand if there is any inconsistency with the version. The following is the content organized according to the official website and other series of websites
1. The property attribute variable needs to be declared in CBUFFER_START/CBUFFER_END
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST; // Texture tiling & offset inspector values
float4 _BaseColor;
float _BumpScale;
float4 _EmissionColor;
float _Smoothness;
float _Cutoff;
CBUFFER_END
2. Data type:
UPR no longer supports fixed type, it should be changed to half type
3. Correspondence between built-in shaders and URP shaders
Unity built-in shaders | Universal Rendering Pipeline Shaders |
---|---|
Standard | Universal Render Pipeline/Lit |
Standard (Specular Setup) | Universal Render Pipeline/Lit |
Standard Terrain | Universal Render Pipeline/Terrain/Lit |
Particles/Standard Surface | Universal Render Pipeline/Particles/Lit |
Particles/Standard Unlit | Universal Render Pipeline/Particles/Unlit |
Mobile/Diffuse | Universal Render Pipeline/Simple Lit |
Mobile/Bumped Specular | Universal Render Pipeline/Simple Lit |
Mobile/Bumped Specular(1 Directional Light) | Universal Render Pipeline/Simple Lit |
Mobile/Unlit (Supports Lightmap) | Universal Render Pipeline/Simple Lit |
Mobile/VertexLit | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Diffuse | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Specular | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Bumped Diffuse | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Bumped Specular | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Self-Illumin/Diffuse | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Self-Illumin/Bumped Diffuse | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Self-Illumin/Specular | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Self-Illumin/Bumped Specular | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Transparent/Diffuse | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Transparent/Specular | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Transparent/Bumped Diffuse | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Transparent/Bumped Specular | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Transparent/Cutout/Diffuse | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Transparent/Cutout/Specular | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Transparent/Cutout/Bumped Diffuse | Universal Render Pipeline/Simple Lit |
Legacy Shaders/Transparent/Cutout/Bumped Specular | Universal Render Pipeline/Simple Lit |
4、Tags
Tags
Added in SubShader"RenderPipeline" = "UniversalPipeline"
5、 通用结构
The format of hlsl and CGPROGRAM-ENDCG recommended by URP is no longer compatible
Built-in | URP |
---|---|
CG PROGRAM HLSL PROGRAM |
HLSL PROGRAM |
ENDCG ENDHLSL |
FINAL SL |
CGINCLUDE HLSLINCLUDE |
HLSLINCLUDE |
6. shader include file
content | Built-in files | URP file |
---|---|---|
Core | Unity.cginc | Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl |
Light | AutoLight.cginc | Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl |
Shadows | AutoLight.cginc | Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl |
其他包含的文件:
- Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl
- Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl
- Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl
- Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl
- Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl
- Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl
- Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTextue.hlsl
7、 Light Modes
此标签的值可以让管线确定在执行渲染管线的不同部分时使用哪个通道。
如果没有在管线中设置 LightMode
标签,URP 将使用该通道的 SRPDefaultUnlit
标签值。
LightMode
标签可以具有以下值。
属性 | 描述 |
---|---|
UniversalForward | 对应build-in ForwardBase前向渲染 |
UniversalGBuffer | 该通道会渲染对象几何体,但不评估任何光源影响。在 Unity 必须在延迟渲染路径中执行的通道中使用此标签值。 |
UniversalForwardOnly | 该通道会渲染对象几何体并评估所有光源影响,类似于 LightMode 具有 UniversalForward 值的情况。与 UniversalForward 的区别在于,URP 可以将该通道用于前向渲染路径和延迟渲染路径两者。 如果在 URP 使用延迟渲染路径时某个通道必须使用前向渲染路径来渲染对象,请使用此值。例如,如果 URP 使用延迟渲染路径来渲染某个场景,并且该场景包含的某些对象具有不适合 G 缓冲区的着色器数据(例如透明涂层法线),则应使用此标签。 如果着色器必须在前向渲染路径和延迟渲染路径两者中进行渲染,请使用 UniversalForward 和 UniversalGBuffer 标签值声明两个通道。如果着色器必须使用前向渲染路径进行渲染,而不管 URP 渲染器使用的渲染路径如何,请仅声明一个 LightMode 标签设置为 UniversalForwardOnly 的通道。如果使用了 SSAO 渲染器功能,请添加一个 LightMode 标签设置为 DepthNormalsOnly 的通道。有关更多信息,请参阅 DepthNormalsOnly 值。 |
DepthNormalsOnly | 将此值与延迟渲染路径中的 UniversalForwardOnly 结合使用。此值允许 Unity 在深度和法线预通道中渲染着色器。在延迟渲染路径中,如果缺少具有 DepthNormalsOnly 标签值的通道,则 Unity 不会在网格周围生成环境光遮挡。 |
Universal2D | 该通道会渲染对象并评估 2D 光源影响。URP 在 2D 渲染器中使用此标签值。 |
ShadowCaster | 该通道从光源的角度将对象深度渲染到阴影贴图或深度纹理中。 |
DepthOnly | 该通道仅从摄像机的角度将深度信息渲染到深度纹理中。 |
Meta | Unity 仅在 Unity 编辑器中烘焙光照贴图时执行该通道。Unity 在构建播放器时会从着色器中剥离该通道。 |
SRPDefaultUnlit | 渲染对象时,使用此 LightMode 标签值绘制额外的通道。应用示例:绘制对象轮廓。此标签值对前向渲染路径和延迟渲染路径均有效。当通道没有 LightMode 标签时,URP 使用此标签值作为默认值。 |
光照渲染方式对应
pass{
//Tasg{"LightMode" = "ForwardBase"}
//换成
Tags{"LightMode" = "UniversalForward"}
...
}
light mode 中build-in 对应UPR
Built-in | URP |
---|---|
ForwardBase | UniversalForward |
ForwardAdd | 启动关键字_AddITIONAL_LIGHTS |
Deferred and related | UniversalGBuffer seems to have just been added to URP |
Vertex and related | 无 |
ShadowCaster | ShadowCaster |
MotionVectors | 已经不支持了 |
8、宏转换
Built-in | URP |
---|---|
UNITY_PROJ_COORD(a) | Gone. Do a.xy/a.w instead |
UNITY_INITIALIZE_OUTPUT(type, name) | ZERO_INITIALIZE(type, name) |
9、阴影
必须先包含 include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl”.
Built-in | URP |
---|---|
UNITY_DECLARE_SHADOWMAP(tex) | TEXTURE2D_SHADOW_PARAM(textureName, samplerName) |
UNITY_SAMPLE_SHADOW(tex, uv) | SAMPLE_TEXTURE2D_SHADOW(textureName, samplerName, coord3) |
UNITY_SAMPLE_SHADOW_PROJ(tex, uv) | SAMPLE_TEXTURE2D_SHADOW(textureName, samplerName, coord4.xyz/coord4.w) |
10、纹理采样定义的宏
Built-in | URP |
---|---|
UNITY_DECLARE_TEX2D(name) | TEXTURE2D(textureName); SAMPLER(samplerName); |
UNITY_DECLARE_TEX2D_NOSAMPLER(name) | TEXTURE2D(textureName); |
UNITY_DECLARE_TEX2DARRAY(name) | TEXTURE2D_ARRAY(textureName); SAMPLER(samplerName); |
UNITY_SAMPLE_TEX2D(name, uv) | SAMPLE_TEXTURE2D(textureName, samplerName, coord2) |
UNITY_SAMPLE_TEX2D_SAMPLER(name, samplername, uv) | SAMPLE_TEXTURE2D(textureName, samplerName, coord2) |
UNITY_SAMPLE_TEX2DARRAY(name, uv) | SAMPLE_TEXTURE2D_ARRAY(textureName, samplerName, coord2, index) |
UNITY_SAMPLE_TEX2DARRAY_LOD(name, uv, lod) | SAMPLE_TEXTURE2D_ARRAY_LOD(textureName, samplerName, coord2, index, lod) |
11、Built-in Shader 函数
include “Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl”.
定点转换函数
Built-in | URP |
---|---|
float4 UnityObjectToClipPos(float3 pos) | float4 TransformObjectToHClip(float3 positionOS) |
float3 UnityObjectToViewPos(float3 pos) | TransformWorldToView(TransformObjectToWorld(positionOS)) |
通用函数
Built-in | URP | |
---|---|---|
float3 WorldSpaceViewDir (float4 v) | float3 GetWorldSpaceViewDir(float3 positionWS) | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl” |
float3 ObjSpaceViewDir (float4 v) | 用TransformWorldToObject (GetCameraPositionWS()) - objectSpacePosition 实现 |
|
float2 ParallaxOffset (half h, half height, half3 viewDir) | 从UnityCG.cginc拷贝 | |
fixed Luminance (fixed3 c) | real Luminance(real3 linearRgb) | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl” |
fixed3 DecodeLightmap (fixed4 color) | real3 DecodeLightmap(real4 encodedIlluminance , real4 decodeInstructions) |
Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl”decodeInstructions is used as half4(LIGHTMAP_HDR_MULTIPLIER, LIGHTMAP_HDR_EXPONENT, 0.0h, 0.0h) by URP |
float4 EncodeFloatRGBA (float v) | 从UnityCG.cginc拷贝 | |
float DecodeFloatRGBA (float4 enc) | 从UnityCG.cginc拷贝 | |
float2 EncodeFloatRG (float v) | 从UnityCG.cginc拷贝 | |
float DecodeFloatRG (float2 enc) | 从UnityCG.cginc拷贝 | |
float2 EncodeViewNormalStereo (float3 n) | 从UnityCG.cginc拷贝 | |
float3 DecodeViewNormalStereo (float4 enc4) | 从UnityCG.cginc拷贝 |
前向渲染通用函数
Built-in | URP | |
---|---|---|
float3 WorldSpaceLightDir (float4 v) | _MainLightPosition.xyz - TransformObjectToWorld(objectSpacePosition) |
Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl” |
float3 ObjSpaceLightDir (float4 v) | TransformWorldToObject(_MainLightPosition.xyz) - objectSpacePosition | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl” |
float3 Shade4PointLights (…) | 可以通过half3 VertexLighting(float3 positionWS, half3 normalWS) |
For VertexLighting(...) include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
屏幕空间函数
Built-in | URP | |
---|---|---|
float4 ComputeScreenPos (float4 clipPos) | float4 ComputeScreenPos(float4 positionCS) | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl” |
float4 ComputeGrabScreenPos (float4 clipPos) | Gone. |
顶点照明
Built-in | URP | |
---|---|---|
float3 ShadeVertexLights (float4 vertex, float3 normal) | Gone. You can try to use UNITY_LIGHTMODEL_AMBIENT.xyz + VertexLighting(...) |
VertexLighting(...) 需要包含 include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
可以在 “Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl”中找到其他通用实例
Built-in Shader 的变量,以 light相关为例
Lighting ↑
Built-in | URP | |
---|---|---|
_LightColor0 | _MainLightColor | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl” |
_WorldSpaceLightPos0 | _MainLightPosition | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl” |
_LightMatrix0 | Gone ? Cookies are not supported yet | |
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0 | 在UPR中 light GetAdditionalLight(uint i, float3 positionWS)获取灯光信息 |
Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
unity_4LightAtten0 | 在UPR中 light GetAdditionalLight(uint i, float3 positionWS)获取灯光信息 |
Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
unity_LightColor | 在UPR中 light GetAdditionalLight(uint i, float3 positionWS)获取灯光信息 |
Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
unity_WorldToShadow | float4x4 _MainLightWorldToShadow[MAX_SHADOW_CASCADES + 1] or _AdditionalLightsWorldToShadow[MAX_VISIBLE_LIGHTS] |
Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl” |
如果想知道添加的光照信息,应该用GetAdditionalLight(...)函数获得,
GetAdditionalLightsCount()函数可以得到使用的光照数量
Shadows 阴影
大多数可以在 “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl”.查找
Built-in | URP | |
---|---|---|
UNITY_SHADOW_COORDS(x) | Gone? DIY, e.g. float4 shadowCoord : TEXCOORD0; |
|
TRANSFER_SHADOW(a) | a.shadowCoord = TransformWorldToShadowCoord(worldSpacePosition) | With cascades on, do this on fragment to avoid visual artifacts |
SHADOWS_SCREEN | 不支持了 |
雾
更多的信息查找 “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl”.
Built-in | URP |
---|---|
UNITY_FOG_COORDS(x) | float fogCoord : TEXCOORD0; |
UNITY_TRANSFER_FOG(o, outpos) | o.fogCoord = ComputeFogFactor(clipSpacePosition.z); |
UNITY_APPLY_FOG(coord, col) | color = MixFog(color, i.fogCoord); |
Depth ↑
include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl” 和 _CameraDepthTexture
在 函数中视使用 SampleSceneDepth(...)
and LoadSceneDepth(...)
.
Built-in | URP | |
---|---|---|
LinearEyeDepth(sceneZ) | LinearEyeDepth(sceneZ, _ZBufferParams) | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl” |
Linear01Depth(sceneZ) | Linear01Depth(sceneZ, _ZBufferParams) | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl” |
后效
URP 不在支持 OnPreCull
, OnPreRender
, OnPostRender
and OnRenderImage
.也不支持OnRenderObject
and OnWillRenderObject
, 它提供了RenderPipelineManager
beginCameraRendering(ScriptableRenderContext context, Camera camera)
endCameraRendering(ScriptableRenderContext context, Camera camera)
beginFrameRendering(ScriptableRenderContext context,Camera[] cameras)
endFrameRendering(ScriptableRenderContext context,Camera[] cameras)
使用实例
void OnEnable()
{
RenderPipelineManager.beginCameraRendering += MyCameraRendering;
}
void OnDisable()
{
RenderPipelineManager.beginCameraRendering -= MyCameraRendering;
}
void MyCameraRendering(ScriptableRenderContext context, Camera camera)
{
...
if(camera == myEffectCamera)
{
...
}
...
}
URP与Post-processing stack插件已经不能很兼容,建议使用unity本身的后效处理使用通过Volumn来实现,具体就不在这里赘述了
主要参考内容:
其中在转换的过程中,包含后效、阴影,光照 以及grabpass(抓屏)等相关内容后续会抽空更新,具体写个实例以便大家探讨