Files
2025-06-09 23:23:13 +08:00

176 lines
6.1 KiB
Plaintext

BEGIN_OPTIONS
Name "Hidden/MicroVerse/LitDev"
Stackable "False"
END_OPTIONS
BEGIN_SUBSHADERS
"Packages/com.jbooth.better-shaders/Samples/Stackables/Stackable_FoliageRendererInstancing.surfshader"
"Packages/com.jbooth.better-shaders/Samples/Stackables/Stackable_VertexWind.surfshader"
"Packages/com.jbooth.better-shaders/Samples/Stackables/Stackable_DoubleSided.surfshader"
END_SUBSHADERS
// Put any properties you have between the begin/end property blocks
BEGIN_PROPERTIES
[BetterHeader(Lit)]
_MainTex("Albedo/Height", 2D) = "white" {}
_Tint ("Tint", Color) = (1, 1, 1, 1)
[KeywordEnum(None, Cut, DitherFade)] _Alpha("Alpha Mode", Float) = 0
_DitherFade("Dither Fade Start/Distance", Vector) = (50, 50, 0, 0)
_AlphaThreshold("Alpha Threshold", Float) = 0.5
[Toggle(_BRUSHLIGHTING)] _BrushLighting ("Brush Lighting", Float) = 0
[Normal][NoScaleOffset]_NormalMap("Normal", 2D) = "bump" {}
_NormalStrength("Normal Strength", Range(0,2)) = 1
[Toggle(_MASKMAP)] _UseMaskMap ("Use Mask Map", Float) = 0
[NoScaleOffset]_MaskMap("Mask Map", 2D) = "black" {}
[Toggle(_EMISSION)] _UseEmission ("Use Emission Map", Float) = 0
[NoScaleOffset]_EmissionMap("Emission Map", 2D) = "black" {}
_EmissionStrength("Emission Strength", Range(0, 100)) = 1
_EmissionTint("Emission Tint", Color) = (1,1,1,1)
[Toggle(_DETAIL)] _UseDetail("Use Detail Map", Float) = 0
_DetailMap("Detail Map", 2D) = "bump" {}
_DetailAlbedoStrength("Detail Albedo Strength", Range(0, 2)) = 1
_DetailNormalStrength("Detail Normal Strength", Range(0, 2)) = 1
_DetailSmoothnessStrength("Detail Smoothness Strength", Range(0, 2)) = 1
END_PROPERTIES
// Any variables you want to have in the per material CBuffer go here.
BEGIN_CBUFFER
half4 _Tint;
float4 _MainTex_ST;
float4 _DetailMap_ST;
half _NormalStrength;
half _EmissionStrength;
half _DetailAlbedoStrength;
half _DetailNormalStrength;
half _DetailSmoothnessStrength;
half _AlphaThreshold;
float2 _DitherFade;
half4 _EmissionTint;
END_CBUFFER
BEGIN_DEFINES
#pragma shader_feature_local_fragment _ _MASKMAP
#pragma shader_feature_local_fragment _ _DETAIL
#pragma shader_feature_local_fragment _ _EMISSION
#pragma shader_feature_local_fragment _ _ALPHA_CUT _ALPHA_DITHERFADE
#pragma shader_feature_local_fragment _ _BRUSHLIGHTING
END_DEFINES
// All code goes here
BEGIN_CODE
float Dither8x8Bayer( int x, int y )
{
const float dither[ 64 ] = {
1, 49, 13, 61, 4, 52, 16, 64,
33, 17, 45, 29, 36, 20, 48, 32,
9, 57, 5, 53, 12, 60, 8, 56,
41, 25, 37, 21, 44, 28, 40, 24,
3, 51, 15, 63, 2, 50, 14, 62,
35, 19, 47, 31, 34, 18, 46, 30,
11, 59, 7, 55, 10, 58, 6, 54,
43, 27, 39, 23, 42, 26, 38, 22};
int r = y * 8 + x;
return dither[r] / 64;
}
float DitheredAlpha(half alpha, ShaderData d)
{
#if _ALPHA_DITHERFADE
float4 screenPosNorm = d.screenPos / d.screenPos.w;
screenPosNorm.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? screenPosNorm.z : screenPosNorm.z * 0.5 + 0.5;
float2 clipScreen = screenPosNorm.xy * _ScreenParams.xy;
float dither = Dither8x8Bayer( fmod(clipScreen.x, 8), fmod(clipScreen.y, 8) );
return (alpha - dither);
#endif
}
half3 LitBlendDetailNormal3(half3 n1, half3 n2)
{
return normalize(half3(n1.xy + n2.xy, n1.z*n2.z));
}
// We share samplers with the albedo - which free's up more for stacking.
// Note that you can use surface shader style texture/sampler declarations here as well.
// They have been emulated in HDRP/URP, however, I think using these is nicer than the
// old surface shader methods.
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex); // naming this way associates it with the sampler properties from the albedo map
TEXTURE2D(_NormalMap);
SAMPLER(sampler_NormalMap);
TEXTURE2D(_MaskMap);
TEXTURE2D(_EmissionMap);
TEXTURE2D(_DetailMap);
void SurfaceFunction(inout Surface o, inout ShaderData d)
{
float2 uv = d.texcoord0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
half4 c = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
#if _ALPHA_CUT
clip(c.a - _AlphaThreshold);
#elif _ALPHA_DITHERFADE
float fadeDist = distance(_WorldSpaceCameraPos, d.worldSpacePosition);
float dither = c.a - _AlphaThreshold;
if (fadeDist > _DitherFade.x)
{
fadeDist -= _DitherFade.x;
fadeDist = 1.0 - saturate(fadeDist/max(_DitherFade.y, 0.001));
dither = DitheredAlpha(c.a * fadeDist, d);
}
clip(dither);
#endif
#if _BRUSHLIGHTING
d.worldSpaceNormal = lerp(d.worldSpaceNormal, float3(0,1,0), 0.5);
d.TBNMatrix = float3x3(d.worldSpaceTangent, cross(d.worldSpaceNormal, d.worldSpaceTangent), d.worldSpaceNormal);
#endif
o.Albedo = c.rgb * _Tint.rgb;
o.Height = c.a;
o.Normal = UnpackScaleNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, uv), _NormalStrength);
o.Normal *= saturate(c.a + 999);
half detailMask = 1;
#if _MASKMAP
// Unity mask map format (R) Metallic, (G) Occlusion, (B) Detail Mask (A) Smoothness
half4 mask = SAMPLE_TEXTURE2D(_MaskMap, sampler_MainTex, uv);
o.Metallic = mask.r;
o.Occlusion = mask.g;
o.Smoothness = mask.a;
detailMask = mask.b;
#endif // separate maps
half3 emission = 0;
#if defined(_EMISSION)
o.Emission = SAMPLE_TEXTURE2D(_EmissionMap, sampler_MainTex, uv).rgb * _EmissionTint.rgb * _EmissionStrength;
#endif
#if defined(_DETAIL)
float2 detailUV = uv * _DetailMap_ST.xy + _DetailMap_ST.zw;
half4 detailSample = SAMPLE_TEXTURE2D(_DetailMap, sampler_MainTex, detailUV);
o.Normal = LitBlendDetailNormal3(o.Normal, UnpackScaleNormal(detailSample, _DetailNormalStrength * detailMask));
o.Albedo = lerp(o.Albedo, o.Albedo * 2 * detailSample.x, detailMask * _DetailAlbedoStrength);
o.Smoothness = lerp(o.Smoothness, o.Smoothness * 2 * detailSample.z, detailMask * _DetailSmoothnessStrength);
#endif
o.Alpha = c.a;
}
END_CODE