2018-07-22 17:20:27 +02:00
|
|
|
tool
|
|
|
|
extends GraphEdit
|
|
|
|
|
2018-07-28 09:37:51 +02:00
|
|
|
signal graph_changed
|
|
|
|
|
2018-07-22 17:20:27 +02:00
|
|
|
func _ready():
|
|
|
|
pass
|
|
|
|
|
|
|
|
func get_source(node, port):
|
|
|
|
for c in get_connection_list():
|
|
|
|
if c.to == node && c.to_port == port:
|
|
|
|
return { node=c.from, slot=c.from_port }
|
|
|
|
|
2018-07-30 08:45:19 +02:00
|
|
|
func add_node(node, position = null):
|
|
|
|
add_child(node)
|
|
|
|
if position != null:
|
|
|
|
node.offset = position
|
|
|
|
node.connect("close_request", self, "remove_node", [ node ])
|
|
|
|
|
|
|
|
func add_node_globalpos(node, global_position):
|
|
|
|
add_node(node, (scroll_offset + global_position - rect_global_position) / zoom)
|
|
|
|
|
2018-07-22 17:20:27 +02:00
|
|
|
func remove_node(node):
|
2018-07-30 08:45:19 +02:00
|
|
|
var node_name = node.name
|
2018-07-22 17:20:27 +02:00
|
|
|
for c in get_connection_list():
|
2018-07-30 08:45:19 +02:00
|
|
|
if c.from == node_name or c.to == node_name:
|
2018-07-24 08:04:25 +02:00
|
|
|
disconnect_node(c.from, c.from_port, c.to, c.to_port)
|
2018-07-30 08:45:19 +02:00
|
|
|
node.queue_free()
|
|
|
|
send_changed_signal()
|
|
|
|
|
2018-08-03 08:16:38 +02:00
|
|
|
func _on_GraphEdit_connection_request(from, from_slot, to, to_slot):
|
|
|
|
var disconnect = get_source(to, to_slot)
|
|
|
|
if disconnect != null:
|
|
|
|
disconnect_node(disconnect.node, disconnect.slot, to, to_slot)
|
|
|
|
connect_node(from, from_slot, to, to_slot)
|
|
|
|
send_changed_signal();
|
|
|
|
|
|
|
|
func _on_GraphEdit_disconnection_request(from, from_slot, to, to_slot):
|
|
|
|
disconnect_node(from, from_slot, to, to_slot)
|
|
|
|
send_changed_signal();
|
|
|
|
|
|
|
|
func clear_material():
|
|
|
|
clear_connections()
|
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode:
|
|
|
|
remove_child(c)
|
|
|
|
c.free()
|
|
|
|
|
|
|
|
func new_material():
|
|
|
|
clear_material()
|
|
|
|
var node_type = load("res://addons/procedural_material/nodes/material.tscn")
|
|
|
|
if node_type != null:
|
|
|
|
var node = node_type.instance()
|
|
|
|
add_node(node)
|
|
|
|
do_send_changed_signal()
|
|
|
|
|
2018-07-30 08:45:19 +02:00
|
|
|
func load_file(filename):
|
|
|
|
var file = File.new()
|
|
|
|
if file.open(filename, File.READ) != OK:
|
|
|
|
return
|
|
|
|
var data = parse_json(file.get_as_text())
|
|
|
|
file.close()
|
2018-08-03 08:16:38 +02:00
|
|
|
clear_material()
|
2018-07-30 08:45:19 +02:00
|
|
|
for n in data.nodes:
|
|
|
|
if !n.has("type"):
|
|
|
|
continue
|
|
|
|
var node_type = load("res://addons/procedural_material/nodes/"+n.type+".tscn")
|
|
|
|
if node_type != null:
|
|
|
|
var node = node_type.instance()
|
|
|
|
node.name = n.name
|
|
|
|
add_node(node)
|
|
|
|
node.deserialize(n)
|
|
|
|
for c in data.connections:
|
|
|
|
connect_node(c.from, c.from_port, c.to, c.to_port)
|
|
|
|
do_send_changed_signal()
|
|
|
|
|
|
|
|
func save_file(filename):
|
|
|
|
var data = { nodes = [] }
|
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode:
|
|
|
|
data.nodes.append(c.serialize())
|
|
|
|
data.connections = get_connection_list()
|
|
|
|
var file = File.new()
|
|
|
|
if file.open(filename, File.WRITE) == OK:
|
|
|
|
file.store_string(to_json(data))
|
|
|
|
file.close()
|
2018-07-24 08:04:25 +02:00
|
|
|
|
2018-07-28 09:37:51 +02:00
|
|
|
func send_changed_signal():
|
|
|
|
$Timer.start()
|
|
|
|
|
|
|
|
func do_send_changed_signal():
|
|
|
|
emit_signal("graph_changed")
|
|
|
|
|
2018-08-01 07:50:58 +02:00
|
|
|
func generate_shader(node):
|
2018-07-25 06:20:36 +02:00
|
|
|
var code
|
2018-08-03 08:16:38 +02:00
|
|
|
code = "shader_type canvas_item;\n\n"
|
2018-07-24 08:04:25 +02:00
|
|
|
var file = File.new()
|
2018-08-03 08:16:38 +02:00
|
|
|
file.open("res://addons/procedural_material/common.shader", File.READ)
|
2018-07-24 08:04:25 +02:00
|
|
|
code += file.get_as_text()
|
|
|
|
code += "\n"
|
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode:
|
|
|
|
c.generated = false
|
|
|
|
c.generated_variants = []
|
|
|
|
var src_code = node.get_shader_code("UV")
|
|
|
|
var shader_code = src_code.defs
|
|
|
|
shader_code += "void fragment() {\n"
|
|
|
|
shader_code += src_code.code
|
2018-08-03 08:16:38 +02:00
|
|
|
if src_code.has("rgb"):
|
|
|
|
shader_code += "COLOR = vec4("+src_code.rgb+", 1.0);\n"
|
|
|
|
elif src_code.has("f"):
|
|
|
|
shader_code += "COLOR = vec4(vec3("+src_code.f+"), 1.0);\n"
|
2018-07-24 08:04:25 +02:00
|
|
|
else:
|
2018-08-03 08:16:38 +02:00
|
|
|
shader_code += "COLOR = vec4(1.0);\n"
|
2018-07-24 08:04:25 +02:00
|
|
|
shader_code += "}\n"
|
2018-07-28 09:37:51 +02:00
|
|
|
#print("GENERATED SHADER:\n"+shader_code)
|
2018-07-24 08:04:25 +02:00
|
|
|
code += shader_code
|
|
|
|
return code
|
2018-07-29 23:43:24 +02:00
|
|
|
|
|
|
|
func setup_material(shader_material, textures, shader_code):
|
|
|
|
for k in textures.keys():
|
|
|
|
shader_material.set_shader_param(k+"_tex", textures[k])
|
|
|
|
shader_material.shader.code = shader_code
|
|
|
|
|
2018-08-03 08:16:38 +02:00
|
|
|
# Save shader to image
|
|
|
|
|
|
|
|
var render_queue = []
|
|
|
|
|
|
|
|
func render_to_viewport(node, size, method, args):
|
|
|
|
render_queue.append( { shader=generate_shader(node), textures=node.get_textures(), size=size, method=method, args=args } )
|
|
|
|
if render_queue.size() == 1:
|
|
|
|
while !render_queue.empty():
|
|
|
|
var job = render_queue.front()
|
|
|
|
$SaveViewport.size = Vector2(job.size, job.size)
|
|
|
|
$SaveViewport/ColorRect.rect_position = Vector2(0, 0)
|
|
|
|
$SaveViewport/ColorRect.rect_size = Vector2(job.size, job.size)
|
|
|
|
var shader_material = $SaveViewport/ColorRect.material
|
|
|
|
shader_material.shader.code = job.shader
|
|
|
|
for k in job.textures.keys():
|
|
|
|
shader_material.set_shader_param(k+"_tex", job.textures[k])
|
|
|
|
$SaveViewport.render_target_update_mode = Viewport.UPDATE_ONCE
|
|
|
|
$SaveViewport.update_worlds()
|
|
|
|
yield(get_tree(), "idle_frame")
|
|
|
|
yield(get_tree(), "idle_frame")
|
|
|
|
yield(get_tree(), "idle_frame")
|
|
|
|
callv(job.method, job.args)
|
|
|
|
render_queue.pop_front()
|
|
|
|
|
2018-07-29 23:43:24 +02:00
|
|
|
func export_texture(node, filename, size = 256):
|
2018-08-03 08:16:38 +02:00
|
|
|
if node == null:
|
|
|
|
return null
|
|
|
|
render_to_viewport(node, size, "do_export_texture", [ filename ])
|
|
|
|
|
|
|
|
func do_export_texture(filename):
|
|
|
|
var viewport_texture = $SaveViewport.get_texture()
|
|
|
|
var viewport_image = viewport_texture.get_data()
|
|
|
|
viewport_image.save_png(filename)
|
2018-07-29 23:43:24 +02:00
|
|
|
|
|
|
|
func precalculate_texture(node, size, object, method, args):
|
|
|
|
if node == null:
|
|
|
|
return null
|
2018-08-03 08:16:38 +02:00
|
|
|
render_to_viewport(node, size, "do_precalculate_texture", [ object, method, args ])
|
|
|
|
|
|
|
|
func do_precalculate_texture(object, method, args):
|
2018-07-29 23:43:24 +02:00
|
|
|
var viewport_texture = $SaveViewport.get_texture()
|
|
|
|
var texture = ImageTexture.new()
|
|
|
|
texture.create_from_image(viewport_texture.get_data())
|
|
|
|
args.append(texture)
|
|
|
|
object.callv(method, args)
|
|
|
|
|