不只使用原色来为 CCSprite 着色

发布于 2024-12-15 19:02:02 字数 436 浏览 1 评论 0原文

我有一个由具有透明背景的 png 创建的 CCSprite。

我希望能够以一种可以自由定义颜色的方式将颜色应用于此精灵,而精灵的实际颜色不会影响我必须添加的每种颜色的数量。

我试过这个:

mySprite.color = ccc3(200,200,255);

试图为我的精灵添加一点蓝色的感觉,但因为它是通过设置将根据精灵的现有颜色显示的色调量来工作的,而我的精灵几乎没有蓝色(大多数它是黄色的)最终的效果非常粗略,一切都变得非常暗,并且有一种轻微的蓝色,但不是我想要的。

在这种情况下,对我来说理想的效果是为其添加一个具有非常低 alpha 的浅蓝色遮罩。

有没有一种简单的方法可以在不编写精灵的情况下做到这一点?

我尝试过使用 CCTexture2D,但没有运气,因为没有用于处理颜色的内置方法,并且大多数教程只教您如何从图像文件构建纹理。

I have a CCSprite that was created from a png with transparent background.

I want to be able to apply colors to this sprite in a way that I`m free to define which color it is, without the actual color of the sprite affecting the amount of each color I have to add.

I`ve tried this:

mySprite.color = ccc3(200,200,255);

In an attempt to add a little blue-ish feel to my sprite, but as it works by setting the amount of tint that's gonna be displayed based on existant color of the sprite, and my sprite has virtually no blue in any of it (most of it is yellow) the resulting effect is pretty sketchy, everything gets really dark, and there is one slight blue-ish coloring, but not as I wanted.

The ideal effect for me on this case would be to ADD a light blue mask to it with very low alpha.

Is there an easy way to do that without composing sprites?

I've tried using CCTexture2D, but had no luck, as there is no built in method for working with colors, and most tutorials only teach you how to build textures out of image files.

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

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

发布评论

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

评论(2

扛刀软妹 2024-12-22 19:02:02

这在原始精灵的代码中看似很难做到。另一种选择是:

  • 创建一个新的精灵,它只是原始精灵的白色轮廓版本,
  • 该白色精灵的颜色属性现在将准确响应您传入的 RGB 值
  • ,因此将浅蓝色值传递给白色精灵并正确设置不透明度
  • ,然后将其覆盖在原始精灵上

有好处吗?

This is deceptively hard to do in code with the original sprite. Another option would be:

  • create a new sprite, which is just a white outline version of your original sprite
  • the color property of this white sprite will now respond exactly to the RGB values you pass in
  • so pass in your light blue value to the white sprite and set the opacity correctly
  • then overlay it on your original sprite

Any good?

安人多梦 2024-12-22 19:02:02

实现此目的的唯一方法是使用适当的 OpenGL 混合函数覆盖(遮罩)精灵。当你说“添加浅蓝色蒙版”时,这就是你需要做的。您可能会发现这个视觉blendfunc工具很有帮助,即使只是为了了解与蒙版精灵混合的工作原理以及作用你可以用它实现。然而,Cocos2D 对此没有内置支持,因此您必须在 cocos2d 节点的 -(void) draw {} 方法中恢复为纯 OpenGL。

着色(更改颜色属性)只会通过更改所有 4 个顶点的顶点颜色来修改整个图像的 RGB 通道。

The only way you can achieve this is by overlaying (masking) the sprite with the proper OpenGL blend functions. When you say "add a light blue mask" then that's what you need to do. You may find this visual blendfunc tool helpful, if only to understand how blending with mask sprites works and what you can achieve with it. There's no built-in support for this by Cocos2D however, so you'll have to revert to pure OpenGL inside a cocos2d node's -(void) draw {} method.

Tinting (changing the color property) will only modify the RGB channels of the entire image by changing the vertex colors of all 4 vertices.

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