Color value node.

This commit is contained in:
Relintai 2021-10-19 12:21:56 +02:00
parent 98ade190f9
commit 31547bf86f
3 changed files with 92 additions and 4 deletions

View File

@ -29,6 +29,11 @@ const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd")
#iterations, float, min: 0, max: 10, default: 3, step:1
#persistence, float, min: 0, max: 1, default: 0.5, step:0.05
static func perlinc(uv : Vector2, size : Vector2, iterations : int, persistence : float, pseed : int) -> Color:
var f : float = perlin(uv, size, iterations, persistence, pseed)
return Color(f, f, f, 1)
#float perlin(vec2 uv, vec2 size, int iterations, float persistence, float seed) {
# vec2 seed2 = rand2(vec2(seed, 1.0-seed));
@ -102,10 +107,33 @@ static func perlin(uv : Vector2, size : Vector2, iterations : int, persistence :
# return rv / acc;
#}
static func perlinc(uv : Vector2, size : Vector2, iterations : int, persistence : float, pseed : int) -> Color:
var f : float = perlin(uv, size, iterations, persistence, pseed)
static func perlin_color(uv : Vector2, size : Vector2, iterations : int, persistence : float, pseed : int) -> Vector3:
var seed2 : Vector2 = Commons.rand2(Vector2(float(pseed), 1.0-float(pseed)));
var rv : Vector3 = Vector3();
var coef : float = 1.0;
var acc : float = 0.0;
return Color(f, f, f, 1)
for i in range(iterations):
var step : Vector2 = Vector2(1, 1) / size;
var xy : Vector2 = Commons.floorv2(uv * size);
var f0 : Vector3 = Commons.rand3(seed2 + Commons.modv2(xy, size));
var f1 : Vector3 = Commons.rand3(seed2 + Commons.modv2(xy + Vector2(1.0, 0.0), size));
var f2 : Vector3 = Commons.rand3(seed2 + Commons.modv2(xy + Vector2(0.0, 1.0), size));
var f3 : Vector3 = Commons.rand3(seed2 + Commons.modv2(xy + Vector2(1.0, 1.0), size));
var mixval : Vector2 = Commons.smoothstepv2(0.0, 1.0, Commons.fractv2(uv * size));
rv += coef * lerp(lerp(f0, f1, mixval.x), lerp(f2, f3, mixval.x), mixval.y)
acc += coef;
size *= 2.0;
coef *= persistence;
return rv / acc;
static func perlin_colorc(uv : Vector2, size : Vector2, iterations : int, persistence : float, pseed : int) -> Color:
var f : Vector3 = perlin_color(uv, size, iterations, persistence, pseed)
return Color(f.x, f.y, f.z, 1)
static func perlin_warp_1(uv : Vector2, size : Vector2, iterations : int, persistence : float, pseed : int, translate : Vector2, rotate : float, size2 : Vector2) -> Color:
var f : float = perlin(uv, size2, iterations, persistence, pseed)

View File

@ -0,0 +1,60 @@
tool
extends MMNode
var NoisePerlin = preload("res://addons/mat_maker_gd/nodes/common/noise_perlin.gd")
export(Resource) var image : Resource
export(Vector2) var scale : Vector2 = Vector2(4, 4)
export(int) var iterations : int = 3
export(float) var persistence : float = 0.5
func _init_properties():
if !image:
image = MMNodeUniversalProperty.new()
image.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE
image.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE
register_output_property(image)
func _register_methods(mm_graph_node) -> void:
mm_graph_node.add_slot_texture_universal(image)
mm_graph_node.add_slot_vector2("get_scale", "set_scale", "Scale")#, Vector2(1, 10))
mm_graph_node.add_slot_int("get_iterations", "set_iterations", "Iterations")#, Vector2(0, 1))
mm_graph_node.add_slot_float("get_persistence", "set_persistence", "Persistence", 0.01)#, Vector2(0, 1))
func get_value_for(uv : Vector2, pseed : int) -> Color:
var ps : float = 1.0 / float(pseed)
#perlin_color($(uv), vec2($(scale_x), $(scale_y)), int($(iterations)), $(persistence), $(seed))
return NoisePerlin.perlin_colorc(uv, scale, iterations, persistence, ps)
func _render(material) -> void:
var img : Image = render_image(material)
image.set_value(img)
func get_scale() -> Vector2:
return scale
func set_scale(val : Vector2) -> void:
scale = val
set_dirty(true)
func get_iterations() -> int:
return iterations
func set_iterations(val : int) -> void:
iterations = val
set_dirty(true)
func get_persistence() -> float:
return persistence
func set_persistence(val : float) -> void:
persistence = val
set_dirty(true)

View File

@ -28,7 +28,7 @@ func get_value_for(uv : Vector2, pseed : int) -> Color:
var ps : float = 1.0 / float(pseed)
#perlin($(uv), vec2($(scale_x), $(scale_y)), int($(iterations)), $(persistence), $(seed))
var f : float = NoisePerlin.perlin(uv, scale, iterations, persistence, pseed)
var f : float = NoisePerlin.perlin(uv, scale, iterations, persistence, ps)
return Color(f, f, f, 1)