Added main code section to shader generators

This commit is contained in:
RodZill4 2019-10-10 07:42:38 +02:00
parent 3adeffe23c
commit 825d98e0fe
5 changed files with 66 additions and 28 deletions

View File

@ -11,12 +11,12 @@ func _init(r : MMGenRenderer):
func has_variant(generator): func has_variant(generator):
return variants.has(generator) return variants.has(generator)
func get_variant(generator, uv): func get_variant(generator, variant):
var rv = -1 var rv = -1
if variants.has(generator): if variants.has(generator):
rv = variants[generator].find(uv) rv = variants[generator].find(variant)
if rv == -1: if rv == -1:
variants[generator].push_back(uv) variants[generator].push_back(variant)
else: else:
variants[generator] = [uv] variants[generator] = [variant]
return rv return rv

View File

@ -134,6 +134,9 @@ func subst(string, context, uv = ""):
var required_code = "" var required_code = ""
var required_textures = {} var required_textures = {}
string = replace_variable(string, "name", genname) string = replace_variable(string, "name", genname)
if uv != "":
var genname_uv = genname+"_"+str(context.get_variant(self, uv))
string = replace_variable(string, "name_uv", genname_uv)
var tmp_string = replace_variable(string, "seed", str(get_seed())) var tmp_string = replace_variable(string, "seed", str(get_seed()))
if tmp_string != string: if tmp_string != string:
string = tmp_string string = tmp_string
@ -196,12 +199,10 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext):
var genname = "o"+str(get_instance_id()) var genname = "o"+str(get_instance_id())
var output_info = [ { field="rgba", type="vec4" }, { field="rgb", type="vec3" }, { field="f", type="float" } ] var output_info = [ { field="rgba", type="vec4" }, { field="rgb", type="vec3" }, { field="f", type="float" } ]
var rv = { globals=[], defs="", code="", textures={} } var rv = { globals=[], defs="", code="", textures={} }
var variant_string = uv+","+str(output_index)
if shader_model != null and shader_model.has("outputs") and shader_model.outputs.size() > output_index: if shader_model != null and shader_model.has("outputs") and shader_model.outputs.size() > output_index:
var output = shader_model.outputs[output_index] var output = shader_model.outputs[output_index]
rv.defs = ""
if shader_model.has("instance") && !context.has_variant(self): if shader_model.has("instance") && !context.has_variant(self):
var subst_output = subst(shader_model.instance, context, uv) var subst_output = subst(shader_model.instance, context, "")
while subst_output is GDScriptFunctionState: while subst_output is GDScriptFunctionState:
subst_output = yield(subst_output, "completed") subst_output = yield(subst_output, "completed")
rv.defs += subst_output.string rv.defs += subst_output.string
@ -214,6 +215,25 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext):
g = MMGradient.new() g = MMGradient.new()
g.deserialize(parameters[p.name]) g.deserialize(parameters[p.name])
rv.defs += g.get_shader(genname+"__"+p.name+"_gradient_fct") rv.defs += g.get_shader(genname+"__"+p.name+"_gradient_fct")
# Add inline code
if shader_model.has("code"):
var variant_index = context.get_variant(self, uv)
if variant_index == -1:
variant_index = context.get_variant(self, uv)
var subst_code = subst(shader_model.code, context, uv)
while subst_code is GDScriptFunctionState:
subst_code = yield(subst_code, "completed")
# Add global definitions
for d in subst_code.globals:
if rv.globals.find(d) == -1:
rv.globals.push_back(d)
# Add generated definitions
rv.defs += subst_code.defs
# Add generated code
rv.code += subst_code.code
rv.code += subst_code.string
# Add output_code
var variant_string = uv+","+str(output_index)
var variant_index = context.get_variant(self, variant_string) var variant_index = context.get_variant(self, variant_string)
if variant_index == -1: if variant_index == -1:
variant_index = context.get_variant(self, variant_string) variant_index = context.get_variant(self, variant_string)

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,9 @@ extends WindowDialog
var model_data = null var model_data = null
onready var global_editor : TextEdit = $Sizer/Tabs/Global onready var main_code_editor : TextEdit = $"Sizer/Tabs/Main Code"
onready var instance_functions_editor : TextEdit = $"Sizer/Tabs/Instance Functions"
onready var global_functions_editor : TextEdit = $"Sizer/Tabs/Global Functions"
const ParameterEditor = preload("res://addons/material_maker/widgets/node_editor/parameter.tscn") const ParameterEditor = preload("res://addons/material_maker/widgets/node_editor/parameter.tscn")
const InputEditor = preload("res://addons/material_maker/widgets/node_editor/input.tscn") const InputEditor = preload("res://addons/material_maker/widgets/node_editor/input.tscn")
@ -12,8 +14,9 @@ const OutputEditor = preload("res://addons/material_maker/widgets/node_editor/ou
signal node_changed signal node_changed
func _ready(): func _ready():
global_editor.add_color_region("//", "", Color(0, 0.5, 0), true) main_code_editor.add_color_region("//", "", Color(0, 0.5, 0), true)
instance_functions_editor.add_color_region("//", "", Color(0, 0.5, 0), true)
global_functions_editor.add_color_region("//", "", Color(0, 0.5, 0), true)
func add_item(parent, scene): func add_item(parent, scene):
var object = scene.instance() var object = scene.instance()
@ -34,15 +37,18 @@ func set_model_data(data):
for o in data.outputs: for o in data.outputs:
add_item($Sizer/Tabs/Outputs/Outputs/Sizer, OutputEditor).set_model_data(o) add_item($Sizer/Tabs/Outputs/Outputs/Sizer, OutputEditor).set_model_data(o)
if data.has("global"): if data.has("global"):
$Sizer/Tabs/Global.text = data.global global_functions_editor.text = data.global
if data.has("instance"): if data.has("instance"):
$Sizer/Tabs/Instance.text = data.instance instance_functions_editor.text = data.instance
if data.has("code"):
main_code_editor.text = data.code
func get_model_data(): func get_model_data():
var data = { var data = {
name=$Sizer/Tabs/General/Name/Name.text, name=$Sizer/Tabs/General/Name/Name.text,
global=$Sizer/Tabs/Global.text, global=global_functions_editor.text,
instance=$Sizer/Tabs/Instance.text, instance=instance_functions_editor.text,
code=main_code_editor.text
} }
data.parameters = [] data.parameters = []
for p in $Sizer/Tabs/General/Parameters/Sizer.get_children(): for p in $Sizer/Tabs/General/Parameters/Sizer.get_children():

View File

@ -146,7 +146,31 @@ size_flags_horizontal = 0
icon = SubResource( 1 ) icon = SubResource( 1 )
flat = true flat = true
[node name="Global" type="TextEdit" parent="Sizer/Tabs"] [node name="Main Code" type="TextEdit" parent="Sizer/Tabs"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 4.0
margin_top = 32.0
margin_right = -4.0
margin_bottom = -4.0
mouse_default_cursor_shape = 0
syntax_highlighting = true
show_line_numbers = true
[node name="Instance Functions" type="TextEdit" parent="Sizer/Tabs"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 4.0
margin_top = 32.0
margin_right = -4.0
margin_bottom = -4.0
mouse_default_cursor_shape = 0
syntax_highlighting = true
show_line_numbers = true
[node name="Global Functions" type="TextEdit" parent="Sizer/Tabs"]
visible = false visible = false
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@ -160,18 +184,6 @@ custom_colors/brace_mismatch_color = Color( 1, 0, 0, 1 )
syntax_highlighting = true syntax_highlighting = true
show_line_numbers = true show_line_numbers = true
[node name="Instance" type="TextEdit" parent="Sizer/Tabs"]
visible = false
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 4.0
margin_top = 32.0
margin_right = -4.0
margin_bottom = -4.0
mouse_default_cursor_shape = 0
syntax_highlighting = true
show_line_numbers = true
[node name="HBoxContainer" type="HBoxContainer" parent="Sizer"] [node name="HBoxContainer" type="HBoxContainer" parent="Sizer"]
margin_left = 307.0 margin_left = 307.0
margin_top = 378.0 margin_top = 378.0