2019-12-08 08:50:14 +01:00
|
|
|
extends ColorRect
|
|
|
|
|
2020-03-13 07:50:04 +01:00
|
|
|
export(String, MULTILINE) var shader : String = ""
|
2019-12-27 22:30:49 +01:00
|
|
|
|
2020-03-13 07:50:04 +01:00
|
|
|
var generator : MMGenBase = null
|
2020-03-25 19:06:31 +01:00
|
|
|
var output : int = 0
|
2019-12-10 08:00:55 +01:00
|
|
|
|
2020-03-25 19:06:31 +01:00
|
|
|
func set_generator(g : MMGenBase, o : int = 0) -> void:
|
|
|
|
if is_instance_valid(generator):
|
|
|
|
generator.disconnect("parameter_changed", self, "on_parameter_changed")
|
2020-04-03 08:01:06 +02:00
|
|
|
var source = MMGenBase.DEFAULT_GENERATED_SHADER
|
2019-12-27 22:30:49 +01:00
|
|
|
if is_instance_valid(g):
|
|
|
|
generator = g
|
2020-03-25 19:06:31 +01:00
|
|
|
output = o
|
|
|
|
generator.connect("parameter_changed", self, "on_parameter_changed")
|
2020-03-13 07:50:04 +01:00
|
|
|
var gen_output_defs = generator.get_output_defs()
|
|
|
|
if ! gen_output_defs.empty():
|
|
|
|
var context : MMGenContext = MMGenContext.new()
|
2020-03-20 07:44:22 +01:00
|
|
|
source = generator.get_shader_code("uv", output, context)
|
2020-03-13 07:50:04 +01:00
|
|
|
while source is GDScriptFunctionState:
|
|
|
|
source = yield(source, "completed")
|
|
|
|
if source.empty():
|
2020-04-03 08:01:06 +02:00
|
|
|
source = MMGenBase.DEFAULT_GENERATED_SHADER
|
2019-12-27 22:30:49 +01:00
|
|
|
else:
|
2020-04-13 09:32:59 +02:00
|
|
|
generator = null
|
2020-03-24 19:21:28 +01:00
|
|
|
# Update shader
|
2020-03-13 07:50:04 +01:00
|
|
|
material.shader.code = MMGenBase.generate_preview_shader(source, source.type, shader)
|
2020-03-24 19:21:28 +01:00
|
|
|
# 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
|
2020-03-18 21:40:02 +01:00
|
|
|
if source.has("textures"):
|
|
|
|
for k in source.textures.keys():
|
|
|
|
material.set_shader_param(k, source.textures[k])
|
2020-03-13 07:50:04 +01:00
|
|
|
|
2020-03-25 19:06:31 +01:00
|
|
|
func on_parameter_changed(n : String, v) -> void:
|
2020-03-28 09:50:51 +01:00
|
|
|
if n == "__output_changed__" and output == v:
|
2020-03-25 22:01:02 +01:00
|
|
|
set_generator(generator, output)
|
2020-03-25 19:06:31 +01:00
|
|
|
var p = generator.get_parameter_def(n)
|
|
|
|
if p.has("type"):
|
|
|
|
match p.type:
|
2020-03-26 21:58:26 +01:00
|
|
|
"float", "color", "gradient":
|
2020-03-25 19:06:31 +01:00
|
|
|
pass
|
|
|
|
_:
|
|
|
|
set_generator(generator, output)
|
|
|
|
|
2020-03-26 21:24:50 +01:00
|
|
|
func on_float_parameters_changed(parameter_changes : Dictionary) -> void:
|
|
|
|
for n in parameter_changes.keys():
|
|
|
|
for p in VisualServer.shader_get_param_list(material.shader.get_rid()):
|
|
|
|
if p.name == n:
|
|
|
|
material.set_shader_param(n, parameter_changes[n])
|
|
|
|
break
|
2020-03-13 07:50:04 +01:00
|
|
|
|
|
|
|
func on_resized() -> void:
|
|
|
|
material.set_shader_param("size", rect_size)
|
2019-12-27 22:30:49 +01:00
|
|
|
|
2020-04-13 09:32:59 +02:00
|
|
|
func _on_Export_id_pressed(id):
|
|
|
|
var dialog = FileDialog.new()
|
|
|
|
add_child(dialog)
|
|
|
|
dialog.rect_min_size = Vector2(500, 500)
|
|
|
|
dialog.access = FileDialog.ACCESS_FILESYSTEM
|
|
|
|
dialog.mode = FileDialog.MODE_SAVE_FILE
|
|
|
|
dialog.add_filter("*.png;PNG image file")
|
|
|
|
dialog.connect("file_selected", self, "export_as_png", [ 256 << id ])
|
|
|
|
dialog.popup_centered()
|
2019-12-27 22:30:49 +01:00
|
|
|
|
2020-04-13 09:32:59 +02:00
|
|
|
func export_as_png(file_name : String, size : int) -> void:
|
|
|
|
var previous_size = material.get_shader_param("size")
|
|
|
|
var previous_margin = material.get_shader_param("margin")
|
|
|
|
var previous_show_tiling = material.get_shader_param("show_tiling")
|
|
|
|
material.set_shader_param("size", Vector2(size, size))
|
|
|
|
material.set_shader_param("margin", 0.0)
|
|
|
|
material.set_shader_param("show_tiling", false)
|
|
|
|
var result = mm_renderer.render_material(material, size, false)
|
|
|
|
while result is GDScriptFunctionState:
|
|
|
|
result = yield(result, "completed")
|
|
|
|
result.save_to_file(file_name)
|
|
|
|
result.release()
|
|
|
|
material.set_shader_param("size", previous_size)
|
|
|
|
material.set_shader_param("margin", previous_margin)
|
|
|
|
material.set_shader_param("show_tiling", previous_show_tiling)
|
2019-12-27 22:30:49 +01:00
|
|
|
|