如何在 LibGDX 中进行混合 [英] How to do blending in LibGDX

查看:17
本文介绍了如何在 LibGDX 中进行混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想在 LibGDX 中使用混合模式,但不知道该怎么做.我在互联网上找到了这张图片.我想在 LibGDX 上做同样的事情.谁能教教我.

I basically want to play around with blending modes in LibGDX but don't know how to do it. I found this image on internet. I want to do the same thing on LibGDX. Can someone teach me how.

我一直在使用 Scene2D.这是我的非工作片段.

I've been playing around using Scene2D. Here's my non-working snippet.

private class MyGroup extends Group {

    Image red, blue;

    public MyGroup() {
        Texture texture = new Texture(Gdx.files.internal("images/red.png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        red = new Image(texture);

        texture = new Texture(Gdx.files.internal("images/blue.png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        blue = new Image(texture);

        red.setX(-25);
        blue.setX(25);
    }
    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.end();
        batch.begin();
        batch.enableBlending();

        red.draw(batch, parentAlpha);

        Gdx.gl.glEnable(Gdx.gl20.GL_BLEND);
        Gdx.gl.glBlendFuncSeparate(
                Gdx.gl20.GL_DST_COLOR, 
                Gdx.gl20.GL_SRC_COLOR,
                Gdx.gl20.GL_ONE,
                Gdx.gl20.GL_ONE);

        blue.draw(batch, parentAlpha);
    }
}

推荐答案

我意识到这不是一个新问题,但我想我会与其他任何人分享一些信息,他们在不了解渲染的情况下解决了这个问题/答案在 OpenGL 中(了解这些术语有很大帮助,因此您不仅仅是猜测混合和匹配)请注意 这个网站是我自己学到的大部分内容,因此可以在那里找到更完整的信息.

I realize this is not a new question, but I thought I would share some information for anyone else who makes it to this question/answer without knowing much about rendering in OpenGL (Knowing these terms helps a lot so you aren't just guessing mix-and-matching) Note that this site is how I learned most of this myself, so more complete information can be found there.

目标颜色:缓冲区中的颜色,除非被修改或被新值覆盖,否则(最终)会被绘制.

Destination Color: the color in the buffer, which will (eventually) be drawn unless it is modified or overwritten with new values.

源颜色:来自附加渲染命令的颜色,可能与目标颜色交互,也可能不交互(取决于我们的设置)

Source Color: the color coming in from additional rendering commands, which may or may not interact with the destination color (depending on our settings)

默认混合方程: Final Color = (SourceColor*SourceBlendingFactor)+(DestinationColor*DestinationBlendingFactor) (这个默认方程可以改变,但我建议阅读我的源链接在顶部了解更多信息)

The default blending equation: Final Color = (SourceColor*SourceBlendingFactor)+(DestinationColor*DestinationBlendingFactor) (This default equation can be changed, but I recommend reading my source link at the top for more information on that)

这两个 BlendingFactors 是我们可以处理的.我们可以将它们设置为:

The two BlendingFactors are what we can mess with. We can set them to:

GL_ZERO:RGB(0,0,0) A(0)
GL_ONE:RGB(1,1,1) A(1)
GL_SRC_COLOR:RGB(sourceR, sourceG, sourceB) A(sourceA)
GL_ONE_MINUS_SRC_COLOR:RGB(1-sourceR, 1-sourceG, 1-sourceB) A(1-sourceA)
GL_DST_COLOR:RGB(目的地R,目的地G,目的地B)A(目的地A)
GL_ONE_MINUS_DST_COLOR:RGB(1-destinationR, 1-destinationG, 1-destinationB) A(1-destinationA)
GL_SRC_ALPHA:RGB(sourceA, sourceA, sourceA) A(sourceA)
GL_ONE_MINUS_SRC_ALPHA:RGB(1-sourceA, 1-sourceA, 1-sourceA) A(1-sourceA)
GL_DST_ALPHA:RGB(目的地A,目的地A,目的地A)A(目的地A)
GL_ONE_MINUS_DST_ALPHA:RGB(1-destinationA, 1-destinationA, 1-destinationA) A(1-destinationA)
GL_SRC_ALPHA_SATURATE:RGB(min(sourceA, 1-destinationA), min(sourceA, 1-destinationA), min(sourceA, 1-destinationA)) A(1)

下面也使用了一些预定义的常量颜色,默认是黑色
GL_CONSTANT_COLOR:RGB(constantR,constantG,constantB)A(constantA)
GL_ONE_MINUS_CONSTANT_COLOR:RGB(1-constantR, 1-constantG, 1-constantB) A(1-constantA)
GL_CONSTANT_ALPHA:RGB(constantA,constantA,constantA)A(constantA)
GL_ONE_MINUS_CONSTANT_ALPHA:RGB(1-constantA, 1-constantA, 1-constantA) A(1-constantA)

GL_ZERO: RGB(0,0,0) A(0)
GL_ONE: RGB(1,1,1) A(1)
GL_SRC_COLOR: RGB(sourceR, sourceG, sourceB) A(sourceA)
GL_ONE_MINUS_SRC_COLOR: RGB(1-sourceR, 1-sourceG, 1-sourceB) A(1-sourceA)
GL_DST_COLOR: RGB(destinationR, destinationG, destinationB) A(destinationA)
GL_ONE_MINUS_DST_COLOR: RGB(1-destinationR, 1-destinationG, 1-destinationB) A(1-destinationA)
GL_SRC_ALPHA: RGB(sourceA, sourceA, sourceA) A(sourceA)
GL_ONE_MINUS_SRC_ALPHA: RGB(1-sourceA, 1-sourceA, 1-sourceA) A(1-sourceA)
GL_DST_ALPHA: RGB(destinationA, destinationA, destinationA) A(destinationA)
GL_ONE_MINUS_DST_ALPHA: RGB(1-destinationA, 1-destinationA, 1-destinationA) A(1-destinationA)
GL_SRC_ALPHA_SATURATE: RGB(min(sourceA, 1-destinationA), min(sourceA, 1-destinationA), min(sourceA, 1-destinationA)) A(1)

The following also uses some predefined constant color, by default it is black
GL_CONSTANT_COLOR: RGB(constantR, constantG, constantB) A(constantA)
GL_ONE_MINUS_CONSTANT_COLOR: RGB(1-constantR, 1-constantG, 1-constantB) A(1-constantA)
GL_CONSTANT_ALPHA: RGB(constantA, constantA, constantA) A(constantA)
GL_ONE_MINUS_CONSTANT_ALPHA: RGB(1-constantA, 1-constantA, 1-constantA) A(1-constantA)

所以所有这些都只是预定义的浮点值,它们与我们的源或目标相乘,然后相加.

So all of these are just predefined float values that are being multiplied with either our source or destination, and then added to the other.

从图中最容易观察到的是 GL_ZERO 和 GL_ONE.我们最终得到具有 ONE 的图像.

The easiest to observe from the image is GL_ZERO and GL_ONE. We end up with whichever image has the ONE.

用 GL_DST_COLOR 理解 GL_ZERO

Understanding GL_ZERO with GL_DST_COLOR

当 GL_ZERO 在目标上时,我们将忽略当前缓冲区中的任何颜色信息(因为将所有内容乘以零).然而,由于源图像上也有 GL_DST_COLOR,我们最终将源图像和目标图像的 r、g、b、a 值相乘.

When GL_ZERO is on the destination, we are ignoring any color information currently in the buffer (because multiplying everything by zero). However, With GL_DST_COLOR also on the source image, we end up multiplying the r, g, b, a values of the source and destination.

由于示例图像的性质,这在图像中看起来不错.一个作为纯色图像,而另一个灰度图像看起来和行为几乎就像一束光束来揭示"GL_ZERO 设置中的颜色.

This looks good in the image because of the nature of the example images. One acts as a solid color image, while the other grayscale image looks and acts almost like a beam of light to "reveal" the color out from our GL_ZERO setting.

希望这有助于解释我们在上面看到的图像,并帮助每个人了解这些图像实际上是如何混合在一起的.

Hopefully, this helps explain the images we can see above and helps everyone understand how those images are actually being blended together.

这篇关于如何在 LibGDX 中进行混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