diff --git a/addons/material_maker/engine/gen_debug.gd b/addons/material_maker/engine/gen_debug.gd new file mode 100644 index 0000000..7065181 --- /dev/null +++ b/addons/material_maker/engine/gen_debug.gd @@ -0,0 +1,16 @@ +tool +extends MMGenBase +class_name MMGenDebug + +""" +Can be used to get generated shader +""" + +func get_type() -> String: + return "debug" + +func get_type_name() -> String: + return "Debug" + +func get_input_defs() -> Array: + return [ { name="in", type="rgba" } ] diff --git a/addons/material_maker/engine/gen_export.gd b/addons/material_maker/engine/gen_export.gd new file mode 100644 index 0000000..7ebc3e7 --- /dev/null +++ b/addons/material_maker/engine/gen_export.gd @@ -0,0 +1,54 @@ +tool +extends MMGenBase +class_name MMGenExport + +""" +Can be used to export an additional texture +""" + +var texture = null + +# The default texture size as a power-of-two exponent +const TEXTURE_SIZE_DEFAULT = 10 # 1024x1024 + +func get_image_size() -> int: + var rv : int + if parameters.has("size"): + rv = int(pow(2, parameters.size)) + else: + rv = int(pow(2, TEXTURE_SIZE_DEFAULT)) + return rv + +func get_type() -> String: + return "export" + +func get_type_name() -> String: + return "Export" + +func get_parameter_defs() -> Array: + return [ + { name="size", type="size", first=4, last=12, default=10 }, + { name="suffix", type="string", default="suffix" } + ] + +func get_input_defs() -> Array: + return [ { name="in", type="rgba" } ] + +func render_textures(renderer : MMGenRenderer) -> void: + print("rendering texture...") + var source = get_source(0) + if source != null: + var result = source.generator.render(source.output_index, renderer, get_image_size()) + while result is GDScriptFunctionState: + result = yield(result, "completed") + texture = ImageTexture.new() + result.copy_to_texture(texture) + result.release() + else: + texture = null + +func export_textures(prefix, __ = null) -> void: + print("exporting texture") + if texture != null: + var image = texture.get_data() + image.save_png("%s_%s.png" % [ prefix, parameters.suffix]) diff --git a/addons/material_maker/engine/gen_shader.gd b/addons/material_maker/engine/gen_shader.gd index ca87782..7c0a77b 100644 --- a/addons/material_maker/engine/gen_shader.gd +++ b/addons/material_maker/engine/gen_shader.gd @@ -176,7 +176,7 @@ func subst(string, context, uv = "") -> Dictionary: elif p.type == "color": value_string = "vec4(%.9f, %.9f, %.9f, %.9f)" % [ value.r, value.g, value.b, value.a ] elif p.type == "gradient": - value_string = genname+"__"+p.name+"_gradient_fct" + value_string = genname+"_p_"+p.name+"_gradient_fct" elif p.type == "boolean": value_string = "true" if value else "false" else: @@ -232,7 +232,7 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext) - if !(g is MMGradient): g = MMGradient.new() g.deserialize(parameters[p.name]) - rv.defs += g.get_shader(genname+"__"+p.name+"_gradient_fct") + rv.defs += g.get_shader(genname+"_p_"+p.name+"_gradient_fct") # Add inline code if shader_model.has("code"): var variant_index = context.get_variant(self, uv) diff --git a/addons/material_maker/engine/loader.gd b/addons/material_maker/engine/loader.gd index 648c1c6..1b6beeb 100644 --- a/addons/material_maker/engine/loader.gd +++ b/addons/material_maker/engine/loader.gd @@ -48,7 +48,9 @@ static func create_gen(data) -> MMGenBase: material = MMGenMaterial, buffer = MMGenBuffer, image = MMGenImage, - switch = MMGenSwitch + switch = MMGenSwitch, + export = MMGenExport, + debug = MMGenDebug } var generator = null if data.has("connections") and data.has("nodes"): @@ -69,23 +71,17 @@ static func create_gen(data) -> MMGenBase: generator = MMGenRemote.new() generator.set_widgets(data.widgets.duplicate(true)) elif data.has("type"): - if data.type == "material": - generator = MMGenMaterial.new() - elif data.type == "buffer": - generator = MMGenBuffer.new() + if types.has(data.type): + generator = types[data.type].new() elif data.type == "comment": generator = MMGenComment.new() if data.has("text"): generator.text = data.text if data.has("size"): generator.size = Vector2(data.size.x, data.size.y) - elif data.type == "image": - generator = MMGenImage.new() elif data.type == "ios": generator = MMGenIOs.new() generator.ports = data.ports - elif data.type == "switch": - generator = MMGenSwitch.new() else: var file = File.new() var gen_paths = [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ] diff --git a/addons/material_maker/graph_edit.gd b/addons/material_maker/graph_edit.gd index 2e751d1..d9fcb24 100644 --- a/addons/material_maker/graph_edit.gd +++ b/addons/material_maker/graph_edit.gd @@ -207,8 +207,11 @@ func export_textures() -> void: if save_path != null: var prefix = save_path.left(save_path.rfind(".")) for c in get_children(): - if c is GraphNode and c.generator.has_method("export_textures"): - c.generator.export_textures(prefix, editor_interface) + if c is GraphNode: + if c.generator.has_method("render_textures"): + c.generator.render_textures(renderer) + if c.generator.has_method("export_textures"): + c.generator.export_textures(prefix, editor_interface) # Cut / copy / paste @@ -270,8 +273,10 @@ func paste(pos = Vector2(0, 0)) -> void: if c is GraphNode: c.selected = false var data = parse_json(OS.clipboard) - for c in create_nodes(data, scroll_offset+0.5*rect_size): - c.selected = true + var new_nodes = create_nodes(data, scroll_offset+0.5*rect_size) + if new_nodes != null: + for c in new_nodes: + c.selected = true # Delay after graph update diff --git a/addons/material_maker/library/base.json b/addons/material_maker/library/base.json index 3d9383c..b0ef25c 100644 --- a/addons/material_maker/library/base.json +++ b/addons/material_maker/library/base.json @@ -406,5 +406,15 @@ { "tree_item":"Miscellaneous/Comment", "type":"comment" + }, + { + "tree_item":"Miscellaneous/Export", + "type":"export", + "parameters":{"size":10} + }, + { + "tree_item":"Miscellaneous/Debug", + "icon":"debug", + "type":"debug" } ]} \ No newline at end of file diff --git a/addons/material_maker/library/base/debug.png b/addons/material_maker/library/base/debug.png new file mode 100644 index 0000000..ca85e2d Binary files /dev/null and b/addons/material_maker/library/base/debug.png differ diff --git a/addons/material_maker/nodes/debug.gd b/addons/material_maker/nodes/debug.gd new file mode 100644 index 0000000..5cce0e9 --- /dev/null +++ b/addons/material_maker/nodes/debug.gd @@ -0,0 +1,32 @@ +extends MMGraphNodeBase + +static func generate_shader(src_code) -> String: + var code + code = "" + var file = File.new() + file.open("res://addons/material_maker/common.shader", File.READ) + code += file.get_as_text() + code += "\n" + if src_code.has("textures"): + for t in src_code.textures.keys(): + code += "uniform sampler2D "+t+";\n" + if src_code.has("globals"): + for g in src_code.globals: + code += g + var shader_code = src_code.defs + shader_code += "\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\nvec2 UV = fragCoord/iResolution.xy;\n" + shader_code += src_code.code + shader_code += "fragColor = "+src_code.rgba+";\n" + shader_code += "}\n" + #print("GENERATED SHADER:\n"+shader_code) + code += shader_code + return code + +func _on_Button_pressed(): + var src = generator.get_source(0) + if src != null: + var context : MMGenContext = MMGenContext.new(null) + var source = src.generator.get_shader_code("UV", src.output_index, context) + var popup = preload("res://addons/material_maker/nodes/debug/debug_popup.tscn").instance() + get_parent().add_child(popup) + popup.show_code(generate_shader(source)) diff --git a/addons/material_maker/nodes/debug.tscn b/addons/material_maker/nodes/debug.tscn new file mode 100644 index 0000000..52a95e0 --- /dev/null +++ b/addons/material_maker/nodes/debug.tscn @@ -0,0 +1,24 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://addons/material_maker/nodes/debug.gd" type="Script" id=1] + +[node name="Debug" type="GraphNode"] +margin_right = 124.0 +margin_bottom = 49.0 +title = "Debug" +show_close = true +slot/0/left_enabled = true +slot/0/left_type = 0 +slot/0/left_color = Color( 0, 1, 0, 0.501961 ) +slot/0/right_enabled = false +slot/0/right_type = 0 +slot/0/right_color = Color( 1, 1, 1, 1 ) +script = ExtResource( 1 ) + +[node name="Button" type="Button" parent="."] +margin_left = 16.0 +margin_top = 24.0 +margin_right = 108.0 +margin_bottom = 44.0 +text = "Show shader" +[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"] diff --git a/addons/material_maker/nodes/debug/debug_popup.gd b/addons/material_maker/nodes/debug/debug_popup.gd new file mode 100644 index 0000000..82447eb --- /dev/null +++ b/addons/material_maker/nodes/debug/debug_popup.gd @@ -0,0 +1,5 @@ +extends Popup + +func show_code(text : String) -> void: + $TextEdit.text = text + popup_centered() \ No newline at end of file diff --git a/addons/material_maker/nodes/debug/debug_popup.tscn b/addons/material_maker/nodes/debug/debug_popup.tscn new file mode 100644 index 0000000..2a8747e --- /dev/null +++ b/addons/material_maker/nodes/debug/debug_popup.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://addons/material_maker/nodes/debug/debug_popup.gd" type="Script" id=1] + +[node name="Popup" type="Popup"] +visible = true +margin_right = 739.0 +margin_bottom = 662.0 +rect_min_size = Vector2( 500, 300 ) +script = ExtResource( 1 ) + +[node name="TextEdit" type="TextEdit" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +syntax_highlighting = true +show_line_numbers = true diff --git a/addons/material_maker/nodes/generic.gd b/addons/material_maker/nodes/generic.gd index 9c145a6..d02e8c5 100644 --- a/addons/material_maker/nodes/generic.gd +++ b/addons/material_maker/nodes/generic.gd @@ -25,7 +25,7 @@ func on_parameter_changed(p, v) -> void: elif controls.has(p): var o = controls[p] if o is LineEdit: - o.text = str(v) + o.text = v elif o is SpinBox: o.value = v elif o is HSlider: @@ -81,7 +81,7 @@ func update_shaders() -> void: func _on_text_changed(new_text, variable) -> void: ignore_parameter_change = variable - generator.set_parameter(variable, float(new_text)) + generator.set_parameter(variable, new_text) ignore_parameter_change = "" update_shaders() @@ -135,6 +135,8 @@ func create_parameter_control(p : Dictionary) -> Control: control = ColorPickerButton.new() elif p.type == "gradient": control = preload("res://addons/material_maker/widgets/gradient_editor.tscn").instance() + elif p.type == "string": + control = LineEdit.new() return control func save_preview_widget() -> void: diff --git a/project.godot b/project.godot index 210aeb8..82e6091 100644 --- a/project.godot +++ b/project.godot @@ -35,6 +35,16 @@ _global_script_classes=[ { "path": "res://addons/material_maker/engine/gen_convolution.gd" }, { "base": "MMGenBase", +"class": "MMGenDebug", +"language": "GDScript", +"path": "res://addons/material_maker/engine/gen_debug.gd" +}, { +"base": "MMGenBase", +"class": "MMGenExport", +"language": "GDScript", +"path": "res://addons/material_maker/engine/gen_export.gd" +}, { +"base": "MMGenBase", "class": "MMGenGraph", "language": "GDScript", "path": "res://addons/material_maker/engine/gen_graph.gd" @@ -135,6 +145,8 @@ _global_script_class_icons={ "MMGenComment": "", "MMGenContext": "", "MMGenConvolution": "", +"MMGenDebug": "", +"MMGenExport": "", "MMGenGraph": "", "MMGenIOs": "", "MMGenImage": "",