【问题标题】:Can I get a repeating Texture with Libgdx when using TextureAtlas?使用 TextureAtlas 时,我可以使用 Libgdx 获得重复的纹理吗?
【发布时间】:2014-09-24 22:14:17
【问题描述】:

我正在使用 TextureAtlas 来组织我的所有图像,当我需要可以缩放以适应的纹理时,它非常有用。但是我想创建一个背景,它是 TextureAtlas 的一个区域,重复多次,直到某个区域被填充。我尝试将 TextureWrap 设置为这样重复:

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

其中 SpriteActor 只是一个带有 Sprite 字段的 Actor:

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

但是,图像仍会被拉伸以适应该区域(12000x12000 像素),并且不会重复。我猜这是因为包装设置为 Texture,它基本上包含 TextureAtlas 中的所有图像。我正在寻找的是一种可能只设置 TextureRegion 重复的方法。有没有一种方法可以同时使用 TextureAtlas 并重复我的图像?

【问题讨论】:

    标签: java libgdx textures texture-mapping


    【解决方案1】:

    如果图像是图集的一部分,则必须绘制多个精灵才能获得重复的效果。纹理环绕不会产生任何效果,因为只有整个纹理的边缘在到达时才会环绕,而且当您使用 TextureAtlas 时,会有多个图像共享同一个纹理。

    因此,您可能会发现为此目的将可平铺图像分离成它们自己的纹理更容易。在这种情况下,您需要像您一样将纹理环绕模式设置为重复,除非直接在 Texture 实例上调用它。

    然后有多种方法可以绘制它。

    如果你想使用精灵,首先创建一个你从纹理构造的 TextureRegion,并给出一个非常大的宽度和高度。我认为在你的情况下你会使用:

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

    那你就可以了

    bg.getSprite().setTextureRegion(backgroundTextureRegion);
    

    但是,如果您不想从图集中分离纹理,则可以将 Actor 子类的 draw 方法设置为循环以一遍又一遍地绘制纹理区域。像这样的:

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

    bgTR 是图集中背景的纹理区域。

    【讨论】:

      猜你喜欢
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      相关资源
      最近更新 更多