在 PlayN 中以编程方式淡入图像

发布于 2024-12-27 12:13:25 字数 608 浏览 0 评论 0原文

我正在使用 PlayN 开发一款游戏,有一点我想拍摄一张图像(目前为 PNG 格式,已经有 8 位 alpha),并且我想将图像乘以一个额外的 alpha 因子,基于我的代码中的值。

具体来说,我有一张当前位于 ImageLayer 中的脸部图片,我想要的效果是这样的:

void init() {
  faceImage = assetManager().getImage("images/face.png");
  graphics().rootLayer().add(faceImage);
}

void update(float deltaMilliseconds) {
  // start at fully transparent, fade to fully opaque 
  float transparency = calcTransparency(deltaMilliseconds);
  faceImage.setTransparency(transparency);
}

我希望有某种方法可以对 GroupLayers 和混合模式进行一些处理,也许可以将图像的 CanvasLayer 涂有纯白色矩形,其透明度由我的代码控制,但对我来说,这是否是实现看似相当常见的效果的最佳方法并不明显。

I'm working on a game using PlayN, and there's a bit where I would like to take an image (which is currently in PNG format, with 8-bit alpha already) and I would like to multiply the image by an additional alpha factor, based on a value from my code.

Specifically, I have a picture of a face that currently lives in an ImageLayer, and the effect that I would like would be to have something like this:

void init() {
  faceImage = assetManager().getImage("images/face.png");
  graphics().rootLayer().add(faceImage);
}

void update(float deltaMilliseconds) {
  // start at fully transparent, fade to fully opaque 
  float transparency = calcTransparency(deltaMilliseconds);
  faceImage.setTransparency(transparency);
}

I expect that there's some way to do some trickiness with GroupLayers and blend modes, perhaps blending the image with a CanvasLayer painted with a solid white rectangle with transparency controlled by my code, but it's not obvious to me if that's the best way to achieve what seems like a pretty common effect.

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

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

发布评论

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

评论(1

生来就爱笑 2025-01-03 12:13:25

如果您只想将图像从完全透明淡入为完全不透明,则只需执行以下操作:

ImageLayer faceLayer;
void init() {
  Image faceImage = assetManager().getImage("images/face.png");
  faceLayer = graphics().createImageLayer(faceImage);
  graphics().rootLayer().add(faceLayer);
}

void update(float delta) {
  float alpha = calcAlpha(delta);
  faceLayer.setAlpha(alpha);
}

其中 alpha 范围从 0(完全透明)到 1(完全不透明)。

If you just want to fade the image in from fully-transparent to fully-opaque, then just do the following:

ImageLayer faceLayer;
void init() {
  Image faceImage = assetManager().getImage("images/face.png");
  faceLayer = graphics().createImageLayer(faceImage);
  graphics().rootLayer().add(faceLayer);
}

void update(float delta) {
  float alpha = calcAlpha(delta);
  faceLayer.setAlpha(alpha);
}

Where alpha ranges from 0 (fully transparent) to 1 (fully opaque).

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