libgdx改变精灵颜色,同时伤害 [英] libgdx changing sprite color while hurt
问题描述
我使用libgdx做一个平台游戏,我想让敌人眨眼红色,而玩家用他的武器伤害他们。
I am using libgdx to make a little platformer and I would like to make the enemies blink in red while the player hurt them with his weapon.
我已经试过要更改sprite颜色和sprite批次颜色没有成功,它只会融化新的颜色与纹理之一。
I already tried to change the sprite color and the sprite batch color with no success, it only melt the new color with the one of the texture.
sprite.setColor(Color.RED);
spriteBatch.draw(sprite);
我想达到的效果是:
从sprite转到纹理到全红,然后再回来。
我认为有一些与混合功能,但我不知道。我想避免为我的游戏的每个怪物制作一些红色精灵。
有人知道如何实现这个效果吗?
going from sprite texture to full red and then back again. I think there is something to do with the Blending function, but I am not sure about that. I want to avoid making some red sprite for each monsters of my game. Does someone know how to achieve this effect?
推荐答案
你可以创建一个像这样的着色器来改变颜色所有像素,而不是将它们乘以该颜色。通过调用 spriteBatch.setShader(shader);
。
You can create a shader like this to change the color of all pixels instead of multiplying them by that color. Use this shader with SpriteBatch by calling spriteBatch.setShader(shader);
.
来使用这个着色器SpriteBatch这基本上是默认的SpriteBatch着色器,除了最终的颜色替换所有非alpha像素。
This is basically the default SpriteBatch shader, except the final color replaces all non-alpha pixels.
要使用这种方法,你必须分开你的彩色精灵与正常的精灵。
To use this method, you must batch your colored sprites separately from your normal sprites. Call spriteBatch.setShader(null);
to go back to drawing regular sprites.
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_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";\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 LOWP vec4 v_color;\n" //
+ "varying vec2 v_texCoords;\n" //
+ "uniform sampler2D u_texture;\n" //
+ "void main()\n"//
+ "{\n" //
+ " gl_FragColor = v_color * texture2D(u_texture, v_texCoords).a;\n" //
+ "}";
ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
这篇关于libgdx改变精灵颜色,同时伤害的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!