[英]Libgdx use transition sprites
i found some transition sprite and want to use them in my programm. 我找到了一些过渡精灵,想在我的程序中使用它们。 It's just a black and white picture.
这只是一张黑白照片。
How can I get every pixel under a brightness value from this sprite and draw these pixel black, to get an animation? 如何从此精灵获得亮度值以下的每个像素并将其绘制为黑色,以获得动画效果?
I tried something like this: 我尝试过这样的事情:
sr.begin(ShapeType.Filled);
sr.setColor(0, 0, 0, 1f);
for(int y = 0; y < 320; y ++) {
for(int x = 0; x < 531; x ++) {
transSprite.getTexture().getTextureData().prepare();
int brightness = transSprite.getTexture().getTextureData().consumePixmap().getPixel(x, y);
if(brightness <= alpha * 255) {
sr.rect(x, y, 1, 1);
}
}
}
sr.end();
But this is too slow. 但这太慢了。
I have a resolution of 531 x 320 Pixel and an alpha value between 0 and 1 for the animation. 我的动画分辨率为531 x 320像素,alpha值介于0和1之间。
Do you have any ideas? 你有什么想法?
This can be done with your shader. 这可以用您的着色器完成。 Make a custom shader with a threshold value to use with SpriteBatch.
制作一个具有阈值的自定义着色器,以用于SpriteBatch。 I copied the default sprite batch shader, but changed the fragment shader part.
我复制了默认的精灵批量着色器,但更改了片段着色器部分。
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" //
+ "uniform float u_threshold;\n" //
+ "void main()\n"//
+ "{\n" //
+ " vec4 texture = texture2D(u_texture, v_texCoords);\n" //
+ " gl_FragColor = v_color * step(u_threshold, texture.r);\n" //
+ "}";
customShader = new ShaderProgram(vertexShader, fragmentShader);
Then you can animate it by changing the u_threshold
value. 然后,可以通过更改
u_threshold
值为其设置动画。
spriteBatch.setShader(customShader);
spriteBatch.begin();
customShader.setUniformf("u_threshold", brightness); //where brightness is from 0 to 1
spriteBatch.draw(sprite);
spriteBatch.end();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.