Skip to content

Commit

Permalink
Fixed rendering bugs (iterate buffer & variations)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed May 5, 2024
1 parent cf681dd commit 9d0b0f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
26 changes: 16 additions & 10 deletions addons/material_maker/engine/nodes/gen_iterate_buffer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func do_update_shaders() -> void:
set_current_iteration(0)

func set_parameter(n : String, v) -> void:
if is_inside_tree():
if n == "size":
mm_deps.dependency_update("o%s_it_tex_size" % get_instance_id(), pow(2, v))
super.set_parameter(n, v)
set_current_iteration(0)

Expand Down Expand Up @@ -169,8 +172,8 @@ func on_dep_update_buffer(buffer_name : String) -> bool:
await get_tree().process_frame
mm_deps.dependency_update(buffer_name, null, true)
is_rendering = false
print("Bad iteration")
return false
print("Bad iteration for buffer %s (%d > %d)" % [ buffer_name, current_iteration, iterations ])
return true
var check_current_iteration : int = current_iteration
var autostop : bool = get_parameter("autostop")

Expand All @@ -190,20 +193,23 @@ func on_dep_update_buffer(buffer_name : String) -> bool:

if check_current_iteration != current_iteration:
mm_deps.dependency_update(buffer_name, texture, true)
print("Iteration mismatch for %s" % buffer_name)
push_warning("Iteration mismatch for %s" % buffer_name)
return false

#todo texture.flags = 0
#print("iteration %d" % current_iteration)

# Calculate iteration index
if autostop and shader_compute.get_difference() == 0:
#print("autostop at %d" % (current_iteration))
set_current_iteration(iterations+1)
else:
set_current_iteration(current_iteration+1)

if current_iteration <= iterations:
mm_deps.dependency_update("o%d_loop_tex" % get_instance_id(), texture, true)
else:
mm_deps.dependency_update("o%d_tex" % get_instance_id(), texture, true)
print("updating texture")
mm_deps.dependency_update("o%d_it_tex" % get_instance_id(), texture, true)
mm_deps.dependency_update(buffer_name, texture, true)

return status
Expand All @@ -224,14 +230,14 @@ func get_globals__(texture_name : String) -> Array[String]:
func get_adjusted_uv(uv : String) -> String:
if not get_parameter("filter"):
var genname = "o"+str(get_instance_id())
return "((floor(%s * %s_tex_size)+vec2(0.5))/%s_tex_size)" % [ uv, genname, genname ]
return "((floor(%s * %s_it_tex_size)+vec2(0.5))/%s_it_tex_size)" % [ uv, genname, genname ]
else:
return uv

func _get_shader_code(uv : String, output_index : int, context : MMGenContext) -> ShaderCode:
var genname = "o"+str(get_instance_id())
var shader_code = _get_shader_code_lod(uv, output_index, context, is_greyscale, -1.0, "_tex" if output_index == 0 else "_loop_tex")
shader_code.add_uniform("%s_tex_size" % genname, "float", pow(2, get_parameter("size")))
var shader_code = _get_shader_code_lod(uv, output_index, context, is_greyscale, -1.0, "_it_tex" if output_index == 0 else "_loop_tex")
shader_code.add_uniform("%s_it_tex_size" % genname, "float", pow(2, get_parameter("size")))
return shader_code

func get_output_attributes(output_index : int) -> Dictionary:
Expand All @@ -240,10 +246,10 @@ func get_output_attributes(output_index : int) -> Dictionary:
match output_index:
0:
attributes.texture = "%s_tex" % genname
attributes.texture_size = "%s_tex_size" % genname
attributes.texture_size = "%s_it_tex_size" % genname
1:
attributes.texture = "%s_loop_tex" % genname
attributes.texture_size = "%s_tex_size" % genname
attributes.texture_size = "%s_it_tex_size" % genname
attributes.iteration = iteration_param_name
return attributes

Expand Down
4 changes: 2 additions & 2 deletions addons/material_maker/engine/nodes/gen_shader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func replace_input_new(input_name : String, suffix : String, parameters : String
if suffix == "variation":
return function_name+parameters
else:
return function_name+"("+parameters+", 0.0)"
return function_name+"("+parameters+", _seed_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 @@ -708,7 +708,7 @@ func get_common_replace_variables(uv : String, rv : ShaderCode) -> Dictionary:
if seed_locked:
variables["seed"] = "seed_"+genname
else:
variables["seed"] = "(seed_"+genname+"+fract(_seed_variation_))"
variables["seed"] = "(seed_"+genname+"+_seed_variation_)"
variables["node_id"] = str(get_instance_id())
return variables

Expand Down
8 changes: 5 additions & 3 deletions material_maker/panels/preview_2d/preview_2d.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends ColorRect

@export var shader_context_defs : String = "" # (String, MULTILINE)
@export var shader : String = "" # (String, MULTILINE)
@export_multiline var shader_context_defs : String = "" # (String, MULTILINE)
@export_multiline var shader : String = "" # (String, MULTILINE)

var generator : MMGenBase = null
var output : int = 0
Expand Down Expand Up @@ -40,11 +40,13 @@ func update_export_menu() -> void:
func generate_preview_shader(source, template) -> String:
return MMGenBase.generate_preview_shader(source, source.output_type, template)

func do_update_material(source, target_material : ShaderMaterial, template):
func do_update_material(source, target_material : ShaderMaterial, template : String):
if source.output_type == "":
return
is_greyscale = source.output_type == "f"
# Update shader
if template.find("TIME") != -1:
print("Template has time") # This should not happen
var code = generate_preview_shader(source, template)
await mm_deps.buffer_create_shader_material("preview_"+str(get_instance_id()), MMShaderMaterial.new(target_material), code)
for u in source.uniforms:
Expand Down
10 changes: 5 additions & 5 deletions material_maker/panels/preview_2d/preview_2d.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ shader = "uniform vec2 preview_2d_size;
void fragment() {
float ms = max(preview_2d_size.x, preview_2d_size.y);
vec2 uv = fract(0.5+1.2*(UV-vec2(0.5))*ms/preview_2d_size.yx);
float is = min(preview_2d_size.x, preview_2d_size.y)/1.2;
vec2 m2 = min(fract(uv), 1.0-fract(uv));
vec2 uv = 0.5+(UV-vec2(0.5))*ms/preview_2d_size.yx;
float is = min(preview_2d_size.x, preview_2d_size.y);
vec4 image = preview_2d(uv);
vec3 image_with_background = mix(vec3(mod(floor(uv.x*32.0)+floor(uv.y*32.0), 2.0)), image.xyz, image.a);
float lines_color = 0.5*(cos(5.0*TIME+100.0*(uv.x+uv.y))+1.0);
COLOR = vec4(mix(image_with_background, vec3(lines_color), step(is*min(m2.x, m2.y), 1.0)), 1.0);
uv -= vec2(0.5);
uv = abs(uv);
COLOR = vec4(image_with_background, step(max(uv.x, uv.y), 0.5)*0.8+0.2);
}"

[connection signal="resized" from="." to="." method="on_resized"]
Expand Down

0 comments on commit 9d0b0f0

Please sign in to comment.