如何使用 UIKit/Quartz 在 iPhone 上的纹理上绘制灯光效果
我有一个带有背景图像(明亮的房间)和黑色图像(阴影)的场景。我需要能够在背景上移动手指并显示场景的某些部分,模拟黑暗房间中的昏暗光源。
我当前的方法是根据触摸的位置生成蒙版,然后将该蒙版应用于阴影图像。问题是我正在生成一个新的蒙版并在每次收到触摸事件时应用它。这是一个大图像(800x600),这会导致性能下降,并增加大量内存使用量,最终导致游戏崩溃(我认为我没有任何内存泄漏,但这并不能保证......无论如何性能本身是不可接受的)。
谁能想到更好的方法(不涉及使用 OpenGL ES——这不是这个项目中的一个选项)来做到这一点?
I have a scene with a background image (a lit room), and a black image (shadow) over that. I need to be able to move my finger over the background and reveal some parts of the scene, simulating a dim light source in a dark room.
My current approach was to generate a mask depending on the position of the touch, and then applying that mask to the shadow image. The problem is I'm generating a new mask and applying it every time I receive a touch event. It's a large image (800x600) and this causes the performance to go down and it increases a lot the memory usage, eventually crashing the game (I think I don't have any memory leaks, but that's not warrantied... anyway the performance itself isn't acceptable).
Can anyone think of a better approach (which doesn't involve using OpenGL ES -- that's not an option in this project) to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
与我上面的评论一致。
也许为了解决不同的阴影级别,您还可以在图像和阴影视图之间有一个视图网格(正方形)。每个网格方块都有不同的 Alpha 不透明度,当光点位于网格方块上方时,网格方块的 Alpha 不透明度会更改为 0。当光点移出网格方块时,其 Alpha 不透明度会变回默认值。
To go with my comments above.
Maybe to get around the different shadow levels you could also have a grid of views (squares) between the image and the shadow view. each grid square has a different alpha opacity and when the spot is over a grid square, the grid square's alpha opacity changes to 0. when the spot moves off the grid square it's alpha opacity changes back to it's default.
如果没有更多信息,很难知道这种方法是否适用于您的情况,但您可以做的是生成单个掩模图像,例如径向 alpha 梯度,然后对其应用仿射变换以根据触摸。这可用于模拟手电筒/手电筒光束。
Without more information it is a little difficult to know whether this approach will work in your case but what you could do is generate a single mask image, say, a radial alpha gradient and then apply an affine transform to it to shape it according to the touches. This can be used to simulate a torch/flashlight beam.
我会尝试这个:使用一个带有自定义drawRect实现的视图:首先绘制阴影图像(灰度),然后绘制白色和alpha的亮点图像。最后是“乘法”混合模式下的背景图像。
I would try this: use one view with a custom drawRect implemetation: first draw the shadow image (in grayscale) then a bright spot image in white an alpha. And finally the background image in a 'multiply' blend mode.
想一想,影子一定是图像吗?也许你可以简单地用一种颜色填充阴影层并遮盖它?这样,内存使用量应该更少,并且效果应该几乎相同(如果不完全相同)。
Just a thought, does the shadow has to be an image? Perhaps you could simply fill the shadow layer with a color and mask it then? This way the memory usage should be less and the effect should be nearly identical (if not exactly the same).
没有理由在每次触摸移动时生成新的蒙版。相反,让掩码初始化一次并根据触摸事件的需要对其进行操作(重置其框架)。
There is no reason to generate a new mask on every touch move. Instead, let the mask be initialized once and manipulate it (reset it's frame) as needed upon touch events.