Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions addons/material_maker/engine/multi_renderer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func generate_shader(src_code : MMGenBase.ShaderCode) -> String:
shader_code += "\nuniform float variation = 0.0;\n"
shader_code += "\nvoid fragment() {\n"
shader_code += "float _seed_variation_ = variation;\n"
shader_code += "vec4 _controlled_variation_ = vec4(0.0);\n"
shader_code += "vec2 uv = mm_chunk_offset+mm_chunk_size*UV;\n"
if src_code.code != "":
shader_code += src_code.code
Expand Down
1 change: 1 addition & 0 deletions addons/material_maker/engine/nodes/buffer_compute.tres
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const float seed_variation = 0.0;

void main() {
float _seed_variation_ = seed_variation;
vec4 _controlled_variation_ = vec4(0.0);
vec2 pixel = gl_GlobalInvocationID.xy+vec2(0.5, 0.5+mm_chunk_y);
vec2 image_size = imageSize(OUTPUT_TEXTURE);
vec2 uv = pixel/image_size;
Expand Down
16 changes: 16 additions & 0 deletions addons/material_maker/engine/nodes/gen_base.gd
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,22 @@ static func find_matching_parenthesis(string : String, i : int, op : String = '(
i = max_p if min_p < 0 else min_p
return i

static func split_parameters(string : String) -> Array[String]:
if string[0] != "(" or find_matching_parenthesis(string, 0) != string.length()-1:
print("bad")
return []
string = string.substr(1, string.length()-2)
var parameters : Array[String] = []
var p : String = ""
for s in string.split(","):
if p != "":
p += ","
p += s
if p.count("(") == p.count(")") and p.count("[") == p.count("]"):
parameters.append(p.strip_edges())
p = ""
return parameters

static var re_line_comment : RegEx = RegEx.create_from_string("//.*")

static func remove_comments(s : String) -> String:
Expand Down
29 changes: 20 additions & 9 deletions addons/material_maker/engine/nodes/gen_shader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ func find_instance_functions(code : String):
for r in result:
if not r.strings[1] in [ "return" ]:
functions.push_back(r.strings[2]);
code = code.replace(r.strings[0], "%s %s(%s, float _seed_variation_) {" % [ r.strings[1], r.strings[2], r.strings[3] ])
code = code.replace(r.strings[0], "%s %s(%s, float _seed_variation_, vec4 _controlled_variation_) {" % [ r.strings[1], r.strings[2], r.strings[3] ])
return { code=code, functions=functions }

func fix_instance_functions(code : String, instance_functions : Array):
var variation_parameter = ", _seed_variation_"
var variation_parameter = ", _seed_variation_, _controlled_variation_"
var variation_parameter_length = variation_parameter.length()
for f in instance_functions:
var location : int = 0
Expand Down Expand Up @@ -411,7 +411,7 @@ func find_keyword_call(string : String, keyword : String):
print(string)
return "#error"

func replace_input_with_function_call(string : String, input : String, seed_parameter : String = ", _seed_variation_", input_suffix : String = "") -> String:
func replace_input_with_function_call(string : String, input : String, seed_parameter : String = ", _seed_variation_, _controlled_variation_", input_suffix : String = "") -> String:
var genname = "o"+str(get_instance_id())
while true:
var uv : String = find_keyword_call(string, input+input_suffix)
Expand All @@ -424,8 +424,10 @@ func replace_input_with_function_call(string : String, input : String, seed_para
string = string.replace("$%s(%s)" % [ input+input_suffix, uv ], "%s_input_%s(%s%s)" % [ genname, input, uv, seed_parameter ])
return string

const WORD_LETTERS : String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_?"

func is_word_letter(l) -> bool:
return "azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN1234567890_".find(l) != -1
return WORD_LETTERS.find(l) != -1

func replace_rnd(string : String, offset : int = 0) -> String:
while true:
Expand Down Expand Up @@ -455,8 +457,6 @@ func replace_rndi(string : String, offset : int = 0) -> String:
string = string.replace(replace, with)
return string

const WORD_LETTERS : String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"

func generate_parameter_declarations(rv : ShaderCode) -> void:
var genname = "o"+str(get_instance_id())
if has_randomness():
Expand Down Expand Up @@ -493,7 +493,7 @@ func generate_input_function(index : int, input: Dictionary, rv : ShaderCode, co
rv.add_uniforms(source_rv.uniforms)
rv.defs += source_rv.defs
rv.add_globals(source_rv.globals)
rv.defs += "%s %s_input_%s(%s, float _seed_variation_) {\n" % [ mm_io_types.types[input.type].type, genname, input.name, mm_io_types.types[input.type].paramdefs ]
rv.defs += "%s %s_input_%s(%s, float _seed_variation_, vec4 _controlled_variation_) {\n" % [ mm_io_types.types[input.type].type, genname, input.name, mm_io_types.types[input.type].paramdefs ]
rv.defs += "%s\n" % source_rv.code
rv.defs += "return %s;\n}\n" % source_rv.output_values[input.type]

Expand Down Expand Up @@ -611,9 +611,16 @@ func replace_input(input_name : String, suffix : String, parameters : String, va
if input_def.has("function") and input_def.function:
var function_name : String = "o%s_input_%s" % [ str(get_instance_id()), input_def.name ]
if suffix == "variation":
return function_name+parameters
var parameter_list : Array[String] = split_parameters(parameters)
var parameters_suffix : String = ")"
match parameter_list.size():
1:
parameters_suffix = ", _seed_variation_, _controlled_variation_)"
2:
parameters_suffix = ", _controlled_variation_)"
return function_name+"("+", ".join(split_parameters(parameters))+parameters_suffix
else:
return function_name+"("+parameters+", _seed_variation_)"
return function_name+"("+parameters+", _seed_variation_, _controlled_variation_)"
var source_rv : ShaderCode = source.generator.get_shader_code(parameters, source.output_index, context)
rv.add_uniforms(source_rv.uniforms)
rv.defs += source_rv.defs
Expand Down Expand Up @@ -709,6 +716,10 @@ func get_common_replace_variables(uv : String, rv : ShaderCode) -> Dictionary:
variables["seed"] = "seed_"+genname
else:
variables["seed"] = "(seed_"+genname+"+fract(_seed_variation_))"
variables["?1"] = "_controlled_variation_.x"
variables["?2"] = "_controlled_variation_.y"
variables["?3"] = "_controlled_variation_.z"
variables["?4"] = "_controlled_variation_.w"
variables["node_id"] = str(get_instance_id())
return variables

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const float seed_variation = 0.0;

void main() {
float _seed_variation_ = seed_variation;
vec4 _controlled_variation_ = vec4(0.0);
vec2 pixel = gl_GlobalInvocationID.xy+vec2(0.5, 0.5+mm_chunk_y);
vec2 image_size = imageSize(OUTPUT_TEXTURE);
vec2 uv = pixel/image_size;
Expand Down
86 changes: 86 additions & 0 deletions addons/material_maker/nodes/controlled_variations.mmg
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"generic_size": 1,
"name": "controlled_variations",
"node_position": {
"x": 0,
"y": 0
},
"parameters": {
"v1": 0.5,
"variable": 0.0
},
"seed_int": 0,
"shader_model": {
"code": [
"#for",
"vec4 $(name_uv)_cv_# = _controlled_variation_;",
"$(name_uv)_cv_#.$variable = $v#;",
"#end"
],
"global": "",
"inputs": [
{
"default": "1.0",
"function": true,
"label": "",
"longdesc": "The input image",
"name": "in",
"shortdesc": "Input",
"type": "f"
}
],
"instance": "",
"longdesc": "Generates controlled variations for its input",
"name": "Controlled Variations",
"outputs": [
{
"f": "$in.variation($uv, _seed_variation_, $(name_uv)_cv_#)",
"longdesc": "Shows a variation of the input",
"shortdesc": "Output1",
"type": "f"
}
],
"parameters": [
{
"control": "None",
"default": 0.0,
"label": "",
"longdesc": "Variable value",
"max": 1.0,
"min": 0.0,
"name": "v#",
"shortdesc": "Value",
"step": 0.01,
"type": "float"
},
{
"default": 0.0,
"label": "Variable",
"longdesc": "Variable name",
"name": "variable",
"shortdesc": "Variable",
"type": "enum",
"values": [
{
"name": "$?1",
"value": "x"
},
{
"name": "$?2",
"value": "y"
},
{
"name": "$?3",
"value": "z"
},
{
"name": "$?4",
"value": "w"
}
]
}
],
"shortdesc": "Controlled Variations"
},
"type": "shader"
}
116 changes: 116 additions & 0 deletions addons/material_maker/nodes/fbm_variations.mmg
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"name": "fbm_variations",
"node_position": {
"x": 0,
"y": 0
},
"parameters": {
"iterations": 10.0,
"persistance": 0.5,
"randomize": true,
"variable": 0.0
},
"seed_int": 0,
"shader_model": {
"code": "",
"global": "",
"inputs": [
{
"default": "1.0",
"function": true,
"label": "",
"longdesc": "The input image",
"name": "in",
"shortdesc": "Input",
"type": "f"
}
],
"instance": [
"float fbm_variations_$(name)(vec2 uv, int iterations, float persistance) {",
"\tfloat v = 0.0;",
"\tfloat keep = 1.0;",
"\tfloat sum = 0.0;",
"\tfloat seedvar = _seed_variation_;",
"\tvec4 var = _controlled_variation_;",
"\tvar.$variable = 1.0;",
"\tfor (int i = 0; i < iterations; i++) {",
"\t\tfloat input_v = $in.variation(uv, seedvar, var);",
"\t\tv += input_v*keep;",
"\t\tif ($randomize) {",
"\t\t\tseedvar = rand(vec2(seedvar,sum));",
"\t\t}",
"\t\tsum += keep;",
"\t\tvar.$variable *= 2.0;",
"\t\tkeep *= persistance;",
"\t}",
"\treturn v/sum;",
"}"
],
"longdesc": "Combines the octaves of controlled variations for its input",
"name": "FBM Variations",
"outputs": [
{
"f": "fbm_variations_$(name)($uv, int($iterations), $persistance)",
"longdesc": "Shows a variation of the input",
"shortdesc": "Output1",
"type": "f"
}
],
"parameters": [
{
"default": 0.0,
"label": "Variable",
"name": "variable",
"type": "enum",
"values": [
{
"name": "$?1",
"value": "x"
},
{
"name": "$?2",
"value": "y"
},
{
"name": "$?3",
"value": "z"
},
{
"name": "$?4",
"value": "w"
}
]
},
{
"control": "None",
"default": 5.0,
"label": "Iterations",
"longdesc": "Seed modifier for output 1",
"max": 10.0,
"min": 1.0,
"name": "iterations",
"shortdesc": "Seed modifier",
"step": 1.0,
"type": "float"
},
{
"control": "None",
"default": 0.5,
"label": "Persistance",
"max": 1.0,
"min": 0.0,
"name": "persistance",
"step": 0.01,
"type": "float"
},
{
"default": true,
"label": "Randomize",
"name": "randomize",
"type": "boolean"
}
],
"shortdesc": "FBM Variations"
},
"type": "shader"
}
Loading