godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.48k stars 21.08k forks source link

Cannot get region of a spritesheet for a frame when using SpriteFrames #57658

Open chucklepie opened 2 years ago

chucklepie commented 2 years ago

Godot version

3.4.2

System information

PopOS

Issue description

  1. You create a large sprite sheet
  2. You create an AnimatedSprite then create the SpriteFrame and 'add frames from a region'
  3. You select your sheet and create your animation

In code you want to grab a texture of the current frame, or even just the first frame, e.g.

var tex=$AnimatedSprite.frames.get_frame("normal",0)
var size=tex.get_size()

size is correct and holds the size, e.g. 64x64

However, unless I'm missing something, tex contains the entire sprite sheet and there is no data holding the rect/region where the sprite is.

var img_tex = ImageTexture.new();
img_tex.create_from_image(tex.get_data().get_rect(?????????),0);

The only place I can find it is inside the scene file, e.g.

[ext_resource path="res://assets/graphics/sheet_sprites.sprites/turrets_original.tres" type="Texture" id=5]
[sub_resource type="AtlasTexture" id=11]
atlas = ExtResource( 5 )
region = Rect2( 256, 1092, 64, 64 )

But this is not referenced in the animated sprite or the sprite frame.

Note, I am using .tres files to represent individual regions within the sprite sheet so my 'add frame from region' points to my .tres file not the whole sprite sheet, but texture still returns the whole sprite sheet, so the same problem occurs.

Steps to reproduce

As description

Minimal reproduction project

No response

timothyqiu commented 2 years ago

I am not able to reproduce this. tex.get_data() returns the cropped image for me. A minimal reproduction project would help debugging.

And FYI, since AtlasTexture is returned for these kind of frames, you can use its region property to get the rect.

chucklepie commented 2 years ago

@timothyqiu I was originally trying to use get_data() but it returns an Image not a texture and on converting to an ImageTexture the output was blank, so presumed this was not the way and switched to my above method trying to get the texture in the way you normally would by grabbing the texture but AnimatedSprites made this difficult.

i.e. this was what I originally had

        var itex = ImageTexture.new()
        itex.create_from_image(image) #the grabbed image using get_data()
        shard_texture=itex

But trying to debug I had to give up because for whatever reason the Godot IDE cannot show ImageTextures so you have no idea what the output actually is because passing in a Texture shows up just fine and displays image not ID, so dunno if this is a 'feature' or bug :(

image

So this might just be a miscalculation on my behalf.

timothyqiu commented 2 years ago

You can try using a TextureRect to display that texture.

Also in case you're using AtlasTexture (i.e. what you get from that sprite frame) in something related to Ploygon2D / Particles2D, there are some long existing issues like #25049 and #13923.