前言
我们使用之前的棋盘格作为测试Shader来添加雾效。
- Unity中 URP 下的棋盘格Shader
一、URP下Shader支持雾效的步骤
1、添加雾效变体
#pragma multi_compile_fog
2、在Varying结构体中添加雾效因子
float fogCoord : TEXCOORD2;
3、在顶点着色器中,我们使用内置函数得到雾效因子
o.fogCoord = ComputeFogFactor(o.vertexCS.z);
4、在片元着色器中,把输出颜色 和 雾效因子混合输出
col.rgb = MixFog(col,i.fogCoord);
二、在Unity中打开雾效
文章来源:https://www.toymoban.com/news/detail-857301.html
文章来源地址https://www.toymoban.com/news/detail-857301.html
三、测试代码
Shader "MyShader/URP/P3_8"
{
Properties
{
_Repeat("Repeat",Float) = 0
_Color("Color",Color) = (1,1,1,1)
}
SubShader
{
Tags
{
"PenderPipeline"="UniversalPipeline"
"RenderType"="Opaque"
"Queue"="Geometry"
}
Cull Front
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Attribute
{
float3 vertexOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varying
{
float3 vertexOS : TEXCOORD0;
float4 vertexCS : SV_POSITION;
float2 uv : TEXCOORD1;
float fogCoord : TEXCOORD2;
};
CBUFFER_START(UnityPerMaterial)
float _Repeat;
float4 _Color;
CBUFFER_END
Varying vert(Attribute v)
{
Varying o;
o.vertexOS = v.vertexOS;
o.vertexCS = TransformObjectToHClip(v.vertexOS);
o.uv = v.uv * _Repeat;
o.fogCoord = ComputeFogFactor(o.vertexCS.z);
return o;
}
half4 frag(Varying i) : SV_Target
{
/*half4 col1;
half4 col2;
if(i.uv.x < 0.5)
col1 = 0;
else
col1 = 0.5;
if(i.uv.y < 0.5)
col2 = 0;
else
col2 = 0.5;*/
half4 col;
float2 uv = floor(i.uv * 2) * 0.5;
col = frac(uv.x + uv.y) * 2;
float mask = i.vertexOS.y;
col = col * _Color + mask;
col.rgb = MixFog(col,i.fogCoord);
return col;
}
ENDHLSL
}
}
SubShader
{
Tags
{
"RenderType"="Opaque"
"Queue"="Geometry"
}
Cull Front
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float3 vertexOS : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float3 vertexOS : TEXCOORD0;
float4 vertexCS : SV_POSITION;
float2 uv : TEXCOORD1;
};
float _Repeat;
float4 _Color;
v2f vert(appdata v)
{
v2f o;
o.vertexOS = v.vertexOS;
o.vertexCS = UnityObjectToClipPos(v.vertexOS);
o.uv = v.uv * _Repeat;
return o;
}
half4 frag(v2f i) : SV_Target
{
half4 col;
float2 uv = floor(i.uv * 2) * 0.5;
col = frac(uv.x + uv.y) * 2;
float mask = i.vertexOS.y;
col = col * _Color + mask;
return col;
}
ENDCG
}
}
}
到了这里,关于Unity中URP下的添加雾效支持的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!