Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LibGDX repeating TextureRegion

I am using LibGDX for a project, and I am trying to tile/repeat a texture across a specific amount of space. At the moment I am using a TextureRegion set to the proper size, and any extra space off of the texture is being filled as you would expect in OpenGL using "Clamp To Edge". I figured there would be an easy way to change this behavior to the "Repeat" mode, however I cannot seem to find a way to make that work, despite it seeming like something that should be fairly easy to change.

So, basically, is there a simple way to repeat an image in a set area using LibGDX, short of having to manually build it by repeating the texture myself in some form of loop?

like image 981
Shamrock Avatar asked Dec 01 '12 22:12

Shamrock


Video Answer


3 Answers

If you're using a TextureRegion, you can use a TiledDrawable like so:

TiledDrawable tile = new TiledDrawable(textureRegion);
tile.draw(spriteBatcher, x, y, width, height);
like image 72
Paul Burke Avatar answered Oct 21 '22 09:10

Paul Burke


You can use "SetWrap" on your texture and create a TextureRegion based on that Texture. Important: It took me a while to figure it out, but to be mirrored, your texture must be a power of two size. It was working on Android but not on iOS and you don't get a message - it was shown as black. So it must be 4x4 or 8x8, 16x16 .. 256x256 or 512x512 .. To Create a 3x3 mirrored image (or axb size)

Texture imgTexture = new Texture(Gdx.files.internal("badlogic.jpg"));
imgTexture.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);
TextureRegion imgTextureRegion = new TextureRegion(imgTexture);
imgTextureRegion.setRegion(0,0,imgTexture.getWidth()*3,imgTexture.getHeight()*3);

Will give you this: enter image description here

Below you can see the sample code that generated that picture using a Stage and Image Actor (Scene2D)

public class GameScreen implements Screen {

    MyGdxGame game;
    private Stage stage;

    public GameScreen(MyGdxGame aGame){
        stage = new Stage(new ScreenViewport());
        game = aGame;
        Texture imgTexture = new Texture(Gdx.files.internal("badlogic.jpg"));
        imgTexture.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);
        TextureRegion imgTextureRegion = new TextureRegion(imgTexture);
        imgTextureRegion.setRegion(0,0,imgTexture.getWidth()*3,imgTexture.getHeight()*3);

        TextureRegionDrawable imgTextureRegionDrawable = new TextureRegionDrawable(imgTextureRegion);
        Image img = new Image();
        img.setDrawable(imgTextureRegionDrawable);
        img.setSize(imgTexture.getWidth()*3,imgTexture.getHeight()*3);
        stage.addActor(img);
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, true);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();
    }
}
like image 33
Julien Avatar answered Oct 21 '22 11:10

Julien


I haven't tried this myself, but I found this post on google when trying to troubleshoot another issue. In the LIBGDX api I found this method, maybe it could help?

http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/graphics/Texture.html#setWrap(com.badlogic.gdx.graphics.Texture.TextureWrap, com.badlogic.gdx.graphics.Texture.TextureWrap)

This isn't for a texture region though, rather the texture itself...

like image 1
evermore Avatar answered Oct 21 '22 11:10

evermore