视差背景平铺不完美

时间:2014-07-05 15:06:41

标签: libgdx parallax

我的视差背景图像存在一些问题。我在下面嵌入了一个显示问题的GIF:(1)在纹理边缘有一条黑色闪烁线和(2)移动不平滑(这不是由于GIF的小帧速率)。我还为我的ScrollableImage类添加了短代码,这是我实现视差的核心。我怀疑这个问题可能是由setScrollOffset方法中的模运算引起的,但事实并非如此,现在我已经没有想法了。我应该尝试解决这个问题?

public class ScrollableImage extends Widget {

   private TextureRegion region;
   private float scrollOffset = 0.0f;

   public ScrollableImage(TextureRegion region) {
      this.region = region;
   }

   @Override
   public void draw(Batch batch, float parentAlpha) {
      super.draw(batch, parentAlpha);

      float w = region.getRegionWidth();
      float h = region.getRegionHeight();

      float scale = getHeight() / h;
      float scaledWidth = w * scale;
      float scaledHeight = h * scale;
      float scaledOffset = scrollOffset * scale;

      Color color = getColor();
      batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);

      for (float x = getX() - scaledOffset; x < getX() +  getWidth(); x += scaledWidth) {
         batch.draw(region, x, getY(), scaledWidth, scaledHeight);
      }
   }

   public float getScrollOffset() {
      return scrollOffset;
   }

   public void setScrollOffset(float value) {
      scrollOffset = Math.max(0, value % (float)region.getRegionWidth());
   }
}

1 个答案:

答案 0 :(得分:2)

我设法解决了这个问题。如果将来有人遇到类似的问题,这就是我的所作所为。有了之前给我带来问题的解决方案,我正在从软件中汲取背景(我有一个for循环,多次绘制纹理);我现在所做的和使用OpenGL&#39; s GL_REPEAT的作用很棒,它将使用硬件(GPU)来重复纹理。这是我的代码:

public class ParallaxWidget extends Widget implements Disposable {

   private static final int LAYER_COUNT = 2;
   private Texture[] textures = new Texture[LAYER_COUNT];   
   private float[] scrollFactors = new float[LAYER_COUNT];
   private float[] scrollAmounts = new float[LAYER_COUNT];


   public ParallaxWidget(String path0, float factor0, String path1, float factor1) {
      scrollFactors[0] = factor0;
      scrollFactors[1] = factor1;

      scrollAmounts[0] = 0.0f;
      scrollAmounts[1] = 0.0f;

      textures[0] = new Texture(Gdx.files.internal(path0));
      textures[1] = new Texture(Gdx.files.internal(path1));

      textures[0].setWrap(TextureWrap.Repeat, TextureWrap.ClampToEdge);
      textures[1].setWrap(TextureWrap.Repeat, TextureWrap.ClampToEdge);
   }

   @Override
   public void draw(Batch batch, float parentAlpha) {
      super.draw(batch, parentAlpha);

      Color color = getColor();
      batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);

      for (int i = 0; i < LAYER_COUNT; ++i) {
         drawLayer(batch, i);
      }
   }

   @Override
   public void dispose() {
       for (Texture texture : textures) {
           texture.dispose();
       }
   } 

   public void updateScroll(float value) {
      for (int i = 0; i < LAYER_COUNT; ++i) {
         scrollAmounts[i] = value * scrollFactors[i];
      }
   }

   private void drawLayer(Batch batch, int index) {
      float x = getX();
      float y = getY();

      float w = getWidth();
      float h = getHeight();

      float th = textures[index].getHeight();
      float tw = textures[index].getWidth() * h / th;

      float u = scrollAmounts[index] / tw;
      float v = 1.0f;

      float u2 = u + (w / tw);
      float v2 = 0.0f;

      batch.draw(textures[index], x, y, w, h, u, v, u2, v2);
   }
}