From 735b1f67cdaa54d870cbe01716f289bae8f291b6 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 2 Oct 2021 17:20:26 +0200 Subject: [PATCH] Moved every voronoi related calculations to a new class. --- .../mat_maker_gd/nodes/common/commons.gd | 34 ------------ .../nodes/common/noise_voronoi.gd | 53 +++++++++++++++++++ .../mat_maker_gd/nodes/noise/noise_voronoi.gd | 32 ++++------- 3 files changed, 63 insertions(+), 56 deletions(-) create mode 100644 game/addons/mat_maker_gd/nodes/common/noise_voronoi.gd diff --git a/game/addons/mat_maker_gd/nodes/common/commons.gd b/game/addons/mat_maker_gd/nodes/common/commons.gd index a2191b67..62ae317a 100644 --- a/game/addons/mat_maker_gd/nodes/common/commons.gd +++ b/game/addons/mat_maker_gd/nodes/common/commons.gd @@ -208,38 +208,6 @@ static func color_dots(uv : Vector2, size : float, pseed : int) -> Vector3: var point_pos : Vector2 = floorv2(uv) + Vector2(0.5, 0.5); return rand3(seed2 + point_pos); - -static func voronoi(uv : Vector2, size : Vector2, stretch : Vector2, intensity : float, randomness : float, pseed : int) -> Color: - var seed2 : Vector2 = rand2(Vector2(float(pseed), 1.0-float(pseed))); - uv *= size; - var best_distance0 : float = 1.0; - var best_distance1 : float = 1.0; - var point0 : Vector2; - var point1 : Vector2; - var p0 : Vector2 = floorv2(uv); - - for dx in range(-1, 2):# (int dx = -1; dx < 2; ++dx) { - for dy in range(-1, 2):# (int dy = -1; dy < 2; ++dy) { - var d : Vector2 = Vector2(float(dx), float(dy)); - var p : Vector2 = p0+d; - - p += randomness * rand2(seed2 + modv2(p, size)); - var distance : float = (stretch * (uv - p) / size).length(); - - if (best_distance0 > distance): - best_distance1 = best_distance0; - best_distance0 = distance; - point1 = point0; - point0 = p; - elif (best_distance1 > distance): - best_distance1 = distance; - point1 = p; - - var edge_distance : float = (uv - 0.5*(point0+point1)).dot((point0-point1).normalized()); - - return Color(point0.x, point0.y, best_distance0 * (size).length() * intensity, edge_distance); - - static func perlin(uv : Vector2, size : Vector2, iterations : int, persistence : float, pseed : int) -> float: var seed2 : Vector2 = rand2(Vector2(float(pseed), 1.0-float(pseed))); var rv : float = 0.0; @@ -316,8 +284,6 @@ static func beehive_center(p : Vector2) -> Color: #return dot(h.xy, h.xy) < dot(h.zw, h.zw) ? Color(h.xy, hC.xy) : Color(h.zw, hC.zw + 9.73); - - static func pattern(uv : Vector2, x_scale : float, y_scale : float, ct : int, catx : int, caty : int) -> float: var x : float = 0 var y : float = 0 diff --git a/game/addons/mat_maker_gd/nodes/common/noise_voronoi.gd b/game/addons/mat_maker_gd/nodes/common/noise_voronoi.gd new file mode 100644 index 00000000..182b209f --- /dev/null +++ b/game/addons/mat_maker_gd/nodes/common/noise_voronoi.gd @@ -0,0 +1,53 @@ +extends Reference + +const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd") + +static func voronoi(uv : Vector2, size : Vector2, stretch : Vector2, intensity : float, randomness : float, pseed : int) -> Color: + var seed2 : Vector2 = Commons.rand2(Vector2(float(pseed), 1.0-float(pseed))); + uv *= size; + var best_distance0 : float = 1.0; + var best_distance1 : float = 1.0; + var point0 : Vector2; + var point1 : Vector2; + var p0 : Vector2 = Commons.floorv2(uv); + + for dx in range(-1, 2):# (int dx = -1; dx < 2; ++dx) { + for dy in range(-1, 2):# (int dy = -1; dy < 2; ++dy) { + var d : Vector2 = Vector2(float(dx), float(dy)); + var p : Vector2 = p0+d; + + p += randomness * Commons.rand2(seed2 + Commons.modv2(p, size)); + var distance : float = (stretch * (uv - p) / size).length(); + + if (best_distance0 > distance): + best_distance1 = best_distance0; + best_distance0 = distance; + point1 = point0; + point0 = p; + elif (best_distance1 > distance): + best_distance1 = distance; + point1 = p; + + var edge_distance : float = (uv - 0.5*(point0+point1)).dot((point0-point1).normalized()); + + return Color(point0.x, point0.y, best_distance0 * (size).length() * intensity, edge_distance); + +static func voronoi_1(uv : Vector2, size : Vector2, stretch : Vector2, intensity : float, randomness : float, pseed : int) -> Color: + var c : Color = voronoi(uv, size, stretch, intensity, randomness, pseed); + + return Color(c.b, c.b, c.b, 1) + +static func voronoi_2(uv : Vector2, size : Vector2, stretch : Vector2, intensity : float, randomness : float, pseed : int) -> Color: + var c : Color = voronoi(uv, size, stretch, intensity, randomness, pseed); + + return Color(c.a, c.a, c.a, 1) + +static func voronoi_3(uv : Vector2, size : Vector2, stretch : Vector2, intensity : float, randomness : float, pseed : int) -> Color: + var c : Color = voronoi(uv, size, stretch, intensity, randomness, pseed); + + var vv : Vector2 = Vector2(c.r, c.g) + + var v : Vector3 = Commons.rand3(Commons.fractv2(vv)); + + return Color(v.x, v.y, v.z, 1) + diff --git a/game/addons/mat_maker_gd/nodes/noise/noise_voronoi.gd b/game/addons/mat_maker_gd/nodes/noise/noise_voronoi.gd index 01054eca..46281457 100644 --- a/game/addons/mat_maker_gd/nodes/noise/noise_voronoi.gd +++ b/game/addons/mat_maker_gd/nodes/noise/noise_voronoi.gd @@ -1,7 +1,7 @@ tool extends TextureRect -var Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd") +var NoiseVoronoi = preload("res://addons/mat_maker_gd/nodes/common/noise_voronoi.gd") var image : Image var tex : ImageTexture @@ -52,27 +52,6 @@ func gen() -> void: tex.create_from_image(image) texture = tex - -func voron_1(uv : Vector2) -> Color: - var c : Color = Commons.voronoi(((uv)), Vector2(p_o8689_scale_x, p_o8689_scale_y), Vector2(p_o8689_stretch_y, p_o8689_stretch_x), p_o8689_intensity, p_o8689_randomness, seed_o8689); - - return Color(c.b, c.b, c.b, 1) - -func voron_2(uv : Vector2) -> Color: - var c : Color = Commons.voronoi(((uv)), Vector2(p_o8689_scale_x, p_o8689_scale_y), Vector2(p_o8689_stretch_y, p_o8689_stretch_x), p_o8689_intensity, p_o8689_randomness, seed_o8689); - - return Color(c.a, c.a, c.a, 1) - -func voron_3(uv : Vector2) -> Color: - var c : Color = Commons.voronoi(((uv)), Vector2(p_o8689_scale_x, p_o8689_scale_y), Vector2(p_o8689_stretch_y, p_o8689_stretch_x), p_o8689_intensity, p_o8689_randomness, seed_o8689); - - var vv : Vector2 = Vector2(c.r, c.g) - - var v : Vector3 = Commons.rand3(Commons.fractv2(vv)); - - return Color(v.x, v.y, v.z, 1) - - var seed_o8689 = 11667; var p_o8689_scale_x = 4.000000000; var p_o8689_scale_y = 4.000000000; @@ -81,6 +60,15 @@ var p_o8689_stretch_y = 1.000000000; var p_o8689_intensity = 1.000000000; var p_o8689_randomness = 0.750000000; +func voron_1(uv : Vector2) -> Color: + return NoiseVoronoi.voronoi_1(uv, Vector2(p_o8689_scale_x, p_o8689_scale_y), Vector2(p_o8689_stretch_y, p_o8689_stretch_x), p_o8689_intensity, p_o8689_randomness, seed_o8689) + +func voron_2(uv : Vector2) -> Color: + return NoiseVoronoi.voronoi_2(uv, Vector2(p_o8689_scale_x, p_o8689_scale_y), Vector2(p_o8689_stretch_y, p_o8689_stretch_x), p_o8689_intensity, p_o8689_randomness, seed_o8689) + +func voron_3(uv : Vector2) -> Color: + return NoiseVoronoi.voronoi_3(uv, Vector2(p_o8689_scale_x, p_o8689_scale_y), Vector2(p_o8689_stretch_y, p_o8689_stretch_x), p_o8689_intensity, p_o8689_randomness, seed_o8689) + func reffg(): return false