由于工作原因需要对项目进行升级,从build-in渲染管线升级到urp渲染管线,我自己对应的unity版本是.版本升级到.3.2版本,由于最近几年unity版本升级比较快,个体版本差异有所不同,如遇与版本不一致问题敬请谅解。以下是根据官网等系列网站整理的内容
1、property 属性变量需要在CBUFFER_START/CBUFFER_END中声明
CBUFFER_START(UnityPerMaterial)float4 _BaseMap_ST; // Texture tiling & offset inspector valuesfloat4 _BaseColor;float _BumpScale;float4 _EmissionColor;float _Smoothness;float _Cutoff;CBUFFER_END
2、数据类型:
UPR已经不支持fixed类型,应该改成half类型
3、内置着色器与URP着色器对应关系
4、Tags
SubShader的Tags
中添加"RenderPipeline" = "UniversalPipeline"
5、 通用结构
URP推荐的hlsl ,CGPROGRAM-ENDCG的格式不再做兼容
6、shader include 文件
其他包含的文件:
Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlslPackages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlslPackages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlslPackages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlslPackages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlslPackages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlslPackages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTextue.hlsl
7、 Light Modes
此标签的值可以让管线确定在执行渲染管线的不同部分时使用哪个通道。
如果没有在管线中设置LightMode
标签,URP 将使用该通道的SRPDefaultUnlit
标签值。
LightMode
标签可以具有以下值。
光照渲染方式对应
pass{//Tasg{"LightMode" = "ForwardBase"}//换成Tags{"LightMode" = "UniversalForward"}...}
light mode 中build-in 对应UPR
8、宏转换
9、阴影
必须先包含 include“Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl”.
10、纹理采样定义的宏
11、Built-in Shader 函数
include“Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl”.
定点转换函数
通用函数
前向渲染通用函数
屏幕空间函数
顶点照明
可以在“Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl”中找到其他通用实例
Built-in Shader 的变量,以 light相关为例
Lighting↑
如果想知道添加的光照信息,应该用GetAdditionalLight(...)函数获得,
GetAdditionalLightsCount()函数可以得到使用的光照数量
Shadows 阴影
大多数可以在“Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl”.查找
雾
更多的信息查找“Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl”.
Depth↑
include“Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl”和_CameraDepthTexture
在 函数中视使用SampleSceneDepth(...)
andLoadSceneDepth(...)
.
后效
URP 不在支持OnPreCull
,OnPreRender
,OnPostRender
andOnRenderImage
.也不支持OnRenderObject
andOnWillRenderObject
, 它提供了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 ()/Packages/com.unity.render-pipelines.universal@12.1/manual/upgrading-your-shaders.html
From Built-in to URP ()/unity/shaders/urp/graphics//05/18/From-Built-in-to-URP/
其中在转换的过程中,包含后效、阴影,光照以及grabpass(抓屏)等相关内容后续会抽空更新,具体写个实例以便大家探讨