Unity 从BuildIn升级到URP以及常用的效果

一、CG->HLSL

CGPROGRAM->HLSLPROGRAM

ENDCG->ENDHLSL

CGINCLUDE->HLSLINCLUDE

二、灯光模式

"LightMode"="ForwardBase->UniversalForward"

删掉以下内容:

#define UNITY_PASS_FORWARDBASE
#pragma multi_compile_fwdbase

URP可以使用的灯光模式,有三种:

"UniversalForward"
"LightweightForward"
“SRPDefaultUnlit"

三、一些常用的方法(修改)

1、把#include "UnityCG.cginc"

改成

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

2、把#include "Lighting.cginc"

改成

#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;

或者

o.pos = TransformObjectToHClip(v.vertex.xyz);

4、o.normalDir = UnityObjectToWorldNormal(v.normal);

改成

VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(v.normal);
o.normalDir = vertexNormalInput.normalWS;

或者

o.normalDir = TransformObjectToWorldNormal(v.normal);

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或者half 的。

比如 fixed4 (finalColor,1)改成float4 (finalColor,1)

四、阴影

如何在unity的URP下实现阴影_unity urp阴影-CSDN博客

五、深度图

Unity URP 获取深度图_urp采样深度图-CSDN博客

六、多Pass

如何在Unity的URP下使用多pass(multi pass)_urp 多pass-CSDN博客

七、雾效

1、添加雾效变体

#pragma multi_compile_fog

2、在Varying结构体中添加雾效因子

float fogCoord : TEXCOORD2;

3、在顶点着色器中,我们使用内置函数得到雾效因子

o.fogCoord = ComputeFogFactor(o.vertexCS.z);

4、在片元着色器中,把输出颜色 和 雾效因子混合输出

col.rgb = MixFog(col,i.fogCoord);

八、环境光

BuildIn

fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb

URP

half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w); 

九、GPUInstancing

Unity URP 中 GPU Instancing的使用_urp gpuinstance-CSDN博客 

猜你喜欢

转载自blog.csdn.net/m0_68267247/article/details/142978391