-
-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into better-nodes
- Loading branch information
Showing
37 changed files
with
1,273 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.