Skip to content

Commit

Permalink
Merge branch 'master' into better-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 authored Aug 3, 2024
2 parents 6fed02d + 0268009 commit 9ecb01a
Show file tree
Hide file tree
Showing 37 changed files with 1,273 additions and 289 deletions.
3 changes: 1 addition & 2 deletions addons/material_maker/engine/multi_renderer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func generate_shader(src_code : MMGenBase.ShaderCode) -> String:
code += "render_mode blend_disabled, unshaded;\n"
code += common_shader
code += "\n"
for g in src_code.globals:
code += g
code += src_code.get_globals_string()
var shader_code = ""
shader_code += src_code.uniforms_as_strings()
shader_code += "\n"
Expand Down
10 changes: 10 additions & 0 deletions addons/material_maker/engine/nodes/gen_base.gd
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ func set_parameter(n : String, v) -> void:
if old_value.size == v.size and old_value.bpp == v.bpp:
mm_deps.dependencies_update(v.get_parameter_values("o%d_%s" % [ get_instance_id(), n ]))
return
elif parameter_def.type == "lattice":
if old_value is Dictionary:
old_value = MMType.deserialize_value(old_value)
if v is Dictionary:
v = MMType.deserialize_value(v)
if old_value is MMLattice and v is MMLattice and old_value != null:
if old_value.size == v.size:
# Only values changed, no need to regenerate the shader
mm_deps.dependencies_update(v.get_parameter_values("o%d_%s" % [ get_instance_id(), n ]))
return
all_sources_changed()

