2021-10-03 19:56:35 +02:00
|
|
|
tool
|
|
|
|
class_name MMMateial
|
|
|
|
extends Resource
|
|
|
|
|
2021-10-31 11:29:36 +01:00
|
|
|
#threads are implemented using my thread pool engine module.
|
|
|
|
#if you want to use this without that module in your engine set this to false,
|
|
|
|
#and comment out the lines that give errors
|
|
|
|
const USE_THREADS = true
|
|
|
|
|
2021-10-04 13:09:43 +02:00
|
|
|
export(Vector2) var image_size : Vector2 = Vector2(128, 128)
|
2021-10-03 19:56:35 +02:00
|
|
|
export(Array) var nodes : Array
|
2021-10-04 13:09:43 +02:00
|
|
|
|
2021-10-09 14:04:09 +02:00
|
|
|
var initialized : bool = false
|
2021-10-31 11:29:36 +01:00
|
|
|
var rendering : bool = false
|
|
|
|
var queued_render : bool = false
|
2021-10-31 12:37:51 +01:00
|
|
|
var job : ThreadPoolExecuteJob = ThreadPoolExecuteJob.new()
|
2021-10-09 14:04:09 +02:00
|
|
|
|
|
|
|
func initialize():
|
|
|
|
if !initialized:
|
|
|
|
initialized = true
|
|
|
|
|
2021-10-31 12:37:51 +01:00
|
|
|
job.setup(self, "_thread_func")
|
|
|
|
|
2021-10-09 14:04:09 +02:00
|
|
|
for n in nodes:
|
|
|
|
n.connect("changed", self, "on_node_changed")
|
2021-10-09 13:33:59 +02:00
|
|
|
|
2021-10-04 13:09:43 +02:00
|
|
|
func add_node(node : MMNode) -> void:
|
|
|
|
nodes.append(node)
|
2021-10-09 13:33:59 +02:00
|
|
|
|
|
|
|
node.connect("changed", self, "on_node_changed")
|
|
|
|
|
|
|
|
emit_changed()
|
|
|
|
|
|
|
|
func remove_node(node : MMNode) -> void:
|
2021-10-24 12:49:49 +02:00
|
|
|
if !node:
|
|
|
|
return
|
|
|
|
|
|
|
|
for op in node.output_properties:
|
|
|
|
for n in nodes:
|
|
|
|
if n:
|
|
|
|
for ip in n.input_properties:
|
|
|
|
if ip.input_property == op:
|
|
|
|
ip.set_input_property(null)
|
|
|
|
|
2021-10-09 13:33:59 +02:00
|
|
|
nodes.erase(node)
|
|
|
|
|
|
|
|
node.disconnect("changed", self, "on_node_changed")
|
|
|
|
|
2021-10-04 13:09:43 +02:00
|
|
|
emit_changed()
|
2021-10-09 13:33:59 +02:00
|
|
|
|
|
|
|
func render() -> void:
|
2022-01-21 13:35:14 +01:00
|
|
|
initialize()
|
|
|
|
|
2021-10-31 11:29:36 +01:00
|
|
|
if USE_THREADS:
|
|
|
|
render_threaded()
|
|
|
|
else:
|
|
|
|
render_non_threaded()
|
|
|
|
|
|
|
|
func render_non_threaded() -> void:
|
2021-10-09 13:33:59 +02:00
|
|
|
var did_render : bool = true
|
|
|
|
|
|
|
|
while did_render:
|
|
|
|
did_render = false
|
|
|
|
|
|
|
|
for n in nodes:
|
|
|
|
if n && n.render(self):
|
|
|
|
did_render = true
|
|
|
|
|
2021-10-31 11:29:36 +01:00
|
|
|
func render_threaded() -> void:
|
2021-10-31 12:37:51 +01:00
|
|
|
job.cancelled = false
|
|
|
|
|
2021-10-31 11:29:36 +01:00
|
|
|
if rendering:
|
|
|
|
queued_render = true
|
|
|
|
return
|
|
|
|
|
|
|
|
if !initialized:
|
|
|
|
initialize()
|
|
|
|
|
2021-10-31 12:37:51 +01:00
|
|
|
if !ThreadPool.has_job(job):
|
|
|
|
ThreadPool.add_job(job)
|
2021-10-31 11:29:36 +01:00
|
|
|
|
|
|
|
func _thread_func() -> void:
|
2021-10-31 12:37:51 +01:00
|
|
|
if job.cancelled:
|
2021-10-31 11:37:16 +01:00
|
|
|
rendering = false
|
|
|
|
return
|
|
|
|
|
2021-10-31 11:29:36 +01:00
|
|
|
rendering = true
|
2021-10-31 12:37:51 +01:00
|
|
|
job.cancelled = false
|
2021-10-31 11:29:36 +01:00
|
|
|
|
|
|
|
var did_render : bool = true
|
|
|
|
|
|
|
|
while did_render:
|
|
|
|
did_render = false
|
|
|
|
|
|
|
|
for n in nodes:
|
|
|
|
if n && n.render(self):
|
|
|
|
did_render = true
|
|
|
|
|
2021-10-31 12:37:51 +01:00
|
|
|
if job.cancelled:
|
2021-10-31 11:37:16 +01:00
|
|
|
rendering = false
|
|
|
|
return
|
|
|
|
|
2021-10-31 11:29:36 +01:00
|
|
|
rendering = false
|
|
|
|
|
|
|
|
if queued_render:
|
|
|
|
queued_render = false
|
|
|
|
_thread_func()
|
|
|
|
|
2021-10-31 12:37:51 +01:00
|
|
|
func cancel_render_and_wait() -> void:
|
|
|
|
if rendering:
|
|
|
|
ThreadPool.cancel_task_wait(job)
|
|
|
|
|
|
|
|
job.cancelled = false
|
2021-10-31 13:03:25 +01:00
|
|
|
|
|
|
|
pass
|
2021-10-31 12:37:51 +01:00
|
|
|
|
2021-10-09 13:33:59 +02:00
|
|
|
func on_node_changed() -> void:
|
|
|
|
call_deferred("render")
|