2019-08-17 17:35:48 +02:00
|
|
|
tool
|
2019-08-25 23:27:07 +02:00
|
|
|
extends MMGenTexture
|
2019-07-31 20:53:00 +02:00
|
|
|
class_name MMGenBuffer
|
2019-08-17 17:35:48 +02:00
|
|
|
|
2019-08-25 23:27:07 +02:00
|
|
|
"""
|
|
|
|
Texture generator buffers, that render their input in a specific resolution and provide the result as output.
|
2019-10-20 16:22:06 +02:00
|
|
|
This is useful when using generators that sample their inputs several times (such as convolutions)
|
2019-08-25 23:27:07 +02:00
|
|
|
"""
|
2019-08-18 16:28:50 +02:00
|
|
|
|
2020-03-18 21:40:02 +01:00
|
|
|
var material : ShaderMaterial = null
|
|
|
|
var updating : bool = false
|
|
|
|
var update_again : bool = false
|
2019-09-14 09:14:27 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _ready() -> void:
|
2020-03-18 21:40:02 +01:00
|
|
|
material = ShaderMaterial.new()
|
|
|
|
material.shader = Shader.new()
|
2019-08-17 17:35:48 +02:00
|
|
|
if !parameters.has("size"):
|
2019-10-21 23:58:14 +02:00
|
|
|
parameters.size = 9
|
2020-03-18 21:40:02 +01:00
|
|
|
add_to_group("preview")
|
2019-08-17 17:35:48 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_type() -> String:
|
2019-08-17 17:35:48 +02:00
|
|
|
return "buffer"
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_type_name() -> String:
|
2019-08-17 17:35:48 +02:00
|
|
|
return "Buffer"
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_parameter_defs() -> Array:
|
2019-10-29 22:12:14 +01:00
|
|
|
return [
|
|
|
|
{ name="size", type="size", first=4, last=12, default=4 },
|
|
|
|
{ name="lod", type="float", min=0, max=10.0, step=0.01, default=0 }
|
|
|
|
]
|
2019-08-17 17:35:48 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_input_defs() -> Array:
|
2019-08-18 16:28:50 +02:00
|
|
|
return [ { name="in", type="rgba" } ]
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func get_output_defs() -> Array:
|
2019-10-29 22:12:14 +01:00
|
|
|
return [ { type="rgba" }, { type="rgba" } ]
|
2019-08-17 17:35:48 +02:00
|
|
|
|
2020-03-18 21:40:02 +01:00
|
|
|
func source_changed(_input_port_index : int) -> void:
|
2020-03-24 19:21:28 +01:00
|
|
|
call_deferred("update_shader")
|
|
|
|
|
|
|
|
func update_shader() -> void:
|
2020-03-18 21:40:02 +01:00
|
|
|
var context : MMGenContext = MMGenContext.new()
|
|
|
|
var source = {}
|
|
|
|
var source_output = get_source(0)
|
|
|
|
if source_output != null:
|
|
|
|
source = source_output.generator.get_shader_code("uv", source_output.output_index, context)
|
|
|
|
while source is GDScriptFunctionState:
|
|
|
|
source = yield(source, "completed")
|
|
|
|
if source.empty():
|
|
|
|
source = { defs="", code="", textures={}, rgba="vec4(0.0)" }
|
|
|
|
material.shader.code = mm_renderer.generate_shader(source)
|
|
|
|
if source.has("textures"):
|
|
|
|
for k in source.textures.keys():
|
|
|
|
material.set_shader_param(k, source.textures[k])
|
|
|
|
update_buffer()
|
2019-09-14 09:14:27 +02:00
|
|
|
|
2020-03-18 21:40:02 +01:00
|
|
|
func on_float_parameter_changed(n : String, v : float) -> void:
|
2020-03-20 07:44:22 +01:00
|
|
|
for p in VisualServer.shader_get_param_list(material.shader.get_rid()):
|
|
|
|
if p.name == n:
|
|
|
|
material.set_shader_param(n, v)
|
|
|
|
update_buffer()
|
|
|
|
return
|
|
|
|
|
|
|
|
func on_texture_changed(n : String) -> void:
|
|
|
|
for p in VisualServer.shader_get_param_list(material.shader.get_rid()):
|
|
|
|
if p.name == n:
|
|
|
|
update_buffer()
|
|
|
|
return
|
2020-03-18 21:40:02 +01:00
|
|
|
|
2020-03-20 07:44:22 +01:00
|
|
|
func update_buffer() -> void:
|
2020-03-18 21:40:02 +01:00
|
|
|
update_again = true
|
|
|
|
if !updating:
|
|
|
|
updating = true
|
|
|
|
while update_again:
|
|
|
|
update_again = false
|
2020-03-24 19:21:28 +01:00
|
|
|
var result = mm_renderer.render_material(material, pow(2, get_parameter("size")))
|
2020-03-18 21:40:02 +01:00
|
|
|
while result is GDScriptFunctionState:
|
|
|
|
result = yield(result, "completed")
|
|
|
|
if !update_again:
|
|
|
|
result.copy_to_texture(texture)
|
|
|
|
result.release()
|
|
|
|
updating = false
|
2020-03-20 07:44:22 +01:00
|
|
|
get_tree().call_group("preview", "on_texture_changed", "o%s_tex" % str(get_instance_id()))
|
2019-09-09 22:00:18 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _serialize(data: Dictionary) -> Dictionary:
|
2019-09-09 22:00:18 +02:00
|
|
|
data.type = "buffer"
|
|
|
|
return data
|