我应该如何渲染2D手电筒效果? [英] How should I render a 2D flashlight effect?

查看:103
本文介绍了我应该如何渲染2D手电筒效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在2D游戏中制作手电筒效果.我的手电筒表示为从实体以特定角度延伸的线段.手电筒可以指向任何方向.手电筒的强度(手电筒光束的长度)也会变化.

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 any direction. The flashlight also varies in intensity (length of the flashlight beam).

尝试找出最佳(最简单?)和最灵活的方式来渲染手电筒效果时遇到问题.尤其是平铺地图.

I'm have a problem trying to figure out the best (easiest?) and most flexible way to render the flashlight effect. Particularly with the tiled map.

我可以想到两种方法.但是我不知道要实现它们:

I can think of two methods. But I have no idea to implement them:

  1. 仅绘制平铺地图的圆锥形/圆弧形部分
  2. 用黑色纹理覆盖屏幕,并用代码在黑色纹理上打孔.这样,我可以更改孔的属性.

我不知道从何处开始/这些东西叫什么/如果libGDX可以做到这一点.

I'm clueless as to where to start with this/what these are called/if libGDX can do them.

推荐答案

第一个被称为模具缓冲区.但是您会发现很难达到柔和的效果. -很简单.

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

第二个:您只需要带有灯光的纹理即可.可以绘制整个黑色区域,在光精灵(左,右,上,下)周围重复一个黑色精灵.或者,您可以将其与模板缓冲区混合.或者,您可以仔细计算纹理坐标,并使用GL.clamp_to_edge传播所有黑色像素.根据渲染场景的方式,可以先使用Alpha信息渲染灯光,然后混合场景(根据dst_alpha变暗). -实施起来并不难.

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.

第三名将是有关着色器的研究(GLES 2.0).您可以渲染网格以填充孔屏幕,并通过一些着色器计算使其变暗. -这是最灵活,也是最困难的选择(无论如何,这远非火箭科学).

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.

这篇关于我应该如何渲染2D手电筒效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