Updated convolution node and added gaussian blur

This commit is contained in:
RodZill4 2019-10-20 22:37:42 +02:00
parent 80f8071d7b
commit d376aa22f8
4 changed files with 28 additions and 29 deletions

View File

@ -94,18 +94,16 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext):
sum[0] += coef sum[0] += coef
sum[1] += coef sum[1] += coef
sum[2] += coef sum[2] += coef
if convolution_params.input_type == "f": coef = [ coef, coef, coef ]
coef = "vec3(%.9f)" % [ coef ] if convolution_params.input_type != "f" and convolution_params.input_type != "rgb":
elif convolution_params.input_type == "rgb":
coef = "%.9f" % coef
else:
errors += 1 errors += 1
if typeof(coef) == TYPE_ARRAY and coef.size() == 3: elif typeof(coef) == TYPE_ARRAY and coef.size() == 3:
if convolution_params.input_type == "f" or convolution_params.input_type == "rgb": if convolution_params.input_type == "f" or convolution_params.input_type == "rgb":
sum[0] += coef[0] sum[0] += coef[0]
sum[1] += coef[1] sum[1] += coef[1]
sum[2] += coef[2] sum[2] += coef[2]
coef = "vec3(%.9f,%.9f,%.9f)" % [ coef[0], coef[1], coef[2] ] else:
errors += 1
else: else:
errors += 1 errors += 1
"rgba": "rgba":
@ -114,19 +112,17 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext):
sum[1] += coef sum[1] += coef
sum[2] += coef sum[2] += coef
sum[3] += coef sum[3] += coef
if convolution_params.input_type == "f": coef = [ coef, coef, coef, coef ]
coef = "vec4(%.9f)" % [ coef ] if convolution_params.input_type != "f" and convolution_params.input_type != "rgba":
if convolution_params.input_type == "rgba":
coef = "%.9f" % coef
else:
errors += 1 errors += 1
if typeof(coef) == TYPE_ARRAY and coef.size() == 3: elif typeof(coef) == TYPE_ARRAY and coef.size() == 4:
if convolution_params.input_type == "f" or convolution_params.input_type == "rgba": if convolution_params.input_type == "f" or convolution_params.input_type == "rgba":
sum[0] += coef[0] sum[0] += coef[0]
sum[1] += coef[1] sum[1] += coef[1]
sum[2] += coef[2] sum[2] += coef[2]
sum[3] += coef[3] sum[3] += coef[3]
coef = "vec4(%.9f,%.9f,%.9f,%.9f)" % [ coef[0], coef[1], coef[2], coef[3] ] else:
errors += 1
else: else:
errors += 1 errors += 1
line.push_back(coef) line.push_back(coef)
@ -136,11 +132,16 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext):
if errors > 0: if errors > 0:
pass pass
else: else:
if convolution_params.has("normalized") and convolution_params.normalized:
for i in range(sum.size()):
sum[i] = 1.0/sum[i]
else:
sum = [ 1.0, 1.0, 1.0, 1.0 ]
for dy in range(-convolution_params.y, convolution_params.y+1): for dy in range(-convolution_params.y, convolution_params.y+1):
var line = matrix[dy+convolution_params.y] var line = matrix[dy+convolution_params.y]
for dx in range(-convolution_params.x, convolution_params.x+1): for dx in range(-convolution_params.x, convolution_params.x+1):
var coef = line[dx+convolution_params.x] var coef = line[dx+convolution_params.x]
var uv_str = "((%s)+vec2(%.9f,%.9f))" % [ uv, dx*epsilon, dy*epsilon ] var uv_str = "fract((%s)+vec2(%.9f,%.9f))" % [ uv, dx*epsilon, dy*epsilon ]
var src_code = source.generator.get_shader_code(uv_str, source.output_index, context) var src_code = source.generator.get_shader_code(uv_str, source.output_index, context)
while src_code is GDScriptFunctionState: while src_code is GDScriptFunctionState:
src_code = yield(src_code, "completed") src_code = yield(src_code, "completed")
@ -152,21 +153,17 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext):
rv.defs += src_code.defs rv.defs += src_code.defs
# Add generated code # Add generated code
rv.code += src_code.code rv.code += src_code.code
rv.code += "%s_%d += %s*%s;\n" % [ genname, variant_index, coef, src_code[convolution_params.input_type] ] var coef_str : String
for t in src_code.textures.keys():
rv.textures[t] = src_code.textures[t]
# normalize
if false:
var sum_str : String
var count : float = float((2*convolution_params.x+1) / (2*convolution_params.y+1))
match convolution_params.output_type: match convolution_params.output_type:
"f": "f":
sum_str = "%.9f" % [ sum[0] / count ] coef_str = "%.9f" % [ coef[0] * sum[0] ]
"rgb": "rgb":
sum_str = "vec3(%.9f, %.9f, %.9f)" % [ sum[0] / count, sum[1] / count, sum[2] / count ] coef_str = "vec3(%.9f, %.9f, %.9f)" % [ coef[0] * sum[0], coef[1] * sum[1], coef[2] * sum[2] ]
"rgba": "rgba":
sum_str = "vec4(%.9f, %.9f, %.9f, %.9f)" % [ sum[0] / count, sum[1] / count, sum[2] / count, sum[3] / count ] coef_str = "vec4(%.9f, %.9f, %.9f, %.9f)" % [ coef[0] * sum[0], coef[1] * sum[1], coef[2] * sum[2], coef[3] * sum[3] ]
rv.code += "%s_%d /= %s;\n" % [ genname, variant_index, sum_str ] 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[convolution_params.output_type] = "%s_%d" % [ genname, variant_index ] rv[convolution_params.output_type] = "%s_%d" % [ genname, variant_index ]
return rv return rv

