From caf682de8834f2ad4d2f073faa8608561f0e9ad5 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Mon, 16 Sep 2019 20:45:47 +0200 Subject: [PATCH] updated generator naming in shaders and added basic convolutions --- addons/material_maker/engine/gen_base.gd | 3 +- .../material_maker/engine/gen_convolution.gd | 60 +++++++++++++++++++ addons/material_maker/engine/gen_material.gd | 21 ++++--- addons/material_maker/engine/gen_shader.gd | 12 ++-- addons/material_maker/engine/gen_texture.gd | 7 ++- addons/material_maker/engine/loader.gd | 3 + addons/material_maker/main_window.tscn | 2 + 7 files changed, 89 insertions(+), 19 deletions(-) diff --git a/addons/material_maker/engine/gen_base.gd b/addons/material_maker/engine/gen_base.gd index 4aa13ce..7d0db5e 100644 --- a/addons/material_maker/engine/gen_base.gd +++ b/addons/material_maker/engine/gen_base.gd @@ -140,12 +140,11 @@ func _serialize(data): return data func serialize(): - var rv = { name=name, parameters={}, node_position={ x=position.x, y=position.y } } + var rv = { name=name, type=get_type(), parameters={}, node_position={ x=position.x, y=position.y } } for p in parameters.keys(): rv.parameters[p] = MMType.serialize_value(parameters[p]) if model != null: rv.type = model else: rv = _serialize(rv) - return rv \ No newline at end of file diff --git a/addons/material_maker/engine/gen_convolution.gd b/addons/material_maker/engine/gen_convolution.gd index aae09ef..59e1e5d 100644 --- a/addons/material_maker/engine/gen_convolution.gd +++ b/addons/material_maker/engine/gen_convolution.gd @@ -1,3 +1,63 @@ tool extends MMGenBase class_name MMGenConvolution + +var convolution_params : Dictionary = {} + +func get_type(): + return "shader" + +func get_type_name(): + if convolution_params.has("name"): + return convolution_params.name + return .get_type_name() + +func get_parameter_defs(): + return [ { name="size", type="size", first=4, last=11, default=4 } ] + +func get_input_defs(): + return [ { name="in", type=convolution_params.input_type } ] + +func get_output_defs(): + return [ { type=convolution_params.output_type } ] + +func set_convolution_params(data: Dictionary): + convolution_params = data + +func _get_shader_code(uv : String, output_index : int, context : MMGenContext): + var genname = "o"+str(get_instance_id()) + var epsilon = 1.0/pow(2, 4+parameters.size) + var types = { "rgba": { type="vec4", init="vec4(0.0)" }, "rgb": { type="vec3", init="vec3(0.0)" }, "f": { type="float", init="0.0" } } + var rv = { defs="", code="", textures={} } + var source = get_source(0) + if source == null: + return rv + var variant_index = context.get_variant(self, uv) + if variant_index == -1: + variant_index = context.get_variant(self, uv) + rv.code += "%s %s_%d = %s;\n" % [ types[convolution_params.output_type].type, genname, variant_index, types[convolution_params.output_type].init ] + for dy in range(-convolution_params.y, convolution_params.y+1): + for dx in range(-convolution_params.x, convolution_params.x+1): + var coef = convolution_params.matrix[dy+convolution_params.y][dx+convolution_params.x] + if typeof(coef) == TYPE_INT: + coef = float(coef) + if typeof(coef) == TYPE_REAL: + coef = Vector3(coef, coef, coef) + if typeof(coef) == TYPE_ARRAY: + coef = Vector3(coef[0], coef[1], coef[2]) + var coef_str = "vec3(%.9f,%.9f,%.9f)" % [ coef.x, coef.y, coef.z ] + var uv_str = "((%s)+vec2(%.9f,%.9f))" % [ uv, dx*epsilon, dy*epsilon ] + var src_code = source.generator.get_shader_code(uv_str, source.output_index, context) + while src_code is GDScriptFunctionState: + src_code = yield(src_code, "completed") + rv.defs += src_code.defs + rv.code += src_code.code + rv.code += "%s_%d += %s*%s;\n" % [ genname, variant_index, coef_str, src_code[convolution_params.input_type] ] + for t in src_code.textures.keys(): + rv.textures[t] = src_code.textures[t] + rv.rgb = "%s_%d" % [ genname, variant_index ] + return rv + +func _serialize(data): + data.convolution_params = convolution_params + return data diff --git a/addons/material_maker/engine/gen_material.gd b/addons/material_maker/engine/gen_material.gd index 77d6f5d..abd407f 100644 --- a/addons/material_maker/engine/gen_material.gd +++ b/addons/material_maker/engine/gen_material.gd @@ -12,22 +12,25 @@ const TEXTURE_LIST = [ { port=1, texture="metallic" }, { port=2, texture="roughness" }, { port=3, texture="emission" }, - { port=4, texture="normal_map" }, + { port=4, texture="normal_texture" }, { port=5, texture="ambient_occlusion" }, - { port=6, texture="depth_map" } + { port=6, texture="depth_texture" } ] const ADDON_TEXTURE_LIST = [ { port=0, texture="albedo" }, { port=3, texture="emission" }, - { port=4, texture="normal_map" }, + { port=4, texture="normal_texture" }, { ports=[1, 2, 5], default_values=["0.0", "1.0", "1.0"], texture="mrao" }, - { port=6, texture="depth_map" } + { port=6, texture="depth_texture" } ] func get_type(): return "material" +func get_type_name(): + return "Material" + func get_parameter_defs(): return [ { name="albedo_color", label="Albedo", type="color", default={ r=1.0, g=1.0, b=1.0, a=1.0} }, @@ -41,7 +44,9 @@ func get_input_defs(): { name="albedo_texture", label="", type="rgb" }, { name="metallic_texture", label="", type="f" }, { name="roughness_texture", label="", type="f" }, - { name="emission_texture", label="", type="rgb" } + { name="emission_texture", label="", type="rgb" }, + { name="normal_texture", label="", type="rgb" }, + { name="depth_texture", label="", type="f" } ] func _ready(): @@ -51,7 +56,6 @@ func _ready(): for t in texture_list: generated_textures[t.texture] = null material = SpatialMaterial.new() - model = material func generate_material(renderer : MMGenRenderer): var source = get_source(0) @@ -111,7 +115,7 @@ func update_spatial_material(m, file_prefix = null): m.emission_texture = texture else: m.emission_enabled = false - texture = get_generated_texture("normal_map", file_prefix) + texture = get_generated_texture("normal_texture", file_prefix) if texture != null: m.normal_enabled = true m.normal_texture = texture @@ -133,7 +137,7 @@ func update_spatial_material(m, file_prefix = null): m.ao_texture = texture else: m.ao_enabled = false - texture = get_generated_texture("depth_map", file_prefix) + texture = get_generated_texture("depth_texture", file_prefix) if texture != null: m.depth_enabled = true #m.depth_scale = parameters.depth_scale @@ -159,5 +163,4 @@ func export_textures(prefix, size = null): resource_filesystem.scan() func _serialize(data): - data.type = "material" return data diff --git a/addons/material_maker/engine/gen_shader.gd b/addons/material_maker/engine/gen_shader.gd index 39ee44d..4fe0902 100644 --- a/addons/material_maker/engine/gen_shader.gd +++ b/addons/material_maker/engine/gen_shader.gd @@ -111,10 +111,11 @@ func replace_variable(string, variable, value): return new_string func subst(string, context, uv = ""): + var genname = "o"+str(get_instance_id()) var required_defs = "" var required_code = "" var required_textures = {} - string = replace_variable(string, "name", name) + string = replace_variable(string, "name", genname) string = replace_variable(string, "seed", str(get_seed())) if uv != "": string = replace_variable(string, "uv", "("+uv+")") @@ -133,7 +134,7 @@ func subst(string, context, uv = ""): 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 = name+"__"+p.name+"_gradient_fct" + value_string = genname+"__"+p.name+"_gradient_fct" elif p.type == "boolean": value_string = "true" if value else "false" else: @@ -164,6 +165,7 @@ func subst(string, context, uv = ""): return { string=string, defs=required_defs, code=required_code, textures=required_textures } func _get_shader_code(uv : String, output_index : int, context : MMGenContext): + var genname = "o"+str(get_instance_id()) var output_info = [ { field="rgba", type="vec4" }, { field="rgb", type="vec3" }, { field="f", type="float" } ] var rv = { defs="", code="", textures={} } var variant_string = uv+","+str(output_index) @@ -183,7 +185,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(name+"__"+p.name+"_gradient_fct") + rv.defs += g.get_shader(genname+"__"+p.name+"_gradient_fct") var variant_index = context.get_variant(self, variant_string) if variant_index == -1: variant_index = context.get_variant(self, variant_string) @@ -194,12 +196,12 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext): subst_output = yield(subst_output, "completed") rv.defs += subst_output.defs rv.code += subst_output.code - rv.code += "%s %s_%d_%d_%s = %s;\n" % [ t.type, name, output_index, variant_index, t.field, subst_output.string ] + rv.code += "%s %s_%d_%d_%s = %s;\n" % [ t.type, genname, output_index, variant_index, t.field, subst_output.string ] for t in subst_output.textures.keys(): rv.textures[t] = subst_output.textures[t] for t in output_info: if output.has(t.field): - rv[t.field] = "%s_%d_%d_%s" % [ name, output_index, variant_index, t.field ] + rv[t.field] = "%s_%d_%d_%s" % [ genname, output_index, variant_index, t.field ] return rv func get_globals(): diff --git a/addons/material_maker/engine/gen_texture.gd b/addons/material_maker/engine/gen_texture.gd index a4a533a..ec8d6f2 100644 --- a/addons/material_maker/engine/gen_texture.gd +++ b/addons/material_maker/engine/gen_texture.gd @@ -12,12 +12,13 @@ func get_output_defs(): return [ { rgba="" } ] func _get_shader_code(uv : String, output_index : int, context : MMGenContext): + var genname = "o"+str(get_instance_id()) var rv = { defs="", code="" } - var texture_name = name+"_tex" + var texture_name = genname+"_tex" var variant_index = context.get_variant(self, uv) if variant_index == -1: variant_index = context.get_variant(self, uv) - rv.code = "vec4 %s_%d = texture(%s, %s);\n" % [ name, variant_index, texture_name, uv ] - rv.rgba = "%s_%d" % [ name, variant_index ] + rv.code = "vec4 %s_%d = texture(%s, %s);\n" % [ genname, variant_index, texture_name, uv ] + rv.rgba = "%s_%d" % [ genname, variant_index ] rv.textures = { texture_name:texture } return rv \ No newline at end of file diff --git a/addons/material_maker/engine/loader.gd b/addons/material_maker/engine/loader.gd index baf9cef..efcf467 100644 --- a/addons/material_maker/engine/loader.gd +++ b/addons/material_maker/engine/loader.gd @@ -35,6 +35,9 @@ static func create_gen(data) -> MMGenBase: elif data.has("shader_model"): generator = MMGenShader.new() generator.set_shader_model(data.shader_model) + elif data.has("convolution_params"): + generator = MMGenConvolution.new() + generator.set_convolution_params(data.convolution_params) elif data.has("model_data"): generator = MMGenShader.new() generator.set_shader_model(data.model_data) diff --git a/addons/material_maker/main_window.tscn b/addons/material_maker/main_window.tscn index a964aaa..fb86ec5 100644 --- a/addons/material_maker/main_window.tscn +++ b/addons/material_maker/main_window.tscn @@ -104,6 +104,8 @@ script = ExtResource( 6 ) [connection signal="tab_changed" from="VBoxContainer/HBoxContainer/Projects" to="." method="_on_Projects_tab_changed"] [connection signal="connection_request" from="VBoxContainer/HBoxContainer/Projects/GraphEdit" to="VBoxContainer/HBoxContainer/Projects/GraphEdit" method="connect_node"] [connection signal="disconnection_request" from="VBoxContainer/HBoxContainer/Projects/GraphEdit" to="VBoxContainer/HBoxContainer/Projects/GraphEdit" method="disconnect_node"] +[connection signal="close_request" from="VBoxContainer/HBoxContainer/Projects/GraphEdit/node_Material" to="VBoxContainer/HBoxContainer/Projects/GraphEdit/node_Material" method="on_close_request"] +[connection signal="offset_changed" from="VBoxContainer/HBoxContainer/Projects/GraphEdit/node_Material" to="VBoxContainer/HBoxContainer/Projects/GraphEdit/node_Material" method="on_offset_changed"] [connection signal="reposition_active_tab_request" from="VBoxContainer/HBoxContainer/Projects/Tabs" to="VBoxContainer/HBoxContainer/Projects" method="move_active_tab_to"] [connection signal="tab_changed" from="VBoxContainer/HBoxContainer/Projects/Tabs" to="VBoxContainer/HBoxContainer/Projects" method="set_current_tab"] [connection signal="tab_close" from="VBoxContainer/HBoxContainer/Projects/Tabs" to="VBoxContainer/HBoxContainer/Projects" method="close_tab"]