我们展示我们Shader效果,一般放于棋盘格中来展示。我们在这篇文章中,制作棋盘格效果。
我们在这篇文章中,主要计算实现该效果
if(i.uv.x < 0.5)
col1 = 0;
else
col1 = 0.5;
if(i.uv.y < 0.5)
col2 = 0;
else
col2 = 0.5;
frac(col1 + col2)
return 2 * frac(col1 + col2);
float2 uv = floor(i.uv * 2) * 0.5;
return uv.x + uv.y;
float col = frac(uv.x +uv.y);
float col = frac(uv.x +uv.y) * 2;
_Repeat(“Repeat”,Float) = 0
CBUFFER_START(UnityPerMaterial)
float _Repeat;
CBUFFER_END
o.uv = v.uv * _Repeat;
因为我们的棋盘格一般为一个Cube。那么要看见内部的东西,则需要把面片的前面剔除
Cull Front
因为,我们棋盘格的颜色单一且过曝,看着很不舒服。所以,我们给其加一个遮罩,让其有点渐变的区分
float mask = i.vertexOS.y;
return col + mask;
col = col *_Color + mask;
我们需要适配BRP。所以,得加一个SubShader以同样的逻辑实现该效果
Shader "MyShader/URP/P3_3_3"
{
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
#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;
};
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;
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;
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
}
}
}