LibGDX repeating TextureRegion
Asked Answered
H

3

7

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?

Hartmunn answered 1/12, 2012 at 22:15 Comment(0)
S
8

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);
Stafford answered 4/3, 2014 at 13:42 Comment(2)
Works perfect, plus it can be used with Texture Atlas & Sprites.Relent
Only works in pixels and not world coordinates. The API would be better if the width and height were in the world coordinates the batch is in via SetProjectionMatrixSorrells
N
4

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();
    }
}
Nemhauser answered 4/10, 2015 at 13:52 Comment(0)
C
1

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...

Charcuterie answered 3/12, 2012 at 1:11 Comment(2)
I found that prior to asking, but I was having trouble getting things working with "Texture" as apposed to "TextureRegion". For now I have gotten around this problem by modifying the classes of LibGDX itself slightly, which certainly isn't ideal, but it does the trick.Hartmunn
@Hartmunn can you post here how you got it done? what did you change?Methaemoglobin

© 2022 - 2024 — McMap. All rights reserved.