View File

@ -0,0 +1 @@
{"connections":[{"from":"buffer","from_port":0,"to":"blurx_convolution","to_port":0},{"from":"buffer_2","from_port":0,"to":"blurx_convolution_2","to_port":0},{"from":"switch","from_port":0,"to":"buffer_2","to_port":0},{"from":"gen_inputs","from_port":0,"to":"buffer","to_port":0},{"from":"gen_inputs","from_port":0,"to":"switch","to_port":1},{"from":"blurx_convolution","from_port":0,"to":"switch","to_port":0},{"from":"switch_2","from_port":0,"to":"gen_outputs","to_port":0},{"from":"blurx_convolution_2","from_port":0,"to":"switch_2","to_port":0},{"from":"switch","from_port":0,"to":"switch_2","to_port":1}],"label":"Gaussian Blur","name":"gaussian_blur","node_position":{"x":0,"y":0},"nodes":[{"convolution_params":{"input_type":"rgba","matrix_function":"exp(-0.5*(pow(x/sigma, 2.0)))/(6.28318530718 *sigma*sigma)","normalized":true,"output_type":"rgba","parameters":[{"max":50,"min":0.05,"name":"sigma","type":"float"}],"x":50,"y":0},"name":"blurx_convolution","node_position":{"x":-407.5,"y":-217.5},"parameters":{"sigma":10.175,"size":5},"type":"convolution"},{"name":"buffer_2","node_position":{"x":-408.875,"y":-90.625},"parameters":{"size":5},"type":"buffer"},{"convolution_params":{"input_type":"rgba","matrix_function":"exp(-0.5*(pow(y/sigma, 2.0)))/(6.28318530718 *sigma*sigma)","normalized":true,"output_type":"rgba","parameters":[{"max":50,"min":0.05,"name":"sigma","type":"float"}],"x":0,"y":50},"name":"blurx_convolution_2","node_position":{"x":-407.125,"y":-44.375},"parameters":{"sigma":10.175,"size":5},"type":"convolution"},{"name":"switch","node_position":{"x":-437.452393,"y":-147.166656},"parameters":{"choices":2,"outputs":1,"source":0},"type":"switch"},{"name":"switch_2","node_position":{"x":-323.452393,"y":-173.666656},"parameters":{"choices":2,"outputs":1,"source":0},"type":"switch"},{"name":"buffer","node_position":{"x":-408.25,"y":-265.75},"parameters":{"size":5},"type":"buffer"},{"name":"gen_parameters","node_position":{"x":-438.666626,"y":-391.666656},"parameters":{"param0":5,"param1":10.175,"param2":0},"type":"remote","widgets":[{"label":"Grid size:","linked_widgets":[{"node":"buffer","widget":"size"},{"node":"blurx_convolution","widget":"size"},{"node":"buffer_2","widget":"size"},{"node":"blurx_convolution_2","widget":"size"}],"type":"linked_control"},{"label":"Sigma:","linked_widgets":[{"node":"blurx_convolution","widget":"sigma"},{"node":"blurx_convolution_2","widget":"sigma"}],"type":"linked_control"},{"configurations":{"Both":[{"node":"switch","value":0,"widget":"source"},{"node":"switch_2","value":0,"widget":"source"}],"X":[{"node":"switch","value":0,"widget":"source"},{"node":"switch_2","value":1,"widget":"source"}],"Y":[{"node":"switch","value":1,"widget":"source"},{"node":"switch_2","value":0,"widget":"source"}]},"label":"Direction:","linked_widgets":[{"node":"switch","widget":"source"},{"node":"switch_2","widget":"source"}],"type":"config_control"}]},{"name":"gen_inputs","node_position":{"x":-738.666626,"y":-190.392853},"parameters":{},"ports":[{"name":"port0","type":"rgba"}],"type":"ios"},{"name":"gen_outputs","node_position":{"x":-23.452393,"y":-190.392853},"parameters":{},"ports":[{"name":"port0","type":"rgba"}],"type":"ios"}],"parameters":{"param0":5,"param2":0},"type":"graph"}