func notify_output_change(output_index : int) -> void:
Expand Down
9 changes: 9 additions & 0 deletions addons/material_maker/engine/nodes/gen_shader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,15 @@ func process_parameters(rv : ShaderCode, variables : Dictionary, generate_declar
rv.defs += g.get_shader(genname+"_"+p.name)
variables[p.name] = genname+"_"+p.name+"_pixels_fct"
variables[p.name+"_size"] = genname+"_"+p.name+"_size"
elif p.type == "lattice":
var value = parameters[p.name]
if not value is MMLattice:
value = MMLattice.new()
value.deserialize(parameters[p.name])
if generate_declarations:
rv.add_uniforms(value.get_parameters(genname+"_"+p.name))
variables[p.name] = value.get_shader(genname+"_"+p.name)
variables[p.name+"_size"] = "ivec2(%d, %d)" % [ value.size.x, value.size.y ]
else:
print("ERROR: Unsupported parameter "+p.name+" of type "+p.type)

Expand Down
4 changes: 4 additions & 0 deletions addons/material_maker/engine/renderer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func save_to_file(fn : String, is_greyscale : bool = false) -> void:
match fn.get_extension():
"png":
export_image.save_png(fn)
"jpg":
export_image.save_jpg(fn, 1.0)
"webp":
export_image.save_webp(fn)
"exr":
if is_greyscale:
export_image = Image.new()
Expand Down
156 changes: 156 additions & 0 deletions addons/material_maker/nodes/distort.mmg
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"name": "distort",
"node_position": {
"x": 0,
"y": 0
},
"parameters": {
"lattice": {
"points": [
{
"x": 0,
"y": 0
},
{
"x": 1,
"y": 0
},
{
"x": 0,
"y": 1
},
{
"x": 1,
"y": 1
}
],
"size": {
"x": 1,
"y": 1
},
"type": "Lattice"
}
},
"seed_int": 0,
"shader_model": {
"code": "",
"global": [
"float perspective_transform_coord(vec2 p, vec2 a, vec2 b, vec2 c, vec2 d) {",
"\tvec2 ab = b-a;",
"\tvec2 ac = c-a;",
"\t// vec2 axb = a+x*ab;",
"\tvec2 cd = d-c;",
"\tvec2 abcd = cd-ab;",
"\tvec2 pa = a-p;",
"\t// vec2 cxd = c+x*cd;",
"\t// vec2 v1 = ac+x*abcd;",
"\t// vec2 v2 = pa+x*ab;",
"\t// (ac.x+x*abcd.x)*(pa.y+x*ab.y)-(ac.y+x*abcd.y)*(pa.x+x*ab.x) = 0;",
"\t// (ac.x*pa.y-ac.y*pa.x)+x*(abcd.x*pa.y+ac.x*ab.y-abcd.y*pa.x-ac.y*ab.x)+x*x*(abcd.x*ab.y-abcd.y*ab.x) = 0",
"\tfloat aa = abcd.x*ab.y-abcd.y*ab.x;",
"\tfloat bb = abcd.x*pa.y+ac.x*ab.y-abcd.y*pa.x-ac.y*ab.x;",
"\tfloat cc = ac.x*pa.y-ac.y*pa.x;",
"\tfloat delta = bb*bb-4.0*aa*cc;",
"\tif (abs(aa) < 0.00001) {",
"\t\treturn -cc/bb;",
"\t} else if (delta >= 0.0) {",
"\t\tfloat s1 = (-bb-sqrt(delta))/(2.0*aa);",
"\t\tfloat s2 = (-bb+sqrt(delta))/(2.0*aa);",
"\t\treturn (abs(s1-0.5) < abs(s2-0.5)) ? s1 : s2;",
"\t}",
"\treturn 0.0;",
"}",
"",
"vec2 perspective_transform(vec2 p, vec2 a, vec2 b, vec2 c, vec2 d) {",
"\treturn vec2(perspective_transform_coord(p, a, b, d, c), perspective_transform_coord(p, a, d, b, c));",
"}",
""
],
"inputs": [
{
"default": "vec4(0.0)",
"function": true,
"label": "",
"longdesc": "The input to be distorted",
"name": "in",
"shortdesc": "Input",
"type": "rgba"
}
],
"instance": [
"vec4 distort_$(name)(vec2 uv) {",
"\tvec2 p[] = $lattice;",
"\tivec2 lsize = $lattice_size;",
"\tvec2 luv;",
"\tbool inside = false;",
"\tfor (int y = 0; y < lsize.y; ++y) {",
"\t\tint yi0 = y*(lsize.x+1);",
"\t\tint yi1 = yi0+lsize.x+1;",
"\t\tfor (int x = 0; x < lsize.x; ++x) {",
"\t\t\tluv = perspective_transform(uv, p[yi0+x], p[yi0+x+1], p[yi1+x+1], p[yi1+x]);",
"\t\t\tvec2 luv_m05 = luv-vec2(0.5);",
"\t\t\tinside = abs(luv_m05.x) < 0.5 && abs(luv_m05.y) < 0.5;",
"\t\t\tif (inside) {",
"\t\t\t\tluv += vec2(float(x), float(y));",
"\t\t\t\tluv /= vec2(float(lsize.x), float(lsize.y));",
"\t\t\t\treturn $in(luv);",
"\t\t\t}",
"\t\t}",
"\t}",
"\t/*",
"\t\tP0 = p0+x*(p1-p0)",
"\t\tP1 = p3+x*(p2-p3)",
"\t\tP = a+x*(b-a)+y*(d-a)+xy*(c-b)+xy*(a-d)",
"\t*/",
"\treturn inside ? $in(luv) : vec4(0.0);",
"}",
""
],
"longdesc": "Distorts its input",
"name": "Distort",
"outputs": [
{
"longdesc": "The distorted image",
"rgba": "distort_$(name)($uv)",
"shortdesc": "Output",
"type": "rgba"
}
],
"parameters": [
{
"default": {
"points": [
{
"x": 0,
"y": 0
},
{
"x": 1,
"y": 0
},
{
"x": 1,
"y": 1
},
{
"x": 0,
"y": 1
}
],
"size": {
"x": 1,
"y": 1
},
"type": "Lattice"
},
"label": "",
"longdesc": "The lattice that describes the distorsion",
"name": "lattice",
"shortdesc": "Lattice",
"type": "lattice"
}
],
"shortdesc": "Distort"
},
"type": "shader"
}
5 changes: 4 additions & 1 deletion addons/material_maker/nodes/warp_dilation2_nobuf.mmg
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
"\tfloat v = 0.0;",
"\tfor (float x = 0.0; x <= $d; x += e) {",
"\t\tv = max(v, $in(fract(uv))*(1.0-x/$d*$a));",
"\t\tvec2 delta = $(name)_slope(uv, 0.0001);",
"\t\tvec2 delta = $(name)_slope(uv, e);",
"\t\tif (delta.x == 0.0 && delta.y == 0.0) {",
"\t\t\tbreak;",
"\t\t}",
"\t\tuv += e*normalize(delta);",
"\t}",
"\treturn v;",
Expand Down
104 changes: 104 additions & 0 deletions addons/material_maker/types/lattice.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
extends RefCounted
class_name MMLattice

