重叠部分透明的形状

发布于 2024-08-14 11:09:54 字数 97 浏览 2 评论 0原文

如果我以部分透明的颜色在表面(带有 SRCALPHA 标志)上绘制任何形状,它会完全替换其下方的形状,而不是像您在图像编辑器中期望的那样重叠。

如何使形状正确重叠?

If I draw any shape onto a surface (with SRCALPHA flag on) in a partially transparent colour it completely replaces what was underneath it, instead of overlapping like you would expect in image editors.

How can I make the shapes overlap properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兰花执着 2024-08-21 11:09:54

如前所述,PyGame 中的绘制函数实际上并不按照您期望的方式进行混合。来自 PyGame 文档:

大多数参数接受 RGB 三元组的颜色参数。它们还可以接受 RGBA 四元组。如果 Surface 包含像素 alpha,则 alpha 值将直接写入 Surface,但绘制函数不会透明绘制。

您可能想做的基本上就是 Alex Martelli 所说的——创建一个带有 Alpha 通道的中间软件表面,将形状绘制到该表面上,然后通过混合传输到最终表面上。如果您使用过 PyGame 的字体渲染,请注意它的工作原理大致相同,只是 Font.render 为您创建临时表面。

这些天我对 Python 和 PyGame 有点生疏,但这里有一个简单的例子,希望不会有太多错误:

# returns a surface with the circle drawn onto it
def render_transparent_circle(color, radius, width=0):
    size = radius * 2
    temp_surf = pygame.Surface((size, size), SRCALPHA)
    temp_surf.fill(Color(0, 0, 0, 0))
    pygame.draw.circle(temp_surf, color, (radius, radius), radius, width)
    return temp_surf

# draw a half-opaque blue circle of radius 30 to the display at point (50, 100)
def test():
    transp_blue = Color(0, 0, 255, 128)
    screen = pygame.display.get_surface()
    circle = render_transparent_circle(transp_blue, 30)
    screen.blit(circle, (50, 100))

ps -- 如果你要多次显示相同的形状,你可能需要考虑缓存中间表面(但不要忘记进行轮廓分析以了解是否有帮助)。

As noted, the draw functions in PyGame don't actually do blending the way you might expect. From the PyGame documentation:

Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently.

What you'll probably want to do is essentially what Alex Martelli said--create an intermediate software surface with an alpha channel, draw your shape onto that surface, then blit with blending onto the final surface. If you've used PyGame's font rendering, notice that it works roughly the same, except that Font.render creates the temporary surface for you.

I'm a bit rusty with Python and PyGame these days, but here's a quick example that hopefully doesn't have too many mistakes:

# returns a surface with the circle drawn onto it
def render_transparent_circle(color, radius, width=0):
    size = radius * 2
    temp_surf = pygame.Surface((size, size), SRCALPHA)
    temp_surf.fill(Color(0, 0, 0, 0))
    pygame.draw.circle(temp_surf, color, (radius, radius), radius, width)
    return temp_surf

# draw a half-opaque blue circle of radius 30 to the display at point (50, 100)
def test():
    transp_blue = Color(0, 0, 255, 128)
    screen = pygame.display.get_surface()
    circle = render_transparent_circle(transp_blue, 30)
    screen.blit(circle, (50, 100))

p.s. -- If you're going to display the same shape multiple times, you may want to consider caching the intermediate surface (but don't forget to profile to find out if it helps).

看海 2024-08-21 11:09:54

您可以将 blit 与适当的 BLEND_*< /code> 值(将形状保留为单独的表面,以实现完整性和编辑,并且仅将它们传输用于显示目的)。

You could use blit with appropriate BLEND_* values (keeping the shapes as separate surfaces, for integrity and editing, and only blitting them for display purposes).

书间行客 2024-08-21 11:09:54

Alpha 本身只是一个可以达到某种目的的额外“价值”。碰巧的是,其目的通常是(但并非总是)透明度(其他用途包括反射率、镜面高光,甚至是伪造 HDR 的方法)。

您需要指定您希望它混合(以及它应该使用的方程式)。在 Google 上简单搜索“pygame 混合模式” 就会显示好资源很少。

正如 Alex 所指出的,blit 采用混合模式作为参数。这个 StackOverflow 问题提供了更多信息。

Alpha on its own is just an extra 'value' that can serve a purpose. It just so happens that the purpose is typically (but not always) transparency (other uses include reflectance, specular highlighting and even an approach for faking HDR).

You need to specify that you want it to blend (and the equation it should use to do it). A simple Google search for "pygame blend mode" shows a few good resources.

As Alex has pointed out, blit takes a Blending mode as a parameter. This StackOverflow question provides more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文