在libgdx中重复纹理


12

如何用重复纹理填充区域?现在,我正在使用下一个方法:

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();

很明显 也许有任何内置方法?

Answers:



3

您可以在纹理上使用“ SetWrap”,然后基于该纹理创建一个TextureRegion。创建3x3镜像图像(或axb大小)

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);

重要:我花了一段时间才弄清楚,但要进行镜像,您的纹理必须是两倍大小的幂。它可以在Android上运行,但不能在iOS上运行,并且您不会收到消息-它显示为黑色。因此它必须是4x4或8x8、16x16 .. 256x256或512x512 ..

会给你这个: 在此处输入图片说明

在下面,您可以看到使用舞台和图像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();
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.