java白色_java-将精灵颜色更改为白色
如果要將精靈中所有形狀的顏色更改為白色,則唯一的方法是使用像素著色器并將所有非黑色的片段設(shè)置為黑色(我假設(shè)黑色在您的游戲中呈現(xiàn)為透明) )到白色.像這樣:
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
void main() {
vec4 color=v_color * texture2D(u_texture, v_texCoords);
if(color.r!=0 && color.g!=0 && color.b!=0){
gl_FragColor=vec4(1,1,1,1);
}
else{
gl_FragColor=color;
}
}
如果您很不幸并且使用的是opengl 1.0(固定管道),我建議您現(xiàn)在就開始使用gles 2.0.固定管道是從90’開始的,我們現(xiàn)在是2013年!
編碼:
初始化:
ShaderProgram.pedantic = false;
ShaderProgram defaultShader=SpriteBatch.createDefaultShader();
ShaderProgram shaderWhiteTexture=new ShaderProgram(Gdx.files.internal("vertexShader.vs").readString(),Gdx.files.internal("fragShader.fs").readString());
渲染:
//Render the textures with the normal colors
spriteBatch.begin();
spriteBatch.draw(sprite1,sprite2,sprite3...);//or whatever code u use to render them
spriteBatch.end();
//Render the textures with the shader
spriteBatch.setShader(shaderWhiteTexture);
spriteBatch.begin();
spriteBatch.draw(sprite4,sprite5,sprite6...);//or whatever code u use to render them
spriteBatch.end();
spriteBatch.setShader(defaultShader);
著色器:
//vertexShader.vs:
attribute highp vec4 a_position;
attribute highp vec4 a_color;
attribute highp vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying highp vec4 v_color;
varying highp vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position ;
}
//fragShader.fs:
varying highp vec4 v_color;
varying highp vec2 v_texCoords;
uniform sampler2D u_texture;
void main() {
gl_FragColor = vec4(0.0);
highp vec4 color = texture2D(u_texture, v_texCoords);
if(color.a > 0.0) {
gl_FragColor = vec4(1.0,0,0,1.0);
}
}
由問題所有者編輯:現(xiàn)在可以使用透明紋理
添加了什么? :
1 . the highp precision to variables
2 . the fragmentShader file Main() fonction edited
總結(jié)
以上是生活随笔為你收集整理的java白色_java-将精灵颜色更改为白色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 矩阵分析理论在实际工程中的应用_【顶管技
- 下一篇: java se 9.0.4_jre 9下