Skip to content

Commit

Permalink
Updated shader code generation for polygons (now use array uniforms).
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Feb 22, 2024
1 parent bfd2f18 commit d80d00d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
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 @@ -295,6 +295,16 @@ func set_parameter(n : String, v) -> void:
# 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
elif parameter_def.type == "polyline" or parameter_def.type == "polygon":
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 MMPolygon and v is MMPolygon and old_value != null:
if old_value.points.size() == v.points.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
elif parameter_def.type == "curve":
if old_value is MMCurve and v is MMCurve and old_value != null:
var parameter_changes = {}
Expand Down
4 changes: 3 additions & 1 deletion addons/material_maker/engine/nodes/gen_shader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,9 @@ func process_parameters(rv : ShaderCode, variables : Dictionary, generate_declar
if !(value is MMPolygon):
value = MMPolygon.new()
value.deserialize(parameters[p.name])
variables[p.name] = value.get_shader()
if generate_declarations:
rv.add_uniforms(value.get_parameters(genname+"_"+p.name))
variables[p.name] = value.get_shader(genname+"_"+p.name)
else:
print("ERROR: Unsupported parameter "+p.name+" of type "+p.type)

Expand Down
5 changes: 5 additions & 0 deletions addons/material_maker/engine/pipeline/pipeline.gd
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ func set_parameter(name : String, value, silent : bool = false) -> void:
parameter_values.encode_float(p.offset, value.x)
parameter_values.encode_float(p.offset+4, value.y)
return
elif value is PackedVector2Array and value.size() == p.array_size:
for i in value.size():
parameter_values.encode_float(p.offset+i*8, value[i].x)
parameter_values.encode_float(p.offset+i*8+4, value[i].y)
return
"vec3":
if value is Vector3:
parameter_values.encode_float(p.offset, value.x)
Expand Down
26 changes: 20 additions & 6 deletions addons/material_maker/types/polygon.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends RefCounted
class_name MMPolygon

var points : Array[Vector2] = [Vector2(0.2, 0.2), Vector2(0.7, 0.4), Vector2(0.4, 0.7)]
var points : PackedVector2Array = PackedVector2Array([Vector2(0.2, 0.2), Vector2(0.7, 0.4), Vector2(0.4, 0.7)])

func to_string() -> String:
var rv = PackedStringArray()
Expand Down Expand Up @@ -61,11 +61,25 @@ func get_point(i : int) -> Vector2:
func set_point(i : int, v : Vector2) -> void:
points[i] = v

func get_shader() -> String:
var elements : PackedStringArray = PackedStringArray()
for p in points:
elements.append("vec2(%.9f, %.9f)" % [p.x, p.y])
return "{"+", ".join(elements)+"}"
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 = []
Expand Down
4 changes: 2 additions & 2 deletions material_maker/panels/preview_2d/preview_2d_panel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func setup_controls(filter : String = "") -> void:
else:
param_defs = generator.get_parameter_defs()
for c in get_children():
if c.has_method("setup_control"):
c.setup_control(generator, param_defs)
if c.has_method("set_view_rect"):
var s : float = min(size.x, size.y)/view_scale
c.set_view_rect(0.5*size-center*s, Vector2(s, s))
if c.has_method("setup_control"):
c.setup_control(generator, param_defs)

var center_transform : Transform2D = Transform2D(0, Vector2(0.0, 0.0))
var local_rotate : float = 0.0
Expand Down

0 comments on commit d80d00d

Please sign in to comment.