libgdx 剪切图像

2022-01-12 00:00:00 crop image java libgdx clipping

一段时间以来,我一直在尝试剪切"图像,我将解释为什么以及我尝试了什么.所以我想创建一个 hp条",除了它不是条而是一颗心,所以我虽然很容易,但我所要做的就是让两张图片将它们相互叠加,然后剪下一张来制作它显示为 hp 正在丢失,但我无法找到剪切图像的方法.

I have been trying to "cut" an image for some time now, I ll explain why and what I tried. So I wanted to create an hp "bar" except it's not a bar but a heart and so I though it would be easy all I had to do is have two pictures draw them on top of each other and then just cut one to make it appear as in hp was being lost, but I was not able to find a way to cut the image.

  • 设置高度只是调整图像的大小,您可能已经猜到了
  • 我尝试使用 textureRegion 来破解它,但效果不佳
  • 我发现了一种名为 clip begin 的方法,它也使用剪刀,但由于某种原因,它似乎不起作用.

我可能使用的剪辑开始错误,但我真的找不到任何真正的文档,我正在做的只是:

I might be using the clip begin wrong but I can't really find any real documentation on it, all I'm doing is:

image.clipBegin(x,y,height,weight);
image.clipEnd();

我差点忘了,我使用的是 scene2d 图像,可能是更好的解决方法,但不确定会是什么.

I almost forgot, I'm using a scene2d Image, might be a better way to go around it but not sure what that would be.

如果有任何关于如何做到这一点的想法,我将不胜感激,谢谢.

I would appreciate any ideas on how to do this, thank you.

推荐答案

您想使用 Libgdx 公开的 OpenGL Scissor 支持.请参阅 Libgdx Clipping wiki和 Libgdx ScissorStack 文档.

You want to use the OpenGL Scissor support that Libgdx exposes. See the Libgdx Clipping wiki and the Libgdx ScissorStack documentation.

API 不是特别友好(它旨在支持动态推送多个约束矩形,据我所知,它并不经常使用).

The API isn't particularly friendly (its designed to support dynamically pushing multiple constraining rectangles, which as far as I've seen, isn't used very often).

使用剪刀堆栈要记住的重要一点是,它仅适用于发出的实际绘图命令.由于大多数 API 尝试批量绘制命令,这意味着实际绘制可能不会在看起来应该发生的时候发生.为确保发生剪裁,您必须在推动剪刀之前刷新所有缓冲的绘图(否则可能会剪裁错误的东西),并且您必须在弹出剪刀之前冲洗任何绘制调用(否则您想要剪裁的东西可能会避开剪刀).

The important point to remember with the scissor stack is that it only applies to actual draw commands that get issued. Since most APIs try to batch up draw commands, this means actual drawing might not happen when it looks like it should happen. To ensure clipping is happening you must flush any buffered draws before pushing the scissor (otherwise the wrong thing might get clipped) and you must flush any draw calls before popping the scissor (otherwise things you want clipped might avoid the scissors).

请参阅 libgdx ScissorStack 未按预期工作 或 libGDX - 如何剪辑 或 如何在 libgdx 中使用 SpriteBatch 在屏幕的一部分上绘制? 或 使组在其范围之外隐藏Actor.

See libgdx ScissorStack not working as expected or libGDX - How to clip or How to draw on just a portion of the screen with SpriteBatch in libgdx? or Making a Group hide Actors outside of its bounds.

相关文章