diff --git a/addons/material_maker/engine/gen_base.gd b/addons/material_maker/engine/gen_base.gd index 86112b1..8a14838 100644 --- a/addons/material_maker/engine/gen_base.gd +++ b/addons/material_maker/engine/gen_base.gd @@ -90,8 +90,8 @@ func init_parameters() -> void: func set_position(p) -> void: position = p - if has_randomness() and !is_seed_locked(): - source_changed(0) + if has_randomness() and !is_seed_locked() and is_inside_tree(): + get_tree().call_group("preview", "on_float_parameter_changed", "seed_o%s" % [ str(get_instance_id()) ], get_seed()) func get_type() -> String: return "generic" diff --git a/addons/material_maker/engine/gen_buffer.gd b/addons/material_maker/engine/gen_buffer.gd index a412302..ad20999 100644 --- a/addons/material_maker/engine/gen_buffer.gd +++ b/addons/material_maker/engine/gen_buffer.gd @@ -37,8 +37,9 @@ func get_output_defs() -> Array: return [ { type="rgba" }, { type="rgba" } ] func source_changed(_input_port_index : int) -> void: - if !is_inside_tree(): - return + call_deferred("update_shader") + +func update_shader() -> void: var context : MMGenContext = MMGenContext.new() var source = {} var source_output = get_source(0) @@ -73,7 +74,7 @@ func update_buffer() -> void: updating = true while update_again: update_again = false - var result = mm_renderer.render_material(material, pow(2, parameters.size)) + var result = mm_renderer.render_material(material, pow(2, get_parameter("size"))) while result is GDScriptFunctionState: result = yield(result, "completed") if !update_again: diff --git a/addons/material_maker/engine/gen_shader.gd b/addons/material_maker/engine/gen_shader.gd index a93e384..1e7fcc7 100644 --- a/addons/material_maker/engine/gen_shader.gd +++ b/addons/material_maker/engine/gen_shader.gd @@ -18,9 +18,11 @@ func toggle_editable() -> bool: func is_editable() -> bool: return editable + func has_randomness() -> bool: return uses_seed + func get_type() -> String: return "shader" @@ -178,7 +180,7 @@ func subst(string : String, context : MMGenContext, uv : String = "") -> Diction if uv != "": var genname_uv = genname+"_"+str(context.get_variant(self, uv)) string = replace_variable(string, "name_uv", genname_uv) - var tmp_string = replace_variable(string, "seed", str(get_seed())) + var tmp_string = replace_variable(string, "seed", "seed_"+genname) if tmp_string != string: string = tmp_string if shader_model.has("parameters") and typeof(shader_model.parameters) == TYPE_ARRAY: @@ -246,6 +248,8 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext) - var output = shader_model.outputs[output_index] if !context.has_variant(self): # Generate functions for gradients + if has_randomness(): + rv.defs += "uniform int seed_%s = %d;\n" % [ genname, get_seed() ] for p in shader_model.parameters: if p.type == "float": rv.defs += "uniform float p_%s_%s = %.9f;\n" % [ genname, p.name, parameters[p.name] ] diff --git a/addons/material_maker/nodes/beehive.mmg b/addons/material_maker/nodes/beehive.mmg index 15f901f..9e9ca63 100644 --- a/addons/material_maker/nodes/beehive.mmg +++ b/addons/material_maker/nodes/beehive.mmg @@ -22,7 +22,7 @@ "type": "f" }, { - "rgb": "rand3(fract($(name_uv)_center.zw/vec2($sx, $sy))+vec2($seed))", + "rgb": "rand3(fract($(name_uv)_center.zw/vec2($sx, $sy))+vec2(float($seed)))", "type": "rgb" }, { diff --git a/addons/material_maker/nodes/bricks.mmg b/addons/material_maker/nodes/bricks.mmg index aba14ac..cc365bb 100644 --- a/addons/material_maker/nodes/bricks.mmg +++ b/addons/material_maker/nodes/bricks.mmg @@ -29,7 +29,7 @@ "type": "f" }, { - "rgb": "rand3(fract($(name_uv)_rect.xy)+rand2(vec2($seed)))", + "rgb": "rand3(fract($(name_uv)_rect.xy)+rand2(vec2(float($seed))))", "type": "rgb" }, { diff --git a/addons/material_maker/nodes/splatter.mmg b/addons/material_maker/nodes/splatter.mmg index 921f71c..1362b70 100644 --- a/addons/material_maker/nodes/splatter.mmg +++ b/addons/material_maker/nodes/splatter.mmg @@ -33,7 +33,7 @@ "name": "Splatter", "outputs": [ { - "f": "splatter_$(name)($uv, int($count), vec2($seed))", + "f": "splatter_$(name)($uv, int($count), vec2(float($seed)))", "type": "f" } ], diff --git a/addons/material_maker/nodes/splatter_color.mmg b/addons/material_maker/nodes/splatter_color.mmg index 4158172..32ae9d7 100644 --- a/addons/material_maker/nodes/splatter_color.mmg +++ b/addons/material_maker/nodes/splatter_color.mmg @@ -33,7 +33,7 @@ "name": "Color Splatter", "outputs": [ { - "rgba": "splatter_$(name)($uv, int($count), vec2($seed))", + "rgba": "splatter_$(name)($uv, int($count), vec2(float($seed)))", "type": "rgba" } ], diff --git a/addons/material_maker/nodes/tiler.mmg b/addons/material_maker/nodes/tiler.mmg index 7469b84..672c963 100644 --- a/addons/material_maker/nodes/tiler.mmg +++ b/addons/material_maker/nodes/tiler.mmg @@ -36,7 +36,7 @@ "name": "Tiler", "outputs": [ { - "f": "tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2($seed))", + "f": "tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2(float($seed)))", "type": "f" } ], diff --git a/addons/material_maker/nodes/tiler_color.mmg b/addons/material_maker/nodes/tiler_color.mmg index ecc07d9..3164a1c 100644 --- a/addons/material_maker/nodes/tiler_color.mmg +++ b/addons/material_maker/nodes/tiler_color.mmg @@ -36,7 +36,7 @@ "name": "Color Tiler", "outputs": [ { - "rgba": "tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2($seed))", + "rgba": "tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2(float($seed)))", "type": "rgba" } ], diff --git a/material_maker/library/base.json b/material_maker/library/base.json index a2962cc..e93c12d 100644 --- a/material_maker/library/base.json +++ b/material_maker/library/base.json @@ -4854,6 +4854,9 @@ { "collapsed": true, "icon": "miscellaneous_buffer", + "parameters": { + "size": 9 + }, "tree_item": "Miscellaneous/Buffer", "type": "buffer" }, diff --git a/material_maker/nodes/generic.gd b/material_maker/nodes/generic.gd index b8c1dd9..1610519 100644 --- a/material_maker/nodes/generic.gd +++ b/material_maker/nodes/generic.gd @@ -5,7 +5,8 @@ var controls = {} var ignore_parameter_change = "" var output_count = 0 -var preview : TextureRect +var preview : ColorRect +#var preview : TextureRect var preview_index : int = -1 var preview_position : int var preview_size : int @@ -151,7 +152,8 @@ func save_preview_widget() -> void: func restore_preview_widget() -> void: if preview == null: - preview = TextureRect.new() + preview = preload("res://material_maker/preview/preview_2d.tscn").instance() + preview.shader = "uniform vec2 size;void fragment() {COLOR = preview_2d(UV);}" preview.visible = false preview_position = get_child_count() if preview.visible: @@ -372,14 +374,9 @@ func update_preview(size : int = 0) -> void: preview_timer.start(0.2) func do_update_preview() -> void: - var result = generator.render(preview_index, preview_size, true) - while result is GDScriptFunctionState: - result = yield(result, "completed") - if preview.texture == null: - preview.texture = ImageTexture.new() - result.copy_to_texture(preview.texture) - result.release() if !preview.visible: add_child(preview) move_child(preview, preview_position) preview.visible = true + preview.set_generator(generator, preview_index) + preview.rect_min_size = Vector2(preview_size, preview_size) diff --git a/material_maker/nodes/generic.tscn b/material_maker/nodes/generic.tscn index 4b1dc50..e260792 100644 --- a/material_maker/nodes/generic.tscn +++ b/material_maker/nodes/generic.tscn @@ -8,3 +8,6 @@ margin_bottom = 29.0 title = "Generic" show_close = true script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} diff --git a/material_maker/preview/preview_2d.gd b/material_maker/preview/preview_2d.gd index 02fc14a..4cfca7e 100644 --- a/material_maker/preview/preview_2d.gd +++ b/material_maker/preview/preview_2d.gd @@ -23,7 +23,14 @@ func set_generator(g : MMGenBase, output : int = 0) -> void: g = null for c in get_children(): c.setup_control(generator, []) + # Update shader material.shader.code = MMGenBase.generate_preview_shader(source, source.type, shader) + # Get parameter values from the shader code + var regex = RegEx.new() + regex.compile("uniform\\s+(\\w+)\\s+([\\w_\\d]+)\\s*=\\s*([^;]+);") + for p in regex.search_all(material.shader.code): + material.set_shader_param(p.strings[2], float(p.strings[3])) + # Set texture params if source.has("textures"): for k in source.textures.keys(): material.set_shader_param(k, source.textures[k]) diff --git a/material_maker/preview/preview_2d.tscn b/material_maker/preview/preview_2d.tscn index 6fb149a..dca101b 100644 --- a/material_maker/preview/preview_2d.tscn +++ b/material_maker/preview/preview_2d.tscn @@ -3,6 +3,7 @@ [ext_resource path="res://material_maker/preview/preview_2d.gd" type="Script" id=1] [sub_resource type="Shader" id=1] +resource_local_to_scene = true [sub_resource type="ShaderMaterial" id=2] resource_local_to_scene = true