2018-07-22 17:20:27 +02:00
|
|
|
tool
|
|
|
|
extends GraphEdit
|
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
var save_path = null
|
2018-08-09 22:19:27 +02:00
|
|
|
var need_save = false
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
signal save_path_changed
|
2018-07-28 09:37:51 +02:00
|
|
|
signal graph_changed
|
|
|
|
|
2018-07-22 17:20:27 +02:00
|
|
|
func _ready():
|
2018-08-19 02:11:52 +02:00
|
|
|
$SaveViewport/ColorRect.material = $SaveViewport/ColorRect.material.duplicate(true)
|
2018-08-12 19:25:18 +02:00
|
|
|
OS.low_processor_usage_mode = true
|
2018-08-22 07:33:50 +02:00
|
|
|
center_view()
|
|
|
|
|
|
|
|
func _gui_input(event):
|
|
|
|
if event is InputEventKey and event.pressed:
|
|
|
|
if event.scancode == KEY_C:
|
|
|
|
center_view()
|
|
|
|
elif event.scancode == KEY_DELETE:
|
|
|
|
remove_selection()
|
|
|
|
|
|
|
|
# Misc. useful functions
|
2018-07-22 17:20:27 +02:00
|
|
|
|
|
|
|
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-08-22 07:33:50 +02:00
|
|
|
func offset_from_global_position(global_position):
|
|
|
|
return (scroll_offset + global_position - rect_global_position) / zoom
|
|
|
|
|
|
|
|
func add_node(node):
|
2018-07-30 08:45:19 +02:00
|
|
|
add_child(node)
|
|
|
|
node.connect("close_request", self, "remove_node", [ node ])
|
|
|
|
|
2018-08-12 19:25:18 +02:00
|
|
|
func connect_node(from, from_slot, to, to_slot):
|
|
|
|
var source_list = [ from ]
|
2018-08-19 02:11:52 +02:00
|
|
|
# Check if the new connection creates a cycle in the graph
|
2018-08-12 19:25:18 +02:00
|
|
|
while !source_list.empty():
|
|
|
|
var source = source_list.pop_front()
|
|
|
|
if source == to:
|
|
|
|
#print("cannot connect %s to %s (%s)" % [from, to, source])
|
|
|
|
return false
|
|
|
|
for c in get_connection_list():
|
|
|
|
if c.to == source and source_list.find(c.from) == -1:
|
|
|
|
source_list.append(c.from)
|
|
|
|
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()
|
|
|
|
return true
|
|
|
|
|
|
|
|
func disconnect_node(from, from_slot, to, to_slot):
|
|
|
|
.disconnect_node(from, from_slot, to, to_slot)
|
|
|
|
send_changed_signal();
|
|
|
|
|
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-08-12 19:25:18 +02:00
|
|
|
send_changed_signal()
|
2018-07-30 08:45:19 +02:00
|
|
|
node.queue_free()
|
2018-08-03 08:16:38 +02:00
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
# Global operations on graph
|
|
|
|
|
|
|
|
func update_tab_title():
|
|
|
|
var title = "[unnamed]"
|
|
|
|
if save_path != null:
|
|
|
|
title = save_path.right(save_path.rfind("/")+1)
|
2018-08-09 22:19:27 +02:00
|
|
|
if need_save:
|
|
|
|
title += " *"
|
2018-08-05 18:46:23 +02:00
|
|
|
get_parent().set_tab_title(get_index(), title)
|
2018-08-09 22:19:27 +02:00
|
|
|
get_parent().update()
|
|
|
|
|
|
|
|
func set_need_save(ns):
|
|
|
|
if ns != need_save:
|
|
|
|
need_save = ns
|
|
|
|
if get_parent() is TabContainer:
|
|
|
|
update_tab_title()
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func set_save_path(path):
|
|
|
|
if path != save_path:
|
|
|
|
save_path = path
|
|
|
|
if get_parent() is TabContainer:
|
|
|
|
update_tab_title()
|
|
|
|
else:
|
|
|
|
emit_signal("save_path_changed", self, path)
|
|
|
|
|
2018-08-03 08:16:38 +02:00
|
|
|
func clear_material():
|
|
|
|
clear_connections()
|
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode:
|
|
|
|
remove_child(c)
|
|
|
|
c.free()
|
2018-08-05 18:46:23 +02:00
|
|
|
send_changed_signal()
|
2018-08-03 08:16:38 +02:00
|
|
|
|
|
|
|
func new_material():
|
|
|
|
clear_material()
|
2018-08-05 18:46:23 +02:00
|
|
|
create_node({name="Material", type="material"})
|
|
|
|
set_save_path(null)
|
2018-08-22 07:33:50 +02:00
|
|
|
center_view()
|
2018-08-05 18:46:23 +02:00
|
|
|
|
2018-08-19 02:11:52 +02:00
|
|
|
func get_free_name(type):
|
|
|
|
var i = 0
|
|
|
|
while true:
|
|
|
|
var node_name = type+"_"+str(i)
|
|
|
|
if !has_node(node_name):
|
|
|
|
return node_name
|
|
|
|
i += 1
|
|
|
|
|
2018-08-22 07:33:50 +02:00
|
|
|
func create_nodes(data, position = null):
|
|
|
|
if data.has("type"):
|
|
|
|
var node_type = load("res://addons/procedural_material/nodes/"+data.type+".tscn")
|
|
|
|
if node_type != null:
|
|
|
|
var node = node_type.instance()
|
|
|
|
if data.has("name") && !has_node(data.name):
|
|
|
|
node.name = data.name
|
|
|
|
else:
|
|
|
|
node.name = get_free_name(data.type)
|
|
|
|
add_node(node)
|
|
|
|
node.deserialize(data)
|
|
|
|
if position != null:
|
|
|
|
node.offset += position
|
|
|
|
send_changed_signal()
|
|
|
|
return node
|
|
|
|
else:
|
|
|
|
if typeof(data.nodes) == TYPE_ARRAY and typeof(data.connections) == TYPE_ARRAY:
|
|
|
|
var names = {}
|
|
|
|
for c in data.nodes:
|
|
|
|
var node = create_nodes(c, position)
|
|
|
|
if node != null:
|
|
|
|
names[c.name] = node.name
|
|
|
|
node.selected = true
|
|
|
|
for c in data.connections:
|
|
|
|
connect_node(names[c.from], c.from_port, names[c.to], c.to_port)
|
|
|
|
return null
|
2018-08-03 08:16:38 +02:00
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
func load_file():
|
|
|
|
var dialog = FileDialog.new()
|
|
|
|
add_child(dialog)
|
|
|
|
dialog.rect_min_size = Vector2(500, 500)
|
|
|
|
dialog.access = FileDialog.ACCESS_FILESYSTEM
|
|
|
|
dialog.mode = FileDialog.MODE_OPEN_FILE
|
|
|
|
dialog.add_filter("*.ptex;Procedural textures file")
|
|
|
|
dialog.connect("file_selected", self, "do_load_file")
|
|
|
|
dialog.popup_centered()
|
|
|
|
|
|
|
|
func do_load_file(filename):
|
2018-07-30 08:45:19 +02:00
|
|
|
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:
|
2018-08-22 07:33:50 +02:00
|
|
|
var node = create_nodes(n)
|
2018-07-30 08:45:19 +02:00
|
|
|
for c in data.connections:
|
|
|
|
connect_node(c.from, c.from_port, c.to, c.to_port)
|
2018-08-05 18:46:23 +02:00
|
|
|
set_save_path(filename)
|
2018-08-09 22:19:27 +02:00
|
|
|
set_need_save(false)
|
2018-08-22 07:33:50 +02:00
|
|
|
center_view()
|
2018-07-30 08:45:19 +02:00
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
func save_file():
|
|
|
|
if save_path != null:
|
|
|
|
do_save_file(save_path)
|
|
|
|
else:
|
|
|
|
save_file_as()
|
|
|
|
|
|
|
|
func save_file_as():
|
|
|
|
var dialog = FileDialog.new()
|
|
|
|
add_child(dialog)
|
|
|
|
dialog.rect_min_size = Vector2(500, 500)
|
|
|
|
dialog.access = FileDialog.ACCESS_FILESYSTEM
|
|
|
|
dialog.mode = FileDialog.MODE_SAVE_FILE
|
|
|
|
dialog.add_filter("*.ptex;Procedural textures file")
|
|
|
|
dialog.connect("file_selected", self, "do_save_file")
|
|
|
|
dialog.popup_centered()
|
|
|
|
|
|
|
|
func do_save_file(filename):
|
2018-07-30 08:45:19 +02:00
|
|
|
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-08-05 18:46:23 +02:00
|
|
|
set_save_path(filename)
|
2018-08-09 22:19:27 +02:00
|
|
|
set_need_save(false)
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func export_textures(size = 512):
|
|
|
|
if save_path != null:
|
|
|
|
var prefix = save_path.left(save_path.rfind("."))
|
2018-08-14 15:09:46 +02:00
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode && c.has_method("export_textures"):
|
2018-08-23 17:01:24 +02:00
|
|
|
c.export_textures(prefix, size)
|
2018-07-24 08:04:25 +02:00
|
|
|
|
2018-08-22 07:33:50 +02:00
|
|
|
# Cut / copy / paste
|
2018-07-28 09:37:51 +02:00
|
|
|
|
2018-08-22 07:33:50 +02:00
|
|
|
func remove_selection():
|
2018-08-19 02:11:52 +02:00
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode and c.selected:
|
|
|
|
remove_node(c)
|
|
|
|
|
2018-08-22 07:33:50 +02:00
|
|
|
func serialize_selection():
|
2018-08-19 02:11:52 +02:00
|
|
|
var data = { nodes = [], connections = [] }
|
2018-08-22 07:33:50 +02:00
|
|
|
var nodes = []
|
2018-08-19 02:11:52 +02:00
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode and c.selected:
|
2018-08-22 07:33:50 +02:00
|
|
|
nodes.append(c)
|
|
|
|
if nodes.empty():
|
|
|
|
return null
|
|
|
|
var center = Vector2(0, 0)
|
|
|
|
for n in nodes:
|
|
|
|
center += n.offset+0.5*n.rect_size
|
|
|
|
center /= nodes.size()
|
|
|
|
for n in nodes:
|
|
|
|
var s = n.serialize()
|
|
|
|
var p = n.offset-center
|
|
|
|
s.node_position = { x=p.x, y=p.y }
|
|
|
|
data.nodes.append(s)
|
2018-08-19 02:11:52 +02:00
|
|
|
for c in get_connection_list():
|
|
|
|
var from = get_node(c.from)
|
|
|
|
var to = get_node(c.to)
|
|
|
|
if from != null and from.selected and to != null and to.selected:
|
|
|
|
data.connections.append(c)
|
2018-08-22 07:33:50 +02:00
|
|
|
return data
|
2018-08-19 02:11:52 +02:00
|
|
|
|
2018-08-22 07:33:50 +02:00
|
|
|
func cut():
|
|
|
|
copy()
|
|
|
|
remove_selection()
|
|
|
|
|
|
|
|
func copy():
|
|
|
|
OS.clipboard = to_json(serialize_selection())
|
|
|
|
|
|
|
|
func paste(pos = Vector2(0, 0)):
|
2018-08-19 02:11:52 +02:00
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode:
|
|
|
|
c.selected = false
|
|
|
|
var data = parse_json(OS.clipboard)
|
2018-08-22 07:33:50 +02:00
|
|
|
create_nodes(data, scroll_offset+0.5*rect_size)
|
|
|
|
|
|
|
|
# Center view
|
|
|
|
|
|
|
|
func center_view():
|
|
|
|
var center = Vector2(0, 0)
|
|
|
|
var node_count = 0
|
|
|
|
for c in get_children():
|
|
|
|
if c is GraphNode:
|
|
|
|
center += c.offset + 0.5*c.rect_size
|
|
|
|
node_count += 1
|
|
|
|
if node_count > 0:
|
|
|
|
center /= node_count
|
|
|
|
scroll_offset = center - 0.5*rect_size
|
|
|
|
|
|
|
|
# Delay after graph update
|
|
|
|
|
|
|
|
func send_changed_signal():
|
|
|
|
set_need_save(true)
|
|
|
|
$Timer.start()
|
|
|
|
|
|
|
|
func do_send_changed_signal():
|
|
|
|
emit_signal("graph_changed")
|
2018-08-19 02:11:52 +02:00
|
|
|
|
2018-08-12 19:25:18 +02:00
|
|
|
# Drag and drop
|
|
|
|
|
2018-08-05 18:46:23 +02:00
|
|
|
func can_drop_data(position, data):
|
2018-08-22 07:33:50 +02:00
|
|
|
return typeof(data) == TYPE_DICTIONARY and (data.has('type') or (data.has('nodes') and data.has('connections')))
|
2018-08-05 18:46:23 +02:00
|
|
|
|
|
|
|
func drop_data(position, data):
|
2018-08-22 07:33:50 +02:00
|
|
|
create_nodes(data, offset_from_global_position(get_global_transform().xform(position)))
|
2018-08-05 18:46:23 +02:00
|
|
|
return true
|
|
|
|
|
|
|
|
# Save shader to image, create image texture
|
|
|
|
|
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
|
|
|
var render_queue = []
|
|
|
|
|
2018-08-12 19:25:18 +02:00
|
|
|
func render_shader_to_viewport(shader, textures, size, method, args):
|
|
|
|
render_queue.append( { shader=shader, textures=textures, size=size, method=method, args=args } )
|
2018-08-03 08:16:38 +02:00
|
|
|
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
|
2018-08-12 19:25:18 +02:00
|
|
|
if job.textures != null:
|
|
|
|
for k in job.textures.keys():
|
|
|
|
shader_material.set_shader_param(k+"_tex", job.textures[k])
|
2018-08-03 08:16:38 +02:00
|
|
|
$SaveViewport.render_target_update_mode = Viewport.UPDATE_ONCE
|
|
|
|
$SaveViewport.update_worlds()
|
|
|
|
yield(get_tree(), "idle_frame")
|
|
|
|
yield(get_tree(), "idle_frame")
|
|
|
|
callv(job.method, job.args)
|
|
|
|
render_queue.pop_front()
|
|
|
|
|
2018-08-12 19:25:18 +02:00
|
|
|
func render_to_viewport(node, size, method, args):
|
|
|
|
render_shader_to_viewport(node.generate_shader(), node.get_textures(), size, method, args)
|
|
|
|
|
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
|
|
|
|
2018-08-12 19:25:18 +02:00
|
|
|
func precalculate_node(node, size, target_texture, object, method, args):
|
2018-07-29 23:43:24 +02:00
|
|
|
if node == null:
|
|
|
|
return null
|
2018-08-03 08:16:38 +02:00
|
|
|
render_to_viewport(node, size, "do_precalculate_texture", [ object, method, args ])
|
|
|
|
|
2018-08-12 19:25:18 +02:00
|
|
|
func precalculate_shader(shader, textures, size, target_texture, object, method, args):
|
|
|
|
render_shader_to_viewport(shader, textures, size, "do_precalculate_texture", [ target_texture, object, method, args ])
|
|
|
|
|
|
|
|
func do_precalculate_texture(target_texture, object, method, args):
|
2018-07-29 23:43:24 +02:00
|
|
|
var viewport_texture = $SaveViewport.get_texture()
|
2018-08-12 19:25:18 +02:00
|
|
|
target_texture.create_from_image(viewport_texture.get_data())
|
2018-07-29 23:43:24 +02:00
|
|
|
object.callv(method, args)
|
|
|
|
|