将精灵颜色变为白色

时间:2013-12-12 20:33:37

标签: java colors libgdx sprite

我正在尝试用白色改变精灵(红色和灰色)的颜色。

    sprite.setColor(1, 1, 1, 1);

但没有发生任何事情。

如何更改白色的所有精灵颜色?拜托,谢谢你

4 个答案:

答案 0 :(得分:4)

如果你想将精灵中所有形状的颜色更改为白色,那么你唯一可以做的就是使用像素着色器并设置它们不是黑色的所有碎片(我假设黑色渲染为透明)在你的游戏中)到白色。这样的事情:

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(固定管道),我建议您现在切换到gles 2.0,因为您是初学者。固定管道来自90',我们在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);
}

}

由问题所有者编辑:现在使用透明纹理

添加了哪些内容?

1 . the highp precision to variables
2 . the fragmentShader file Main() fonction edited

答案 1 :(得分:1)

那不行。颜色WHITE不会影响较暗的颜色(读取:每隔一种颜色)。

实际上是sprite的默认值,因此它的渲染方式与您创建的图像完全相同(这就是您没有看到更改的原因)。如果您要使用Sprite Color来影响精灵在运行时的渲染方式,请考虑将其变为白色,然后将其更改为任何其他颜色。

答案 2 :(得分:1)

使用模板缓冲区,您可以在没有任何OpenGL2函数或着色器或精灵的预先复制版本或任何其他疯狂的东西的情况下实现此目的。

首先将精灵绘制到模板缓冲区,然后在模板上绘制一个适当颜色的填充矩形,它只会绘制模板所在的位置,为您留下彩色轮廓。

这是它的工作原理:

//Turn off drawing to the color buffers
Gdx.gl.glColorMask(false, false, false, false);

//Enable drawing to the stencil buffer
Gdx.gl.glEnable(GL10.GL_STENCIL_TEST);

//Set the stencil mask you want to use, mask 1 is as good as any other
Gdx.gl.glStencilMask(1);

//When drawing, Always attempt to draw a 1 into mask 1 for every pixel
Gdx.gl.glStencilFunc(GL10.GL_ALWAYS, 1, 1);
//When drawing, replace whatever is currently in the mask
Gdx.gl.glStencilOp(GL10.GL_REPLACE, GL10.GL_REPLACE, GL10.GL_REPLACE);

//unlike the name would have you believe, this call actually sets the 'color'
//to clear the stencil with and doesnt actually clear the stencil. We want to
//set the mask to all 0's
Gdx.gl.glClearStencil(0); 
//This then clears the stencil buffer with all 0's
Gdx.gl.glClear(GL10.GL_STENCIL_BUFFER_BIT);

//A sprite typically has transparent pixels, and we don't want to draw them
//into the mask, so filter out pixels with an alpha less than 0.5f. Only
//those greater are drawn into the stencil buffer
Gdx.gl.glEnable(GL10.GL_ALPHA_TEST);
Gdx.gl10.glAlphaFunc(GL10.GL_GREATER, 0.5f);

//Draw the sprite into the stencil buffer
spriteBatch.begin();
spriteBatch.draw(...)
spriteBatch.end();

//Finished filtering the alpha channel
Gdx.gl.glDisable(GL10.GL_ALPHA_TEST);

//Now that we want to use the mask instead of drawing into it so we turn
//drawing to the color buffer back on
Gdx.gl.glColorMask(true, true, true, true);
//Set us up to only draw where the stencil buffer equals 1
Gdx.gl.glStencilFunc(GL10.GL_EQUAL, 1, 1);
//And since drawing into the mask is actually still on, we set this to keep the
//values in the stencil buffer, and not overwrite them
Gdx.gl.glStencilOp(GL10.GL_KEEP, GL10.GL_KEEP, GL10.GL_KEEP);

//Draw a coloured rectangle over the mask, and it will only draw pixels where the
//stencil buffer has been set giving you a silhoutte of your sprite! You can also
//use a transparent rect and draw over a normal render of your sprite to fade the
//sprite up into whatever color you want
shapeRenderer.begin(ShapeType.Fill);
shapeRenderer.rect(...);
shapeRenderer.end();

//Done with the stencil
Gdx.gl.glDisable(GL10.GL_STENCIL_TEST);

答案 3 :(得分:-2)

在我说对之前回答! 你可以这样做:sprite.setColor(new Color(Color.WHITE)); 我觉得这对你来说更简单......