unity build-in 渲染管线升级urp渲染 shader篇

        由于工作原因需要对项目进行升级,从build-in渲染管线升级到urp渲染管线,我自己对应的unity版本是2018.版本升级到2021.3.2版本,由于最近几年unity版本升级比较快,个体版本差异有所不同,如遇与版本不一致问题敬请谅解。以下是根据官网等系列网站整理的内容

1、property 属性变量需要在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、数据类型:

        UPR已经不支持fixed类型,应该改成half类型


3、内置着色器与URP着色器对应关系

Unity 内置着色器 通用渲染管线着色器
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

  • SubShader的Tags中添加 "RenderPipeline" = "UniversalPipeline"

5、 通用结构

        URP推荐的hlsl ,CGPROGRAM-ENDCG 的格式不再做兼容

Built-in URP
CGPROGRAM
HLSLPROGRAM
HLSLPROGRAM
ENDCG
ENDHLSL
ENDHLSL
CGINCLUDE
HLSLINCLUDE
HLSLINCLUDE

6、shader include 文件

内容 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

其他包含的文件:


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(typename) ZERO_INITIALIZE(typename)

9、阴影

必须先包含 include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl”.

Built-in URP
UNITY_DECLARE_SHADOWMAP(tex) TEXTURE2D_SHADOW_PARAM(textureNamesamplerName)
UNITY_SAMPLE_SHADOW(texuv) SAMPLE_TEXTURE2D_SHADOW(textureNamesamplerNamecoord3)
UNITY_SAMPLE_SHADOW_PROJ(texuv) SAMPLE_TEXTURE2D_SHADOW(textureNamesamplerNamecoord4.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(nameuv) SAMPLE_TEXTURE2D(textureNamesamplerNamecoord2)
UNITY_SAMPLE_TEX2D_SAMPLER(namesamplernameuv) SAMPLE_TEXTURE2D(textureNamesamplerNamecoord2)
UNITY_SAMPLE_TEX2DARRAY(nameuv) SAMPLE_TEXTURE2D_ARRAY(textureNamesamplerNamecoord2index)
UNITY_SAMPLE_TEX2DARRAY_LOD(nameuvlod) SAMPLE_TEXTURE2D_ARRAY_LOD(textureNamesamplerNamecoord2indexlod)

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 hhalf heighthalf3 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 vertexfloat3 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_4LightPosX0unity_4LightPosY0unity_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(ooutpos) o.fogCoord = ComputeFogFactor(clipSpacePosition.z);
UNITY_APPLY_FOG(coordcol) color = MixFog(colori.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 不在支持 OnPreCullOnPreRenderOnPostRender 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来实现,具体就不在这里赘述了

主要参考内容:

Converting your shaders | Universal RP | 12.1.7 (unity3d.com)icon-default.png?t=M666https://docs.unity3d.com/Packages/[email protected]/manual/upgrading-your-shaders.html 

From Built-in to URP (teodutra.com)icon-default.png?t=M666https://teodutra.com/unity/shaders/urp/graphics/2020/05/18/From-Built-in-to-URP/

其中在转换的过程中,包含后效、阴影,光照 以及grabpass(抓屏)等相关内容后续会抽空更新,具体写个实例以便大家探讨

猜你喜欢

转载自blog.csdn.net/lejian/article/details/125697507