简体   繁体   中英

Can I get a repeating Texture with Libgdx when using TextureAtlas?

I'm using a TextureAtlas to organize all my images, and it works great when I need textures that scale to fit. However I want to create a background that is one region of the TextureAtlas repeated multiple times until a certain area is filled. I've tried setting the TextureWrap to Repeat like so:

public class Background extends Group {

SpriteActor bg;

public Background(int complexity) {
    bg = new SpriteActor("background");
    bg.getSprite().getTexture().setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
    bg.setSize(Match.TRACK_WIDTH,Match.TRACK_HEIGHT); //These constants equal 12000
    this.setSize(Match.TRACK_WIDTH,Match.TRACK_HEIGHT);
    this.addActor(bg);
}}

where SpriteActor is just an Actor with a Sprite field:

 public class SpriteActor extends Actor {

private Sprite sprite;

public SpriteActor(String assetName) {
    super();
    setSprite(new Sprite(Art.assets.findRegion(assetName)));
    setWidth(sprite.getWidth());
    setHeight(sprite.getHeight());
}
...

However the image is still stretched to fit the area (12000x12000 pixels), and not repeated. I'm guessing this is because the wrapping is set to the Texture, which basically contains ALL images in the TextureAtlas. What i'm looking for is a way to maybe set only the TextureRegion to repeat. Is there a way I can use both TextureAtlas AND have my image being repeated?

If the image is part of an atlas, you must draw multiple sprites to get the effect of it repeating. Texture wrapping won't have any effect, because only the edges of the entire Texture will wrap around when they are reached, and you have multiple images sharing the same Texture when you use a TextureAtlas.

So you may find it easier to separate tilable images into their own Textures for this purpose. In that case, you would need to set the texture wrap mode to repeat as you did, except calling it directly on the Texture instance.

Then there are multiple ways to draw this.

If you want to use a sprite, first create a TextureRegion that you construct from your Texture and give a very large width and height. I think in your case you would use:

TextureRegion backgroundTextureRegion = new TextureRegion(backgroundTexture);
backgroundTextureRegion.setRegion(0f, 0f,
    Match.TRACK_WIDTH/(float)(backgroundTextureRegion.regionWidth),
    Match.TRACK_HEIGHT/(float)(backgroundTextureRegion.regionHeight));

Then you could do

bg.getSprite().setTextureRegion(backgroundTextureRegion);

But if you prefer not to break out textures from your atlas, you could just set the draw method of your Actor subclass to loop to draw your texture region over and over. Something like this:

public void draw (Batch batch, float parentAlpha) {
    for (int i=0; i<sceneWidth/bgTR.getWidth(); i++){
        for (int j=0; j<sceneHeihgt/bgTR.getHeight(); j++){
            batch.draw(bgTR, i*bgTR.getWidth(), j*bgTR.getHeight());
        }
    }
}

Where bgTR is the texture region of the background from your atlas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM