2013-12-12 266 views
3

我正在嘗試更改精靈(其顏色爲紅色和灰色),顏色爲白色。將精靈顏色更改爲白色

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

但是什麼都沒有發生。

如何更改所有的白色精靈顏色?請,謝謝你

+0

您更希望是這樣的:乘0,0,0,1和反轉顏色通道。 –

回答

3

如果您想要將精靈中所有形狀的顏色更改爲白色,那麼唯一的辦法就是使用像素着色器並設置它們不是黑色的所有碎片(我假定黑色渲染爲在你的遊戲中進行transperant)到白色。類似的東西:

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,現在你是一個beginer.Fixed管道是從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 
+0

我使用的是OpenGL 2.0,但我並不是很擅長ShaderProgram,我想渲染一個白色的精靈,然後其他人使用它們的原始顏色 – LeSam

+0

@ user2372006,所以爲了確保我們正在談論相同的事情,想在本場比賽中像老闆一樣呈現它http://www.youtube.com/watch?v=OWB6Bu0zCSE 10:00:00 – SteveL

+0

是的,這正是我想要的 – LeSam

-2

在我之前的答案是正確的! 你可以這樣做:sprite.setColor(new Color(Color.WHITE)); 這是更簡單的你,我認爲...

+1

白色不影響精靈,請檢查我的答案。 – Lestat

+1

@Tekkzz這和我在問題 – LeSam

+0

中寫的完全一樣的代碼不是不行!你的顏色定義是錯誤的!你的不是一種顏色! – Tekkzz

1

這將無法正常工作。白色不影響較深的顏色(閱讀:每隔一種顏色)。

這實際上是精靈默認,所以它完全按照您創建它的圖像呈現(這就是您沒有看到更改的原因)。如果您要使用Sprite Color來影響在運行時呈現精靈的方式,請考慮將其變爲白色,然後將其更改爲任何其他顏色。

+0

好的,但我不能讓精靈全白作爲默認,因爲精靈包含不同的顏色(紅色,灰色,一些藍色等),是否有另一種解決方案來顯示白色精靈而不使用sprite.setColor? – LeSam

+1

afaik,no。除非你使用OpenglEs2.0,並且知道如何使用着色器,但我不知道。我會創建一個全白的不同sprite,並在需要時渲染它。 – Lestat

+0

如果你的加載簡單的形狀像一個_circle或rectangle_或其他形狀成精靈,那麼你可以使用**形狀渲染**來獲得不同的顏色形狀,這是很容易...... 但如果不是那麼它的如上所述,最好以白色加載所有的精靈。 – srikanth

1

你可以沒有任何OpenGL2功能或着色器,或者通過使用模板緩存精靈或其他任何瘋狂的事情的預whiteouted副本實現這一目標。

首先,您將精靈繪製到模具緩衝區,然後在模具上繪製一個適當顏色的填充矩形,它將只繪製模具的位置,留下彩色輪廓。

這裏是如何工作的:

//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); 
相關問題