URP透明效果
该节我们会实现URP下透明度测试、透明度混合和开启深度写入的透明度混合效果
本系列URP不再阐述具体的效果实现逻辑与公式推导,侧重于URP下对《Shader入门精要》中Demo的复刻。如果对该Shader实现原理层面不太了解,建议移步我之前对《Shader入门精要》一书的学习笔记博客。在此感谢该书作者冯乐乐女神为我们这些新手铺垫了求学之路。
透明度测试
效果图
使用到的语法
-
在Tags中声明渲染类型为镂空
"RenderType" = "TransparentCutout"
-
声明渲染路径为前向渲染
Tags { "LightMode" = "UniversalForward" }
-
clip用于剔除透明度低于参数的像素
clip(texColor.a - _Cutoff);
完整代码
Shader "URP/AlphaTestWithBothSide"
{
Properties
{
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" { }
_Cutoff ("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
}
SubShader
{
// 指定渲染通道使用URP渲染管线 渲染队列 = 透明度测试 忽略投影 渲染类型 = 透明镂空
Tags { "RenderPipeline" = "UniversalRenderPipeline" "Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" }
Pass
{
Tags { "LightMode" = "UniversalForward" }
// 关闭渲染剔除
Cull Off
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// 引用URP函数库
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
// 声明纹理
TEXTURE2D(_MainTex);
// 声明采样器
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
half4 _Color;
float4 _MainTex_ST;
half _Cutoff;
CBUFFER_END
struct a2v
{
float4 vertex: POSITION;
float3 normal: NORMAL;
float4 texcoord: TEXCOORD0;
};
struct v2f
{
float4 pos: SV_POSITION;
float3 worldNormal: TEXCOORD0;
float3 worldPos: TEXCOORD1;
float2 uv: TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
// 初始化变量
ZERO_INITIALIZE(v2f, o);
o.pos = TransformObjectToHClip(v.vertex.xyz);
o.worldNormal = TransformObjectToWorldNormal(v.normal);
o.worldPos = TransformObjectToWorld(v.vertex.xyz);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
half4 frag(v2f i): SV_Target
{
half3 worldNormal = normalize(i.worldNormal);
half3 worldLightDir = normalize(TransformObjectToWorldDir(_MainLightPosition.xyz));
half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
// 使用clip函数剔除透明像素
clip(texColor.a - _Cutoff);
half3 albedo = texColor.rgb * _Color.rgb;
half3 ambient = _GlossyEnvironmentColor * albedo;
half3 diffuse = _MainLightColor.rgb * albedo * max(0, dot(worldNormal, worldLightDir));
return half4(ambient + diffuse, 1.0);
}
ENDHLSL
}
}
FallBack "Packages/com.unity.render-pipelines.universal/SimpleLit"
}
透明度混合双面渲染
效果
使用到的语法
暂无新语法
完整代码
与CG实现下不同的是,在URP下无需用双Pass进行正反面渲染,直接关闭渲染剔除即可实现对透明物体的双面渲染
Shader "URP/AlphaBlendWithBothSide"
{
Properties
{
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" { }
_AlphaScale ("Alpha Scale", Range(0, 1)) = 1
}
SubShader
{
// 指定渲染通道使用URP渲染管线 渲染队列 = 透明度混合 忽略投影 渲染类型 = 透明物体
Tags { "RenderPipeline" = "UniversalRenderPipeline" "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Pass
{
Tags { "LightMode" = "UniversalForward" }
// 关闭渲染剔除
Cull Off
// 关闭深度写入
ZWrite Off
// 混合因子设置
Blend SrcAlpha OneMinusSrcAlpha
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// 引用URP函数库
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
// 声明纹理
TEXTURE2D(_MainTex);
// 声明采样器
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
half4 _Color;
float4 _MainTex_ST;
half _AlphaScale;
CBUFFER_END
struct a2v
{
float4 vertex: POSITION;
float3 normal: NORMAL;
float4 texcoord: TEXCOORD0;
};
struct v2f
{
float4 pos: SV_POSITION;
float3 worldNormal: TEXCOORD0;
float3 worldPos: TEXCOORD1;
float2 uv: TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
// 初始化变量
ZERO_INITIALIZE(v2f, o);
o.pos = TransformObjectToHClip(v.vertex.xyz);
o.worldNormal = TransformObjectToWorldNormal(v.normal);
o.worldPos = TransformObjectToWorld(v.vertex.xyz);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
half4 frag(v2f i): SV_Target
{
half3 worldNormal = normalize(i.worldNormal);
half3 worldLightDir = normalize(TransformObjectToWorldDir(_MainLightPosition.xyz));
half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
half3 albedo = texColor.rgb * _Color.rgb;
half3 ambient =_GlossyEnvironmentColor.xyz * albedo;
half3 diffuse = _MainLightColor.rgb * albedo * max(0, dot(worldNormal, worldLightDir));
return half4(ambient + diffuse, texColor.a * _AlphaScale);
}
ENDHLSL
}
}
FallBack "Packages/com.unity.render-pipelines.universal/SimpleLit"
}
开启深度写入的透明度混合
效果
使用到的语法
暂无新语法
完整代码
与CG实现下不同的是,在URP下无需额外增加一个Pass去单独渲染深度缓冲区,可以直接开启深度写入
Shader "URP/AlphaBlendingWithZWrite"
{
Properties
{
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" { }
_AlphaScale ("Alpha Scale", Range(0, 1)) = 1
}
SubShader
{
// 指定渲染通道使用URP渲染管线 渲染队列 = 透明度混合 忽略投影 = Ture 渲染类型 = 透明物体
Tags { "RenderPipeline" = "UniversalRenderPipeline" "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
/*
// 添加额外的Pass,仅用于渲染到深度缓冲区
Pass
{
// 开启深度写入
ZWrite On
// 用于控制Pass不写入任何颜色通道
ColorMask 0
}
*/
Pass
{
Tags { "LightMode" = "UniversalForward" }
// 关闭渲染剔除
Cull Off
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// 引用URP函数库
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
// 声明纹理
TEXTURE2D(_MainTex);
// 声明采样器
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
half4 _Color;
float4 _MainTex_ST;
half _AlphaScale;
CBUFFER_END
struct a2v
{
float4 vertex: POSITION;
float3 normal: NORMAL;
float4 texcoord: TEXCOORD0;
};
struct v2f
{
float4 pos: SV_POSITION;
float3 worldNormal: TEXCOORD0;
float3 worldPos: TEXCOORD1;
float2 uv: TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
// 初始化变量
ZERO_INITIALIZE(v2f, o);
o.pos = TransformObjectToHClip(v.vertex.xyz);
o.worldNormal = TransformObjectToWorldNormal(v.normal);
o.worldPos = TransformObjectToWorld(v.vertex.xyz);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
half4 frag(v2f i): SV_Target
{
half3 worldNormal = normalize(i.worldNormal);
half3 worldLightDir = normalize(TransformObjectToWorldDir(_MainLightPosition.xyz));
half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
half3 albedo = texColor.rgb * _Color.rgb;
half3 ambient = _GlossyEnvironmentColor.xyz * albedo;
half3 diffuse = _MainLightColor.rgb * albedo * max(0, dot(worldNormal, worldLightDir));
return half4(ambient + diffuse, texColor.a * _AlphaScale);
}
ENDHLSL
}
}
FallBack "Packages/com.unity.render-pipelines.universal/SimpleLit"
}
网友评论