Reimplemented generator instanciation

This commit is contained in:
Rodolphe Suescun 2019-08-15 11:53:21 +02:00
parent 470e55b212
commit b614ab50ed
8 changed files with 62 additions and 55 deletions

View File

@ -2,7 +2,7 @@ tool
extends MMGenBase extends MMGenBase
class_name MMGenGraph class_name MMGenGraph
var connections = null var connections = []
func get_port_source(gen_name: String, input_index: int) -> OutputPort: func get_port_source(gen_name: String, input_index: int) -> OutputPort:
for c in connections: for c in connections:

View File

@ -7,12 +7,16 @@ var generated_variants = []
func set_model_data(data: Dictionary): func set_model_data(data: Dictionary):
model_data = data model_data = data
for p in model_data.parameters:
if !parameters.has(p.name) and p.has("default"):
parameters[p.name] = MMType.deserialize_value(p.default)
func initialize(data: Dictionary): func initialize(data: Dictionary):
if data.has("name"): if data.has("name"):
name = data.name name = data.name
if data.has("parameters"): if data.has("parameters"):
parameters = data.parameters for p in data.parameters.keys():
parameters[p] = data.parameters[p]
func find_keyword_call(string, keyword): func find_keyword_call(string, keyword):
var search_string = "$%s(" % keyword var search_string = "$%s(" % keyword

View File

@ -9,15 +9,29 @@ func load_gen(filename: String) -> MMGenBase:
return create_gen(data) return create_gen(data)
return null return null
func add_to_gen_graph(gen_graph, generators, connections):
var rv = { generators=[], connections=[] }
for n in generators:
var g = create_gen(n)
if g != null:
var name = g.name
var index = 1
while gen_graph.has_node(name):
index += 1
name = g.name + "_" + str(index)
g.name = name
gen_graph.add_child(g)
rv.generators.append(g)
for c in connections:
gen_graph.connections.append(c)
rv.connections.append(c)
return rv
func create_gen(data) -> MMGenBase: func create_gen(data) -> MMGenBase:
var generator = null var generator = null
if data.has("connections") and data.has("nodes"): if data.has("connections") and data.has("nodes"):
generator = MMGenGraph.new() generator = MMGenGraph.new()
for n in data.nodes: add_to_gen_graph(generator, data.nodes, data.connections)
var g = create_gen(n)
if g != null:
generator.add_child(g)
generator.connections = data.connections
elif data.has("model_data"): elif data.has("model_data"):
generator = MMGenShader.new() generator = MMGenShader.new()
generator.set_model_data(data.model_data) generator.set_model_data(data.model_data)
@ -39,9 +53,12 @@ func create_gen(data) -> MMGenBase:
file.close() file.close()
else: else:
print("Cannot find description for "+data.type) print("Cannot find description for "+data.type)
generator.name = data.type
else: else:
print(data) print(data)
if generator != null: if generator != null:
if data.has("name"):
generator.name = data.name
if data.has("node_position"): if data.has("node_position"):
generator.position.x = data.node_position.x generator.position.x = data.node_position.x
generator.position.y = data.node_position.y generator.position.y = data.node_position.y

View File

@ -98,24 +98,24 @@ func clear_material():
generator = null generator = null
send_changed_signal() send_changed_signal()
func update_graph(generators, connections):
for g in generators:
var node = node_factory.create_node(g.get_type())
if node != null:
node.name = "node_"+g.name
add_node(node)
node.generator = g
node.offset = g.position
for c in connections:
.connect_node("node_"+c.from, c.from_port, "node_"+c.to, c.to_port)
func new_material(): func new_material():
print("New material")
clear_material() clear_material()
var loader = MMGenLoader.new() var loader = MMGenLoader.new()
generator = loader.create_gen({nodes=[{name="Material", type="material"}], connections=[]}) generator = loader.create_gen({nodes=[{name="Material", type="material"}], connections=[]})
if generator != null: if generator != null:
print("Not null !")
add_child(generator) add_child(generator)
for g in generator.get_children(): update_graph(generator.get_children(), generator.connections)
var node = node_factory.create_node(g.get_type())
if node != null:
node.name = "node_"+g.name
add_node(node)
node.generator = g
node.offset = g.position
if generator.get("connections") != null:
for c in generator.connections:
.connect_node("node_"+c.from, c.from_port, "node_"+c.to, c.to_port)
set_save_path(filename) set_save_path(filename)
set_need_save(false) set_need_save(false)
center_view() center_view()
@ -132,29 +132,11 @@ func create_nodes(data, position = null):
if data == null: if data == null:
return return
if data.has("type"): if data.has("type"):
var node = node_factory.create_node(data.type) data = { nodes=[data], connections=[] }
if node != null: if typeof(data.nodes) == TYPE_ARRAY and typeof(data.connections) == TYPE_ARRAY:
if data.has("name") and !has_node(data.name): var loader = MMGenLoader.new()
node.name = data.name var new_stuff = loader.add_to_gen_graph(generator, data.nodes, data.connections)
else: update_graph(new_stuff.generators, new_stuff.connections)
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, "Material" if c.to == "Material" else names[c.to], c.to_port)
return null
func load_file(filename): func load_file(filename):
clear_material() clear_material()
@ -162,16 +144,7 @@ func load_file(filename):
generator = loader.load_gen(filename) generator = loader.load_gen(filename)
if generator != null: if generator != null:
add_child(generator) add_child(generator)
for g in generator.get_children(): update_graph(generator.get_children(), generator.connections)
var node = node_factory.create_node(g.get_type())
if node != null:
node.name = "node_"+g.name
add_node(node)
node.generator = g
node.offset = g.position
if generator.get("connections") != null:
for c in generator.connections:
.connect_node("node_"+c.from, c.from_port, "node_"+c.to, c.to_port)
set_save_path(filename) set_save_path(filename)
set_need_save(false) set_need_save(false)
center_view() center_view()

