Skip to content

Commit

Permalink
Fixes and updates in painting tool
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Feb 3, 2024
1 parent c809baf commit 53144bd
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
18 changes: 14 additions & 4 deletions addons/material_maker/engine/pipeline/pipeline.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Parameter:
size = 4
"vec2":
size = 8
"vec3":
size = 16
"vec4":
size = 16
"mat4x4":
Expand Down Expand Up @@ -122,8 +124,10 @@ func add_parameter_or_texture(n : String, t : String, v, parameter_as_constant :
if t == "sampler2D":
if input_texture_indexes.has(n):
print("ERROR: Redefining texture "+n)
input_texture_indexes[n] = input_textures.size()
input_textures.append(InputTexture.new(n, v))
input_textures[input_texture_indexes[n]] = InputTexture.new(n, v)
else:
input_texture_indexes[n] = input_textures.size()
input_textures.append(InputTexture.new(n, v))
elif parameter_as_constant:
if constants.has(n):
print("ERROR: Redefining constant "+n)
Expand Down Expand Up @@ -154,9 +158,15 @@ func set_parameter(name : String, value, silent : bool = false) -> void:
parameter_values.encode_float(p.offset, value)
return
"vec2":
if value is Vector2:
if value is Vector2 or value is Vector2i:
parameter_values.encode_float(p.offset, value.x)
parameter_values.encode_float(p.offset+4, value.y)
return
"vec3":
if value is Vector3:
parameter_values.encode_float(p.offset, value.x)
parameter_values.encode_float(p.offset+4, value.y)
parameter_values.encode_float(p.offset+8, value.z)
return
"vec4":
if value is Color:
Expand Down Expand Up @@ -191,7 +201,7 @@ func constant_as_string(value, type : String) -> String:
func get_uniform_declarations() -> String:
var uniform_declarations : String = ""
var size : int = 0
for type in [ "mat4x4", "vec4", "vec2", "float", "int", "bool" ]:
for type in [ "mat4x4", "vec4", "vec3", "vec2", "float", "int", "bool" ]:
for p in parameters.keys():
var parameter : Parameter = parameters[p]
if parameter.type != type:
Expand Down
38 changes: 20 additions & 18 deletions addons/material_maker/engine/pipeline/texture.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func _notification(what : int) -> void:
func get_texture_rid(target_rd : RenderingDevice) -> RID:
if ! rid.is_valid():
var image : Image = texture.get_image()
if image == null:
if image == null or image.get_width() == 0 or image.get_height() == 0:
print("No image for texture %s" % str(texture))
image = Image.create(1, 1, false, Image.FORMAT_RH)
texture_format = RenderingDevice.DATA_FORMAT_R16_SFLOAT
Expand Down Expand Up @@ -57,7 +57,7 @@ func get_texture() -> Texture2D:
# Use Texture2DRD
texture = Texture2DRD.new()
texture.texture_rd_rid = rid
else:
elif rd and rid.is_valid():
var byte_data : PackedByteArray = rd.texture_get_data(rid, 0)
var image_format : Image.Format
match texture_format:
Expand All @@ -80,22 +80,24 @@ func set_texture(new_texture : ImageTexture) -> void:
texture = new_texture
texture_size = texture.get_size()
var image : Image = texture.get_image()
match image.get_format():
Image.FORMAT_RF:
texture_format = RenderingDevice.DATA_FORMAT_R32_SFLOAT
Image.FORMAT_RGBAF:
texture_format = RenderingDevice.DATA_FORMAT_R32G32B32A32_SFLOAT
Image.FORMAT_RH:
texture_format = RenderingDevice.DATA_FORMAT_R16_SFLOAT
Image.FORMAT_RGBAH:
texture_format = RenderingDevice.DATA_FORMAT_R16G16B16A16_SFLOAT
Image.FORMAT_RGBA8:
texture_format = RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM
_:
print("Unsupported texture format "+str(image.get_format()))
image.convert(Image.FORMAT_RGBAH)
texture.set_image(image)
texture_format = RenderingDevice.DATA_FORMAT_R16G16B16A16_SFLOAT
if image:
match image.get_format():
Image.FORMAT_RF:
texture_format = RenderingDevice.DATA_FORMAT_R32_SFLOAT
Image.FORMAT_RGBAF:
texture_format = RenderingDevice.DATA_FORMAT_R32G32B32A32_SFLOAT
Image.FORMAT_RH:
texture_format = RenderingDevice.DATA_FORMAT_R16_SFLOAT
Image.FORMAT_RGBAH:
texture_format = RenderingDevice.DATA_FORMAT_R16G16B16A16_SFLOAT
#Image.FORMAT_RGBA8:
# texture_format = RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM
_:
print("Unsupported texture format "+str(image.get_format()))
image.convert(Image.FORMAT_RGBAH)
texture.set_image(image)
texture_format = RenderingDevice.DATA_FORMAT_R16G16B16A16_SFLOAT
texture_size = image.get_size()
if rid.is_valid():
rd.free_rid(rid)
rid = RID()
Expand Down
9 changes: 6 additions & 3 deletions material_maker/panels/paint/paint.gd
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func set_object(o):
# Center camera on mesh
var aabb : AABB = painted_mesh.get_aabb()
camera_position.transform.origin = aabb.position+0.5*aabb.size
update_camera()
# Set the painter target mesh
painter.set_mesh(o.mesh)
update_view()
Expand Down Expand Up @@ -686,14 +687,16 @@ func do_paint(pos : Vector2, pressure : float = 1.0, tilt : Vector2 = Vector2(0,
if end_of_stroke:
stroke_seed = randf()

func update_view():
var mesh_instance = painted_mesh
var mesh_aabb = mesh_instance.get_aabb()
func update_camera():
var mesh_aabb = painted_mesh.get_aabb()
var mesh_center = mesh_aabb.position+0.5*mesh_aabb.size
var mesh_size = 0.5*mesh_aabb.size.length()
var cam_to_center = (camera.global_transform.origin-mesh_center).length()
camera.near = max(0.01, 0.99*(cam_to_center-mesh_size))
camera.far = 1.01*(cam_to_center+mesh_size)

func update_view():
update_camera()
var transform = camera.global_transform.affine_inverse()*painted_mesh.global_transform
if painter != null:
painter.update_view(camera, transform, main_view.size)
Expand Down
4 changes: 2 additions & 2 deletions material_maker/tools/painter/painter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ func update_brush(update_shaders : bool = false):

paint_shader.add_parameter_or_texture("fill", "bool", false)
paint_shader.add_parameter_or_texture("erase", "bool", false)
paint_shader.add_parameter_or_texture("reset", "bool", false)

paint_shader.add_parameter_or_texture("brush_position", "vec2", Vector2(0, 0))
paint_shader.add_parameter_or_texture("brush_previous_position", "vec2", Vector2(0, 0))
Expand Down Expand Up @@ -434,7 +435,7 @@ func update_brush(update_shaders : bool = false):
pattern_code += pattern_shader_code.code
pattern_code += "vec4 %s_value = %s;\n" % [ c.name, pattern_shader_code.output_values.rgba ]
pattern_code += "vec4 old_%s_stroke_value = imageLoad(%s_stroke, pixel);\n" % [ c.name, c.name ]
pattern_code += "vec4 old_%s_layer_value = imageLoad(%s_layer, pixel);\n" % [ c.name, c.name ]
pattern_code += "vec4 old_%s_layer_value = reset ? vec4(0.0) : imageLoad(%s_layer, pixel);\n" % [ c.name, c.name ]
pattern_code += "vec4 new_%s_stroke_value;\n" % [ c.name ]
pattern_code += "vec4 new_%s_layer_value;\n" % [ c.name ]
pattern_code += "do_paint_%s(%s_value, brush_value, old_%s_stroke_value, old_%s_layer_value, new_%s_stroke_value, new_%s_layer_value);\n" % [ c.type, c.name, c.name, c.name, c.name, c.name ]
Expand Down Expand Up @@ -560,7 +561,6 @@ func paint(shader_params : Dictionary, end_of_stroke : bool = false, emit_end_of
init_shader.set_parameter("use_input_image", false)
await init_shader.render_ext([ paint_textures[i*3+1] ], Vector2i(texture_size, texture_size))
paint_textures[i*3+1].get_texture()

var stroke_state = {}
for v in active_viewports:
pass
Expand Down
16 changes: 12 additions & 4 deletions material_maker/tools/painter/shaders/paint_shader_template.tres
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,12 @@ void do_paint_rgba(vec4 paint_value, float brush_value, vec4 old_stroke_value, v
float new_alpha = min(1.0, max(stroke_alpha, old_stroke_value.a));
new_stroke_value = vec4(mix(paint_value.rgb, old_stroke_value.rgb, new_alpha-stroke_alpha), new_alpha);
float layer_apply_alpha = new_alpha*brush_opacity;
float layer_alpha_sum = min(1.0, layer_apply_alpha + old_layer_value.a);
new_layer_value = vec4(mix(new_stroke_value.rgb, old_layer_value.rgb, layer_alpha_sum-layer_apply_alpha), layer_alpha_sum);
if (erase) {
new_layer_value = vec4(old_layer_value.rgb, max(0.0, old_layer_value.a-layer_apply_alpha));
} else {
float layer_alpha_sum = min(1.0, layer_apply_alpha + old_layer_value.a);
new_layer_value = vec4(mix(new_stroke_value.rgb, old_layer_value.rgb, layer_alpha_sum-layer_apply_alpha), layer_alpha_sum);
}
}
void do_paint_ggaa(vec4 paint_value, float brush_value, vec4 old_stroke_value, vec4 old_layer_value, out vec4 new_stroke_value, out vec4 new_layer_value) {
Expand All @@ -331,8 +335,12 @@ void do_paint_ggaa(vec4 paint_value, float brush_value, vec4 old_stroke_value, v
vec2 new_alpha = min(vec2(1.0), stroke_alpha + old_stroke_value.ba);
new_stroke_value = vec4(mix(paint_value.rg, old_stroke_value.rg, new_alpha-stroke_alpha), new_alpha);
vec2 layer_apply_alpha = new_alpha*brush_opacity;
vec2 layer_alpha_sum = min(vec2(1.0), layer_apply_alpha + old_layer_value.ba);
new_layer_value = vec4(mix(new_stroke_value.rg, old_layer_value.rg, layer_alpha_sum-layer_apply_alpha), layer_alpha_sum);
if (erase) {
new_layer_value = vec4(old_layer_value.rg, max(vec2(0.0), old_layer_value.ba-layer_apply_alpha));
} else {
vec2 layer_alpha_sum = min(vec2(1.0), layer_apply_alpha + old_layer_value.ba);
new_layer_value = vec4(mix(new_stroke_value.rg, old_layer_value.rg, layer_alpha_sum-layer_apply_alpha), layer_alpha_sum);
}
}
void main() {
Expand Down

0 comments on commit 53144bd

Please sign in to comment.