Libgdx 视差背景不完全平铺
我的视差背景图像有一些问题。我在下面嵌入了一个显示问题的GIF:(1)纹理边缘有一条黑色闪烁线,(2)移动不平滑(这不是因为GIF的帧速率太小)。我还为我的Libgdx 视差背景不完全平铺,libgdx,parallax,Libgdx,Parallax,我的视差背景图像有一些问题。我在下面嵌入了一个显示问题的GIF:(1)纹理边缘有一条黑色闪烁线,(2)移动不平滑(这不是因为GIF的帧速率太小)。我还为我的ScrollableImage类添加了短代码,这是我实现视差的核心。我怀疑这个问题可能是由于setScrolOffset方法中的模运算造成的,但事实并非如此,现在我已经没有主意了。我应该如何解决这个问题 public class ScrollableImage extends Widget { private TextureRegio
ScrollableImage
类添加了短代码,这是我实现视差的核心。我怀疑这个问题可能是由于setScrolOffset
方法中的模运算造成的,但事实并非如此,现在我已经没有主意了。我应该如何解决这个问题
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());
}
}
公共类ScrollableImage扩展小部件{
私有区域;
专用浮动滚动偏移=0.0f;
公共可滚动图像(纹理区域){
这个区域=区域;
}
@凌驾
公共作废提取(批处理、浮动parentAlpha){
超级绘图(批处理,父alpha);
float w=region.getRegionWidth();
float h=region.getRegionHeight();
浮动比例=getHeight()/h;
浮动比例宽度=w*比例;
浮动刻度高度=h*刻度;
浮动比例偏移=滚动偏移*比例;
Color=getColor();
batch.setColor(color.r、color.g、color.b、color.a*parentAlpha);
对于(float x=getX()-scaledOffset;x
我设法解决了这个问题。如果将来有人遇到类似的问题,下面是我所做的。使用之前给我带来问题的解决方案,我从软件中平铺背景(我有一个循环,循环多次绘制纹理);我现在做的是使用OpenGL的GL\u 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);
}
}
公共类视差小部件扩展小部件{
私有静态最终整数层计数=2;
私有纹理[]纹理=新纹理[层计数];
私有浮动[]滚动因子=新浮动[层计数];
私有浮动[]滚动金额=新浮动[层计数];
公共视差小部件(字符串路径0、浮点因子0、字符串路径1、浮点因子1){
滚动因子[0]=因子0;
滚动因子[1]=因子1;
滚动量[0]=0.0f;
滚动量[1]=0.0f;
纹理[0]=新纹理(Gdx.files.internal(path0));
纹理[1]=新纹理(Gdx.files.internal(path1));
纹理[0].setWrap(TextureWrap.Repeat,TextureWrap.ClampToEdge);
纹理[1].setWrap(TextureWrap.Repeat,TextureWrap.ClampToEdge);
}
@凌驾
公共作废提取(批处理、浮动parentAlpha){
超级绘图(批处理,父alpha);
Color=getColor();
batch.setColor(color.r、color.g、color.b、color.a*parentAlpha);
对于(int i=0;i