Moved every voronoi related calculations to a new class.

This commit is contained in:
Relintai 2021-10-02 17:20:26 +02:00
parent fdac5e5e85
commit 735b1f67cd
3 changed files with 63 additions and 56 deletions

View File

@ -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

View File

@ -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)

View File

@ -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