主要就一点:
如果原版Shader下含有:”LightMode” = “ForwardBase”
需要更改为:”LightMode” = “UniversalForward”
再是,需要在Tag里添加”RenderPipeline” = “UniversalPipeline”
Built-in到URP函数对照整理
整体结构
:
- 需要在Tag里添加”RenderPipeline” = “UniversalPipeline”
-
所有的URP Shader代码均包含在
HLSLPROGRAM/ENDHLSL
内
Built-in | URP |
---|---|
CGPROGRAM HLSLPROGRAM |
HLSLPROGRAM |
ENDCG ENDHLSL |
ENDHLSL |
CGINCLUDE HLSLINCLUDE |
HLSLINCLUDE |
Include文件:
Content | Built-in | URP |
---|---|---|
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 |
Surface shaders | Lighting.cginc | URP内不支持 |
所有的HLSL代码均在
Packages/Universal RP
内,可以翻看详细源码
Light Modes(照明模式)
Built-in | URP |
---|---|
ForwardBase | UniversalForward |
ForwardAdd | 移除 |
Deferred and related | 尚未支持 |
Vertex and related | 移除 |
ShadowCaster | ShadowCaster |
MotionVectors | 尚未支持 |
还有一些其他的Light Modes:
- DepthOnly
- Meta (for lightmap baking)
- Universal2D
Variants(变体)
URP支持某些变体,需要根据你使用的功能
#pragma multi_compile
添加一些关键字:
-
_MAIN_LIGHT_SHADOWS
-
_MAIN_LIGHT_SHADOWS_CASCADE
-
_ADDITIONAL_LIGHTS_VERTEX
-
_ADDITIONAL_LIGHTS
-
_ADDITIONAL_LIGHT_SHADOWS
-
_SHADOWS_SOFT
-
_MIXED_LIGHTING_SUBTRACTIVE
预定义的着色器宏
Built-in | URP |
---|---|
UNITY_PROJ_COORD(a) | 使用a.xy / a.w代替 |
UNITY_INITIALIZE_OUTPUT(type, name) | ZERO_INITIALIZE(type,name) |
Shadow Mapping(阴影贴图)
#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) |
Texture/Sampler Declaration Macros(纹理贴图采样)
这里仅列举一些常用的,更多的可以查
API includes
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) |
Shader辅助函数
代码文件可以查看:
顶点转换函数
Built-in | URP |
---|---|
float4 UnityObjectToClipPos(float3 pos) | float4 TransformObjectToHClip(float3 positionOS) |
float3 UnityObjectToViewPos(float3 pos) | TransformWorldToView(TransformObjectToWorld(positionOS)) |
法线转换函数
Built-in | URP |
---|---|
float4 UnityObjectToWorldNormal(float3 pos) | float4 TransformObjectToWorldNormal(float3 normalOS) |
通用辅助函数
Built-in | URP | |
---|---|---|
float3 WorldSpaceViewDir (float4 v) | float3 GetWorldSpaceViewDir(float3 positionWS) | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl” |
float3 UnityObjectToWorldDir(float4 v) | TransformObjectToWorldDir(real3 dirOS) | |
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” URP中的decodeInstructions是half4(LIGHTMAP_HDR_MULTIPLIER, LIGHTMAP_HDR_EXPONENT, 0.0h, 0.0h) |
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) | 对于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) | 移除了 |
顶点照明辅助函数
Built-in | URP | |
---|---|---|
loat3 ShadeVertexLights (float4 vertex, float3 normal) | 移除了,可以尝试使用UNITY_LIGHTMODEL_AMBIENT.xyz + VertexLighting(…) | 对于VertexLighting(…) include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
内置着色器变量
除了光照相关的变量外,其他的变量名都基本没变
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 | 移除了。目前尚不支持Cookie | |
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0 | 在URP中,其他光源存储在数组/缓冲区中(取决于平台)。使用Light GetAdditionalLight(uint i, float3 positionWS)获取额外光源信息 | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
unity_4LightAtten0 | 在URP中,其他光源存储在数组/缓冲区中(取决于平台)。使用Light GetAdditionalLight(uint i, float3 positionWS)获取额外光源信息 | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
unity_LightColor | 在URP中,其他光源存储在数组/缓冲区中(取决于平台)。使用Light GetAdditionalLight(uint i, float3 positionWS)获取额外光源信息 | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
unity_WorldToShadow | float4x4 _MainLightWorldToShadow[MAX_SHADOW_CASCADES + 1] 或者_AdditionalLightsWorldToShadow[MAX_VISIBLE_LIGHTS] | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl” |
如果要使用循环所有其他灯光
GetAdditionalLight(...)
,
GetAdditionalLightsCount()
可以使用来查询其他灯光计数。
其他
Shadows(阴影)
Built-in | URP |
---|---|
UNITY_SHADOW_COORDS(x) | 移除了。DIY,例如float4 shadowCoord : TEXCOORD0; |
TRANSFER_SHADOW(a) | a.shadowCoord = TransformWorldToShadowCoord(worldSpacePosition) |
SHADOWS_SCREEN | 移除了 |
Fog(雾)
Built-in | URP |
---|---|
UNITY_FOG_COORDS(x) | 移除了。DIY,例如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(...)
和
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” |
其他
Built-in | URP | |
---|---|---|
ShadeSH9(normal) | SampleSH(normal) | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
unity_ColorSpaceLuminance | 移除了。使用Luminance() | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl” |
基本上Shader代码对照上述整理的内容,做完更替后即可完成升级。