Skip to content

Commit

Permalink
cRaySchism: Finish color-grading component
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku authored Oct 26, 2024
2 parents 42686cf + 8e7b676 commit 7b31d1a
Show file tree
Hide file tree
Showing 2 changed files with 394 additions and 63 deletions.
198 changes: 135 additions & 63 deletions shaders/cRaySchism.fx
Original file line number Diff line number Diff line change
Expand Up @@ -100,59 +100,159 @@
#endif

uniform float _GradePostExposure <
ui_category = "Color Grading";
ui_category = "Color Grading | Color Adjustments";
ui_label = "Post Exposure";
ui_type = "drag";
> = 0.0;

uniform float _GradeContrast <
ui_category = "Color Grading";
ui_category = "Color Grading | Color Adjustments";
ui_label = "Contrast";
ui_type = "slider";
ui_min = -100.0;
ui_max = 100.0;
ui_min = -1.0;
ui_max = 1.0;
> = 0.0;

uniform float3 _GradeColorFilter <
ui_category = "Color Grading";
ui_category = "Color Grading | Color Adjustments";
ui_label = "Color Filter";
ui_type = "color";
ui_min = 0.0;
ui_max = 1.0;
> = 1.0;

uniform float _GradeHueShift <
ui_category = "Color Grading";
ui_category = "Color Grading | Color Adjustments";
ui_label = "Hue Shift";
ui_type = "slider";
ui_min = -180.0;
ui_max = 180.0;
> = 0.0;

uniform float _GradeSaturation <
ui_category = "Color Grading";
ui_category = "Color Grading | Color Adjustments";
ui_label = "Saturation";
ui_type = "slider";
ui_min = -100.0;
ui_max = 100.0;
ui_min = -1.0;
ui_max = 1.0;
> = 0.0;

uniform float _GradeTemperature <
ui_category = "Color Grading";
ui_category = "Color Grading | White Balance";
ui_label = "Temperature";
ui_type = "slider";
ui_min = -1.0;
ui_max = 1.0;
> = 0.0;

uniform float _GradeTint <
ui_category = "Color Grading";
ui_category = "Color Grading | White Balance";
ui_label = "Tint";
ui_type = "slider";
ui_min = -1.0;
ui_max = 1.0;
> = 0.0;

uniform float3 _GradeShadows <
ui_category = "Color Grading | Split Toning";
ui_label = "Shadows";
ui_type = "color";
> = float3(0.5, 0.5, 0.5);

uniform float3 _GradeHighLights <
ui_category = "Color Grading | Split Toning";
ui_label = "Highlights";
ui_type = "color";
> = float3(0.5, 0.5, 0.5);

uniform float _GradeBalance <
ui_category = "Color Grading | Split Toning";
ui_label = "Balance";
ui_type = "slider";
ui_min = -100.0;
ui_max = 100.0;
> = 0.0;

uniform float3 _GradeMixRed <
ui_category = "Color Grading | Channel Mixer";
ui_label = "Red";
ui_type = "slider";
ui_min = 0.0;
ui_max = 1.0;
> = float3(1.0, 0.0, 0.0);

uniform float3 _GradeMixGreen <
ui_category = "Color Grading | Channel Mixer";
ui_label = "Green";
ui_type = "slider";
ui_min = 0.0;
ui_max = 1.0;
> = float3(0.0, 1.0, 0.0);

uniform float3 _GradeMixBlue <
ui_category = "Color Grading | Channel Mixer";
ui_label = "Blue";
ui_type = "slider";
ui_min = 0.0;
ui_max = 1.0;
> = float3(0.0, 0.0, 1.0);

uniform float3 _GradeMidtoneShadowColor <
ui_category = "Color Grading | Shadows Midtones Hightlights";
ui_label = "Shadow Color";
ui_type = "color";
ui_min = 0.0;
ui_max = 1.0;
> = float3(1.0, 1.0, 1.0);

uniform float3 _GradeMidtoneColor <
ui_category = "Color Grading | Shadows Midtones Hightlights";
ui_label = "Midtone Color";
ui_type = "color";
ui_min = 0.0;
ui_max = 1.0;
> = float3(1.0, 1.0, 1.0);