var size : Vector2i = Vector2i(1, 1)
var points : PackedVector2Array = PackedVector2Array([Vector2(0, 0), Vector2(1, 0), Vector2(1, 1), Vector2(0, 1)])

func to_string() -> String:
var rv = PackedStringArray()
for p in points:
rv.append("("+str(p.x)+","+str(p.y)+")")
return ",".join(rv)

func duplicate() -> Object:
var copy = MMLattice.new()
copy.size = size
copy.points.clear()
for p in points:
copy.points.append(p)
return copy

func compare(lattice) -> bool:
if lattice.size != size:
return false
for i in points.size():
if points[i] != lattice.points[i]:
return false
return true

static func get_point_from_array(v : Vector2i, size : Vector2i, points : PackedVector2Array) -> Vector2:
return points[v.x+(size.x+1)*v.y]

static func interpolate_point(v : Vector2, size : Vector2i, points : PackedVector2Array) -> Vector2:
v *= Vector2(size)
var vs : Vector2i = Vector2i(floor(v.x), floor(v.y))
vs.x = clampi(vs.x, 0, size.x-1)
vs.y = clampi(vs.y, 0, size.y-1)
var vt : Vector2 = Vector2(v.x-vs.x, v.y-vs.y)
print(str(vs)+" "+str(vt))
var p00 : Vector2 = get_point_from_array(vs, size, points)
var p01 : Vector2 = get_point_from_array(vs+Vector2i(0, 1), size, points)
var p10 : Vector2 = get_point_from_array(vs+Vector2i(1, 0), size, points)
var p11 : Vector2 = get_point_from_array(vs+Vector2i(1, 1), size, points)
var p0 : Vector2 = lerp(p00, p01, vt.y)
var p1 : Vector2 = lerp(p10, p11, vt.y)
return lerp(p0, p1, vt.x)

func resize(sx : int, sy : int) -> void:
var old_size : Vector2i = size
size.x = sx
size.y = sy
var old_points : PackedVector2Array = points
points = PackedVector2Array()
points.resize((sx+1)*(sy+1))
for ix in sx+1:
var x : float = float(ix)/float(size.x)
for iy in sy+1:
var y : float = float(iy)/float(size.y)
set_point(ix, iy, interpolate_point(Vector2(x, y), old_size, old_points))

func get_point(x : int, y : int) -> Vector2:
return points[x+(size.x+1)*y]

func set_point(x : int, y : int, v : Vector2) -> void:
points[x+(size.x+1)*y] = v

func get_shader_params(parameter_name : String, attribute : String = "uniform") -> String:
var rv = ""
for p : MMGenBase.ShaderUniform in get_parameters(parameter_name):
rv += p.to_str(attribute)
return rv

func get_parameters(parameter_name : String) -> Array[MMGenBase.ShaderUniform]:
var rv : Array[MMGenBase.ShaderUniform] = []
var parameter_values : Dictionary = get_parameter_values(parameter_name)
rv.append(MMGenBase.ShaderUniform.new("p_%s_pos" % parameter_name, "vec2", parameter_values["p_%s_pos" % parameter_name], points.size()))
return rv

func get_parameter_values(parameter_name : String) -> Dictionary:
var rv : Dictionary = {}
rv["p_%s_pos" % parameter_name] = points
return rv

func get_shader(parameter_name : String) -> String:
return "p_%s_pos" % parameter_name

func serialize() -> Dictionary:
var rv = []
for p in points:
rv.append({ x=p.x, y=p.y })
return { type="Lattice", size={ x=size.x, y=size.y }, points=rv }

func deserialize(v) -> void:
if typeof(v) == TYPE_DICTIONARY and v.has("type") and v.type == "Lattice":
size = Vector2i(int(v.size.x), int(v.size.y))
points.clear()
for p in v.points:
points.push_back(Vector2(p.x, p.y))
elif typeof(v) == TYPE_OBJECT and v.get_script() == get_script():
size = v.size
points.clear()
for p in v.points:
points.push_back(p)
else:
print("Cannot deserialize lattice")
4 changes: 4 additions & 0 deletions addons/material_maker/types/types.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ static func deserialize_value(data):
var splines = MMPixels.new()
splines.deserialize(data)
return splines
elif data.type == "Lattice":
var lattice = MMLattice.new()
lattice.deserialize(data)
return lattice
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
Expand Down
6 changes: 3 additions & 3 deletions export_presets.cfg

Large diffs are not rendered by default.

Loading

0 comments on commit 9ecb01a

Please sign in to comment.