我正在尝试向我的着色器添加一个统一的变量,这是我的着色器:
String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
+ "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
+ "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
+ "uniform mat4 u_projTrans;\n" //
+ "varying vec4 v_color;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "\n" //
+ "void main()\n" //
+ "{\n" //
+ " v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
+ " gl_Position = u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
+ "}\n";
String fragmentShader = "#ifdef GL_ES\n" //
+ "#define LOWP lowp\n" //
+ "precision mediump float;\n" //
+ "#else\n" //
+ "#define LOWP \n" //
+ "#endif\n" //
+ "varying vec2 v_texCoords;\n" //
+ "uniform sampler2D u_texture;\n" //
+ "uniform sampler2D u_texturePalette;\n" //
+ "void main()\n"//
+ "{\n" //
+ " vec4 textureColor = texture2D(u_texture, v_texCoords).rgba;\n"
+ " vec4 paletteColor = texture2D(u_texturePalette, vec2(textureColor.b,0)).rgba;\n"
+ " gl_FragColor = paletteColor;\n" //
+ "}";
问题随之而来
统一的 sampler2D u_texturePalette;
ShaderProgram 无法识别着色器中的新制服。我也这样做了:
shader = new ShaderProgram(vertexShader, fragmentShader);
shader.setUniformi("u_texturePalette", 0);
Texture text = new Texture(Gdx.files.internal("./data/palette.png"));
text.bind(shader.getUniformLocation("u_texturePalette"));
batch = new SpriteBatch();
batch.setShader(shader);
当我调用函数 shader.hasUniform("u_texturePalette")
时,它解析为true,但是当我去看它时,shader.getUniformLocation("u_texturePalette")
它解析为-1
这是一个错误吗?. 知道我在做什么坏事吗?