Skip to content

Commit 8e437b5

Browse files
committed
2 parents 8c99568 + ef44d11 commit 8e437b5

41 files changed

Lines changed: 975 additions & 19 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

addons/material_maker/engine/multi_renderer.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func generate_shader(src_code : MMGenBase.ShaderCode) -> String:
7272
shader_code += "\nuniform float variation = 0.0;\n"
7373
shader_code += "\nvoid fragment() {\n"
7474
shader_code += "float _seed_variation_ = variation;\n"
75+
shader_code += "vec4 _controlled_variation_ = vec4(0.0);\n"
7576
shader_code += "vec2 uv = mm_chunk_offset+mm_chunk_size*UV;\n"
7677
if src_code.code != "":
7778
shader_code += src_code.code

addons/material_maker/engine/nodes/buffer_compute.tres

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const float seed_variation = 0.0;
2727
2828
void main() {
2929
float _seed_variation_ = seed_variation;
30+
vec4 _controlled_variation_ = vec4(0.0);
3031
vec2 pixel = gl_GlobalInvocationID.xy+vec2(0.5, 0.5+mm_chunk_y);
3132
vec2 image_size = imageSize(OUTPUT_TEXTURE);
3233
vec2 uv = pixel/image_size;

addons/material_maker/engine/nodes/gen_base.gd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,22 @@ static func find_matching_parenthesis(string : String, i : int, op : String = '(
635635
i = max_p if min_p < 0 else min_p
636636
return i
637637

638+
static func split_parameters(string : String) -> Array[String]:
639+
if string[0] != "(" or find_matching_parenthesis(string, 0) != string.length()-1:
640+
print("bad")
641+
return []
642+
string = string.substr(1, string.length()-2)
643+
var parameters : Array[String] = []
644+
var p : String = ""
645+
for s in string.split(","):
646+
if p != "":
647+
p += ","
648+
p += s
649+
if p.count("(") == p.count(")") and p.count("[") == p.count("]"):
650+
parameters.append(p.strip_edges())
651+
p = ""
652+
return parameters
653+
638654
static var re_line_comment : RegEx = RegEx.create_from_string("//.*")
639655

640656
static func remove_comments(s : String) -> String:

addons/material_maker/engine/nodes/gen_shader.gd

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ func find_instance_functions(code : String):
105105
for r in result:
106106
if not r.strings[1] in [ "return" ]:
107107
functions.push_back(r.strings[2]);
108-
code = code.replace(r.strings[0], "%s %s(%s, float _seed_variation_) {" % [ r.strings[1], r.strings[2], r.strings[3] ])
108+
code = code.replace(r.strings[0], "%s %s(%s, float _seed_variation_, vec4 _controlled_variation_) {" % [ r.strings[1], r.strings[2], r.strings[3] ])
109109
return { code=code, functions=functions }
110110

111111
func fix_instance_functions(code : String, instance_functions : Array):
112-
var variation_parameter = ", _seed_variation_"
112+
var variation_parameter = ", _seed_variation_, _controlled_variation_"
113113
var variation_parameter_length = variation_parameter.length()
114114
for f in instance_functions:
115115
var location : int = 0
@@ -411,7 +411,7 @@ func find_keyword_call(string : String, keyword : String):
411411
print(string)
412412
return "#error"
413413

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

427+
const WORD_LETTERS : String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_?"
428+
427429
func is_word_letter(l) -> bool:
428-
return "azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN1234567890_".find(l) != -1
430+
return WORD_LETTERS.find(l) != -1
429431

430432
func replace_rnd(string : String, offset : int = 0) -> String:
431433
while true:
@@ -455,8 +457,6 @@ func replace_rndi(string : String, offset : int = 0) -> String:
455457
string = string.replace(replace, with)
456458
return string
457459

458-
const WORD_LETTERS : String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
459-
460460
func generate_parameter_declarations(rv : ShaderCode) -> void:
461461
var genname = "o"+str(get_instance_id())
462462
if has_randomness():
@@ -493,7 +493,7 @@ func generate_input_function(index : int, input: Dictionary, rv : ShaderCode, co
493493
rv.add_uniforms(source_rv.uniforms)
494494
rv.defs += source_rv.defs
495495
rv.add_globals(source_rv.globals)
496-
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 ]
496+
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 ]
497497
rv.defs += "%s\n" % source_rv.code
498498
rv.defs += "return %s;\n}\n" % source_rv.output_values[input.type]
499499

@@ -611,9 +611,16 @@ func replace_input(input_name : String, suffix : String, parameters : String, va
611611
if input_def.has("function") and input_def.function:
612612
var function_name : String = "o%s_input_%s" % [ str(get_instance_id()), input_def.name ]
613613
if suffix == "variation":
614-
return function_name+parameters
614+
var parameter_list : Array[String] = split_parameters(parameters)
615+
var parameters_suffix : String = ")"
616+
match parameter_list.size():
617+
1:
618+
parameters_suffix = ", _seed_variation_, _controlled_variation_)"
619+
2:
620+
parameters_suffix = ", _controlled_variation_)"
621+
return function_name+"("+", ".join(split_parameters(parameters))+parameters_suffix
615622
else:
616-
return function_name+"("+parameters+", _seed_variation_)"
623+
return function_name+"("+parameters+", _seed_variation_, _controlled_variation_)"
617624
var source_rv : ShaderCode = source.generator.get_shader_code(parameters, source.output_index, context)
618625
rv.add_uniforms(source_rv.uniforms)
619626
rv.defs += source_rv.defs
@@ -709,6 +716,10 @@ func get_common_replace_variables(uv : String, rv : ShaderCode) -> Dictionary:
709716
variables["seed"] = "seed_"+genname
710717
else:
711718
variables["seed"] = "(seed_"+genname+"+fract(_seed_variation_))"
719+
variables["?1"] = "_controlled_variation_.x"
720+
variables["?2"] = "_controlled_variation_.y"
721+
variables["?3"] = "_controlled_variation_.z"
722+
variables["?4"] = "_controlled_variation_.w"
712723
variables["node_id"] = str(get_instance_id())
713724
return variables
714725

addons/material_maker/engine/nodes/iterate_buffer_compute.tres

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const float seed_variation = 0.0;
2727
2828
void main() {
2929
float _seed_variation_ = seed_variation;
30+
vec4 _controlled_variation_ = vec4(0.0);
3031
vec2 pixel = gl_GlobalInvocationID.xy+vec2(0.5, 0.5+mm_chunk_y);
3132
vec2 image_size = imageSize(OUTPUT_TEXTURE);
3233
vec2 uv = pixel/image_size;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"generic_size": 1,
3+
"name": "controlled_variations",
4+
"node_position": {
5+
"x": 0,
6+
"y": 0
7+
},
8+
"parameters": {
9+
"v1": 0.5,
10+
"variable": 0.0
11+
},
12+
"seed_int": 0,
13+
"shader_model": {
14+
"code": [
15+
"#for",
16+
"vec4 $(name_uv)_cv_# = _controlled_variation_;",
17+
"$(name_uv)_cv_#.$variable = $v#;",
18+
"#end"
19+
],
20+
"global": "",
21+
"inputs": [
22+
{
23+
"default": "1.0",
24+
"function": true,
25+
"label": "",
26+
"longdesc": "The input image",
27+
"name": "in",
28+
"shortdesc": "Input",
29+
"type": "f"
30+
}
31+
],
32+
"instance": "",
33+
"longdesc": "Generates controlled variations for its input",
34+
"name": "Controlled Variations",
35+
"outputs": [
36+
{
37+
"f": "$in.variation($uv, _seed_variation_, $(name_uv)_cv_#)",
38+
"longdesc": "Shows a variation of the input",
39+
"shortdesc": "Output1",
40+
"type": "f"
41+
}
42+
],
43+
"parameters": [
44+
{
45+
"control": "None",
46+
"default": 0.0,
47+
"label": "",
48+
"longdesc": "Variable value",
49+
"max": 1.0,
50+
"min": 0.0,
51+
"name": "v#",
52+
"shortdesc": "Value",
53+
"step": 0.01,
54+
"type": "float"
55+
},
56+
{
57+
"default": 0.0,
58+
"label": "Variable",
59+
"longdesc": "Variable name",
60+
"name": "variable",
61+
"shortdesc": "Variable",
62+
"type": "enum",
63+
"values": [
64+
{
65+
"name": "$?1",
66+
"value": "x"
67+
},
68+
{
69+
"name": "$?2",
70+
"value": "y"
71+
},
72+
{
73+
"name": "$?3",
74+
"value": "z"
75+
},
76+
{
77+
"name": "$?4",
78+
"value": "w"
79+
}
80+
]
81+
}
82+
],
83+
"shortdesc": "Controlled Variations"
84+
},
85+
"type": "shader"
86+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"name": "fbm_variations",
3+
"node_position": {
4+
"x": 0,
5+
"y": 0
6+
},
7+
"parameters": {
8+
"iterations": 10.0,
9+
"persistance": 0.5,
10+
"randomize": true,
11+
"variable": 0.0
12+
},
13+
"seed_int": 0,
14+
"shader_model": {
15+
"code": "",
16+
"global": "",
17+
"inputs": [
18+
{
19+
"default": "1.0",
20+
"function": true,
21+
"label": "",
22+
"longdesc": "The input image",
23+
"name": "in",
24+
"shortdesc": "Input",
25+
"type": "f"
26+
}
27+
],
28+
"instance": [
29+
"float fbm_variations_$(name)(vec2 uv, int iterations, float persistance) {",
30+
"\tfloat v = 0.0;",
31+
"\tfloat keep = 1.0;",
32+
"\tfloat sum = 0.0;",
33+
"\tfloat seedvar = _seed_variation_;",
34+
"\tvec4 var = _controlled_variation_;",
35+
"\tvar.$variable = 1.0;",
36+
"\tfor (int i = 0; i < iterations; i++) {",
37+
"\t\tfloat input_v = $in.variation(uv, seedvar, var);",
38+
"\t\tv += input_v*keep;",
39+
"\t\tif ($randomize) {",
40+
"\t\t\tseedvar = rand(vec2(seedvar,sum));",
41+
"\t\t}",
42+
"\t\tsum += keep;",
43+
"\t\tvar.$variable *= 2.0;",
44+
"\t\tkeep *= persistance;",
45+
"\t}",
46+
"\treturn v/sum;",
47+
"}"
48+
],
49+
"longdesc": "Combines the octaves of controlled variations for its input",
50+
"name": "FBM Variations",
51+
"outputs": [
52+
{
53+
"f": "fbm_variations_$(name)($uv, int($iterations), $persistance)",
54+
"longdesc": "Shows a variation of the input",
55+
"shortdesc": "Output1",
56+
"type": "f"
57+
}
58+
],
59+
"parameters": [
60+
{
61+
"default": 0.0,
62+
"label": "Variable",
63+
"name": "variable",
64+
"type": "enum",
65+
"values": [
66+
{
67+
"name": "$?1",
68+
"value": "x"
69+
},
70+
{
71+
"name": "$?2",
72+
"value": "y"
73+
},
74+
{
75+
"name": "$?3",
76+
"value": "z"
77+
},
78+
{
79+
"name": "$?4",
80+
"value": "w"
81+
}
82+
]
83+
},
84+
{
85+
"control": "None",
86+
"default": 5.0,
87+
"label": "Iterations",
88+
"longdesc": "Seed modifier for output 1",
89+
"max": 10.0,
90+
"min": 1.0,
91+
"name": "iterations",
92+
"shortdesc": "Seed modifier",
93+
"step": 1.0,
94+
"type": "float"
95+
},
96+
{
97+
"control": "None",
98+
"default": 0.5,
99+
"label": "Persistance",
100+
"max": 1.0,
101+
"min": 0.0,
102+
"name": "persistance",
103+
"step": 0.01,
104+
"type": "float"
105+
},
106+
{
107+
"default": true,
108+
"label": "Randomize",
109+
"name": "randomize",
110+
"type": "boolean"
111+
}
112+
],
113+
"shortdesc": "FBM Variations"
114+
},
115+
"type": "shader"
116+
}

0 commit comments

Comments
 (0)