Java 没有box2d的Libgdx灯
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21278229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Libgdx light without box2d
提问by Springrbua
I just started creating a game using libgdx. It is a top down 2d shooter using scene2d ui. Now i thought, that i could add darkness and light to some levels, but i don't want to rewrite everything using box2d. I don't need realistic shadows just some kind of ambient light and a lightcircle arround my character, which is not affected by walls and other obstacles arround him. So i wanted to know if there is any kind of lightsystem in libgdx? Or can i use box2dlights without using box2d bodies/world...? Thanks
我刚开始使用 libgdx 创建游戏。这是一个使用scene2d ui的自上而下的2d射击游戏。现在我想,我可以在某些级别添加黑暗和光明,但我不想使用 box2d 重写所有内容。我不需要逼真的阴影,只需要某种环境光和我角色周围的光环,不受墙壁和他周围其他障碍物的影响。所以我想知道 libgdx 中是否有任何类型的光系统?或者我可以在不使用 box2d body/world 的情况下使用 box2dlights 吗?谢谢
采纳答案by noone
Unfortunately there is nothing like this already provided by LibGDX.
不幸的是,LibGDX 没有提供这样的东西。
But you can easily do it yourself if you do not want shadows.
但是如果你不想要阴影,你可以很容易地自己做。
Hereis a little video of someone who has done it via LibGDX. Hereis the article to this video, with code and descriptions and everything provided.
You can do it this way with shaders, but you could also do the same by just rendering a lightmap to an FBO (in the given links you can see how to do that) and then just render it the usual way with blending activated on top of your screen. The standard SpriteBatch
can do that and you don't need any custom shaders.
这是一个通过 LibGDX 完成的人的小视频。这是此视频的文章,包含代码和说明以及提供的所有内容。您可以使用着色器以这种方式执行此操作,但您也可以通过将光照贴图渲染到 FBO(在给定的链接中您可以看到如何执行此操作)来执行相同的操作,然后以通常的方式渲染它并在顶部激活混合你的屏幕。标准SpriteBatch
可以做到这一点,您不需要任何自定义着色器。
If you still want to have real shadows with obstacles, you will probably find thisarticle very useful. But this is a lot slower and needs special shaders.
如果你仍然想要有障碍物的真实阴影,你可能会发现这篇文章非常有用。但这要慢得多,并且需要特殊的着色器。
There is also no way to use Box2dLights without Box2D btw.
顺便说一句,没有 Box2D 也无法使用 Box2dLights。
回答by Kedu
In my opinion the marked answer is wrong.
在我看来,标记的答案是错误的。
Its actually pretty simple to use box2dLights without using box2d if u dont want any shadows. The question was, if its possible to add some kind of circle light around a character.
如果您不想要任何阴影,那么使用 box2dLights 而不使用 box2d 实际上非常简单。问题是,是否可以在角色周围添加某种圆形光。
I used two different approaches, only one using box2dlights.
我使用了两种不同的方法,只有一种使用 box2dlights。
The article in the marked answer discripes a method using FBO. U dont really need that if u just want to lighten an area. U just need a sprite, like this. Now place it somewhere on ur screen, and when rendering , do the following:
标记答案中的文章描述了使用 FBO 的方法。如果你只是想照亮一个区域,你真的不需要那个。你只需要一个精灵,就像这样。现在把它放在你屏幕上的某个地方,在渲染时,执行以下操作:
batch.setBlendFunction(GL20.GL_DST_COLOR, GL20.GL_SRC_ALPHA);
theLightSprite.draw(batch, parentAlpha);
batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
First the blend function is changed, so we dont just render the sprite, but blend it in a lighting way on the background. After drawing it, reset the blend function to normal, so everything else after this is rendered normally again. Thats it.
首先改变了混合函数,所以我们不只是渲染精灵,而是在背景上以照明方式混合它。绘制后,将混合功能重置为正常,以便此后的所有其他内容再次正常渲染。就是这样。
Second approach uses box2dlights. Yes, we do need a box2d world object, but we dont need to do something with it. So what we do is:
第二种方法使用 box2dlights。是的,我们确实需要一个 box2d 世界对象,但我们不需要用它做任何事情。所以我们要做的是:
world = new World(new Vector2(0,0),false);
rayHandler = new RayHandler(world);
rayHandler.setCombinedMatrix(stage.getCamera().combined);
new PointLight(rayHandler,1000, Color.BLUE,radius,x_position,y_position);
First we create our world, thats just doing nothing. We only need it for the second statement, where we create our RayHandler, that calculates our lights. In the third statement we set the matrix of the rayHandler. In this case i use scene2d, and thus using the stages camera combined matrix for it. If u use another camera just use its combined matrix here. The last statement creates a pointlight with the rayHandler, and discribe the number of rays, the lights color, its radius, and its position.
首先我们创造我们的世界,那只是什么都不做。我们只需要在第二个语句中使用它,在那里我们创建我们的 RayHandler,它计算我们的灯光。在第三条语句中,我们设置了 rayHandler 的矩阵。在这种情况下,我使用了scene2d,因此使用了舞台相机组合矩阵。如果您使用另一台相机,只需在此处使用其组合矩阵。最后一条语句使用 rayHandler 创建一个点光源,并描述光线的数量、灯光颜色、半径和位置。
All we have to do now, is draw our stage or sprites in our render() method and call
我们现在要做的就是在我们的 render() 方法中绘制我们的舞台或精灵并调用
rayHandler.updateAndRender();
after that in the render method. Pretty easy.
之后在渲染方法中。挺容易。