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
|
2019-12-10 08:00:55 +01:00
|
|
|
|
2020-03-13 07:50:04 +01:00
|
|
|
func set_generator(g : MMGenBase) -> void:
|
|
|
|
if generator != null and is_instance_valid(generator):
|
|
|
|
generator.disconnect("float_param_changed", self, "on_float_param_changed")
|
|
|
|
var source = { defs="", code="", textures={}, type="f", f="1.0" }
|
2019-12-27 22:30:49 +01:00
|
|
|
if is_instance_valid(g):
|
|
|
|
generator = g
|
|
|
|
var param_defs : Array = generator.get_parameter_defs()
|
|
|
|
for c in get_children():
|
|
|
|
c.setup_control(generator, param_defs)
|
2020-03-13 07:50:04 +01:00
|
|
|
generator.connect("float_param_changed", self, "on_float_param_changed")
|
|
|
|
var gen_output_defs = generator.get_output_defs()
|
|
|
|
if ! gen_output_defs.empty():
|
|
|
|
var context : MMGenContext = MMGenContext.new()
|
|
|
|
source = generator.get_shader_code("uv", 0, context)
|
|
|
|
while source is GDScriptFunctionState:
|
|
|
|
source = yield(source, "completed")
|
|
|
|
if source.empty():
|
|
|
|
source = { defs="", code="", textures={}, type="f", f="1.0" }
|
2019-12-27 22:30:49 +01:00
|
|
|
else:
|
|
|
|
g = null
|
|
|
|
for c in get_children():
|
|
|
|
c.setup_control(generator, [])
|
2020-03-13 07:50:04 +01:00
|
|
|
material.shader.code = MMGenBase.generate_preview_shader(source, source.type, shader)
|
|
|
|
|
|
|
|
func on_float_parameter_changed(n : String, v : float) -> void:
|
|
|
|
material.set_shader_param(n, v)
|
|
|
|
|
|
|
|
func on_resized() -> void:
|
|
|
|
material.set_shader_param("size", rect_size)
|
|
|
|
set_generator(generator)
|
2019-12-27 22:30:49 +01:00
|
|
|
|
|
|
|
func value_to_pos(value : Vector2) -> Vector2:
|
2019-12-29 18:12:35 +01:00
|
|
|
return rect_size*0.5+value*min(rect_size.x, rect_size.y)/1.2
|
2019-12-27 22:30:49 +01:00
|
|
|
|
|
|
|
func value_to_offset(value : Vector2) -> Vector2:
|
|
|
|
return value*min(rect_size.x, rect_size.y)/1.2
|
|
|
|
|
|
|
|
func pos_to_value(pos : Vector2) -> Vector2:
|
2019-12-29 18:12:35 +01:00
|
|
|
return (pos - rect_size*0.5)*1.2/min(rect_size.x, rect_size.y)
|