Skip to content

Commit

Permalink
fix slowdown due to repeated sprite rect calc
Browse files Browse the repository at this point in the history
  • Loading branch information
tom95 committed Oct 24, 2023
1 parent ec671b5 commit 5effe55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
28 changes: 20 additions & 8 deletions addons/pronto/behaviors/PlaceholderBehavior.gd
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func _update_shape():
_parent.shape_owner_get_shape(_owner_id, 0).height = capsule_height
else:
# no shape has been set, should only exist for legacy scenes
print(undefined_shape_string)
print(undefined_shape_string)
_parent.shape_owner_get_shape(_owner_id, 0).size = placeholder_size

var _owner_id: int = 0
Expand Down Expand Up @@ -374,26 +374,38 @@ func _draw():
debug_color.a = 1
draw_rect(r, debug_color, false)


func find_non_transparent_rect():
if not sprite_texture: return null
var w = sprite_texture.get_width()
var h = sprite_texture.get_height()
var scale = placeholder_size / 16
var r = _opaque_rect(sprite_texture)
return Rect2(r.position * scale, r.size * scale)

static var _opaque_rect_cache = {}

func _opaque_rect(texture):
if _opaque_rect_cache.has(texture.resource_path):
return _opaque_rect_cache[texture.resource_path]
var w = texture.get_width()
var h = texture.get_height()
var minX = w
var minY = h
var maxX = 0
var maxY = 0
for y in range(h):
for x in range(w):
var pixel = sprite_texture.get_image().get_pixel(x,y)
var pixel = texture.get_image().get_pixel(x,y)
if pixel.a > 0:
minX = min(minX, x)
minY = min(minY, y)
maxX = max(maxX, x)
maxY = max(maxY, y)

var scale = placeholder_size / 16
var size = Vector2(maxX - minX + 1, maxY - minY + 1) * scale
return Rect2(-Vector2(w/2-minX,h/2-minY) * scale, size)

_opaque_rect_cache[texture.resource_path] = Rect2(
-Vector2(w/2-minX,h/2-minY),
Vector2(maxX - minX + 1, maxY - minY + 1)
)
return _opaque_rect_cache[texture.resource_path]

func _re_add_shape():
_parent = get_parent() as CollisionObject2D
Expand Down
2 changes: 1 addition & 1 deletion addons/pronto/helpers/SpriteInspector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SpriteProperty extends EditorProperty:
icon_window.texture_selected.connect(_on_change)

var tile_map = icon_window.get_node("TileMap")
var tile_size = tile_map.cell_quadrant_size
var tile_size = tile_map.rendering_quadrant_size
var tile_source = tile_map.tile_set.get_source(0)
var tile_texture = tile_source.texture
var img = tile_source.texture.get_image()
Expand Down

0 comments on commit 5effe55

Please sign in to comment.