uniform float3 _GradeMidtoneHighlightColor <
ui_category = "Color Grading | Shadows Midtones Hightlights";
ui_label = "Highlight Color";
ui_type = "color";
ui_min = 0.0;
ui_max = 1.0;
> = float3(1.0, 1.0, 1.0);

uniform float _GradeMidtoneShadowStart <
ui_category = "Color Grading | Shadows Midtones Hightlights";
ui_label = "Shadows Start";
ui_type = "slider";
ui_min = 0.0;
ui_max = 1.0;
> = 0.0;

uniform float _GradeMidtoneShadowEnd <
ui_category = "Color Grading | Shadows Midtones Hightlights";
ui_label = "Shadows End";
ui_type = "slider";
ui_min = 0.0;
ui_max = 1.0;
> = 0.3;

uniform float _GradeMidtoneHighlightStart <
ui_category = "Color Grading | Shadows Midtones Hightlights";
ui_label = "Highlights Start";
ui_type = "slider";
ui_min = 0.0;
ui_max = 1.0;
> = 0.55;

uniform float _GradeMidtoneHighlightEnd <
ui_category = "Color Grading | Shadows Midtones Hightlights";
ui_label = "Highlights End";
ui_type = "slider";
ui_min = 0.0;
ui_max = 1.0;
> = 1.0;

#include "shared/cShadeHDR.fxh"
#if ENABLE_AUTOEXPOSURE
#include "shared/cCameraInput.fxh"
Expand Down Expand Up @@ -378,57 +478,6 @@ uniform float _GradeTint <
CREATE_PS_UPSCALE(PS_Upscale1, SampleTempTex2)
#endif

/*
Modification of Jasper's color grading tutorial
https://catlikecoding.com/unity/tutorials/custom-srp/color-grading/
MIT No Attribution (MIT-0)
Copyright 2021 Jasper Flick
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

void ApplyColorGrading(inout float3 Color)
{
// Constants
const float ACEScc_MIDGRAY = 0.4135884;

// Convert user-friendly uniform settings
float PostExposure = exp2(_GradePostExposure);
float Contrast = (_GradeContrast / 100.0) + 1.0;
float HueShift = (_GradeHueShift / 360.0) * CMath_GetPi();
float Saturation = (_GradeSaturation / 100.0) + 1.0;

// Apply post exposure
Color *= PostExposure;

// Apply contrast
Color = CColor_EncodeLogC(Color);
Color = (Color - ACEScc_MIDGRAY) * Contrast + ACEScc_MIDGRAY;
Color = CColor_DecodeLogC(Color);
Color = max(Color, 0.0);

// Apply color filter
Color *= _GradeColorFilter;

// Convert RGB to OKLch
Color = CColor_GetOKLCHfromRGB(Color, false);

// Apply hue shift
Color.z += HueShift;

// Apply saturation
Color.y *= Saturation;

// Convert OKLch to RGB
Color = CColor_GetRGBfromOKLCH(Color);

Color = max(Color, 0.0);
}

float4 PS_Composite(CShade_VS2PS_Quad Input) : SV_TARGET0
{
float3 BaseColor = CShade_BackBuffer2D(Input.Tex0).rgb;
Expand All @@ -447,7 +496,30 @@ float4 PS_Composite(CShade_VS2PS_Quad Input) : SV_TARGET0
BaseColor = (_BloomRenderMode == 0) ? BaseColor + BloomColor : BloomColor;
#endif

ApplyColorGrading(BaseColor);
// Apply color-grading
CColor_ApplyColorGrading(
BaseColor,
_GradePostExposure,
_GradeContrast,
_GradeColorFilter,
_GradeHueShift,
_GradeSaturation,
_GradeTemperature,
_GradeTint,
_GradeShadows,
_GradeHighLights,
_GradeBalance,
_GradeMixRed,
_GradeMixGreen,
_GradeMixBlue,
_GradeMidtoneShadowColor,
_GradeMidtoneColor,
_GradeMidtoneHighlightColor,
_GradeMidtoneShadowStart,
_GradeMidtoneShadowEnd,
_GradeMidtoneHighlightStart,
_GradeMidtoneHighlightEnd
);

// Apply tonemapping
BaseColor = CTonemap_ApplyOutputTonemap(BaseColor);
Expand Down
Loading

0 comments on commit 7b31d1a

Please sign in to comment.