From Built-in to URP
- General Structure (通用结构)
- Shader Include Files (着色器包含文件)
- Light Modes (灯光模式)
- Variants (变体)
- Predefined Shader Preprocessor Macros (预定义的着色器宏)
- Helpers (辅助宏)
- Shadow Mapping (阴影贴图)
- Texture/Sampler Declaration Macros (纹理/采样器声明宏)
- Built-in Shader Helper Functions (内置着色器辅助函数)
- Vertex Transformation Functions (顶点变换函数)
- Normal Transformation Functions (法线转换函数)
- Generic Helper Functions (通用辅助函数)
General Structure (通用结构)
First of all, add “RenderPipeline” = “UniversalPipeline” to your tags. Next, all URP shaders are written using HLSL embraced by HLSLPROGRAM/ENDHLSL/etc. macros. To avoid headaches, use them as well.
| Built-in | URP |
|---|---|
| CGPROGRAM | HLSLPROGRAM |
| ENDCG | ENDHLSL |
| CGINCLUDE | HLSLINCLUDE |
Shader Include Files (着色器包含文件)
| 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 | None, but you can find a side project for this here |
Other useful includes:
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.hlslLight Modes (灯光模式)
| Built-in | URP |
|---|---|
ForwardBase |
UniversalForward |
ForwardAdd |
Gone |
Deferred and related |
UniversalGBuffer seems to have just been added to URP |
Vertex and related |
Gone |
ShadowCaster |
ShadowCaster |
MotionVectors |
Not suppoted yet |
The other light modes supported are:
- DepthOnly
- Meta (for lightmap baking)
- Universal2D
Variants (变体)
URP support some variants, so depending on the things you are using, you might need to add some #pragma multi_compile for some of the following keywords:
URP 支持一些变体,所以根据你正在使用的东西,你可能需要添加一些 #pragma multi_compile 的一些关键字:
- MAINLIGHT_SHADOWS
- MAINLIGHT_SHADOWS_CASCADE
- ADDITIONALLIGHTS_VERTEX
- ADDITIONALLIGHTS
- ADDITIONALLIGHT_SHADOWS
- SHADOWSSOFT
- MIXEDLIGHTING_SUBTRACTIVE
Predefined Shader Preprocessor Macros (预定义的着色器宏)
Helpers (辅助宏)
| Built-in | URP |
|---|---|
UNITY_PROJ_COORD(a) |
Gone. Do a.xy/a.w instead (移除了, 使用a.xy / a.w代替) |
UNITY_INITIALIZE_OUTPUT(type, name) |
ZERO_INITIALIZE(type, name) |
Shadow Mapping (阴影贴图)
You must 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 (纹理/采样器声明宏)
Unity has a bunch of texture/sampler macros to improve cross compatibility between APIs, but people are not used to use them. Those still exist in URP, but now with different names and new additions. I will not put all of them here because it’s a lot, but you can check their definitions per platform in the API includes.
Unity 有一堆纹理/采样宏来改善api之间的交叉兼容性,但人们并不习惯使用它们. 它们仍然存在于URP中,但现在有了不同的名称和新的添加. 我不会把它们全部放在这里,因为它们太多了,但您可以在包含的API中检查每个平台的定义.
| 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) |
Important to note that
SCREENSPACE_TEXTUREhas becomeTEXTURE2D_X. If you are working on some screen space effect for VR in Single Pass Instanced or Multi-view modes, you must declare the textures used withTEXTURE2D_X. This macro will handle for you the correct texture (array or not) declaration. You also have to sample the textures usingSAMPLE_TEXTURE2D_Xand useUnityStereoTransformScreenSpaceTexfor the uv.
需要注意
SCREENSPACE_TEXTURE变成了TEXTURE2D_X. 如果你想要在VR中(Single Pass Instanced 或 Multi-view 模式)制作屏幕效果,你必须使用TEXTURE2D_X定义纹理。这个宏会为你处理正确的纹理声明 (是否为数组). 对这个纹理采样的时候必须使用SAMPLE_TEXTURE2D_X,并且对uv使用UnityStereoTransformScreenSpaceTex.
Built-in Shader Helper Functions (内置着色器辅助函数)
You can find them all in
Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl
Vertex Transformation Functions (顶点变换函数)
| Built-in | URP |
|---|---|
float4 UnityObjectToClipPos(float3 pos) |
float4 TransformObjectToHClip(float3 positionOS) |
float3 UnityObjectToViewPos(float3 pos) |
TransformWorldToView(TransformObjectToWorld(positionOS)) |
Normal Transformation Functions (法线转换函数)
| Built-in | URP |
|---|---|
float4 UnityObjectToWorldNormal(float3 pos) |
float4 TransformObjectToWorldNormal(float3 normalOS) |
Generic Helper Functions (通用辅助函数)
| 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) |
Gone. Do TransformWorldToObject(GetCameraPositionWS()) - objectSpacePosition; |
float2 ParallaxOffset (half h, half height, half3 viewDir) |
Gone? Copy from 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) |
Gone? Copy from UnityCG.cginc |
float DecodeFloatRGBA (float4 enc) |
Gone? Copy from UnityCG.cginc |
float2 EncodeFloatRG (float v) |
Gone? Copy from UnityCG.cginc |
float DecodeFloatRG (float2 enc) |
Gone? Copy from UnityCG.cginc |
float2 EncodeViewNormalStereo (float3 n) |
Gone? Copy from UnityCG.cginc |
float3 DecodeViewNormalStereo (float4 enc4) |
Gone? Copy from UnityCG.cginc |
https://www.jianshu.com/p/3fef69e2efb6
https://teodutra.com/unity/shaders/urp/graphics/2020/05/18/From-Built-in-to-URP/
最后编辑:admin 更新时间:2023-04-13 18:03