diff --git a/addons/material_maker/engine/gen_convolution.gd b/addons/material_maker/engine/gen_convolution.gd index 60c223f..143ea8c 100644 --- a/addons/material_maker/engine/gen_convolution.gd +++ b/addons/material_maker/engine/gen_convolution.gd @@ -62,7 +62,8 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext) - expr_values.push_back(0) errors += 1 print("No value for "+p.name) - expr_variables_x_index = expr_variables.size() + print(p.name+" = "+str(expr_values[expr_values.size()-1])) + expr_variables_x_index = expr_values.size() expr_variables.push_back("x") expr_values.push_back(0) expr_variables.push_back("y") @@ -70,19 +71,22 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext) - var error = expr.parse(convolution_params.matrix_function, expr_variables) if error != OK: print("Error in expression: "+expr.get_error_text()) - return + return rv for dy in range(-convolution_params.y, convolution_params.y+1): var line = [] for dx in range(-convolution_params.x, convolution_params.x+1): var coef = 0.0 - if convolution_params.has("matrix"): + if convolution_params.has("matrix") and dy+convolution_params.y < convolution_params.matrix.size() and dx+convolution_params.x < convolution_params.matrix[dy+convolution_params.y].size() and convolution_params.matrix[dy+convolution_params.y][dx+convolution_params.x] != null: coef = convolution_params.matrix[dy+convolution_params.y][dx+convolution_params.x] + elif convolution_params.has("matrix_sparse") and convolution_params.matrix_sparse.has(str(dy)) and convolution_params.matrix_sparse[str(dy)].has(str(dx)): + coef = convolution_params.matrix_sparse[str(dy)][str(dx)] elif expr != null: expr_values[expr_variables_x_index] = dx expr_values[expr_variables_x_index+1] = dy coef = expr.execute(expr_values) if typeof(coef) == TYPE_INT: coef = float(coef) + print(str(dx)+", "+str(dy)+" = "+str(coef)) match convolution_params.output_type: "f": if typeof(coef) == TYPE_REAL or convolution_params.input_type == "f": @@ -134,7 +138,10 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext) - else: if convolution_params.has("normalized") and convolution_params.normalized: for i in range(sum.size()): - sum[i] = 1.0/sum[i] + if sum[i] != 0: + sum[i] = 1.0/sum[i] + else: + sum[i] = 1.0 else: sum = [ 1.0, 1.0, 1.0, 1.0 ] for dy in range(-convolution_params.y, convolution_params.y+1): @@ -146,17 +153,20 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext) - while src_code is GDScriptFunctionState: src_code = yield(src_code, "completed") # Add global definitions - for d in src_code.globals: - if rv.globals.find(d) == -1: - rv.globals.push_back(d) + if src_code.has("globals"): + for d in src_code.globals: + if rv.globals.find(d) == -1: + rv.globals.push_back(d) # Add generated definitions - rv.defs += src_code.defs + if src_code.has("defs"): + rv.defs += src_code.defs # Add generated code - rv.code += src_code.code + if src_code.has("code"): + rv.code += src_code.code var coef_str : String match convolution_params.output_type: "f": - coef_str = "%.9f" % [ coef[0] * sum[0] ] + coef_str = "%.9f" % [ coef * sum[0] ] "rgb": coef_str = "vec3(%.9f, %.9f, %.9f)" % [ coef[0] * sum[0], coef[1] * sum[1], coef[2] * sum[2] ] "rgba": diff --git a/addons/material_maker/nodes/emboss.mmg b/addons/material_maker/nodes/emboss.mmg new file mode 100644 index 0000000..17e4098 --- /dev/null +++ b/addons/material_maker/nodes/emboss.mmg @@ -0,0 +1,193 @@ +{ + "connections": [ + { + "from": "_2", + "from_port": 0, + "to": "gen_outputs", + "to_port": 0 + }, + { + "from": "gen_inputs", + "from_port": 0, + "to": "emboss_convolution_2_2", + "to_port": 0 + }, + { + "from": "emboss_convolution_2_2", + "from_port": 0, + "to": "_2", + "to_port": 0 + } + ], + "label": "Emboss", + "name": "emboss", + "node_position": { + "x": 0, + "y": 0 + }, + "nodes": [ + { + "convolution_params": { + "input_type": "f", + "matrix_function": "cos(atan2(y, x)-angle*3.14159265359/180.0)", + "matrix_sparse": { + "0": { + "0": 0 + } + }, + "normalized": false, + "output_type": "f", + "parameters": [ + { + "max": 180, + "min": -180, + "name": "angle", + "type": "float" + } + ], + "x": 1, + "y": 1 + }, + "name": "emboss_convolution_2_2", + "node_position": { + "x": -341.916626, + "y": -251.958313 + }, + "parameters": { + "angle": 0, + "size": 9 + }, + "type": "convolution" + }, + { + "name": "_2", + "node_position": { + "x": -337.833313, + "y": -174.166656 + }, + "parameters": { + "amount": 5 + }, + "shader_model": { + "code": "", + "global": "", + "inputs": [ + { + "default": "0.0", + "label": "", + "name": "conv", + "type": "f" + } + ], + "instance": "", + "name": "Emboss postprocess", + "outputs": [ + { + "f": "$amount*($conv($uv))+0.5", + "type": "f" + } + ], + "parameters": [ + { + "default": 0, + "label": "Label", + "max": 10, + "min": 0, + "name": "amount", + "step": 0.01, + "type": "float" + } + ] + }, + "type": "shader" + }, + { + "name": "gen_inputs", + "node_position": { + "x": -461.916626, + "y": -199.062485 + }, + "parameters": { + + }, + "ports": [ + { + "name": "port0", + "type": "rgba" + } + ], + "type": "ios" + }, + { + "name": "gen_outputs", + "node_position": { + "x": -103.833313, + "y": -222.062485 + }, + "parameters": { + + }, + "ports": [ + { + "name": "port0", + "type": "rgba" + } + ], + "type": "ios" + }, + { + "name": "gen_parameters", + "node_position": { + "x": -376.875, + "y": -385.958313 + }, + "parameters": { + "param0": 9, + "param1": 0, + "param2": 5 + }, + "type": "remote", + "widgets": [ + { + "label": "Size", + "linked_widgets": [ + { + "node": "emboss_convolution_2_2", + "widget": "size" + } + ], + "name": "param0", + "type": "linked_control" + }, + { + "label": "Angle", + "linked_widgets": [ + { + "node": "emboss_convolution_2_2", + "widget": "angle" + } + ], + "name": "param1", + "type": "linked_control" + }, + { + "label": "Amount", + "linked_widgets": [ + { + "node": "_2", + "widget": "amount" + } + ], + "name": "param2", + "type": "linked_control" + } + ] + } + ], + "parameters": { + "param0": 9, + "param1": 0, + "param2": 5 + }, + "type": "graph" +} \ No newline at end of file diff --git a/addons/material_maker/nodes/emboss/emboss.gd b/addons/material_maker/nodes/emboss/emboss.gd deleted file mode 100644 index c5d15fb..0000000 --- a/addons/material_maker/nodes/emboss/emboss.gd +++ /dev/null @@ -1,63 +0,0 @@ -tool -extends "res://addons/material_maker/node_base.gd" - -var input_shader = "" -var input_texture -var final_texture - -const CONVOLUTION = { - x=1, - y=1, - kernel=[ - 1, 2, 1, - 0, 0, 0, - -1, -2, -1 - ], - epsilon=1.0/1024, - scale=0.5, - translate=Vector3(0.5, 0.5, 0.5) -} - -const INDICES = [ 0, 1, 2, 5, 8, 7, 6, 3 ] -const COEFS = [ 1, 2, 1, 0, -1, -2, -1, 0 ] - -func _ready() -> void: - $HBoxContainer1/size.clear() - for i in range(7): - $HBoxContainer1/size.add_item(str(int(pow(2, 5+i))), i) - $HBoxContainer1/size.selected = 5 - input_texture = ImageTexture.new() - final_texture = ImageTexture.new() - initialize_properties([ $HBoxContainer1/size, $HBoxContainer2/direction ]) - -func _rerender() -> void: - get_parent().renderer.precalculate_shader(input_shader, get_source().get_textures(), int(pow(2, 5+parameters.size)), input_texture, self, "pass_1", []) - -func pass_1() -> void: - var convolution = CONVOLUTION - convolution.epsilon=1.0/pow(2, 5+parameters.size) - for i in range(8): - convolution.kernel[INDICES[i]] = COEFS[(i+8-int(parameters.direction))%8] - get_parent().renderer.precalculate_shader(get_convolution_shader(convolution), {input=input_texture}, int(pow(2, 5+parameters.size)), final_texture, self, "rerender_targets", []) - -func get_textures() -> Dictionary: - var list = {} - list[name] = final_texture - return list - -func _get_shader_code(uv, slot = 0) -> Dictionary: - var rv = { defs="", code="" } - var src = get_source() - if src == null: - return rv - input_shader = get_parent().renderer.generate_shader(src.get_shader_code_with_globals("UV")) - _rerender() - if generated_variants.empty(): - rv.defs = "uniform sampler2D %s_tex;\n" % [ name ] - var variant_index = generated_variants.find(uv) - if variant_index == -1: - variant_index = generated_variants.size() - generated_variants.append(uv) - rv.code = "vec3 %s_%d_rgb = texture(%s_tex, %s).rgb;\n" % [ name, variant_index, name, uv ] - rv.rgb = "%s_%d_rgb" % [ name, variant_index ] - return rv diff --git a/addons/material_maker/nodes/emboss/emboss.tscn b/addons/material_maker/nodes/emboss/emboss.tscn deleted file mode 100644 index 074e437..0000000 --- a/addons/material_maker/nodes/emboss/emboss.tscn +++ /dev/null @@ -1,186 +0,0 @@ -[gd_scene load_steps=3 format=2] - -[ext_resource path="res://addons/material_maker/nodes/emboss/emboss.gd" type="Script" id=1] - - -[sub_resource type="Theme" id=1] - - -[node name="Emboss" type="GraphNode" index="0"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 1.0 -margin_top = 1.0 -margin_right = 173.0 -margin_bottom = 71.0 -rect_pivot_offset = Vector2( 0, 0 ) -rect_clip_content = false -mouse_filter = 1 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 1 -size_flags_vertical = 1 -theme = SubResource( 1 ) -title = "Emboss" -offset = Vector2( 0, 0 ) -show_close = true -resizable = false -selected = false -comment = false -overlay = 0 -slot/0/left_enabled = true -slot/0/left_type = 0 -slot/0/left_color = Color( 0.5, 0.5, 1, 1 ) -slot/0/right_enabled = true -slot/0/right_type = 0 -slot/0/right_color = Color( 0.5, 0.5, 1, 1 ) -slot/1/left_enabled = false -slot/1/left_type = 0 -slot/1/left_color = Color( 1, 1, 1, 1 ) -slot/1/right_enabled = false -slot/1/right_type = 0 -slot/1/right_color = Color( 1, 1, 1, 1 ) -script = ExtResource( 1 ) -_sections_unfolded = [ "Theme", "slot", "slot/0" ] - -[node name="HBoxContainer1" type="HBoxContainer" parent="." index="0"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 16.0 -margin_top = 24.0 -margin_right = 158.0 -margin_bottom = 44.0 -rect_pivot_offset = Vector2( 0, 0 ) -rect_clip_content = false -mouse_filter = 1 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 3 -size_flags_vertical = 1 -alignment = 0 -_sections_unfolded = [ "Size Flags" ] - -[node name="Label" type="Label" parent="HBoxContainer1" index="0"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_top = 3.0 -margin_right = 63.0 -margin_bottom = 17.0 -rect_pivot_offset = Vector2( 0, 0 ) -rect_clip_content = false -mouse_filter = 2 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 3 -size_flags_vertical = 4 -text = "Grid size:" -percent_visible = 1.0 -lines_skipped = 0 -max_lines_visible = -1 -_sections_unfolded = [ "Anchor", "Margin", "Size Flags" ] - -[node name="size" type="OptionButton" parent="HBoxContainer1" index="1"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 67.0 -margin_right = 142.0 -margin_bottom = 20.0 -rect_min_size = Vector2( 75, 0 ) -rect_pivot_offset = Vector2( 0, 0 ) -rect_clip_content = false -focus_mode = 2 -mouse_filter = 0 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 11 -size_flags_vertical = 1 -toggle_mode = false -action_mode = 0 -enabled_focus_mode = 2 -shortcut = null -group = null -text = "1024" -flat = false -align = 0 -items = [ "32", null, false, 0, null, "64", null, false, 1, null, "128", null, false, 2, null, "256", null, false, 3, null, "512", null, false, 4, null, "1024", null, false, 5, null, "2048", null, false, 6, null ] -selected = 5 -_sections_unfolded = [ "Anchor", "Caret", "Placeholder", "Rect", "Size Flags" ] - -[node name="HBoxContainer2" type="HBoxContainer" parent="." index="1"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 16.0 -margin_top = 44.0 -margin_right = 158.0 -margin_bottom = 64.0 -rect_pivot_offset = Vector2( 0, 0 ) -rect_clip_content = false -mouse_filter = 1 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 3 -size_flags_vertical = 1 -alignment = 0 -_sections_unfolded = [ "Size Flags" ] - -[node name="Label" type="Label" parent="HBoxContainer2" index="0"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_top = 3.0 -margin_right = 63.0 -margin_bottom = 17.0 -rect_pivot_offset = Vector2( 0, 0 ) -rect_clip_content = false -mouse_filter = 2 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 3 -size_flags_vertical = 4 -text = "Direction:" -percent_visible = 1.0 -lines_skipped = 0 -max_lines_visible = -1 -_sections_unfolded = [ "Anchor", "Margin", "Size Flags" ] - -[node name="direction" type="OptionButton" parent="HBoxContainer2" index="1"] - -anchor_left = 0.0 -anchor_top = 0.0 -anchor_right = 0.0 -anchor_bottom = 0.0 -margin_left = 67.0 -margin_right = 142.0 -margin_bottom = 20.0 -rect_min_size = Vector2( 75, 0 ) -rect_pivot_offset = Vector2( 0, 0 ) -rect_clip_content = false -focus_mode = 2 -mouse_filter = 0 -mouse_default_cursor_shape = 0 -size_flags_horizontal = 11 -size_flags_vertical = 1 -toggle_mode = false -action_mode = 0 -enabled_focus_mode = 2 -shortcut = null -group = null -text = "N" -flat = false -align = 0 -items = [ "N", null, false, 0, null, "NE", null, false, 1, null, "E", null, false, 2, null, "SE", null, false, 3, null, "S", null, false, 4, null, "SW", null, false, 5, null, "W", null, false, 6, null, "NW", null, false, 7, null ] -selected = 0 -_sections_unfolded = [ "Caret", "Placeholder", "Rect", "Size Flags" ] - -