Files
2026-03-27 16:34:08 +09:00

176 lines
8.2 KiB
GLSL

Shader "MesshingAround/Stylized_Glass (Built-in)"
{
Properties
{
[Header(Surface Options)]
[Header(Outer Rim Light)]
_OuterRimpower("OuterRimpower", Range(0, 1)) = 0.015
_OuterRimSmoothness("OuterRimSmoothness", Range(0, 1)) = 0
_RimColor("RimColor", Color) = (0.7568628, 0.7568628, 0.7568628, 1)
[Header(Inner Rim Light)]
_InnerRimPower("InnerRimPower", Range(0, 0.5)) = 0.131
_InnerRimSmoothness("InnerRimSmoothness", Range(0, 0.5)) = 0.1
_InnerRimColor("InnerRimColor", Color) = (1, 1, 1, 1)
[Header(Glass Color)]
_Alpha("Alpha", Range(0, 1)) = 0
_MainTintColor("MainTintColor", Color) = (1, 1, 1, 0.1411765)
_GradientIndex("GradientIndex", Float) = 0
_GradientCount("GradientCount", Float) = 110
[NoScaleOffset]_GradientTexture("GradientTexture", 2D) = "white" {}
[Header(Specular Light)]
_LightCutoff("LightCutoff", Range(0, 1)) = 0
_LightCutoffSmoothness("LightCutoffSmoothness", Range(0, 1)) = 0.249
_ViewSpecCutoff("ViewSpecCutoff", Range(0, 1)) = 0.014
_SpecularColor("SpecularColor", Color) = (1, 1, 1, 1)
// Hidden properties
[HideInInspector][NoScaleOffset]_GrayscaleTex("GrayscaleTexture", 2D) = "white" {}
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True"}
LOD 200
// Match URP's Alpha blending
Blend One OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
// Unlit shader to match URP Unlit behavior
#pragma surface surf Unlit alpha:blend noshadow noambient novertexlights nolightmap nodynlightmap nodirlightmap nofog nometa noforwardadd
#pragma target 3.0
#pragma multi_compile_instancing
sampler2D _GrayscaleTex;
sampler2D _GradientTexture;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float, _OuterRimpower)
UNITY_DEFINE_INSTANCED_PROP(float, _OuterRimSmoothness)
UNITY_DEFINE_INSTANCED_PROP(float4, _RimColor)
UNITY_DEFINE_INSTANCED_PROP(float, _InnerRimPower)
UNITY_DEFINE_INSTANCED_PROP(float, _InnerRimSmoothness)
UNITY_DEFINE_INSTANCED_PROP(float4, _InnerRimColor)
UNITY_DEFINE_INSTANCED_PROP(float, _Alpha)
UNITY_DEFINE_INSTANCED_PROP(float4, _MainTintColor)
UNITY_DEFINE_INSTANCED_PROP(float, _GradientIndex)
UNITY_DEFINE_INSTANCED_PROP(float, _GradientCount)
UNITY_DEFINE_INSTANCED_PROP(float, _LightCutoff)
UNITY_DEFINE_INSTANCED_PROP(float, _LightCutoffSmoothness)
UNITY_DEFINE_INSTANCED_PROP(float, _ViewSpecCutoff)
UNITY_DEFINE_INSTANCED_PROP(float4, _SpecularColor)
UNITY_INSTANCING_BUFFER_END(Props)
// Unlit lighting model (no lighting)
half4 LightingUnlit(SurfaceOutput s, half3 lightDir, half atten)
{
return half4(s.Albedo, s.Alpha);
}
struct Input
{
float2 uv_GrayscaleTex;
float3 worldNormal;
float3 viewDir;
float3 worldPos;
};
// Helper function: Sample LUT
float4 SampleLUT(float2 uv, float gradientIndex, float gradientCount)
{
float grayscale = tex2D(_GrayscaleTex, uv).r;
float v = (round(gradientIndex) + 0.5) / gradientCount;
float2 lutUV = float2(grayscale, v);
return tex2D(_GradientTexture, lutUV);
}
void surf (Input IN, inout SurfaceOutput o)
{
// Access instanced properties
float outerRimPower = UNITY_ACCESS_INSTANCED_PROP(Props, _OuterRimpower);
float outerRimSmoothness = UNITY_ACCESS_INSTANCED_PROP(Props, _OuterRimSmoothness);
float4 rimColor = UNITY_ACCESS_INSTANCED_PROP(Props, _RimColor);
float innerRimPower = UNITY_ACCESS_INSTANCED_PROP(Props, _InnerRimPower);
float innerRimSmoothness = UNITY_ACCESS_INSTANCED_PROP(Props, _InnerRimSmoothness);
float4 innerRimColor = UNITY_ACCESS_INSTANCED_PROP(Props, _InnerRimColor);
float alpha = UNITY_ACCESS_INSTANCED_PROP(Props, _Alpha);
float4 mainTintColor = UNITY_ACCESS_INSTANCED_PROP(Props, _MainTintColor);
float gradientIndex = UNITY_ACCESS_INSTANCED_PROP(Props, _GradientIndex);
float gradientCount = UNITY_ACCESS_INSTANCED_PROP(Props, _GradientCount);
float lightCutoff = UNITY_ACCESS_INSTANCED_PROP(Props, _LightCutoff);
float lightCutoffSmoothness = UNITY_ACCESS_INSTANCED_PROP(Props, _LightCutoffSmoothness);
float viewSpecCutoff = UNITY_ACCESS_INSTANCED_PROP(Props, _ViewSpecCutoff);
float4 specularColor = UNITY_ACCESS_INSTANCED_PROP(Props, _SpecularColor);
// Calculate vectors
float3 worldNormal = normalize(IN.worldNormal);
float3 worldViewDir = normalize(IN.viewDir);
float3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
// Object space conversions
float3 objNormal = normalize(mul(worldNormal, (float3x3)unity_ObjectToWorld));
float3 objViewDir = normalize(mul((float3x3)unity_WorldToObject, worldViewDir));
float NdotV = saturate(dot(worldNormal, worldViewDir));
// === LUT COLOR SYSTEM ===
float4 lutColor = SampleLUT(IN.uv_GrayscaleTex, gradientIndex, gradientCount);
float4 lutWithAlpha = lutColor * alpha;
float4 baseColor = mainTintColor * lutWithAlpha;
// === OUTER RIM (Fresnel) ===
float outerRimPowerInverted = 1.0 - outerRimPower;
float fresnelOuter = pow(1.0 - NdotV, outerRimPowerInverted);
float outerEdge = 0.5 + outerRimSmoothness;
float outerRimMask = smoothstep(0.5, outerEdge, fresnelOuter);
float3 outerRimEmission = outerRimMask * rimColor.rgb;
// === INNER RIM (Fresnel from inside) ===
float fresnelInner = pow(1.0 - NdotV, innerRimPower);
float fresnelInnerInverted = 1.0 - fresnelInner;
float innerEdgeMin = 0.5 - innerRimSmoothness;
float innerRimMask = smoothstep(innerEdgeMin, 0.5, fresnelInnerInverted);
float3 innerRimEmission = innerRimMask * innerRimColor.rgb;
// === SPECULAR SYSTEM ===
float NdotL = dot(worldLightDir, worldNormal);
float lightSpec = smoothstep(0.5, 0.55, NdotL);
float3 lightMinusView = worldLightDir - objViewDir;
float combinedDot1 = dot(lightMinusView, objNormal);
float lightCutoffEdge = lightCutoff + lightCutoffSmoothness;
float combinedSpec1 = smoothstep(lightCutoff, lightCutoffEdge, combinedDot1);
float combinedDot2 = dot(objViewDir, objNormal);
float viewSpecInverted = 1.0 - viewSpecCutoff;
float viewSpecEdge = viewSpecInverted + lightCutoffSmoothness;
float combinedSpec2 = smoothstep(viewSpecInverted, viewSpecEdge, combinedDot2);
float totalCombinedSpec = combinedSpec1 + combinedSpec2;
float3 finalSpecular = specularColor.rgb * (lightSpec * totalCombinedSpec);
// === COMBINE - UNLIT LIKE URP ===
// URP combines EVERYTHING (including alpha channels) then saturates
// CRITICAL: InnerRim must contribute to alpha to be visible when baseColor alpha is low
float4 combinedColor = baseColor + float4(finalSpecular, 0) + float4(outerRimEmission, outerRimMask) + float4(innerRimEmission, innerRimMask);
float4 saturatedResult = saturate(combinedColor);
// Use the RGBA from the saturated result (like URP does)
float3 finalRGB = saturatedResult.rgb;
float finalAlpha = saturatedResult.a;
// Set surface properties (Unlit - no lighting)
o.Albedo = finalRGB;
o.Emission = float3(0, 0, 0);
o.Alpha = finalAlpha;
}
ENDCG
}
FallBack "Transparent/VertexLit"
}