Last active
January 6, 2025 12:30
-
-
Save Forenard/1d7e4fe20ea71b69f7d2cbb5982dcc85 to your computer and use it in GitHub Desktop.
FULLSCREEN LUT COLORGRADING SHADER FOR VRCHAT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "SFX/LUT" | |
{ | |
Properties | |
{ | |
[NoScaleOffset]_LUTTex ("LUT Texture", 2D) = "white" { } | |
} | |
SubShader | |
{ | |
Tags { "RenderType" = "Overlay" "Queue" = "Overlay" "ForceNoShadowCasting" = "True" "IgnoreProjector" = "True" "PreviewType" = "Plane" "DisableBatching" = "True" } | |
Cull Front | |
ZWrite Off | |
ZTest Always | |
GrabPass | |
{ | |
Tags { "LightMode" = "ForwardBase" } | |
"_SFXGrabTex" | |
} | |
Pass | |
{ | |
Tags { "LightMode" = "ForwardBase" } | |
CGPROGRAM | |
#pragma target 5.0 | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float4 pos : SV_POSITION; | |
float4 uv : TEXCOORD0; | |
}; | |
sampler2D _SFXGrabTex; | |
sampler2D _LUTTex; | |
float4 _LUTTex_TexelSize; | |
#define _LUT_Res (_LUTTex_TexelSize.zw) | |
// from: https://github.com/MochiesCode/Mochies-Unity-Shaders/blob/master/Mochie/Common/Utilities.cginc | |
float4 GetScreenspaceVertexPos(float4 vertex) | |
{ | |
vertex.x *= 1.4;// ??????????????????????????????? | |
#if UNITY_SINGLE_PASS_STEREO || defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED) | |
float ipd = length(mul(unity_WorldToObject, | |
float4(unity_StereoWorldSpaceCameraPos[0].xyz - unity_StereoWorldSpaceCameraPos[1].xyz, 0))); | |
float4 absPos = vertex + float4(ipd * (0.5 - unity_StereoEyeIndex), 0, 0, 0); | |
#else | |
float ipd = 0.0; | |
float4 absPos = vertex; | |
#endif | |
float4 wPos = mul(unity_CameraToWorld, absPos); | |
float4 oPos = mul(unity_WorldToObject, wPos); | |
return UnityObjectToClipPos(oPos); | |
} | |
v2f vert(appdata v) | |
{ | |
v2f o; | |
UNITY_INITIALIZE_OUTPUT(v2f, o); | |
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); | |
o.pos = GetScreenspaceVertexPos(v.vertex); | |
o.uv = ComputeGrabScreenPos(o.pos); | |
return o; | |
} | |
// from: https://github.com/Unity-Technologies/PostProcessing/blob/v2/PostProcessing/Shaders/Colors.hlsl | |
float3 ApplyLut2D(float3 uvw) | |
{ | |
float3 scaleOffset = float3(1.0 / _LUT_Res.x, 1.0 / _LUT_Res.y, _LUT_Res.y - 1.0); | |
// Strip format where `height = sqrt(width)` | |
uvw.z *= scaleOffset.z; | |
float shift = floor(uvw.z); | |
uvw.xy = uvw.xy * scaleOffset.z * scaleOffset.xy + scaleOffset.xy * 0.5; | |
uvw.x += shift * scaleOffset.y; | |
// I think sampler_LinearClamp | |
uvw.xyz = lerp( | |
tex2D(_LUTTex, uvw.xy).rgb, | |
tex2D(_LUTTex, uvw.xy + float2(scaleOffset.y, 0.0)).rgb, | |
uvw.z - shift | |
); | |
return uvw; | |
} | |
float4 frag(v2f i) : SV_Target | |
{ | |
float2 uv = i.uv.xy / i.uv.w; | |
float4 col = tex2D(_SFXGrabTex, uv); | |
col.rgb = ApplyLut2D(col.rgb); | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment