Optimized seed and thumbnail previews

This commit is contained in:
RodZill4 2020-03-24 19:21:28 +01:00
parent fcfdcef286
commit fd92c57905
14 changed files with 37 additions and 21 deletions

View File

@ -90,8 +90,8 @@ func init_parameters() -> void:
func set_position(p) -> void:
position = p
if has_randomness() and !is_seed_locked():
source_changed(0)
if has_randomness() and !is_seed_locked() and is_inside_tree():
get_tree().call_group("preview", "on_float_parameter_changed", "seed_o%s" % [ str(get_instance_id()) ], get_seed())
func get_type() -> String:
return "generic"

View File

@ -37,8 +37,9 @@ func get_output_defs() -> Array:
return [ { type="rgba" }, { type="rgba" } ]
func source_changed(_input_port_index : int) -> void:
if !is_inside_tree():
return
call_deferred("update_shader")
func update_shader() -> void:
var context : MMGenContext = MMGenContext.new()
var source = {}
var source_output = get_source(0)
@ -73,7 +74,7 @@ func update_buffer() -> void:
updating = true
while update_again:
update_again = false
var result = mm_renderer.render_material(material, pow(2, parameters.size))
var result = mm_renderer.render_material(material, pow(2, get_parameter("size")))
while result is GDScriptFunctionState:
result = yield(result, "completed")
if !update_again:

View File

@ -18,9 +18,11 @@ func toggle_editable() -> bool:
func is_editable() -> bool:
return editable
func has_randomness() -> bool:
return uses_seed
func get_type() -> String:
return "shader"
@ -178,7 +180,7 @@ func subst(string : String, context : MMGenContext, uv : String = "") -> Diction
if uv != "":
var genname_uv = genname+"_"+str(context.get_variant(self, uv))
string = replace_variable(string, "name_uv", genname_uv)
var tmp_string = replace_variable(string, "seed", str(get_seed()))
var tmp_string = replace_variable(string, "seed", "seed_"+genname)
if tmp_string != string:
string = tmp_string
if shader_model.has("parameters") and typeof(shader_model.parameters) == TYPE_ARRAY:
@ -246,6 +248,8 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext) -
var output = shader_model.outputs[output_index]
if !context.has_variant(self):
# Generate functions for gradients
if has_randomness():
rv.defs += "uniform int seed_%s = %d;\n" % [ genname, get_seed() ]
for p in shader_model.parameters:
if p.type == "float":
rv.defs += "uniform float p_%s_%s = %.9f;\n" % [ genname, p.name, parameters[p.name] ]

View File

@ -22,7 +22,7 @@
"type": "f"
},
{
"rgb": "rand3(fract($(name_uv)_center.zw/vec2($sx, $sy))+vec2($seed))",
"rgb": "rand3(fract($(name_uv)_center.zw/vec2($sx, $sy))+vec2(float($seed)))",
"type": "rgb"
},
{

View File

@ -29,7 +29,7 @@
"type": "f"
},
{
"rgb": "rand3(fract($(name_uv)_rect.xy)+rand2(vec2($seed)))",
"rgb": "rand3(fract($(name_uv)_rect.xy)+rand2(vec2(float($seed))))",
"type": "rgb"
},
{

View File

@ -33,7 +33,7 @@
"name": "Splatter",
"outputs": [
{
"f": "splatter_$(name)($uv, int($count), vec2($seed))",
"f": "splatter_$(name)($uv, int($count), vec2(float($seed)))",
"type": "f"
}
],

View File

@ -33,7 +33,7 @@
"name": "Color Splatter",
"outputs": [
{
"rgba": "splatter_$(name)($uv, int($count), vec2($seed))",
"rgba": "splatter_$(name)($uv, int($count), vec2(float($seed)))",
"type": "rgba"
}
],

View File

@ -36,7 +36,7 @@
"name": "Tiler",
"outputs": [
{
"f": "tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2($seed))",
"f": "tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2(float($seed)))",
"type": "f"
}
],

View File

@ -36,7 +36,7 @@
"name": "Color Tiler",
"outputs": [
{
"rgba": "tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2($seed))",
"rgba": "tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2(float($seed)))",
"type": "rgba"
}
],

View File

@ -4854,6 +4854,9 @@
{
"collapsed": true,
"icon": "miscellaneous_buffer",
"parameters": {
"size": 9
},
"tree_item": "Miscellaneous/Buffer",
"type": "buffer"
},

View File

@ -5,7 +5,8 @@ var controls = {}
var ignore_parameter_change = ""
var output_count = 0
var preview : TextureRect
var preview : ColorRect
#var preview : TextureRect
var preview_index : int = -1
var preview_position : int
var preview_size : int
@ -151,7 +152,8 @@ func save_preview_widget() -> void:
func restore_preview_widget() -> void:
if preview == null:
preview = TextureRect.new()
preview = preload("res://material_maker/preview/preview_2d.tscn").instance()
preview.shader = "uniform vec2 size;void fragment() {COLOR = preview_2d(UV);}"
preview.visible = false
preview_position = get_child_count()
if preview.visible:
@ -372,14 +374,9 @@ func update_preview(size : int = 0) -> void:
preview_timer.start(0.2)
func do_update_preview() -> void:
var result = generator.render(preview_index, preview_size, true)
while result is GDScriptFunctionState:
result = yield(result, "completed")
if preview.texture == null:
preview.texture = ImageTexture.new()
result.copy_to_texture(preview.texture)
result.release()
if !preview.visible:
add_child(preview)
move_child(preview, preview_position)
preview.visible = true
preview.set_generator(generator, preview_index)
preview.rect_min_size = Vector2(preview_size, preview_size)

View File

@ -8,3 +8,6 @@ margin_bottom = 29.0
title = "Generic"
show_close = true
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}

View File

@ -23,7 +23,14 @@ func set_generator(g : MMGenBase, output : int = 0) -> void:
g = null
for c in get_children():
c.setup_control(generator, [])
# Update shader
material.shader.code = MMGenBase.generate_preview_shader(source, source.type, shader)
# Get parameter values from the shader code
var regex = RegEx.new()
regex.compile("uniform\\s+(\\w+)\\s+([\\w_\\d]+)\\s*=\\s*([^;]+);")
for p in regex.search_all(material.shader.code):
material.set_shader_param(p.strings[2], float(p.strings[3]))
# Set texture params
if source.has("textures"):
for k in source.textures.keys():
material.set_shader_param(k, source.textures[k])

View File

@ -3,6 +3,7 @@
[ext_resource path="res://material_maker/preview/preview_2d.gd" type="Script" id=1]
[sub_resource type="Shader" id=1]
resource_local_to_scene = true
[sub_resource type="ShaderMaterial" id=2]
resource_local_to_scene = true