Skip to content

Commit

Permalink
Feature/adjustment mask all (#15)
Browse files Browse the repository at this point in the history
* Working adjustment masks for shape, source, and gradient masks.
  • Loading branch information
FiniteSingularity authored Nov 5, 2023
1 parent 6cbe9c8 commit 6f5a8e5
Show file tree
Hide file tree
Showing 8 changed files with 862 additions and 207 deletions.
17 changes: 14 additions & 3 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ AdvancedMasks.Effect="Mask Effect"
AdvancedMasks.Effects.Alpha="Alpha Mask"
AdvancedMasks.Effects.Adjustment="Adjustment Mask"
AdvancedMasks.Adjustments.Label="Adjustments"
AdvancedMasks.Adjustments.Brightness="Brightness"
AdvancedMasks.Adjustments.MinBrightness="Minimum Brightness"
AdvancedMasks.Adjustments.MaxBrightness="Maximum Brightness"
AdvancedMasks.Adjustments.Brightness="Adjust Brightness?"
AdvancedMasks.Adjustments.MinBrightness="Brightness @ min"
AdvancedMasks.Adjustments.MaxBrightness="Brightness @ max"
AdvancedMasks.Adjustments.Contrast="Adjust Contrast?"
AdvancedMasks.Adjustments.MinContrast="Contrast @ min"
AdvancedMasks.Adjustments.MaxContrast="Contrast @ max"
AdvancedMasks.Adjustments.Saturation="Adjust Saturation?"
AdvancedMasks.Adjustments.MinSaturation="Saturation @ min"
AdvancedMasks.Adjustments.MaxSaturation="Saturation @ max"
AdvancedMasks.Adjustments.HueShift="Adjust Hue Shift?"
AdvancedMasks.Adjustments.MinHueShift="Hue Shift @ min"
AdvancedMasks.Adjustments.MaxHueShift="Hue Shift @ max"
AdvancedMasks.Type="Mask Type"
AdvancedMasks.Shape="Shape"
AdvancedMasks.Source="Source"
Expand All @@ -16,6 +25,7 @@ AdvancedMasks.GradientMask.Width="Width"
AdvancedMasks.GradientMask.Position="Position"
AdvancedMasks.GradientMask.Rotation="Rotation"
AdvancedMasks.GradientMask.DebugLines="Debug Lines"
AdvancedMasks.GradientMask.Invert="Invert?"
AdvancedMasks.Shape.Rectangle="Rectangle"
AdvancedMasks.Shape.Circle="Circle"
AdvancedMasks.Shape.Ellipse="Ellipse"
Expand All @@ -26,6 +36,7 @@ AdvancedMasks.Shape.ScalePosition="Scale/Position Properties"
AdvancedMasks.Shape.Rectangle.Width="Width"
AdvancedMasks.Shape.Rectangle.Height="Height"
AdvancedMasks.Shape.Rectangle.SourceGroup="Source Mask Properties"
AdvancedMasks.Shape.Rectangle.GeometryGroup="Adjustment Mask Geometry"
AdvancedMasks.Shape.Rectangle.CornerRadius="Corner Radius"
AdvancedMasks.Shape.Rectangle.CornerRadius.CustomGroup="Corner Radius Properties"
AdvancedMasks.Shape.Rectangle.CornerRadius.TopLeft="Top Left"
Expand Down
41 changes: 41 additions & 0 deletions data/shaders/common.effect
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

#define PI180 0.0174533f
#define SQRT3 0.5773503f
#define LUM_R 0.299f
#define LUM_G 0.587f
#define LUM_B 0.114f

// Convert pre-multiplied rgba values
// to linear rgba.
float4 pmrgba_to_rgba(float4 color)
Expand All @@ -9,3 +16,37 @@ float4 adjust_brightness(float4 color, float brightness)
{
return saturate(float4(color.rgb + brightness, color.a));
}

float4 adjustments(float4 color, float b, float c, float s, float hue_shift) {
// Calculate Contrast Value
float ca = c < 0.0f ? 1.0f / (-c + 1.0f) : (c + 1.0f);

// Calculate Saturation Values
float s_r = (1.0f - s) * LUM_R;
float s_g = (1.0f - s) * LUM_G;
float s_b = (1.0f - s) * LUM_B;

float3 col = float3(
ca * (s_r + s) * color.r + ca * s_g * color.g + ca * s_b * color.b + b,
ca * s_r * color.r + ca * (s_g + s) * color.g + ca * s_b * color.b + b,
ca * s_r * color.r + ca * s_g * color.g + ca * (s_b + s) * color.b + b
);

// Calculate Hue shift values
float half_angle = 0.5f * hue_shift * PI180;
float rq = SQRT3 * sin(half_angle);
float cross = rq * rq;
float sq = 2.0f * cross;
float d = 2.0f * (0.5f - sq);
float w_imag = rq * cos(half_angle);
float a_l = 2.0f * (cross + w_imag);
float b_l = 2.0f * (cross - w_imag);


return saturate(float4(
col.r * d + col.g * a_l + col.b * b_l,
col.r * b_l + col.g * d + col.b * a_l,
col.r * a_l + col.g * b_l + col.b * d,
color.a
));
}
96 changes: 78 additions & 18 deletions data/shaders/gradient-mask.effect
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ uniform float2 uv_size; // source dimensions in px
uniform float width; // width in pixels
uniform float position; // Pixel position
uniform float rotation; // rotation in radians
uniform bool adj_brightness;
uniform bool invert;
uniform float min_brightness;
uniform float max_brightness;
uniform float min_contrast;
uniform float max_contrast;
uniform float min_saturation;
uniform float max_saturation;
uniform float min_hue_shift;
uniform float max_hue_shift;

sampler_state textureSampler{
Filter = Linear;
Expand Down Expand Up @@ -46,7 +52,7 @@ float4 mainAlphaImage(VertData v_in) : TARGET

float alpha = saturate((coord_p.x - position + width / 2.0f) / width);
float4 col = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
return float4(col.rgb, col.a * alpha);
return float4(col.rgb, col.a * (invert ? 1.0f - alpha : alpha));
}

float4 mainAdjustmentsImage(VertData v_in) : TARGET
Expand All @@ -63,13 +69,17 @@ float4 mainAdjustmentsImage(VertData v_in) : TARGET
);

