2019-08-02 08:17:32 +02:00
|
|
|
tool
|
2019-07-31 20:53:00 +02:00
|
|
|
extends Node
|
|
|
|
class_name MMGenBase
|
|
|
|
|
2019-08-25 23:27:07 +02:00
|
|
|
"""
|
|
|
|
Base class for texture generators, that defines their API
|
|
|
|
"""
|
|
|
|
|
2019-09-22 22:17:26 +02:00
|
|
|
signal parameter_changed
|
|
|
|
|
2019-09-14 09:14:27 +02:00
|
|
|
class InputPort:
|
|
|
|
var generator : MMGenBase = null
|
|
|
|
var input_index : int = 0
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func _init(g : MMGenBase, i : int) -> void:
|
2019-09-14 09:14:27 +02:00
|
|
|
generator = g
|
|
|
|
input_index = i
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func to_str() -> String:
|
2019-09-14 09:14:27 +02:00
|
|
|
return generator.name+".in("+str(input_index)+")"
|
|
|
|
|
2019-08-02 08:17:32 +02:00
|
|
|
class OutputPort:
|
|
|
|
var generator : MMGenBase = null
|
|
|
|
var output_index : int = 0
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func _init(g : MMGenBase, o : int) -> void:
|
2019-08-02 08:17:32 +02:00
|
|
|
generator = g
|
|
|
|
output_index = o
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func to_str() -> String:
|
2019-09-14 09:14:27 +02:00
|
|
|
return generator.name+".out("+str(output_index)+")"
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-08-12 15:58:42 +02:00
|
|
|
var position : Vector2 = Vector2(0, 0)
|
2019-09-09 22:00:18 +02:00
|
|
|
var model = null
|
2019-08-12 15:58:42 +02:00
|
|
|
var parameters = {}
|
|
|
|
|
2019-11-04 07:58:17 +01:00
|
|
|
var seed_locked : bool = false
|
|
|
|
var seed_value : int = 0
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _ready() -> void:
|
2019-08-17 17:35:48 +02:00
|
|
|
init_parameters()
|
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func _post_load() -> void:
|
|
|
|
pass
|
|
|
|
|
2019-10-19 01:30:25 +02:00
|
|
|
func can_be_deleted() -> bool:
|
|
|
|
return true
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func toggle_editable() -> bool:
|
2019-10-20 00:59:51 +02:00
|
|
|
return false
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func is_editable() -> bool:
|
2019-10-20 00:59:51 +02:00
|
|
|
return false
|
|
|
|
|
2019-11-04 07:58:17 +01:00
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func has_randomness() -> bool:
|
2019-11-03 10:04:23 +01:00
|
|
|
return false
|
|
|
|
|
2019-11-04 07:58:17 +01:00
|
|
|
func get_seed() -> int:
|
|
|
|
if !seed_locked:
|
|
|
|
var s : int = ((int(position.x) * 0x1f1f1f1f) ^ int(position.y)) % 65536
|
|
|
|
if get_parent().get("transmits_seed") != null and get_parent().transmits_seed:
|
|
|
|
s += get_parent().get_seed()
|
|
|
|
return s
|
|
|
|
else:
|
|
|
|
return seed_value
|
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func toggle_lock_seed() -> bool:
|
2019-11-04 07:58:17 +01:00
|
|
|
if !seed_locked:
|
|
|
|
seed_value = get_seed()
|
|
|
|
seed_locked = !seed_locked
|
|
|
|
return seed_locked
|
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func is_seed_locked() -> bool:
|
2019-11-04 07:58:17 +01:00
|
|
|
return seed_locked
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func init_parameters() -> void:
|
2019-08-17 17:35:48 +02:00
|
|
|
for p in get_parameter_defs():
|
|
|
|
if !parameters.has(p.name):
|
|
|
|
if p.has("default"):
|
|
|
|
parameters[p.name] = MMType.deserialize_value(p.default)
|
2019-10-14 20:42:11 +02:00
|
|
|
if p.type == "size":
|
|
|
|
parameters[p.name] -= p.first
|
2019-08-17 17:35:48 +02:00
|
|
|
else:
|
|
|
|
print("No default value for parameter "+p.name)
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func set_position(p) -> void:
|
2019-10-02 23:06:20 +02:00
|
|
|
position = p
|
2019-11-04 07:58:17 +01:00
|
|
|
if has_randomness() and !is_seed_locked():
|
2019-11-17 07:40:50 +01:00
|
|
|
source_changed(0)
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_type() -> String:
|
2019-08-12 15:58:42 +02:00
|
|
|
return "generic"
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_type_name() -> String:
|
2019-08-17 17:35:48 +02:00
|
|
|
return "Unnamed"
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_parameter_defs() -> Array:
|
2019-08-17 17:35:48 +02:00
|
|
|
return []
|
|
|
|
|
2019-10-24 09:00:31 +02:00
|
|
|
func get_parameter_def(param_name : String) -> Dictionary:
|
|
|
|
var parameter_defs = get_parameter_defs()
|
|
|
|
for p in parameter_defs:
|
|
|
|
if p.name == param_name:
|
|
|
|
return p
|
|
|
|
return {}
|
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func set_parameter(n : String, v) -> void:
|
2019-08-25 23:27:07 +02:00
|
|
|
parameters[n] = v
|
2019-09-14 09:14:27 +02:00
|
|
|
source_changed(0)
|
2019-09-22 22:17:26 +02:00
|
|
|
emit_signal("parameter_changed", n, v)
|
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func notify_output_change(output_index : int) -> void:
|
2019-09-22 22:17:26 +02:00
|
|
|
var targets = get_targets(output_index)
|
|
|
|
for target in targets:
|
|
|
|
target.generator.source_changed(target.input_index)
|
2019-09-14 09:14:27 +02:00
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func source_changed(__) -> void:
|
2019-10-09 20:39:08 +02:00
|
|
|
emit_signal("parameter_changed", "__input_changed__", 0)
|
2019-09-14 09:14:27 +02:00
|
|
|
for i in range(get_output_defs().size()):
|
2019-09-22 22:17:26 +02:00
|
|
|
notify_output_change(i)
|
2019-08-25 23:27:07 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_input_defs() -> Array:
|
2019-08-17 17:35:48 +02:00
|
|
|
return []
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_output_defs() -> Array:
|
2019-08-17 17:35:48 +02:00
|
|
|
return []
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_source(input_index : int) -> OutputPort:
|
2019-08-02 08:17:32 +02:00
|
|
|
return get_parent().get_port_source(name, input_index)
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func get_targets(output_index : int) -> Array:
|
2019-10-21 23:58:14 +02:00
|
|
|
var parent = get_parent()
|
|
|
|
if parent != null:
|
|
|
|
return get_parent().get_port_targets(name, output_index)
|
|
|
|
return []
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-10-19 01:30:25 +02:00
|
|
|
# get the list of outputs that depend on the input whose index is passed as parameter
|
|
|
|
func follow_input(input_index : int) -> Array:
|
|
|
|
var rv = []
|
|
|
|
for i in range(get_output_defs().size()):
|
|
|
|
rv.push_back(OutputPort.new(self, i))
|
|
|
|
return rv
|
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func get_input_shader(input_index : int) -> Dictionary:
|
2019-08-02 08:17:32 +02:00
|
|
|
var source = get_source(input_index)
|
|
|
|
if source != null:
|
|
|
|
return source.get_shader()
|
2019-11-06 23:49:26 +01:00
|
|
|
return {}
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_shader(output_index : int, context) -> Dictionary:
|
|
|
|
return get_shader_code("UV", output_index, context)
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-11-19 07:16:09 +01:00
|
|
|
func render(output_index : int, size : int) -> Object:
|
2019-11-06 22:33:01 +01:00
|
|
|
var context : MMGenContext = MMGenContext.new()
|
2019-08-17 17:35:48 +02:00
|
|
|
var source = get_shader_code("UV", output_index, context)
|
2019-08-18 16:28:50 +02:00
|
|
|
while source is GDScriptFunctionState:
|
|
|
|
source = yield(source, "completed")
|
2019-10-20 16:22:06 +02:00
|
|
|
if source.empty():
|
2019-10-05 11:04:50 +02:00
|
|
|
source = { defs="", code="", textures={}, rgba="vec4(0.0)" }
|
2019-11-06 22:33:01 +01:00
|
|
|
var shader : String = mm_renderer.generate_shader(source)
|
|
|
|
var result = mm_renderer.render_shader(shader, source.textures, size)
|
2019-10-05 11:04:50 +02:00
|
|
|
while result is GDScriptFunctionState:
|
|
|
|
result = yield(result, "completed")
|
|
|
|
return result
|
2019-08-17 17:35:48 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_shader_code(uv : String, output_index : int, context : MMGenContext) -> Dictionary:
|
2019-08-17 17:35:48 +02:00
|
|
|
var rv = _get_shader_code(uv, output_index, context)
|
2019-08-18 16:28:50 +02:00
|
|
|
while rv is GDScriptFunctionState:
|
|
|
|
rv = yield(rv, "completed")
|
2019-10-20 16:22:06 +02:00
|
|
|
if !rv.empty():
|
2019-08-12 15:58:42 +02:00
|
|
|
if !rv.has("f"):
|
|
|
|
if rv.has("rgb"):
|
|
|
|
rv.f = "(dot("+rv.rgb+", vec3(1.0))/3.0)"
|
|
|
|
elif rv.has("rgba"):
|
|
|
|
rv.f = "(dot("+rv.rgba+".rgb, vec3(1.0))/3.0)"
|
|
|
|
else:
|
|
|
|
rv.f = "0.0"
|
|
|
|
if !rv.has("rgb"):
|
|
|
|
if rv.has("rgba"):
|
|
|
|
rv.rgb = rv.rgba+".rgb"
|
|
|
|
else:
|
|
|
|
rv.rgb = "vec3("+rv.f+")"
|
|
|
|
if !rv.has("rgba"):
|
|
|
|
rv.rgba = "vec4("+rv.rgb+", 1.0)"
|
2019-08-02 08:17:32 +02:00
|
|
|
return rv
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _get_shader_code(__, __, __) -> Dictionary:
|
|
|
|
return {}
|
2019-09-09 22:00:18 +02:00
|
|
|
|
2019-11-04 07:58:17 +01:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _serialize(data: Dictionary) -> Dictionary:
|
2019-09-09 22:00:18 +02:00
|
|
|
print("cannot save "+name)
|
|
|
|
return data
|
|
|
|
|
2019-11-06 23:49:26 +01:00
|
|
|
func serialize() -> Dictionary:
|
2019-09-16 20:45:47 +02:00
|
|
|
var rv = { name=name, type=get_type(), parameters={}, node_position={ x=position.x, y=position.y } }
|
2019-09-09 22:00:18 +02:00
|
|
|
for p in parameters.keys():
|
|
|
|
rv.parameters[p] = MMType.serialize_value(parameters[p])
|
2019-11-04 07:58:17 +01:00
|
|
|
if seed_locked:
|
|
|
|
rv.seed_value = seed_value
|
2019-09-09 22:00:18 +02:00
|
|
|
if model != null:
|
|
|
|
rv.type = model
|
|
|
|
else:
|
|
|
|
rv = _serialize(rv)
|
2019-10-18 08:35:54 +02:00
|
|
|
return rv
|
2019-11-04 07:58:17 +01:00
|
|
|
|
|
|
|
func _deserialize(data : Dictionary) -> void:
|
|
|
|
pass
|
|
|
|
|
|
|
|
func deserialize(data : Dictionary) -> void:
|
|
|
|
_deserialize(data)
|
|
|
|
if data.has("name"):
|
|
|
|
name = data.name
|
|
|
|
if data.has("node_position"):
|
|
|
|
position.x = data.node_position.x
|
|
|
|
position.y = data.node_position.y
|
|
|
|
if data.has("parameters"):
|
|
|
|
for p in data.parameters.keys():
|
|
|
|
set_parameter(p, MMType.deserialize_value(data.parameters[p]))
|
|
|
|
else:
|
|
|
|
for p in get_parameter_defs():
|
|
|
|
if data.has(p.name) and p.name != "type":
|
|
|
|
set_parameter(p.name, MMType.deserialize_value(data[p.name]))
|
|
|
|
if data.has("seed_value"):
|
|
|
|
seed_locked = true
|
|
|
|
seed_value = data.seed_value
|
|
|
|
else:
|
|
|
|
seed_locked = false
|
|
|
|
_post_load()
|