写一个准管线中写的shader改成URP管线支持的shader, 的示例代码
时间: 2024-10-18 20:01:13 浏览: 29
在Unity引擎中,从经典管线(Legacy Pipeline)升级到Universal Render Pipeline (URP) 或 High Definition Render Pipeline (HDRP) 需要对Shader编写做一些调整。下面是将一个经典管线的简单顶点着色器(Vertex Shader)转换为URP兼容版本的一个示例:
经典管线(Legacy Shader)的Vertex Shader:
```glsl
Shader "Custom/LegacyVS"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 color : COLOR;
};
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.color = v.color;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return i.color;
}
ENDCG
}
}
}
```
转换为URP的Shader(Universal Render Pipeline支持):
```hlsl
Shader "Custom/URPVS"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Opaque"}
LOD 200
PSScript
{
Name "CustomFragment"
File "UberShaderAllPasses.cginc"
// 注入主纹理和颜色属性
Properties
{
[HideInInspector] _MainTex ("Base (RGB)", 2D) = "white" {}
[HideInInspector] _Color ("Color", Color) = (1,1,1,1)
}
耐光GI "_EmissiveStrength" = 1.0
耐光GI "_IndirectLighting" = 1.0
耐光GI "_IndirectSpecular" = 0.0
耐光GI "_AlbedoScale" = 1.0
耐光GI "_MetallicRoughnessScale" = 1.0
// 替换CGPROGRAM部分并开始脚本程序
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : SV_POSITION;
float4 texcoord : TEXCOORD0;
float4 color : COLOR;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 texcoord : TEXCOORD0;
float4 color : COLOR;
};
sampler2D _MainTex;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
o.color = v.color;
return o;
}
half4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord.xy) * i.color;
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
```
在这个例子中,我们保留了基本的结构,并添加了一些URP特有的设置和注释,如`Properties`块、`LOD`和`PSScript`部分。此外,我们导入了一个通用的脚本库文件(`UberShaderAllPasses.cginc`)。
阅读全文