【发布时间】:2018-09-27 17:20:20
【问题描述】:
我正在尝试使用here 列出的着色器代码制作支持着色器的视差效果。但我得到的只是一个白屏。
另外论坛帖子here 说“代码不使用SpriteBatch,它直接使用着色器中设置的纹理参数渲染网格”。
我以前从未见过在不将其设置为批处理的情况下使用 ShaderProgram,所以我可能在这里遗漏了一些非常基本的东西。
public class Parallax {
private final ShaderProgram parallaxShader;
private final FullscreenQuad fullscreenQuad;
private final Array<Texture> backgrounds;
public Parallax(final String parallaxTexturesSubfolderName) {
parallaxShader = initShader();
fullscreenQuad = new FullscreenQuad();
backgrounds = populateBackgrounds(parallaxTexturesSubfolderName);
}
private Array<Texture> populateBackgrounds(String parallaxTexturesSubfolderName) {
final Array<Texture> textures = new Array<Texture>();
for (int i = 0; i < 4; i++) {
final Texture texture = new Texture(Gdx.files.internal("parallax/" + parallaxTexturesSubfolderName + "/" + "img" + i + ".png"));
texture.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);
textures.add(texture);
}
return textures;
}
private ShaderProgram initShader() {
ShaderProgram.pedantic = false;
final String vertexShader = Gdx.files.internal("shaders/parallax-vert.glsl").readString();
final String fragmentShader = Gdx.files.internal("shaders/parallax-frag.glsl").readString();
final ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
if (!shader.isCompiled()) {
Gdx.app.log("ShaderTest", shader.getLog());
Gdx.app.exit();
}
return shader;
}
public void render(final Camera camera, final SpriteBatch batch) {
// batch.setShader(parallaxShader); //tried with batch also - doesn't seems to work
// batch.begin();
for (int i = 0; i < backgrounds.size; i++) {
backgrounds.get(i).bind(i);
}
float travelDistance = camera.position.x / 800f / 15f;
parallaxShader.begin();
// parallaxShader.setUniformMatrix("u_projTrans", camera.combined);
parallaxShader.setUniformf("travelDistance", travelDistance);
parallaxShader.setUniformi("u_texture0", 0);
parallaxShader.setUniformi("u_texture1", 1);
parallaxShader.setUniformi("u_texture2", 2);
parallaxShader.setUniformi("u_texture3", 3);
fullscreenQuad.render(parallaxShader);
parallaxShader.end();
// batch.setShader(null);
// batch.end();
}
public void dispose() {
...
}
}
FullscreenQuad 已列出 here。谢谢!
【问题讨论】:
标签: libgdx glsl shader parallax