【发布时间】:2021-11-22 12:26:44
【问题描述】:
我正在制作一个带有背景纹理的游戏,该纹理存储为精灵,因为我需要调整它的大小。但是,当我调整它的大小时,它会改变图像的纵横比而不是重复它。我为创建 Sprite 而传入的纹理将包裹设置为 Texture.TextureWrap.Repeat。
我现在的班级:
package com.lance.seajam;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
public class Water extends ApplicationAdapter {
private final Texture waterTexture;
private final Texture noiseTexture;
private SpriteBatch batch;
private Sprite sprite;
private ShaderProgram shaderProgram;
private String vertexShaderString = Gdx.files.internal("shaders/water/mainvs.glsl").readString();
private String fragmentShaderString = Gdx.files.internal("shaders/water/mainfs.glsl").readString();
private float[] floatArrOf(float... A) {
return A;
}
private void compileShader() {
shaderProgram = new ShaderProgram(vertexShaderString, fragmentShaderString);
if (!shaderProgram.isCompiled()) {
System.out.println(shaderProgram.getLog());
}
}
public Water(SpriteBatch batch, String imgDir) {
this.batch = batch;
waterTexture = new Texture(Gdx.files.internal(imgDir + "/water.png"));
noiseTexture = new Texture(Gdx.files.internal(imgDir + "/noise.png"));
noiseTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat); // make the texture repeat
waterTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat); // make the texture repeat
waterTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
noiseTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
sprite = new Sprite(waterTexture);
sprite.setSize((float) Gdx.graphics.getWidth(), (float) Gdx.graphics.getHeight()); // setting sprite size to graphics size
compileShader();
}
float time = 0f;
public void Draw() {
shaderProgram.setUniformf("u_noise_scale", 0.1f);
shaderProgram.setUniform2fv("u_noise_scroll_velocity", floatArrOf(0.004f, 0.003f), 0, 2);
shaderProgram.setUniformf("u_distortion", 0.04f);
shaderProgram.setUniformf("u_time", time);
noiseTexture.bind();
batch.begin();
time += Gdx.graphics.getDeltaTime(); // Gets how much seconds has passed
Gdx.gl.glEnable(GL20.GL_BLEND);
batch.setShader(shaderProgram);
batch.draw(sprite, sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
Gdx.gl.glDisable(GL20.GL_BLEND);
batch.end();
}
}
有什么方法可以在调整大小时将其更改为重复?如果有,怎么做?
【问题讨论】: