[英]Tint a pixmap in with libgdx
I posted a question about drawing text onto a pixmap to generate dynamic textures at runtime, and that problem was resolved here 我发布了一个有关在pixmap上绘制文本以在运行时生成动态纹理的问题,此问题已在此处解决。
Is there an easy way that I can tint the font that I copy to a new pixmap, or do I need to do it manually pixel by pixel? 是否有一种简单的方法可以为复制到新的像素图的字体添加颜色,还是需要逐个像素手动进行设置? I can't find anything obvious in the libgdx library or on Google.
我在libgdx库或Google上找不到任何明显的内容。
My current process is: 我当前的流程是:
At some point in that process I'm like to be able to tint the font at runtime. 在此过程中的某个时刻,我希望能够在运行时为字体着色。
Code updated to test Jyro117's answer. 更新代码以测试Jyro117的答案。 (Not working, see image below)
(不起作用,请参见下图)
// Set up the base image
Pixmap newPixmap = new Pixmap(backImage.getWidth(),
backImage.getHeight(), Format.RGBA8888);
newPixmap.drawPixmap(backImage, 0, 0);
// Copy out the letter from our fontsheet
// and draw the char to a new pixmap
Glyph glyph = fontData.getGlyph(getLetterToDraw().charAt(0));
Pixmap charPixmap = new Pixmap(glyph.width, glyph.height,
Format.RGBA8888);
charPixmap.drawPixmap(fontPixmap, 0, 0, glyph.srcX, glyph.srcY,
glyph.width, glyph.height);
// try to tint it?!
charPixmap.setColor(1, 0, 0, 100);
charPixmap.fillRectangle(0, 0, newPixmap.getWidth(),
newPixmap.getHeight());
// copy the character to our new bitmap
newPixmap.drawPixmap(charPixmap, (BLOCK_SIZE - glyph.width) / 2,
(BLOCK_SIZE - glyph.height) / 2);
texture = new Texture(newPixmap);
newPixmap.dispose();
charPixmap.dispose();
Pixmap has all the functions you need to tint it. Pixmap具有着色所需的所有功能。
Hopefully that clears it up for you. 希望可以为您清除它。
Edit: 编辑:
Alright, so I see what exactly you mean, it would appear there is no way by default to change the 'type' of blending for Pixmap. 好吧,所以我明白您的意思了,默认情况下似乎无法更改Pixmap混合的“类型”。 At least without me looking into the native code under the covers.
至少在没有我调查底层代码的情况下。 BitmapFont gets around this by using vertex data for the glyph and renders each one as a shape instead of image.
BitmapFont通过使用字形的顶点数据来解决此问题,并将每个渲染为一种形状而不是图像。 So in order to do this you will have to do it with two passes:
因此,要执行此操作,您将必须执行两次以下操作:
Note: You can iterate over the pixel data by calling getPixel() or get the Bytebuffer with getPixels() and the 4th byte will be the pixel's alpha value between 0 and 255. 注意:您可以通过调用getPixel()来遍历像素数据,或使用getPixels()获取字节缓冲区,第4个字节将是像素的alpha值,介于0和255之间。
Edit 2: 编辑2:
I just thought of an alternative way to do this is to use a Frame Buffer . 我只是想到了一种替代方法,可以使用帧缓冲区 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.