Fixes in Perlin and Voronoi

Perlin and Voronoi have better randomness, and Voronoi color output is now tileable
This commit is contained in:
Rodolphe Suescun 2018-08-18 01:53:07 +02:00
parent 93e0765f1d
commit 27f6de16e9
6 changed files with 24 additions and 14 deletions

View File

@ -18,7 +18,7 @@ float wave_constant(float x) {
} }
float wave_sin(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) { 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; 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 transform(vec2 uv, vec2 translate, float rotate, vec2 scale) {
vec2 rv; vec2 rv;
uv -= vec2(0.5); 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) { 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 rv = 0.0;
float coef = 1.0; float coef = 1.0;
float acc = 0.0; float acc = 0.0;
for (int i = 0; i < iterations; ++i) { for (int i = 0; i < iterations; ++i) {
vec2 step = vec2(1.0)/size; vec2 step = vec2(1.0)/size;
float f0 = rand(floor(fract(uv)*size)); vec2 xy = seed2+floor(fract(uv)*size);
float f1 = rand(floor(fract(uv+vec2(step.x, 0.0))*size)); float f0 = rand(xy);
float f2 = rand(floor(fract(uv+vec2(0.0, step.y))*size)); float f1 = rand(xy+vec2(1.0, 0.0));
float f3 = rand(floor(fract(uv+vec2(step.x, step.y))*size)); float f2 = rand(xy+vec2(0.0, 1.0));
vec2 mixval = fract(uv*size); float f3 = rand(xy+vec2(1.0, 1.0));
mixval = 0.5*(1.0-cos(3.1415928*mixval)); 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); rv += coef * mix(mix(f0, f1, mixval.x), mix(f2, f3, mixval.x), mixval.y);
acc += coef; acc += coef;
size *= 2.0; 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) { 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; uv *= size;
float best_distance0 = 1.0; float best_distance0 = 1.0;
float best_distance1 = 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) { for (int dy = -1; dy < 2; ++dy) {
vec2 d = vec2(float(dx), float(dy)); vec2 d = vec2(float(dx), float(dy));
vec2 p = p0+d; vec2 p = p0+d;
p += rand2(mod(p, size)); p += rand2(seed2+mod(p, size));
float distance = length((uv - p) / size); float distance = length((uv - p) / size);
if (best_distance0 > distance) { if (best_distance0 > distance) {
best_distance1 = best_distance0; best_distance1 = best_distance0;

View File

@ -16,7 +16,8 @@ func get_drag_data(position):
elif data.has("type") and data.type == "uniform": elif data.has("type") and data.type == "uniform":
preview = ColorRect.new() preview = ColorRect.new()
preview.rect_size = Vector2(32, 32) 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: else:
preview = Label.new() preview = Label.new()
preview.text = data.tree_item preview.text = data.tree_item

View File

@ -11,6 +11,10 @@
"tree_item":"Miscellaneous", "tree_item":"Miscellaneous",
"collapsed":false "collapsed":false
}, },
{
"tree_item":"Generators/Uniform",
"type":"uniform"
},
{ {
"tree_item":"Generators/Image", "tree_item":"Generators/Image",
"type":"image", "type":"image",

View File

@ -15,7 +15,8 @@ const BLEND_TYPES = [
{ name="Burn", shortname="burn" }, { name="Burn", shortname="burn" },
{ name="Dodge", shortname="dodge" }, { name="Dodge", shortname="dodge" },
{ name="Lighten", shortname="lighten" }, { name="Lighten", shortname="lighten" },
{ name="Darken", shortname="darken" } { name="Darken", shortname="darken" },
{ name="Difference", shortname="difference" }
] ]
func _ready(): func _ready():

View File

@ -80,7 +80,7 @@ group = null
text = "Normal" text = "Normal"
flat = false flat = false
align = 0 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 selected = 0
[node name="Label1" type="Label" parent="." index="1"] [node name="Label1" type="Label" parent="." index="1"]

View File

@ -23,7 +23,7 @@ func _get_shader_code(uv, slot = 0):
elif slot == 1: elif slot == 1:
rv.f = name+"_"+str(variant_index)+"_xyzw.w" rv.f = name+"_"+str(variant_index)+"_xyzw.w"
else: else:
rv.rgb = "rand3("+name+"_"+str(variant_index)+"_xyzw.xy)" rv.rgb = "rand3(fract("+name+"_"+str(variant_index)+"_xyzw.xy))"
return rv return rv
func _on_offset_changed(): func _on_offset_changed():