Files
QuizzinMk5.1/Packages/com.z3y.vrcmarkerpro/Runtime/Shaders/Marker Mesh.shader
2025-12-03 01:44:40 +00:00

210 lines
7.1 KiB
GLSL

Shader "Hidden/VRCMarker Internal/Mesh"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
[NoScaleOffset] _MainTex ("Desaturated Albedo (R), Color Mask B, Smoothness A", 2D) = "white" {}
_Decal ("Decal (RGB), Mask (A)", 2D) = "black" {}
//[NoScaleOffset] _Data("Occlusion G, Color Mask B, Smoothness A", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_AccentSaturation ("Saturation", Range(0,1)) = 0.8
[Space(10)]
[Toggle(_LTCGI)] _LTCGI ("Enable LTCGI", Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" "LTCGI"="_LTCGI"}
LOD 200
CGPROGRAM
#pragma surface surf Standard addshadow
#pragma target 4.5
#pragma shader_feature_local _LTCGI
#ifdef SHADER_API_MOBILE
#undef _LTCGI
#endif
#ifndef SHADER_TARGET_SURFACE_ANALYSIS
#if defined(_LTCGI)
#include "Packages/at.pimaker.ltcgi/Shaders/LTCGI.cginc"
#endif
#endif
float GSAA(float3 worldNormal, float perceptualRoughness)
{
// Kaplanyan 2016, "Stable specular highlights"
// Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing"
// Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing"
// This implementation is meant for deferred rendering in the original paper but
// we use it in forward rendering as well (as discussed in Tokuyoshi and Kaplanyan
// 2019). The main reason is that the forward version requires an expensive transform
// of the half vector by the tangent frame for every light. This is therefore an
// approximation but it works well enough for our needs and provides an improvement
// over our original implementation based on Vlachos 2015, "Advanced VR Rendering".
float3 du = ddx(worldNormal);
float3 dv = ddy(worldNormal);
float variance = 0.15 * (dot(du, du) + dot(dv, dv));
float roughness = perceptualRoughness * perceptualRoughness;
float kernelRoughness = min(2.0 * variance, 0.1);
float squareRoughness = saturate(roughness * roughness + kernelRoughness);
return sqrt(sqrt(squareRoughness));
}
struct Gradient
{
int type;
int colorsLength;
half4 colors[8];
};
Gradient NewGradient(int type, int colorsLength, half4 colors[8])
{
Gradient output =
{
type, colorsLength, colors
};
return output;
}
half3 EvaluateGradient(Gradient gradient, half time)
{
half3 color = gradient.colors[0].rgb;
[unroll(6)]
for (int c = 1; c < gradient.colorsLength; c++)
{
half colorPos = saturate((time - gradient.colors[c - 1].w) / (gradient.colors[c].w - gradient.colors[c - 1].w)) * step(c, gradient.colorsLength - 1);
color = lerp(color, gradient.colors[c].rgb, lerp(colorPos, step(0.01, colorPos), gradient.type));
}
return color;
}
struct Input
{
float2 uv_MainTex;
float2 uv2_MainTex;
float4 color : COLOR;
float3 worldNormal; INTERNAL_DATA
#ifdef _LTCGI
float2 uv2_LightMap;
float3 worldPos;
#endif
};
sampler2D _MainTex;
sampler2D _Decal;
float4 _Decal_ST;
half _AccentSaturation;
half _Glossiness;
//SamplerState sampler_MainTex;
//SamplerState sampler_MetallicGlossMap;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(half3, _Color)
UNITY_INSTANCING_BUFFER_END(Props)
half4 _Gradient[8];
uint _GradientLength;
float3 get_camera_pos() {
float3 worldCam;
worldCam.x = unity_CameraToWorld[0][3];
worldCam.y = unity_CameraToWorld[1][3];
worldCam.z = unity_CameraToWorld[2][3];
return worldCam;
}
float3 F_Schlick(float u, float3 f0, float f90)
{
return f0 + (f90 - f0) * pow(1.0 - u, 5.0);
}
float _WritePosition;
#ifndef SHADER_TARGET_SURFACE_ANALYSIS
#include "Line.cginc"
#endif
void surf (Input IN, inout SurfaceOutputStandard o)
{
half4 dataTex = tex2D(_MainTex, IN.uv_MainTex);
half4 decal = tex2D(_Decal, IN.uv_MainTex * _Decal_ST.xy + _Decal_ST.zw);
half3 color = UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
half3 albedo = dataTex.r;
half3 colorMask = dataTex.b;
UNITY_BRANCH
if (_GradientLength > 0)
{
Gradient g = NewGradient(0, _GradientLength, _Gradient);
color = EvaluateGradient(g, IN.uv2_MainTex.y);
}
half grayscale = dot(color, float3(0.2125, 0.7154, 0.0721));
half3 desaturatedColor = lerp(grayscale, color, _AccentSaturation);
albedo = lerp(albedo, desaturatedColor, colorMask);
albedo = lerp(albedo, decal.rgb, decal.a);
o.Albedo = albedo;
o.Metallic = 0;
float rawSmoothness = dataTex.a;
o.Smoothness = (1.0f - GSAA(IN.worldNormal, 1.0f - dataTex.a)) * _Glossiness;
o.Alpha = 1;
#ifndef SHADER_TARGET_SURFACE_ANALYSIS
float positionCount = _WritePosition;
half inkLevelMask = ((IN.uv2_MainTex.y) > 0) * (1-colorMask);
half inkLevel = (IN.uv2_MainTex.y) > (positionCount / COUNT);
o.Albedo += lerp(positionCount >= COUNT ? half3(1,0,0) : 0.4, 1, inkLevel) * inkLevelMask * 0.25;
#endif
#ifndef SHADER_TARGET_SURFACE_ANALYSIS
#ifdef _LTCGI
{
float3 normal = WorldNormalVector(IN, o.Normal);
float3 spec = 0, diff = 0;
float3 viewDir = normalize(get_camera_pos() - IN.worldPos);
float NoV = saturate(dot(normal, viewDir));
float f0 = 0.16 * 0.5 * 0.5;
float fr = F_Schlick(NoV, f0, 1);
LTCGI_Contribution(
IN.worldPos,
normalize(normal),
viewDir,
1 - o.Smoothness,
IN.uv2_LightMap,
diff,
spec
);
o.Emission += spec * fr * UNITY_PI * dataTex.a;
o.Emission += diff * o.Albedo;
}
#endif
#endif
}
ENDCG
}
FallBack "Diffuse"
}