float scale = saturate((coord_p.x - position + width / 2.0f) / width);
float4 col = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
if (adj_brightness)
{
col = adjust_brightness(col, lerp(min_brightness, max_brightness, scale));
}
scale = invert ? 1.0f - scale : scale;
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));

return float4(col.rgb, col.a);
color = adjustments(
color,
lerp(min_brightness, max_brightness, scale),
lerp(min_contrast, max_contrast, scale),
lerp(min_saturation, max_saturation, scale),
lerp(min_hue_shift, max_hue_shift, scale)
);
return color;
}

float4 debugAlphaImage(VertData v_in) : TARGET
Expand All @@ -88,24 +98,64 @@ float4 debugAlphaImage(VertData v_in) : TARGET

float alpha = saturate((coord_p.x - position + width / 2.0f) / width);

float4 col = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
if (coord_p.x - position > -(width / 2.0 + 2.0) && coord_p.x - position < -(width / 2.0))
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
if (coord_p.x - position > -(width / 2.0f + 2.0f) && coord_p.x - position < -(width / 2.0f))
{
return float4(0.0, 1.0, 0.0, 1.0);
return float4(0.0f, 1.0f, 0.0f, 1.0f);
}
if (coord_p.x - position < (width / 2.0 + 2.0) && coord_p.x - position > width / 2.0)
if (coord_p.x - position < (width / 2.0f + 2.0f) && coord_p.x - position > width / 2.0f)
{
return float4(0.0, 1.0, 0.0, 1.0);
return float4(0.0f, 1.0f, 0.0f, 1.0f);
}
if (coord_p.x - position > -1.0 && coord_p.x - position < 1.0)
if (coord_p.x - position > -1.0f && coord_p.x - position < 1.0f)
{
return float4(1.0, 0.0, 0.0, 1.0);
return float4(1.0f, 0.0f, 0.0f, 1.0f);
}
return float4(col.rgb, col.a * alpha);

return float4(color.rgb, color.a * (invert ? 1.0f - alpha : alpha));
}


technique DrawAlpha
float4 debugAdjustmentsImage(VertData v_in) : TARGET
{
float2 coord = v_in.uv * uv_size;
float h = uv_size.x / 2.0f;
float k = uv_size.y / 2.0f;
float sina = sin(rotation);
float cosa = cos(rotation);

float2 coord_p = float2(
cosa * coord.x + sina * coord.y - h * cosa - k * sina,
-sina * coord.x + cosa * coord.y + h * sina - k * cosa
);

float scale = saturate((coord_p.x - position + width / 2.0f) / width);
scale = invert ? 1.0f - scale : scale;
float4 color = pmrgba_to_rgba(image.Sample(textureSampler, v_in.uv));
if (coord_p.x - position > -(width / 2.0f + 2.0f) && coord_p.x - position < -(width / 2.0f))
{
return float4(0.0f, 1.0f, 0.0f, 1.0f);
}
if (coord_p.x - position < (width / 2.0f + 2.0f) && coord_p.x - position > width / 2.0f)
{
return float4(0.0f, 1.0f, 0.0f, 1.0f);
}
if (coord_p.x - position > -1.0f && coord_p.x - position < 1.0f)
{
return float4(1.0f, 0.0f, 0.0f, 1.0f);
}

color = adjustments(
color,
lerp(min_brightness, max_brightness, scale),
lerp(min_contrast, max_contrast, scale),
lerp(min_saturation, max_saturation, scale),
lerp(min_hue_shift, max_hue_shift, scale)
);
return color;
}

technique Alpha
{
pass
{
Expand All @@ -114,7 +164,7 @@ technique DrawAlpha
}
}

technique DrawAdjustments
technique Adjustments
{
pass
{
Expand All @@ -132,3 +182,13 @@ technique DebugAlpha

}
}

technique DebugAdjustments
{
pass
{
vertex_shader = mainTransform(v_in);
pixel_shader = debugAdjustmentsImage(v_in);

}
}
Loading

0 comments on commit 6f5a8e5

Please sign in to comment.