From 46dae7373b0ede6fd5cecbb6ba13ff7a190d1158 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 3 Oct 2021 17:12:13 +0200 Subject: [PATCH] Moved color related methods to a new colors.gd file. --- .../mat_maker_gd/nodes/common/colors.gd | 71 ++++++++++ .../mat_maker_gd/nodes/common/commons.gd | 130 ++++-------------- .../mat_maker_gd/nodes/filter/filter.gd | 8 +- .../mat_maker_gd/nodes/filter/grayscale.gd | 6 +- 4 files changed, 109 insertions(+), 106 deletions(-) create mode 100644 game/addons/mat_maker_gd/nodes/common/colors.gd diff --git a/game/addons/mat_maker_gd/nodes/common/colors.gd b/game/addons/mat_maker_gd/nodes/common/colors.gd new file mode 100644 index 00000000..e9b4c3fb --- /dev/null +++ b/game/addons/mat_maker_gd/nodes/common/colors.gd @@ -0,0 +1,71 @@ +extends Reference + +const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd") + +static func rgb_to_hsv(c : Vector3) -> Vector3: + var K : Color = Color(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + + var p : Color + + if c.y < c.z: + p = Color(c.z, c.y, K.a, K.b) + else: + p = Color(c.y, c.z, K.r, K.g); + + var q : Color + + if c.x < p.r: + q = Color(p.r, p.g, p.a, c.x) + else: + q = Color(c.x, p.g, p.b, p.r); + + var d : float = q.r - min(q.a, q.g); + var e : float = 1.0e-10; + + return Vector3(abs(q.b + (q.a - q.g) / (6.0 * d + e)), d / (q.r + e), q.r); + +static func hsv_to_rgb(c : Vector3) -> Vector3: + var K : Color = Color(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + + var p : Vector3 = Commons.absv3(Commons.fractv3(Vector3(c.x, c.x, c.x) + Vector3(K.r, K.g, K.b)) * 6.0 - Vector3(K.a, K.a, K.a)); + + return c.z * lerp(Vector3(K.r, K.r, K.r), Commons.clampv3(p - Vector3(K.r, K.r, K.r), Vector3(), Vector3(1, 1, 1)), c.y); + +static func adjust_hsv(color : Color, hue : float, saturation : float, value : float) -> Color: + var hsv : Vector3 = rgb_to_hsv(Vector3(color.r, color.g, color.b)); + + var x : float = Commons.fract(hsv.x + hue) + var y : float = clamp(hsv.y * saturation, 0.0, 1.0) + var z : float = clamp(hsv.z * value, 0.0, 1.0) + + var h : Vector3 = hsv_to_rgb(Vector3(x, y, z)) + + return Color(h.x, h.y, h.z, color.a); + +static func brightness_contrast(color : Color, brightness : float, contrast : float) -> Color: + var bv : Vector3 = Vector3(brightness, brightness, brightness) + var cvv : Vector3 = Vector3(color.r * contrast, color.g * contrast, color.b * contrast) + + var cv : Vector3 = cvv + bv + Vector3(0.5, 0.5, 0.5) - (Vector3(contrast, contrast, contrast) * 0.5) + + var v : Vector3 = Commons.clampv3(cv, Vector3(), Vector3(1, 1, 1)) + + return Color(v.x, v.y, v.z, 1); + +static func grayscale_min(c : Vector3) -> float: + return min(c.x, min(c.y, c.z)); + +static func grayscale_max(c : Vector3) -> float: + return max(c.x, max(c.y, c.z)); + +static func grayscale_lightness(c : Vector3) -> float: + return 0.5*(max(c.x, max(c.y, c.z)) + min(c.x, min(c.y, c.z))); + +static func grayscale_average(c : Vector3) -> float: + return 0.333333333333*(c.x + c.y + c.z); + +static func grayscale_luminosity(c : Vector3) -> float: + return 0.21 * c.x + 0.72 * c.y + 0.07 * c.z; + +static func invert(color : Color) -> Color: + return Color(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a); diff --git a/game/addons/mat_maker_gd/nodes/common/commons.gd b/game/addons/mat_maker_gd/nodes/common/commons.gd index 37522661..79ddb9b3 100644 --- a/game/addons/mat_maker_gd/nodes/common/commons.gd +++ b/game/addons/mat_maker_gd/nodes/common/commons.gd @@ -17,7 +17,6 @@ static func floorc(a : Color) -> Color: return v - static func floorv2(a : Vector2) -> Vector2: var v : Vector2 = Vector2() @@ -138,36 +137,6 @@ static func step(edge : float, x : float) -> float: else: return 1.0 - -static func rgb_to_hsv(c : Vector3) -> Vector3: - var K : Color = Color(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - - var p : Color - - if c.y < c.z: - p = Color(c.z, c.y, K.a, K.b) - else: - p = Color(c.y, c.z, K.r, K.g); - - var q : Color - - if c.x < p.r: - q = Color(p.r, p.g, p.a, c.x) - else: - q = Color(c.x, p.g, p.b, p.r); - - var d : float = q.r - min(q.a, q.g); - var e : float = 1.0e-10; - - return Vector3(abs(q.b + (q.a - q.g) / (6.0 * d + e)), d / (q.r + e), q.r); - -static func hsv_to_rgb(c : Vector3) -> Vector3: - var K : Color = Color(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - - var p : Vector3 = absv3(fractv3(Vector3(c.x, c.x, c.x) + Vector3(K.r, K.g, K.b)) * 6.0 - Vector3(K.a, K.a, K.a)); - - return c.z * lerp(Vector3(K.r, K.r, K.r), clampv3(p - Vector3(K.r, K.r, K.r), Vector3(), Vector3(1, 1, 1)), c.y); - static func transform(uv : Vector2, translate : Vector2, rotate : float, scale : Vector2, repeat : bool) -> Vector2: var rv : Vector2 = Vector2(); uv -= translate; @@ -182,11 +151,26 @@ static func transform(uv : Vector2, translate : Vector2, rotate : float, scale : else: return clampv2(rv, Vector2(0, 0), Vector2(1, 1)); -static func color_dots(uv : Vector2, size : float, pseed : int) -> Vector3: - var seed2 : Vector2 = rand2(Vector2(float(pseed), 1.0 - float(pseed))); - uv /= size; - var point_pos : Vector2 = floorv2(uv) + Vector2(0.5, 0.5); - return rand3(seed2 + point_pos); +static func mix_mul(x : float, y : float) -> float: + return x*y; + +static func mix_add(x : float, y : float) -> float: + return min(x+y, 1.0); + +static func mix_max(x : float, y : float) -> float: + return max(x, y); + +static func mix_min(x : float, y : float) -> float: + return min(x, y); + +static func mix_xor(x : float, y : float) -> float: + return min(x+y, 2.0-x-y); + +static func mix_pow(x : float, y : float) -> float: + return pow(x, y); + +static func fractf(x : float) -> float: + return x - floor(x) static func wave_constant(x : float) -> float: return 1.0; @@ -211,29 +195,10 @@ static func wave_bounce(x : float) -> float: x = 2.0*(fractf(x)-0.5); return sqrt(1.0-x*x); -static func mix_mul(x : float, y : float) -> float: - return x*y; - -static func mix_add(x : float, y : float) -> float: - return min(x+y, 1.0); - -static func mix_max(x : float, y : float) -> float: - return max(x, y); - -static func mix_min(x : float, y : float) -> float: - return min(x, y); - -static func mix_xor(x : float, y : float) -> float: - return min(x+y, 2.0-x-y); - -static func mix_pow(x : float, y : float) -> float: - return pow(x, y); - -static func fractf(x : float) -> float: - return x - floor(x) - -static func invert(color : Color) -> Color: - return Color(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a); +static func sinewave(uv : Vector2, amplitude : float, frequency : float, phase : float) -> Color: + var f : float = 1.0- abs(2.0 * (uv.y-0.5) - amplitude * sin((frequency* uv.x + phase) * 6.28318530718)); + + return Color(f, f, f, 1) static func ThickLine(uv : Vector2, posA : Vector2, posB : Vector2, radiusInv : float) -> float: var dir : Vector2 = posA - posB; @@ -246,48 +211,11 @@ static func ThickLine(uv : Vector2, posA : Vector2, posB : Vector2, radiusInv : return finalGray; -static func adjust_hsv(color : Color, hue : float, saturation : float, value : float) -> Color: - var hsv : Vector3 = rgb_to_hsv(Vector3(color.r, color.g, color.b)); - - var x : float = fract(hsv.x + hue) - var y : float = clamp(hsv.y * saturation, 0.0, 1.0) - var z : float = clamp(hsv.z * value, 0.0, 1.0) - - var h : Vector3 = hsv_to_rgb(Vector3(x, y, z)) - - return Color(h.x, h.y, h.z, color.a); - -static func brightness_contrast(color : Color, brightness : float, contrast : float) -> Color: - var bv : Vector3 = Vector3(brightness, brightness, brightness) - var cvv : Vector3 = Vector3(color.r * contrast, color.g * contrast, color.b * contrast) - - var cv : Vector3 = cvv + bv + Vector3(0.5, 0.5, 0.5) - (Vector3(contrast, contrast, contrast) * 0.5) - - var v : Vector3 = clampv3(cv, Vector3(), Vector3(1, 1, 1)) - - return Color(v.x, v.y, v.z, 1); - - -static func grayscale_min(c : Vector3) -> float: - return min(c.x, min(c.y, c.z)); - -static func grayscale_max(c : Vector3) -> float: - return max(c.x, max(c.y, c.z)); - -static func grayscale_lightness(c : Vector3) -> float: - return 0.5*(max(c.x, max(c.y, c.z)) + min(c.x, min(c.y, c.z))); - -static func grayscale_average(c : Vector3) -> float: - return 0.333333333333*(c.x + c.y + c.z); - -static func grayscale_luminosity(c : Vector3) -> float: - return 0.21 * c.x + 0.72 * c.y + 0.07 * c.z; - -static func sinewave(uv : Vector2, amplitude : float, frequency : float, phase : float) -> Color: - var f : float = 1.0- abs(2.0 * (uv.y-0.5) - amplitude * sin((frequency* uv.x + phase) * 6.28318530718)); - - return Color(f, f, f, 1) - +static func color_dots(uv : Vector2, size : float, pseed : int) -> Vector3: + var seed2 : Vector2 = rand2(Vector2(float(pseed), 1.0 - float(pseed))); + uv /= size; + var point_pos : Vector2 = floorv2(uv) + Vector2(0.5, 0.5); + return rand3(seed2 + point_pos); static func noise_color(uv : Vector2, pseed : int) -> Color: var v : Vector3 = color_dots(((uv)), 1.0/512.000000000, pseed); diff --git a/game/addons/mat_maker_gd/nodes/filter/filter.gd b/game/addons/mat_maker_gd/nodes/filter/filter.gd index a922c8c6..6e346847 100644 --- a/game/addons/mat_maker_gd/nodes/filter/filter.gd +++ b/game/addons/mat_maker_gd/nodes/filter/filter.gd @@ -2,6 +2,8 @@ tool extends TextureRect var Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd") +var Colors = preload("res://addons/mat_maker_gd/nodes/common/colors.gd") +var Shapes = preload("res://addons/mat_maker_gd/nodes/common/shapes.gd") var image : Image var tex : ImageTexture @@ -38,7 +40,7 @@ func gen() -> void: for y in range(image.get_height()): var v : Vector2 = Vector2(x / w, y / h) - var f : float = Commons.shape_circle(v, 3, 1.0 * 1.0, 1.0) + var f : float = Shapes.shape_circle(v, 3, 1.0 * 1.0, 1.0) var c : Color = Color(f, f, f, 1) # c = invert(c) @@ -59,14 +61,14 @@ var p_o91644_brightness = 0.000000000; var p_o91644_contrast = 1.000000000; func brightness_contrast(color : Color) -> Color: - return Commons.brightness_contrast(color, p_o91644_brightness, p_o91644_contrast); + return Colors.brightness_contrast(color, p_o91644_brightness, p_o91644_contrast); var p_o102649_hue = 0.000000000; var p_o102649_saturation = 1.000000000; var p_o102649_value = 1.000000000; func adjust_hsv(color : Color) -> Color: - return Commons.adjust_hsv(color, p_o102649_hue, p_o102649_saturation, p_o102649_value) + return Colors.adjust_hsv(color, p_o102649_hue, p_o102649_saturation, p_o102649_value) func reffg(): return false diff --git a/game/addons/mat_maker_gd/nodes/filter/grayscale.gd b/game/addons/mat_maker_gd/nodes/filter/grayscale.gd index 9b7b8a6b..61a73561 100644 --- a/game/addons/mat_maker_gd/nodes/filter/grayscale.gd +++ b/game/addons/mat_maker_gd/nodes/filter/grayscale.gd @@ -2,6 +2,8 @@ tool extends TextureRect var Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd") +var Colors = preload("res://addons/mat_maker_gd/nodes/common/colors.gd") +var Shapes = preload("res://addons/mat_maker_gd/nodes/common/shapes.gd") var image : Image var tex : ImageTexture @@ -38,9 +40,9 @@ func gen() -> void: for y in range(image.get_height()): var v : Vector2 = Vector2(x / w, y / h) - var f : float = Commons.shape_circle(v, 3, 1.0 * 1.0, 1.0) + var f : float = Shapes.shape_circle(v, 3, 1.0 * 1.0, 1.0) - f = Commons.grayscale_luminosity(Vector3(f, f, f)); + f = Colors.grayscale_luminosity(Vector3(f, f, f)); var c : Color = Color(f, f, f, 1)