Unity 在URP中将shader更新为了HLSL语言,使用build-in shader 无法直接在URP中使用
这里讲一下关于shader的更新方法
参考 From Built-in to URP
更新步骤
-
Tags
添加"RenderPipeline" = "UniversalPipeline"
-
CGPROGRAM
ENDCG
改变为HLSLPROGRAM
ENDHLSL
-
#include "UnityCG.cginc"
更改为#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
-
fixed4
类型更改为half4
-
函数替换对照
Build-in RP | URP |
---|---|
UnityObjectToClipPos(v.vertex); | TransformObjectToHClip(v.vertex.xyz); |
mul(unity_ObjectToWorld,v.vertex).xyz | TransformObjectToWorld(v.vertex.xyz) |
mul(float4(v.normal, 0.0),unity_WorldToObject).xyz /UnityObjectToWorldNormal(v.normal) | TransformObjectToWorldNormal(v.normal) |
代码
上代码 一个简单的uv流动shader
内置渲染管线
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
Shader "Custom/liuguang"
{
Properties
{
_FlowTex("Texture", 2D) = "white" {}
_FlowTilling("Flow Tilling",Vector) = (1,1,0,0)
_FlowSpeed("Flow Speed",Vector) = (1,1,0,0)
}
SubShader
{
Tags { "Queue" = "Transparent" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
float3 pos_world : TEXCOORD1;
float3 normal_world : TEXCOORD2;
float3 pivot_world : TEXCOORD3;
};
sampler2D _FlowTex;
float4 _FlowTilling;
float4 _FlowSpeed;
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float3 normal_world = mul(float4(v.normal, 0.0),unity_WorldToObject).xyz;
o.normal_world = normalize(normal_world);
o.pos_world = mul(unity_ObjectToWorld,v.vertex).xyz;
o.pivot_world = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
o.uv = v.texcoord;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
half2 uv_flow = (i.pos_world.xy - i.pivot_world.xy) * _FlowTilling.xy;
uv_flow = uv_flow + _Time.y * _FlowSpeed.xy;
float4 flow_rgba = tex2D(_FlowTex, uv_flow);
return flow_rgba;
}
ENDCG
}
}
}
URP文章来源:https://www.toymoban.com/news/detail-451440.html
Shader "Custom/flowlight"
{
Properties
{
_FlowTex("Texture", 2D) = "white" {}
_FlowTilling("Flow Tilling",Vector) = (1,1,0,0)
_FlowSpeed("Flow Speed",Vector) = (1,1,0,0)
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
LOD 100
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct appdata
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
float3 pos_world : TEXCOORD1;
float3 normal_world : TEXCOORD2;
float3 pivot_world : TEXCOORD3;
};
sampler2D _FlowTex;
float4 _FlowTilling;
float4 _FlowSpeed;
v2f vert(appdata v)
{
v2f o;
o.pos = TransformObjectToHClip(v.vertex.xyz);
float3 normal_world = TransformObjectToWorldNormal(v.normal);
o.normal_world = normalize(normal_world);
o.pos_world = TransformObjectToWorld(v.vertex.xyz);
o.pivot_world = TransformObjectToWorld(float3(0,0,0));
o.uv = v.texcoord;
return o;
}
half4 frag(v2f i) : SV_Target
{
half2 uv_flow = (i.pos_world.xy - i.pivot_world.xy) * _FlowTilling.xy;
uv_flow = uv_flow + _Time.y * _FlowSpeed.xy;
float4 flow_rgba = tex2D(_FlowTex, uv_flow);
return flow_rgba;
}
ENDHLSL
}
}
}
多pass的情况
对于跟光照相关的shader ,迁移到urp可能会只执行第一个pass
需要通过lightmode 锁定到pass上
urp最多支持三个pass ,按如下的顺序为pass 添加tag即可Tags{"LightMode" = "SRPDefaultUnlit"}
Tags{"LightMode" = "UniversalForward"}
Tags{"LightMode" = "LightweightForward"}
文章来源地址https://www.toymoban.com/news/detail-451440.html
到了这里,关于Unity Shader从内置渲染管线迁移到URP的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!