From 27f6de16e931117310b6430a11aaa69de8fbaebf Mon Sep 17 00:00:00 2001 From: Rodolphe Suescun Date: Sat, 18 Aug 2018 01:53:07 +0200 Subject: [PATCH] Fixes in Perlin and Voronoi Perlin and Voronoi have better randomness, and Voronoi color output is now tileable --- addons/procedural_material/common.shader | 24 ++++++++++++-------- addons/procedural_material/library.gd | 3 ++- addons/procedural_material/library/base.json | 4 ++++ addons/procedural_material/nodes/blend.gd | 3 ++- addons/procedural_material/nodes/blend.tscn | 2 +- addons/procedural_material/nodes/voronoi.gd | 2 +- 6 files changed, 24 insertions(+), 14 deletions(-) diff --git a/addons/procedural_material/common.shader b/addons/procedural_material/common.shader index 9e75963..68b6495 100644 --- a/addons/procedural_material/common.shader +++ b/addons/procedural_material/common.shader @@ -18,7 +18,7 @@ float wave_constant(float x) { } float wave_sin(float x) { - return 0.5-0.5*cos(3.1415928*2.0*x); + return 0.5-0.5*cos(3.14159265359*2.0*x); } float wave_triangle(float x) { @@ -122,6 +122,10 @@ vec3 blend_darken(vec2 uv, vec3 c1, vec3 c2, float opacity) { return opacity*min(c1, c2) + (1.0-opacity)*c2; } +vec3 blend_difference(vec2 uv, vec3 c1, vec3 c2, float opacity) { + return opacity*clamp(c2-c1, vec3(0.0), vec3(1.0)) + (1.0-opacity)*c2; +} + vec2 transform(vec2 uv, vec2 translate, float rotate, vec2 scale) { vec2 rv; uv -= vec2(0.5); @@ -165,18 +169,18 @@ float colored_bricks(vec2 uv, vec2 count, float offset) { } float perlin(vec2 uv, vec2 size, int iterations, float persistence, int seed) { - uv += vec2(float(seed)*0.1234567); + vec2 seed2 = rand2(vec2(float(seed), 1.0-float(seed))); float rv = 0.0; float coef = 1.0; float acc = 0.0; for (int i = 0; i < iterations; ++i) { vec2 step = vec2(1.0)/size; - float f0 = rand(floor(fract(uv)*size)); - float f1 = rand(floor(fract(uv+vec2(step.x, 0.0))*size)); - float f2 = rand(floor(fract(uv+vec2(0.0, step.y))*size)); - float f3 = rand(floor(fract(uv+vec2(step.x, step.y))*size)); - vec2 mixval = fract(uv*size); - mixval = 0.5*(1.0-cos(3.1415928*mixval)); + vec2 xy = seed2+floor(fract(uv)*size); + float f0 = rand(xy); + float f1 = rand(xy+vec2(1.0, 0.0)); + float f2 = rand(xy+vec2(0.0, 1.0)); + float f3 = rand(xy+vec2(1.0, 1.0)); + vec2 mixval = smoothstep(0.0, 1.0, fract(uv*size)); rv += coef * mix(mix(f0, f1, mixval.x), mix(f2, f3, mixval.x), mixval.y); acc += coef; size *= 2.0; @@ -187,7 +191,7 @@ float perlin(vec2 uv, vec2 size, int iterations, float persistence, int seed) { } vec4 voronoi(vec2 uv, vec2 size, float intensity, int seed) { - uv += vec2(float(seed)*0.1234567); + vec2 seed2 = rand2(vec2(float(seed), 1.0-float(seed))); uv *= size; float best_distance0 = 1.0; float best_distance1 = 1.0; @@ -198,7 +202,7 @@ vec4 voronoi(vec2 uv, vec2 size, float intensity, int seed) { for (int dy = -1; dy < 2; ++dy) { vec2 d = vec2(float(dx), float(dy)); vec2 p = p0+d; - p += rand2(mod(p, size)); + p += rand2(seed2+mod(p, size)); float distance = length((uv - p) / size); if (best_distance0 > distance) { best_distance1 = best_distance0; diff --git a/addons/procedural_material/library.gd b/addons/procedural_material/library.gd index e301f1b..7ed4113 100644 --- a/addons/procedural_material/library.gd +++ b/addons/procedural_material/library.gd @@ -16,7 +16,8 @@ func get_drag_data(position): elif data.has("type") and data.type == "uniform": preview = ColorRect.new() preview.rect_size = Vector2(32, 32) - preview.color = Color(data.color.r, data.color.g, data.color.b, data.color.a) + if data.has("color"): + preview.color = Color(data.color.r, data.color.g, data.color.b, data.color.a) else: preview = Label.new() preview.text = data.tree_item diff --git a/addons/procedural_material/library/base.json b/addons/procedural_material/library/base.json index 84ab8ac..284adc2 100644 --- a/addons/procedural_material/library/base.json +++ b/addons/procedural_material/library/base.json @@ -11,6 +11,10 @@ "tree_item":"Miscellaneous", "collapsed":false }, + { + "tree_item":"Generators/Uniform", + "type":"uniform" + }, { "tree_item":"Generators/Image", "type":"image", diff --git a/addons/procedural_material/nodes/blend.gd b/addons/procedural_material/nodes/blend.gd index 5f10a17..0d302d5 100644 --- a/addons/procedural_material/nodes/blend.gd +++ b/addons/procedural_material/nodes/blend.gd @@ -15,7 +15,8 @@ const BLEND_TYPES = [ { name="Burn", shortname="burn" }, { name="Dodge", shortname="dodge" }, { name="Lighten", shortname="lighten" }, - { name="Darken", shortname="darken" } + { name="Darken", shortname="darken" }, + { name="Difference", shortname="difference" } ] func _ready(): diff --git a/addons/procedural_material/nodes/blend.tscn b/addons/procedural_material/nodes/blend.tscn index 1147fd4..f18b8eb 100644 --- a/addons/procedural_material/nodes/blend.tscn +++ b/addons/procedural_material/nodes/blend.tscn @@ -80,7 +80,7 @@ group = null text = "Normal" flat = false align = 0 -items = [ "Normal", null, false, 0, null, "Dissolve", null, false, 1, null, "Multiply", null, false, 2, null, "Screen", null, false, 3, null, "Overlay", null, false, 4, null, "Hard Light", null, false, 5, null, "Soft Light", null, false, 6, null ] +items = [ "Normal", null, false, 0, null, "Dissolve", null, false, 1, null, "Multiply", null, false, 2, null, "Screen", null, false, 3, null, "Overlay", null, false, 4, null, "Hard Light", null, false, 5, null, "Soft Light", null, false, 6, null, "Burn", null, false, 7, null, "Dodge", null, false, 8, null, "Lighten", null, false, 9, null, "Darken", null, false, 10, null ] selected = 0 [node name="Label1" type="Label" parent="." index="1"] diff --git a/addons/procedural_material/nodes/voronoi.gd b/addons/procedural_material/nodes/voronoi.gd index 28e273a..a2ccbc3 100644 --- a/addons/procedural_material/nodes/voronoi.gd +++ b/addons/procedural_material/nodes/voronoi.gd @@ -23,7 +23,7 @@ func _get_shader_code(uv, slot = 0): elif slot == 1: rv.f = name+"_"+str(variant_index)+"_xyzw.w" else: - rv.rgb = "rand3("+name+"_"+str(variant_index)+"_xyzw.xy)" + rv.rgb = "rand3(fract("+name+"_"+str(variant_index)+"_xyzw.xy))" return rv func _on_offset_changed():