我把我的项目从内置渲染管线升级到URP,会遇到材质丢失变粉的问题。
对于非unity官方的自定义shader要查看shader代码中2中渲染管线不一致的写法进行修改。
灯光模式是forward的接口,在URP下不是这么写的。
修改步骤如下;
LightMode里的值改成UniversalForward。如下:
"LightMode"="UniversalForward"
删掉以下内容:
#define UNITY_PASS_FORWARDBASE
#pragma multi_compile_fwdbase
可以使用的灯光模式,有三种:
"UniversalForward"
"LightweightForward"
“SRPDefaultUnlit"
修改后:
这是灯光模式的问题其他写法也存在需要修改的地方,如下。
FallBack
一般FallBack为"Standard"的或其他之前Build-in 内置的shader都无法使用了,
删除即可。
一些CGInclude常用的接口(重要)
非常重要的一步来了,如果按照上面的做完后,还发现有CGInclude的接口不对的地方。
可能就要把之前Builid-in的shader完全改写成URP管线下的,那就必须把全部CGInclude的接口转写成URP下的。
具体如下:
1、把CGPROGRAM改成HLSLPROGRAM,
把ENDCG改成ENDHLSL。
CGINCLUDE改成HLSLINCLUDE。
2、把#include "UnityCG.cginc"
改成
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
3、o.pos = UnityObjectToClipPos( v.vertex );
改成
VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
o.pos = vertexInput.positionCS;
4、o.normalDir = UnityObjectToWorldNormal(v.normal);
改成
VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(v.normal);
o.normalDir = vertexNormalInput.normalWS;
5、灯光方向和灯光颜色
_WorldSpaceLightPos0.xyz改成
在vert函数里:
o.shadowCoord = GetShadowCoord(vertexInput);
frag函数里:
Light mainLight = mainLight = GetMainLight(i.shadowCoord);
float3 lightDirection = mainLight.direction;
_LightColor0 改成 mainLight.color
6、把fixed 定义的变量都改成float 的。
比如 fixed4 (finalColor,1)改成float4 (finalColor,1)
7、阴影
参考以下的文章:
8、深度图
具体可参考
9、_GrabTexture
URP管线下,截图不再使用GrabTexture,而是使用_CameraOpaqueTexture或_CameraColorTexture
删掉GrabPass{ }
sampler2D _GrabTexture; 改成 SAMPLER(_CameraOpaqueTexture);
同时,还得开启URP管线设置的Opaque Texture。
具体可参考
Unity URP管线如何截屏,及热扰动(热扭曲)效果的实现
10、"LightMode" = "ForwardAdd"的pass已经不能用了。
直接删掉该pass即可。
如果想使用多pass,可以参考以下文章:
如何在Unity的URP下使用多pass(multi pass)
借鉴:https://zhuanlan.zhihu.com/p/391437161
unity官方修改教学:【Unity教程】将自定义shader转换到URP_哔哩哔哩_bilibili