libgdx changing sprite color while hurt(libgdx 在受伤时改变精灵颜色)
问题描述
我正在使用 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.
我已经尝试更改精灵颜色和精灵批次颜色但没有成功,它只会将新颜色与纹理之一融合.
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);
我想要达到的效果是:
从精灵纹理变为全红色,然后再返回.我认为这与混合功能有关,但我不确定.我想避免为我的游戏中的每个怪物制作一些红色精灵.有人知道如何实现这种效果吗?
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); 将此着色器与 SpriteBatch 一起使用.
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 着色器,除了最终颜色替换所有非 alpha 像素.
This is basically the default SpriteBatch shader, except the final color replaces all non-alpha pixels.
要使用此方法,您必须将彩色精灵与普通精灵分开批处理.调用 spriteBatch.setShader(null); 返回绘制常规精灵.
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 + ";
" //
+ "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";
" //
+ "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;
" //
+ "uniform mat4 u_projTrans;
" //
+ "varying vec4 v_color;
" //
+ "varying vec2 v_texCoords;
" //
+ "
" //
+ "void main()
" //
+ "{
" //
+ " v_color = " + ShaderProgram.COLOR_ATTRIBUTE + ";
" //
+ " v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;
" //
+ " gl_Position = u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";
" //
+ "}
";
String fragmentShader = "#ifdef GL_ES
" //
+ "#define LOWP lowp
" //
+ "precision mediump float;
" //
+ "#else
" //
+ "#define LOWP
" //
+ "#endif
" //
+ "varying LOWP vec4 v_color;
" //
+ "varying vec2 v_texCoords;
" //
+ "uniform sampler2D u_texture;
" //
+ "void main()
"//
+ "{
" //
+ " gl_FragColor = v_color * texture2D(u_texture, v_texCoords).a;
" //
+ "}";
ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
这篇关于libgdx 在受伤时改变精灵颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:libgdx 在受伤时改变精灵颜色
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
