2019-08-02 08:17:32 +02:00
|
|
|
tool
|
2019-07-31 20:53:00 +02:00
|
|
|
extends MMGenBase
|
|
|
|
class_name MMGenShader
|
|
|
|
|
2019-08-17 17:35:48 +02:00
|
|
|
var shader_model : Dictionary = {}
|
2019-10-02 23:06:20 +02:00
|
|
|
var uses_seed = false
|
2019-07-31 20:53:00 +02:00
|
|
|
|
2019-08-17 17:35:48 +02:00
|
|
|
func get_type():
|
|
|
|
return "shader"
|
2019-07-31 20:53:00 +02:00
|
|
|
|
2019-08-17 17:35:48 +02:00
|
|
|
func get_type_name():
|
|
|
|
if shader_model.has("name"):
|
|
|
|
return shader_model.name
|
|
|
|
return .get_type_name()
|
|
|
|
|
|
|
|
func get_parameter_defs():
|
|
|
|
if shader_model == null or !shader_model.has("parameters"):
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return shader_model.parameters
|
|
|
|
|
2019-08-18 16:28:50 +02:00
|
|
|
func get_input_defs():
|
|
|
|
if shader_model == null or !shader_model.has("inputs"):
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return shader_model.inputs
|
|
|
|
|
|
|
|
func get_output_defs():
|
|
|
|
if shader_model == null or !shader_model.has("outputs"):
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return shader_model.outputs
|
|
|
|
|
2019-08-17 17:35:48 +02:00
|
|
|
func set_shader_model(data: Dictionary):
|
|
|
|
shader_model = data
|
|
|
|
init_parameters()
|
2019-09-11 07:53:06 +02:00
|
|
|
if shader_model.has("outputs"):
|
|
|
|
for i in range(shader_model.outputs.size()):
|
|
|
|
var output = shader_model.outputs[i]
|
|
|
|
if output.has("rgba"):
|
|
|
|
shader_model.outputs[i].type = "rgba"
|
|
|
|
elif output.has("rgb"):
|
|
|
|
shader_model.outputs[i].type = "rgb"
|
|
|
|
else:
|
|
|
|
shader_model.outputs[i].type = "f"
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-10-02 23:06:20 +02:00
|
|
|
func set_position(p):
|
|
|
|
.set_position(p)
|
|
|
|
if uses_seed:
|
|
|
|
source_changed(0)
|
|
|
|
|
|
|
|
func get_seed():
|
|
|
|
var s = ((int(position.x) * 0x1f1f1f1f) ^ int(position.y)) % 65536
|
|
|
|
return s
|
|
|
|
|
2019-08-02 08:17:32 +02:00
|
|
|
func find_keyword_call(string, keyword):
|
|
|
|
var search_string = "$%s(" % keyword
|
|
|
|
var position = string.find(search_string)
|
|
|
|
if position == -1:
|
|
|
|
return null
|
|
|
|
var parenthesis_level = 0
|
|
|
|
var parameter_begin = position+search_string.length()
|
|
|
|
var parameter_end = -1
|
|
|
|
for i in range(parameter_begin, string.length()):
|
|
|
|
if string[i] == '(':
|
|
|
|
parenthesis_level += 1
|
|
|
|
elif string[i] == ')':
|
|
|
|
if parenthesis_level == 0:
|
|
|
|
return string.substr(parameter_begin, i-parameter_begin)
|
|
|
|
parenthesis_level -= 1
|
|
|
|
return ""
|
|
|
|
|
2019-08-12 15:58:42 +02:00
|
|
|
func replace_input(string, context, input, type, src, default):
|
2019-09-29 10:07:32 +02:00
|
|
|
var required_globals = []
|
2019-08-02 08:17:32 +02:00
|
|
|
var required_defs = ""
|
|
|
|
var required_code = ""
|
2019-08-18 16:28:50 +02:00
|
|
|
var required_textures = {}
|
2019-09-12 21:16:07 +02:00
|
|
|
var new_pass_required = false
|
2019-08-02 08:17:32 +02:00
|
|
|
while true:
|
|
|
|
var uv = find_keyword_call(string, input)
|
|
|
|
if uv == null:
|
|
|
|
break
|
|
|
|
elif uv == "":
|
|
|
|
print("syntax error")
|
|
|
|
break
|
2019-09-12 21:16:07 +02:00
|
|
|
elif uv.find("$") != -1:
|
|
|
|
new_pass_required = true
|
|
|
|
break
|
2019-08-02 08:17:32 +02:00
|
|
|
var src_code
|
|
|
|
if src == null:
|
2019-08-18 16:28:50 +02:00
|
|
|
src_code = subst(default, context, "(%s)" % uv)
|
2019-08-02 08:17:32 +02:00
|
|
|
else:
|
2019-08-17 17:35:48 +02:00
|
|
|
src_code = src.generator.get_shader_code(uv, src.output_index, context)
|
2019-08-18 16:28:50 +02:00
|
|
|
while src_code is GDScriptFunctionState:
|
|
|
|
src_code = yield(src_code, "completed")
|
2019-08-02 08:17:32 +02:00
|
|
|
src_code.string = src_code[type]
|
2019-09-29 10:07:32 +02:00
|
|
|
# Add global definitions
|
|
|
|
for d in src_code.globals:
|
|
|
|
if required_globals.find(d) == -1:
|
|
|
|
required_globals.push_back(d)
|
|
|
|
# Add generated definitions
|
2019-08-02 08:17:32 +02:00
|
|
|
required_defs += src_code.defs
|
2019-09-29 10:07:32 +02:00
|
|
|
# Add generated code
|
2019-08-02 08:17:32 +02:00
|
|
|
required_code += src_code.code
|
2019-08-18 16:28:50 +02:00
|
|
|
required_textures = src_code.textures
|
2019-08-02 08:17:32 +02:00
|
|
|
string = string.replace("$%s(%s)" % [ input, uv ], src_code.string)
|
2019-09-29 10:07:32 +02:00
|
|
|
return { string=string, globals=required_globals, defs=required_defs, code=required_code, textures=required_textures, new_pass_required=new_pass_required }
|
2019-08-02 08:17:32 +02:00
|
|
|
|
|
|
|
func is_word_letter(l):
|
|
|
|
return "azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN1234567890_".find(l) != -1
|
|
|
|
|
|
|
|
func replace_variable(string, variable, value):
|
|
|
|
string = string.replace("$(%s)" % variable, value)
|
|
|
|
var keyword_size = variable.length()+1
|
|
|
|
var new_string = ""
|
|
|
|
while !string.empty():
|
|
|
|
var pos = string.find("$"+variable)
|
|
|
|
if pos == -1:
|
|
|
|
new_string += string
|
|
|
|
break
|
|
|
|
new_string += string.left(pos)
|
|
|
|
string = string.right(pos)
|
|
|
|
if string.empty() or !is_word_letter(string[0]):
|
|
|
|
new_string += value
|
|
|
|
else:
|
|
|
|
new_string += "$"+variable
|
|
|
|
string = string.right(keyword_size)
|
|
|
|
return new_string
|
|
|
|
|
2019-08-12 15:58:42 +02:00
|
|
|
func subst(string, context, uv = ""):
|
2019-09-16 20:45:47 +02:00
|
|
|
var genname = "o"+str(get_instance_id())
|
2019-09-29 10:07:32 +02:00
|
|
|
var required_globals = []
|
2019-08-02 08:17:32 +02:00
|
|
|
var required_defs = ""
|
|
|
|
var required_code = ""
|
2019-08-18 16:28:50 +02:00
|
|
|
var required_textures = {}
|
2019-09-16 20:45:47 +02:00
|
|
|
string = replace_variable(string, "name", genname)
|
2019-10-10 07:42:38 +02:00
|
|
|
if uv != "":
|
|
|
|
var genname_uv = genname+"_"+str(context.get_variant(self, uv))
|
|
|
|
string = replace_variable(string, "name_uv", genname_uv)
|
2019-10-02 23:06:20 +02:00
|
|
|
var tmp_string = replace_variable(string, "seed", str(get_seed()))
|
|
|
|
if tmp_string != string:
|
|
|
|
string = tmp_string
|
|
|
|
uses_seed = true
|
2019-08-02 08:17:32 +02:00
|
|
|
if uv != "":
|
|
|
|
string = replace_variable(string, "uv", "("+uv+")")
|
2019-08-17 17:35:48 +02:00
|
|
|
if shader_model.has("parameters") and typeof(shader_model.parameters) == TYPE_ARRAY:
|
|
|
|
for p in shader_model.parameters:
|
2019-08-02 08:17:32 +02:00
|
|
|
if !p.has("name") or !p.has("type"):
|
|
|
|
continue
|
|
|
|
var value = parameters[p.name]
|
|
|
|
var value_string = null
|
|
|
|
if p.type == "float":
|
|
|
|
value_string = "%.9f" % value
|
|
|
|
elif p.type == "size":
|
|
|
|
value_string = "%.9f" % pow(2, value+p.first)
|
|
|
|
elif p.type == "enum":
|
|
|
|
value_string = p.values[value].value
|
|
|
|
elif p.type == "color":
|
|
|
|
value_string = "vec4(%.9f, %.9f, %.9f, %.9f)" % [ value.r, value.g, value.b, value.a ]
|
2019-08-09 08:16:24 +02:00
|
|
|
elif p.type == "gradient":
|
2019-09-16 20:45:47 +02:00
|
|
|
value_string = genname+"__"+p.name+"_gradient_fct"
|
2019-09-12 21:16:07 +02:00
|
|
|
elif p.type == "boolean":
|
|
|
|
value_string = "true" if value else "false"
|
|
|
|
else:
|
|
|
|
print("Cannot replace parameter of type "+p.type)
|
2019-08-02 08:17:32 +02:00
|
|
|
if value_string != null:
|
|
|
|
string = replace_variable(string, p.name, value_string)
|
2019-08-17 17:35:48 +02:00
|
|
|
if shader_model.has("inputs") and typeof(shader_model.inputs) == TYPE_ARRAY:
|
2019-09-12 21:16:07 +02:00
|
|
|
var cont = true
|
|
|
|
while cont:
|
|
|
|
var changed = false
|
|
|
|
var new_pass_required = false
|
|
|
|
for i in range(shader_model.inputs.size()):
|
|
|
|
var input = shader_model.inputs[i]
|
|
|
|
var source = get_source(i)
|
|
|
|
var result = replace_input(string, context, input.name, input.type, source, input.default)
|
|
|
|
while result is GDScriptFunctionState:
|
|
|
|
result = yield(result, "completed")
|
|
|
|
if string != result.string:
|
|
|
|
changed = true
|
|
|
|
if result.new_pass_required:
|
|
|
|
new_pass_required = true
|
|
|
|
string = result.string
|
2019-09-29 10:07:32 +02:00
|
|
|
# Add global definitions
|
|
|
|
for d in result.globals:
|
|
|
|
if required_globals.find(d) == -1:
|
|
|
|
required_globals.push_back(d)
|
|
|
|
# Add generated definitions
|
2019-09-12 21:16:07 +02:00
|
|
|
required_defs += result.defs
|
2019-09-29 10:07:32 +02:00
|
|
|
# Add generated code
|
2019-09-12 21:16:07 +02:00
|
|
|
required_code += result.code
|
|
|
|
for t in result.textures.keys():
|
|
|
|
required_textures[t] = result.textures[t]
|
|
|
|
cont = changed and new_pass_required
|
2019-09-29 10:07:32 +02:00
|
|
|
return { string=string, globals=required_globals, defs=required_defs, code=required_code, textures=required_textures }
|
2019-08-02 08:17:32 +02:00
|
|
|
|
2019-08-17 17:35:48 +02:00
|
|
|
func _get_shader_code(uv : String, output_index : int, context : MMGenContext):
|
2019-10-02 23:06:20 +02:00
|
|
|
uses_seed = false
|
2019-09-16 20:45:47 +02:00
|
|
|
var genname = "o"+str(get_instance_id())
|
2019-08-02 08:17:32 +02:00
|
|
|
var output_info = [ { field="rgba", type="vec4" }, { field="rgb", type="vec3" }, { field="f", type="float" } ]
|
2019-09-29 10:07:32 +02:00
|
|
|
var rv = { globals=[], defs="", code="", textures={} }
|
2019-08-17 17:35:48 +02:00
|
|
|
if shader_model != null and shader_model.has("outputs") and shader_model.outputs.size() > output_index:
|
|
|
|
var output = shader_model.outputs[output_index]
|
|
|
|
if shader_model.has("instance") && !context.has_variant(self):
|
2019-10-10 07:42:38 +02:00
|
|
|
var subst_output = subst(shader_model.instance, context, "")
|
2019-08-18 16:28:50 +02:00
|
|
|
while subst_output is GDScriptFunctionState:
|
|
|
|
subst_output = yield(subst_output, "completed")
|
|
|
|
rv.defs += subst_output.string
|
|
|
|
for t in subst_output.textures.keys():
|
|
|
|
rv.textures[t] = subst_output.textures[t]
|
2019-08-17 17:35:48 +02:00
|
|
|
for p in shader_model.parameters:
|
2019-08-09 08:16:24 +02:00
|
|
|
if p.type == "gradient":
|
|
|
|
var g = parameters[p.name]
|
|
|
|
if !(g is MMGradient):
|
|
|
|
g = MMGradient.new()
|
|
|
|
g.deserialize(parameters[p.name])
|
2019-09-16 20:45:47 +02:00
|
|
|
rv.defs += g.get_shader(genname+"__"+p.name+"_gradient_fct")
|
2019-10-10 07:42:38 +02:00
|
|
|
# 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)
|
2019-08-12 15:58:42 +02:00
|
|
|
var variant_index = context.get_variant(self, variant_string)
|
2019-08-02 08:17:32 +02:00
|
|
|
if variant_index == -1:
|
2019-08-12 15:58:42 +02:00
|
|
|
variant_index = context.get_variant(self, variant_string)
|
2019-08-02 08:17:32 +02:00
|
|
|
for t in output_info:
|
|
|
|
if output.has(t.field):
|
2019-08-12 15:58:42 +02:00
|
|
|
var subst_output = subst(output[t.field], context, uv)
|
2019-08-18 16:28:50 +02:00
|
|
|
while subst_output is GDScriptFunctionState:
|
|
|
|
subst_output = yield(subst_output, "completed")
|
2019-09-29 10:07:32 +02:00
|
|
|
# Add global definitions
|
|
|
|
for d in subst_output.globals:
|
|
|
|
if rv.globals.find(d) == -1:
|
|
|
|
rv.globals.push_back(d)
|
|
|
|
# Add generated definitions
|
2019-08-02 08:17:32 +02:00
|
|
|
rv.defs += subst_output.defs
|
2019-09-29 10:07:32 +02:00
|
|
|
# Add generated code
|
2019-08-02 08:17:32 +02:00
|
|
|
rv.code += subst_output.code
|
2019-09-16 20:45:47 +02:00
|
|
|
rv.code += "%s %s_%d_%d_%s = %s;\n" % [ t.type, genname, output_index, variant_index, t.field, subst_output.string ]
|
2019-08-18 16:28:50 +02:00
|
|
|
for t in subst_output.textures.keys():
|
|
|
|
rv.textures[t] = subst_output.textures[t]
|
2019-08-02 08:17:32 +02:00
|
|
|
for t in output_info:
|
|
|
|
if output.has(t.field):
|
2019-09-16 20:45:47 +02:00
|
|
|
rv[t.field] = "%s_%d_%d_%s" % [ genname, output_index, variant_index, t.field ]
|
2019-09-29 10:07:32 +02:00
|
|
|
if shader_model.has("global") && rv.globals.find(shader_model.global) == -1:
|
|
|
|
rv.globals.push_back(shader_model.global)
|
2019-08-02 08:17:32 +02:00
|
|
|
return rv
|
|
|
|
|
2019-09-09 22:00:18 +02:00
|
|
|
func _serialize(data):
|
|
|
|
data.shader_model = shader_model
|
|
|
|
return data
|
2019-09-19 07:56:32 +02:00
|
|
|
|
|
|
|
func edit(node):
|
|
|
|
if shader_model != null:
|
|
|
|
var edit_window = load("res://addons/material_maker/widgets/node_editor/node_editor.tscn").instance()
|
|
|
|
node.get_parent().add_child(edit_window)
|
|
|
|
edit_window.set_model_data(shader_model)
|
|
|
|
edit_window.connect("node_changed", node, "update_generator")
|
|
|
|
edit_window.popup_centered()
|