简体   繁体   English

用libgdx着色像素图

[英]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: 我当前的流程是:

  1. create a new pixmap 创建一个新的像素图
  2. draw a character onto that pixmap 在该像素图上绘制一个字符
  3. convert the pixmap into a texture for rendering. 将像素图转换为纹理以进行渲染。

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具有着色所需的所有功能。

  • Get a pixmap of the font (in your case fontPixmap) 获取字体的像素图(在您的情况下为fontPixmap)
  • Make sure alpha blending is enabled. 确保已启用Alpha混合。
  • Set the color , (setting alpha to the desired level) 设置颜色 (将Alpha设置为所需的级别)
  • Draw rectangle over entire font image (0,0,width,height) 在整个字体图像(0、0,宽度,高度)上绘制矩形
  • Now the pixmap of your font has the desired tinting 现在,您的字体的像素图具有所需的色彩

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: 因此,要执行此操作,您将必须执行两次以下操作:

  • First we fill the pixmap with the desired color and alpha (note: the values for this are between 0f and 1f, so your alpha should be something like 0.3f not 100) 首先,我们用所需的颜色和Alpha填充像素图(注意:此值在0f和1f之间,因此您的Alpha应该是0.3f而不是100)
  • Next you iterate over all pixels of the image and are looking for pixels which have their alpha value below a certain threshold (should be around 0.3f). 接下来,您遍历图像的所有像素,并寻找其alpha值低于特定阈值(应为0.3f左右)的像素。 Then you could either set the alpha of those pixels below the threshold to 0, or maybe do something fancier like Math.pow(alpha, 3), etc. Basically the alpha you just added to the image (using source over blending) you want to reduce/remove, but only for pixels which are supposed to be transparent. 然后,您可以将低于阈值的那些像素的alpha设置为0,或者做一些像Math.pow(alpha,3)之类的事情。基本上,您刚刚添加到图像中的alpha(使用源而不是混合)减少/删除,但仅适用于应该是透明的像素。 You may have to play around with different values or reduction techniques to get it looking correctly. 您可能需要尝试不同的值或归约技术,才能使其看起来正确。

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 . 我只是想到了一种替代方法,可以使用帧缓冲区

  • Render your font texture onto the frame buffer. 将字体纹理渲染到帧缓冲区上。
  • Draw a colored rectangle with alpha over the entire frame buffer using the proper alpha blending function (Not sure which one off the top of my head but you want one which only applies if dst has an alpha greater than 0). 使用适当的alpha混合功能在整个帧缓冲区上绘制一个带有alpha的彩色矩形(不知道哪一个在我头顶上方,但您希望仅当dst的alpha大于0时才适用)。
  • Grab the texture from the frame buffer and use that as your tinted font. 从帧缓冲区中获取纹理并将其用作着色字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM