Skip to content

Instantly share code, notes, and snippets.

@boruok
Created April 6, 2022 00:06
Show Gist options
  • Save boruok/fe18ee0090556cc380332b3807fa4f75 to your computer and use it in GitHub Desktop.
Save boruok/fe18ee0090556cc380332b3807fa4f75 to your computer and use it in GitHub Desktop.
Script for animated atlas texture (custom)
# based on https://gist.github.com/Aldlevine/93b8958a39fe7ce7ad8c2b253ccec1c5
class_name AnimatedAtlasTexture extends AtlasTexture
export(int, 1, 100) var h_frames := 1
export(int, 1, 100) var v_frames := 1
export var fps := 10.0
var frame_position : Vector2
var frame_size : Vector2
var frame_last := 0
var frame := 0
func init() -> void:
frame_size = region.size / Vector2(h_frames, v_frames)
frame_position = region.position
VisualServer.connect("frame_pre_draw", self, "_on_frame_pre_draw")
func _on_frame_pre_draw() -> void:
if !atlas: return
frame = int(OS.get_ticks_msec() / (1000.0 / fps)) % (h_frames * v_frames)
region = Rect2(
frame_position + (frame_size * Vector2(frame % h_frames, frame / h_frames)),
frame_size)
if frame_last != frame:
frame_last = frame
emit_changed()
# based on https://gist.github.com/Aldlevine/93b8958a39fe7ce7ad8c2b253ccec1c5
extends TileMap
func _ready() -> void:
for tile in tile_set.get_tiles_ids():
var texture := tile_set.tile_get_texture(tile)
if texture is AnimatedAtlasTexture:
texture.init()
texture.connect("changed", self, "_on_AnimatedAtlasTexture_changed")
func _on_AnimatedAtlasTexture_changed() -> void:
tile_set.emit_changed()
@boruok
Copy link
Author

boruok commented Nov 17, 2022

noticed framedrop up to 10% of total perf. every time when tileset updated. It's better to use AnimatedTexture in cost of drawcalls instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment