13
\$\begingroup\$

How to fill region with repeated texture? Now I'm using next method:

spriteBatch.begin();

final int tWidth = texture.getWidth();
final int tHeight = texture.getHeight();

for (int i = 0; i < regionWidth / tWidth; i++) {
    for (int k = 0; k < regionHeight / tHeight; k++) {
        spriteBatch.draw(texture, i*tWidth, k);
    }
}

spriteBatch.end();

It's very obvious. Maybe is there any built in method?

\$\endgroup\$

2 Answers 2

13
\$\begingroup\$

You want to set the texture's TextureWrap setting to Repeat. See the documentation for more information, and the texture method.

Specifically:

setWrap(Repeat, Repeat);
\$\endgroup\$
0
4
\$\begingroup\$

You can use "SetWrap" on your texture and create a TextureRegion based on that Texture. 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);

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

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();
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .