How should I render a 2D flashlight effect?

后端 未结 2 1577
無奈伤痛
無奈伤痛 2021-02-11 01:03

I\'m trying to make a flashlight effect in my 2D game. My flashlight is represented as a line segment extending from the entity at a specific angle. The flashlight can point in

2条回答
  •  遥遥无期
    2021-02-11 01:49

    Number one is called stencil buffer. But you'll find it difficult to achieve soft effects. -- Easy.

    Number two: You just need a texture with the light. The completely black areas can be drawn repeating a black sprite around the light sprite (left, right, up, down). Or you can mix it with the stencil buffer. Or you can carefully calculate your texture coordinates and use GL.clamp_to_edge to propagate all the black pixels. Depending on how you render your scene, you can render your light first with alpha information and then blend the scene (darkening according to the dst_alpha). -- Not that hard to implement.

    Number three would be research on shaders (GLES 2.0). You can render a mesh to fill the hole screen and darken it with some shader calculations. -- This is the most flexible option, and the most difficult (far from rocket science anyway).

    Some options sound better than others, but providing the info you gave, there's nothing more I can tell you. You have a good start point from where to start to research on.

    **If you choose working with textures, consider to have a couple of different textures depending on the battery. You can scale them a bit or tint them, but having different textures is way more flexible.

提交回复
热议问题