当前位置:   article > 正文

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

cbuffer_start

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

1、property 属性变量需要在CBUFFER_START/CBUFFER_END中声明

  1. CBUFFER_START(UnityPerMaterial)
  2. float4 _BaseMap_ST; // Texture tiling & offset inspector values
  3. float4 _BaseColor;
  4. float _BumpScale;
  5. float4 _EmissionColor;
  6. float _Smoothness;
  7. float _Cutoff;
  8. CBUFFER_END

 

2、数据类型:

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


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

Unity 内置着色器通用渲染管线着色器
StandardUniversal Render Pipeline/Lit
Standard (Specular Setup)Universal Render Pipeline/Lit
Standard TerrainUniversal Render Pipeline/Terrain/Lit
Particles/Standard SurfaceUniversal Render Pipeline/Particles/Lit
Particles/Standard UnlitUniversal Render Pipeline/Particles/Unlit
Mobile/DiffuseUniversal Render Pipeline/Simple Lit
Mobile/Bumped SpecularUniversal 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/VertexLitUniversal Render Pipeline/Simple Lit
Legacy Shaders/DiffuseUniversal Render Pipeline/Simple Lit
Legacy Shaders/SpecularUniversal Render Pipeline/Simple Lit
Legacy Shaders/Bumped DiffuseUniversal Render Pipeline/Simple Lit
Legacy Shaders/Bumped SpecularUniversal Render Pipeline/Simple Lit
Legacy Shaders/Self-Illumin/DiffuseUniversal Render Pipeline/Simple Lit
Legacy Shaders/Self-Illumin/Bumped DiffuseUniversal Render Pipeline/Simple Lit
Legacy Shaders/Self-Illumin/SpecularUniversal Render Pipeline/Simple Lit
Legacy Shaders/Self-Illumin/Bumped SpecularUniversal Render Pipeline/Simple Lit
Legacy Shaders/Transparent/DiffuseUniversal Render Pipeline/Simple Lit
Legacy Shaders/Transparent/SpecularUniversal Render Pipeline/Simple Lit
Legacy Shaders/Transparent/Bumped DiffuseUniversal Render Pipeline/Simple Lit
Legacy Shaders/Transparent/Bumped SpecularUniversal Render Pipeline/Simple Lit
Legacy Shaders/Transparent/Cutout/DiffuseUniversal Render Pipeline/Simple Lit
Legacy Shaders/Transparent/Cutout/SpecularUniversal Render Pipeline/Simple Lit
Legacy Shaders/Transparent/Cutout/Bumped DiffuseUniversal Render Pipeline/Simple Lit
Legacy Shaders/Transparent/Cutout/Bumped SpecularUniversal Render Pipeline/Simple Lit

4、Tags

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

 

5、 通用结构

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

Built-inURP
CGPROGRAM
HLSLPROGRAM
HLSLPROGRAM
ENDCG
ENDHLSL
ENDHLSL
CGINCLUDE
HLSLINCLUDE
HLSLINCLUDE

6、shader include 文件

其他包含的文件:


 

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该通道仅从摄像机的角度将深度信息渲染到深度纹理中。
MetaUnity 仅在 Unity 编辑器中烘焙光照贴图时执行该通道。Unity 在构建播放器时会从着色器中剥离该通道。
SRPDefaultUnlit渲染对象时,使用此 LightMode 标签值绘制额外的通道。应用示例:绘制对象轮廓。此标签值对前向渲染路径和延迟渲染路径均有效。
当通道没有 LightMode 标签时,URP 使用此标签值作为默认值。

 

光照渲染方式对应

  1. pass{
  2. //Tasg{"LightMode" = "ForwardBase"}
  3. //换成
  4. Tags{"LightMode" = "UniversalForward"}
  5. ...
  6. }

light mode 中build-in 对应UPR 

Built-inURP
ForwardBaseUniversalForward
ForwardAdd启动关键字_AddITIONAL_LIGHTS
Deferred and relatedUniversalGBuffer seems to have just been added to URP
Vertex and related
ShadowCasterShadowCaster
MotionVectors已经不支持了

 

8、宏转换

Built-inURP
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-inURP
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-inURP
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-inURP
float4 UnityObjectToClipPos(float3 pos)float4 TransformObjectToHClip(float3 positionOS)
float3 UnityObjectToViewPos(float3 pos)TransformWorldToView(TransformObjectToWorld(positionOS))

 

 通用函数

 

Built-inURP
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-inURP
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) - objectSpacePositionInclude “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-inURP
float4 ComputeScreenPos (float4 clipPos)float4 ComputeScreenPos(float4 positionCS)Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl”
float4 ComputeGrabScreenPos (float4 clipPos)Gone.

 

顶点照明 

Built-inURP
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-inURP
_LightColor0_MainLightColorInclude “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl”
_WorldSpaceLightPos0_MainLightPositionInclude “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl”
_LightMatrix0Gone ? 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_WorldToShadowfloat4x4 _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-inURP
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-inURP
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-inURP
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)

使用实例

  1. void OnEnable()
  2. {
  3. RenderPipelineManager.beginCameraRendering += MyCameraRendering;
  4. }
  5. void OnDisable()
  6. {
  7. RenderPipelineManager.beginCameraRendering -= MyCameraRendering;
  8. }
  9. void MyCameraRendering(ScriptableRenderContext context, Camera camera)
  10. {
  11. ...
  12. if(camera == myEffectCamera)
  13. {
  14. ...
  15. }
  16. ...
  17. }

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/com.unity.render-pipelines.universal@12.1/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(抓屏)等相关内容后续会抽空更新,具体写个实例以便大家探讨

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/101778
推荐阅读
相关标签
  

闽ICP备14008679号