View File

@ -1,3 +1,4 @@
tool
extends Popup extends Popup
signal item_double_clicked(generator) signal item_double_clicked(generator)

View File

@ -1 +1 @@
{"connections":[{"from":"bricks","from_port":0,"to":"buffer","to_port":0},{"from":"buffer","from_port":0,"to":"blurx_convolution","to_port":0}],"label":"Graph","name":"49","node_position":{"x":0,"y":0},"nodes":[{"name":"Material","node_position":{"x":0,"y":0},"parameters":{"albedo_color":{"a":1,"b":1,"g":1,"r":1,"type":"Color"},"ao_light_affect":1,"depth_scale":1,"emission_energy":1,"metallic":1,"normal_scale":1,"roughness":1,"size":2},"type":"material"},{"convolution_params":{"input_type":"rgba","matrix_function":"exp(-0.5*(pow(float(x)/float(sigma), 2.0)))/(6.28318530718 *sigma*sigma)","output_type":"rgba","parameters":[{"max":50,"min":0.05,"name":"sigma","type":"float"}],"x":50,"y":0},"name":"blurx_convolution","node_position":{"x":-223.5,"y":-124.5},"parameters":{"size":4},"type":"convolution"},{"name":"bricks","node_position":{"x":-522.25,"y":-158.75},"parameters":{"bevel":0.1,"columns":3,"mortar":0.1,"pattern":0,"repeat":1,"row_offset":0.5,"rows":6},"type":"bricks"},{"name":"buffer","node_position":{"x":-212.25,"y":-189.75},"parameters":{"size":4},"type":"buffer"}],"parameters":{},"type":"graph"} {"connections":[{"from":"bricks","from_port":0,"to":"graph","to_port":0},{"from":"bricks","from_port":0,"to":"graph","to_port":0},{"from":"graph","from_port":0,"to":"Material","to_port":0}],"label":"Graph","name":"49","node_position":{"x":0,"y":0},"nodes":[{"name":"Material","node_position":{"x":-44,"y":-208},"parameters":{"albedo_color":{"a":1,"b":1,"g":1,"r":1,"type":"Color"},"ao_light_affect":1,"depth_scale":1,"emission_energy":1,"metallic":1,"normal_scale":1,"roughness":1,"size":2},"type":"material"},{"name":"bricks","node_position":{"x":-749.25,"y":-215.75},"parameters":{"bevel":0,"columns":3,"mortar":0.173828,"pattern":0,"repeat":1,"row_offset":0.5,"rows":6},"type":"bricks"},{"connections":[{"from":"buffer","from_port":0,"to":"blurx_convolution","to_port":0},{"from":"buffer_2","from_port":0,"to":"blurx_convolution_2","to_port":0},{"from":"switch","from_port":0,"to":"buffer_2","to_port":0},{"from":"gen_inputs","from_port":0,"to":"buffer","to_port":0},{"from":"gen_inputs","from_port":0,"to":"switch","to_port":1},{"from":"blurx_convolution","from_port":0,"to":"switch","to_port":0},{"from":"switch_2","from_port":0,"to":"gen_outputs","to_port":0},{"from":"blurx_convolution_2","from_port":0,"to":"switch_2","to_port":0},{"from":"switch","from_port":0,"to":"switch_2","to_port":1}],"label":"Gaussian Blur","name":"graph","node_position":{"x":-411.61734,"y":-216.392853},"nodes":[{"convolution_params":{"input_type":"rgba","matrix_function":"exp(-0.5*(pow(x/sigma, 2.0)))/(6.28318530718 *sigma*sigma)","normalized":true,"output_type":"rgba","parameters":[{"max":50,"min":0.05,"name":"sigma","type":"float"}],"x":50,"y":0},"name":"blurx_convolution","node_position":{"x":-407.5,"y":-217.5},"parameters":{"sigma":10.175,"size":5},"type":"convolution"},{"name":"buffer_2","node_position":{"x":-408.875,"y":-90.625},"parameters":{"size":5},"type":"buffer"},{"convolution_params":{"input_type":"rgba","matrix_function":"exp(-0.5*(pow(y/sigma, 2.0)))/(6.28318530718 *sigma*sigma)","normalized":true,"output_type":"rgba","parameters":[{"max":50,"min":0.05,"name":"sigma","type":"float"}],"x":0,"y":50},"name":"blurx_convolution_2","node_position":{"x":-407.125,"y":-44.375},"parameters":{"sigma":10.175,"size":5},"type":"convolution"},{"name":"switch","node_position":{"x":-437.452393,"y":-147.166656},"parameters":{"choices":2,"outputs":1,"source":0},"type":"switch"},{"name":"switch_2","node_position":{"x":-323.452393,"y":-173.666656},"parameters":{"choices":2,"outputs":1,"source":0},"type":"switch"},{"name":"buffer","node_position":{"x":-408.25,"y":-265.75},"parameters":{"size":5},"type":"buffer"},{"name":"gen_parameters","node_position":{"x":-438.666626,"y":-391.666656},"parameters":{"param0":5,"param1":10.175,"param2":0},"type":"remote","widgets":[{"label":"Grid size:","linked_widgets":[{"node":"buffer","widget":"size"},{"node":"blurx_convolution","widget":"size"},{"node":"buffer_2","widget":"size"},{"node":"blurx_convolution_2","widget":"size"}],"type":"linked_control"},{"label":"Sigma:","linked_widgets":[{"node":"blurx_convolution","widget":"sigma"},{"node":"blurx_convolution_2","widget":"sigma"}],"type":"linked_control"},{"configurations":{"Both":[{"node":"switch","value":0,"widget":"source"},{"node":"switch_2","value":0,"widget":"source"}],"X":[{"node":"switch","value":0,"widget":"source"},{"node":"switch_2","value":1,"widget":"source"}],"Y":[{"node":"switch","value":1,"widget":"source"},{"node":"switch_2","value":0,"widget":"source"}]},"label":"Direction:","linked_widgets":[{"node":"switch","widget":"source"},{"node":"switch_2","widget":"source"}],"type":"config_control"}]},{"name":"gen_inputs","node_position":{"x":-738.666626,"y":-190.392853},"parameters":{},"ports":[{"name":"port0","type":"rgba"}],"type":"ios"},{"name":"gen_outputs","node_position":{"x":-23.452393,"y":-190.392853},"parameters":{},"ports":[{"name":"port0","type":"rgba"}],"type":"ios"}],"parameters":{"param0":5,"param2":0},"type":"graph"}],"parameters":{},"type":"graph"}