3

I am trying to get the Texture from Sprite or Atlas Region created by Texture Atlas.

    atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
    region=atlas.findRegion("green");

or

 Sprite s = atlas.createSprite("red");
 texture = s.getTexture();      

I have given the name of image in findRegion and createSprite method, it always picks the other image object. I want to get the texture of the image so that part of that texture can be rendered.

Here is the Packer image: http://i61.tinypic.com/neupo2.png

Even if i try to get green region or red, it always returns blue. Any help would be appreciated.

Jatin
  • 341
  • 3
  • 8
  • I tried with every possible way. Only way left is to code by myself using Sprite Sheet and reading stored coordinates of images. – Jatin Jun 02 '14 at 06:05

4 Answers4

4

When you do "textureAtlas.findRegion("objectTexture").getTexture();" (from James Skemp's answer) you're getting the whole texture from the atlas, not just from the region "objectTexture".

Don't bother creating Texture to create the Sprite, you can use the TextureRegion to create the Sprite. Something like this:

atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
regionGreen=atlas.findRegion("green");
Sprite s = new Sprite(regionGreen);
1

My use case is slightly different, but I was running into the same issue.

I have an object, which extends Actor, that I want to draw using a region in an atlas.

public class MyObject extends Actor {
    private Texture texture;

    public MyObject() {
        TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
        texture = textureAtlas.findRegion("objectTexture").getTexture();
        Gdx.app.log(TAG, "Texture width: " + texture.getWidth());
        Gdx.app.log(TAG, "Texture height: " + texture.getHeight());

        setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
        // ...
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(texture,
                worldPosition.x * Constants.STANDARD_TILE_WIDTH, worldPosition.y * Constants.STANDARD_TILE_WIDTH,
                texture.getWidth(), texture.getHeight());
    }
}

Instead of just the region I was expecting I instead received the entire atlas, so my logged texture width and height were 1024 x 128.

Unfortunate, and still not sure why getTexture() returns too much, but switching over to batchDraw(TextureRegion, ...) at least got me in a better place.

public class MyObject extends Actor {
    private TextureRegion texture;

    public MyObject() {
        TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
        texture = textureAtlas.findRegion("objectTexture");

        setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
        // ...
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(texture, getX(), getY());
    }
}

Based upon what I saw with my sprites, the questioner was seeing a blue square for the same reason I was; the entire image is getting loaded by getTexture() and since it starts at the bottom left, you're always seeing a blue square.

Using the Sprite(TextureRegion) constructor may have also resolved their issue.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
0

Seems to me your problem is the Test.pack file, not the texture.

It works with this one I packed with GDX Texture Packer:

Test.png format: RGBA8888 filter: Nearest,Nearest repeat: none blue rotate: false xy: 1, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 green rotate: false xy: 54, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 red rotate: false xy: 107, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 yellow rotate: false xy: 160, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1

NoWone
  • 65
  • 2
  • 10
-1

Use index also to get it.Get the index from pack file or .atlas file.

atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
    region=atlas.findRegion("green",index);

or

 Sprite s = atlas.createSprite("red",index);
 texture = s.getTexture();   
arv
  • 250
  • 3
  • 15
  • index in pack file(.txt) or .pack file is -1 for all the images. Do i need to change the index manually in the file and then access it? Let me try with that method as well. – Jatin Jun 02 '14 at 13:33
  • I tried but it did not work out. Any other solution? – Jatin Jun 03 '14 at 03:29
  • @Jatin You just use index mention in .pack file.(whatever it is -1 or any other number) – arv Jun 03 '14 at 04:53
  • @Jatin No need to change the index number in .pack file. – arv Jun 03 '14 at 06:31
  • @Jatin if solution work then please accept it as right answer. – arv Jun 04 '14 at 09:31
  • it did not work. I have coded now with the image file from pack. – Jatin Jun 04 '14 at 09:38
  • @Jatin it works... i think your pack file is not correct.Download the texture packer software from libgdx site and then go for pack file. – arv Jun 04 '14 at 17:25
  • @Jatin get texture packer from ... https://code.google.com/p/libgdx-texturepacker-gui/ – arv Jun 04 '14 at 17:48
  • @Mickey Tin ... before conclusion,plz do some R & D work. Follow the above mention link.It will work if u go in right direction. – arv Apr 06 '15 at 06:34