Weave pattern node.

This commit is contained in:
Relintai 2021-10-18 17:59:57 +02:00
parent e6468b8226
commit 2b2c485992
3 changed files with 142 additions and 68 deletions

View File

@ -496,7 +496,14 @@ static func weave(uv : Vector2, count : Vector2, width : float) -> float:
#}
static func weave2(uv : Vector2, count : Vector2, stitch : float, width_x : float, width_y : float) -> Vector3:
return Vector3();
uv.x *= stitch
uv.y *= stitch
uv *= count
var c1 : float = (sin(3.1415926 / stitch * (uv.x + floor(uv.y) - (stitch - 1.0))) * 0.25 + 0.75 ) * Commons.step(abs(Commons.fract(uv.y) - 0.5), width_x * 0.5);
var c2 : float = (sin(3.1415926 / stitch * (1.0 + uv.y + floor(uv.x) ))* 0.25 + 0.75 ) * Commons.step(abs(Commons.fract(uv.x)-0.5), width_y * 0.5);
return Vector3(max(c1, c2), 1.0 - Commons.step(c1, c2), 1.0 - Commons.step(c2, c1));
static func sinewavec(uv : Vector2, amplitude : float, frequency : float, phase : float) -> Color:
var f : float = 1.0- abs(2.0 * (uv.y-0.5) - amplitude * sin((frequency* uv.x + phase) * 6.28318530718));

View File

@ -1,67 +0,0 @@
tool
extends MMNode
var Patterns = preload("res://addons/mat_maker_gd/nodes/common/patterns.gd")
var image : Image
var tex : ImageTexture
export(Vector2) var bmin : Vector2 = Vector2(0.1, 0.1)
export(Vector2) var bmax : Vector2 = Vector2(1, 1)
export(bool) var refresh setget reff,reffg
func _ready():
if !Engine.editor_hint:
gen()
func gen() -> void:
if !image:
image = Image.new()
image.create(300, 300, false, Image.FORMAT_RGBA8)
if !tex:
tex = ImageTexture.new()
# var bmin : Vector2 = Vector2(0.1, 0.1)
# var bmax : Vector2 = Vector2(1, 1)
image.lock()
var w : float = image.get_width()
var h : float = image.get_width()
var pseed : float = randf() + randi()
for x in range(image.get_width()):
for y in range(image.get_height()):
var v : Vector2 = Vector2(x / w, y / h)
# var f : float = pattern(v, 4, 4, CombinerType.MULTIPLY, CombinerAxisType.SINE, CombinerAxisType.SINE)
var col : Color = weavecol(v)
image.set_pixel(x, y, col)
image.unlock()
tex.create_from_image(image)
# texture = tex
var p_o46354_columns = 4.000000000;
var p_o46354_rows = 4.000000000;
var p_o46354_width = 1.000000000;
func weavecol(uv : Vector2) -> Color:
return Patterns.weavec(uv, Vector2(p_o46354_columns, p_o46354_rows), p_o46354_width*1.0);
func reffg():
return false
func reff(bb):
if bb:
gen()

View File

@ -0,0 +1,134 @@
tool
extends MMNode
var Patterns = preload("res://addons/mat_maker_gd/nodes/common/patterns.gd")
export(Resource) var out_main : Resource
export(Resource) var out_horizontal_map : Resource
export(Resource) var out_vertical_map : Resource
export(Vector2) var size : Vector2 = Vector2(4, 4)
export(Resource) var width : Resource
export(int) var stitch : int = 1
func _init_properties():
if !out_main:
out_main = MMNodeUniversalProperty.new()
out_main.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE
out_main.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE
if !out_horizontal_map:
out_horizontal_map = MMNodeUniversalProperty.new()
out_horizontal_map.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE
out_horizontal_map.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE
if !out_vertical_map:
out_vertical_map = MMNodeUniversalProperty.new()
out_vertical_map.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE
out_vertical_map.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE
if !width:
width = MMNodeUniversalProperty.new()
width.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_VECTOR2
width.set_default_value(Vector2(0.9, 0.9))
width.input_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_UNIVERSAL
width.slot_name = "Width"
width.value_step = 0.01
width.value_range = Vector2(0, 1)
register_output_property(out_main)
register_output_property(out_horizontal_map)
register_output_property(out_vertical_map)
register_input_property(width)
func _register_methods(mm_graph_node) -> void:
mm_graph_node.add_slot_texture_universal(out_main)
mm_graph_node.add_slot_texture_universal(out_horizontal_map)
mm_graph_node.add_slot_texture_universal(out_vertical_map)
mm_graph_node.add_slot_vector2("get_size", "set_size", "Size")#, Vector2(1, 32))#, Vector2(0, 32))
mm_graph_node.add_slot_vector2_universal(width)
mm_graph_node.add_slot_int("get_stitch", "set_stitch", "Stitch")
func _render(material) -> void:
var main_pattern : Image = Image.new()
var horizontal_map : Image = Image.new()
var vertical_map : Image = Image.new()
main_pattern.create(material.image_size.x, material.image_size.y, false, Image.FORMAT_RGBA8)
horizontal_map.create(material.image_size.x, material.image_size.y, false, Image.FORMAT_RGBA8)
vertical_map.create(material.image_size.x, material.image_size.y, false, Image.FORMAT_RGBA8)
main_pattern.lock()
horizontal_map.lock()
vertical_map.lock()
var w : float = material.image_size.x
var h : float = material.image_size.y
var pseed : float = randf() + randi()
for x in range(material.image_size.x):
for y in range(material.image_size.y):
var uv : Vector2 = Vector2(x / w, y / h)
var width_val : Vector2 = width.get_value(uv)
#vec3 $(name_uv) = weave2($uv, vec2($columns, $rows), $stitch, $width_x*$width_map($uv), $width_y*$width_map($uv));
var weave : Vector3 = Patterns.weave2(uv, size, stitch, width_val.x, width_val.y);
#Outputs:
#Output (float) - Shows the generated greyscale weave pattern.
#$(name_uv).x
var main_pattern_col : Color = Color(weave.x, weave.x, weave.x, 1)
#Horizontal mask (float) - Horizontal mask
#$(name_uv).y
var horizontal_map_col : Color = Color(weave.y, weave.y, weave.y, 1)
#Vertical mask (float) - Mask for vertical stripes
#$(name_uv).z
var vertical_map_col : Color = Color(weave.z, weave.z, weave.z, 1)
main_pattern.set_pixel(x, y, main_pattern_col)
horizontal_map.set_pixel(x, y, horizontal_map_col)
vertical_map.set_pixel(x, y, vertical_map_col)
main_pattern.unlock()
horizontal_map.unlock()
vertical_map.unlock()
out_main.set_value(main_pattern)
out_horizontal_map.set_value(horizontal_map)
out_vertical_map.set_value(vertical_map)
func get_value_for(uv : Vector2, pseed : int) -> Color:
return Color()
#size
func get_size() -> Vector2:
return size
func set_size(val : Vector2) -> void:
size = val
set_dirty(true)
#stitch
func get_stitch() -> int:
return stitch
func set_stitch(val : int) -> void:
stitch = val
set_dirty(true)