View File

@ -21,7 +21,10 @@ func initialize_properties():
for o in controls: for o in controls:
if o == null: if o == null:
print("error in node "+name) print("error in node "+name)
elif o is LineEdit: continue
if !generator.parameters.has(o.name):
continue
if o is LineEdit:
o.text = str(generator.parameters[o.name]) o.text = str(generator.parameters[o.name])
o.connect("text_changed", self, "_on_text_changed", [ o.name ]) o.connect("text_changed", self, "_on_text_changed", [ o.name ])
elif o is SpinBox: elif o is SpinBox:
@ -37,7 +40,7 @@ func initialize_properties():
o.pressed = generator.parameters[o.name] o.pressed = generator.parameters[o.name]
o.connect("toggled", self, "_on_value_changed", [ o.name ]) o.connect("toggled", self, "_on_value_changed", [ o.name ])
elif o is ColorPickerButton: elif o is ColorPickerButton:
o.color = generator.parameters[o.name] o.color = MMType.deserialize_value(generator.parameters[o.name])
o.connect("color_changed", self, "_on_color_changed", [ o.name ]) o.connect("color_changed", self, "_on_color_changed", [ o.name ])
elif o is Control and o.filename == "res://addons/material_maker/widgets/gradient_editor.tscn": elif o is Control and o.filename == "res://addons/material_maker/widgets/gradient_editor.tscn":
var gradient : MMGradient = MMGradient.new() var gradient : MMGradient = MMGradient.new()

View File

@ -1,4 +1,5 @@
extends Node extends Node
class_name MMType
const Gradient = preload("res://addons/material_maker/types/gradient.gd") const Gradient = preload("res://addons/material_maker/types/gradient.gd")
@ -18,6 +19,8 @@ static func deserialize_value(data):
var gradient = Gradient.new() var gradient = Gradient.new()
gradient.deserialize(data) gradient.deserialize(data)
return gradient return gradient
elif data.has("r") and data.has("g") and data.has("b") and data.has("a"):
return Color(data.r, data.g, data.b, data.a)
# in previous releases, Gradients were serialized as arrays # in previous releases, Gradients were serialized as arrays
elif typeof(data) == TYPE_ARRAY: elif typeof(data) == TYPE_ARRAY:
var gradient = Gradient.new() var gradient = Gradient.new()

View File

@ -58,6 +58,11 @@ _global_script_classes=[ {
"class": "MMGradient", "class": "MMGradient",
"language": "GDScript", "language": "GDScript",
"path": "res://addons/material_maker/types/gradient.gd" "path": "res://addons/material_maker/types/gradient.gd"
}, {
"base": "Node",
"class": "MMType",
"language": "GDScript",
"path": "res://addons/material_maker/types/types.gd"
} ] } ]
_global_script_class_icons={ _global_script_class_icons={
"MMGenBase": "", "MMGenBase": "",
@ -69,7 +74,8 @@ _global_script_class_icons={
"MMGenMaterial": "", "MMGenMaterial": "",
"MMGenRenderer": "", "MMGenRenderer": "",
"MMGenShader": "", "MMGenShader": "",
"MMGradient": "" "MMGradient": "",
"MMType": ""
} }
[application] [application]

1
test.ptex Normal file
View File

@ -0,0 +1 @@
{"connections":[{"from":"bricks_0","from_port":0,"to":"custom_0","to_port":0},{"from":"custom_0","from_port":0,"to":"adjust_hsv_0","to_port":0},{"from":"adjust_hsv_0","from_port":0,"to":"Material","to_port":0}],"nodes":[{"name":"Material","node_position":{"x":143,"y":-210},"parameters":{"albedo_color":{"a":1,"b":1,"g":1,"r":1,"type":"Color"},"ao_light_affect":1,"depth_scale":1,"emission_energy":1,"metallic":1,"normal_scale":1,"resolution":1,"roughness":1},"type":"material"},{"model_data":{"global":"","inputs":[{"default":"0.0","label":"Input","name":"input","type":"f"}],"instance":"","name":"Colorize","outputs":[{"rgba":"$gradient($input($uv))"}],"parameters":[{"default":{"points":[{"a":1,"b":0,"g":0,"pos":0,"r":0},{"a":1,"b":1,"g":1,"pos":1,"r":1}],"type":"Gradient"},"label":"Gradient","name":"gradient","type":"gradient"}]},"name":"custom_0","node_position":{"x":-433,"y":-109.416656},"parameters":{"gradient":{"points":[{"a":1,"b":0,"g":0,"pos":0,"r":0},{"a":1,"b":0,"g":0,"pos":0.517045,"r":1},{"a":0,"b":1,"g":1,"pos":1,"r":1}],"type":"Gradient"}},"type":"custom"},{"name":"adjust_hsv_0","node_position":{"x":-135.541687,"y":-150.597229},"parameters":{"hue":0,"saturation":1,"value":1},"type":"adjust_hsv"},{"name":"bricks_0","node_position":{"x":-599,"y":-332},"parameters":{"bevel":0.209961,"columns":3,"mortar":0.116211,"pattern":0,"repeat":1,"row_offset":0.5,"rows":6},"type":"bricks"}]}