如何使box2dlights在环境照明中忽略纹理和精灵?例如,我有一个舞台,周围的灯光设置为黑暗。我希望我的灯照亮一个平台直接在灯光下,但后面的背景图像应该保持黑暗,而不是点燃。目前,灯光是最上面的渲染层,灯光下的所有东西都被照亮。
发布于 2015-06-06 11:02:07
实现这一目标的正确途径如下:
RayHandler的FrameBuffer中获取纹理。FrameBuffer对象,但不要在其中呈现光映射。不要渲染您的HUD或任何最顶层您不希望受到您的照明影响。FBO的呈现并开始呈现到屏幕上。Texture Units 0和1,光地图和顶层的FBO Texture。Shader,您将使用它将您的光地图与您的FBO Texture混合。混合非常简单(发生在Fragment Shader中):glFragColor = tex0.rgb * tex1.rgb,并保持tex1.a不受影响(tex0 =光图纹理,tex1 = fbo纹理)。RayHandler的环境光由于这种渲染方法而丢失,所以你可以将环境光色传递给阴影,并将它添加到光地图通道中。Texture Unit,以便正确地完成剩余的呈现(TEXTURE_0):呈现任何剩余的最顶层层和HUD (如果有的话)。一些示例代码:
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
tweenManager.update(delta);
worldUpdate(delta);
/* We have three cameras (foreground + double parallax background) */
moveForegroundCamera(player.getPosition().x, player.getPosition().y);
moveBackground0Camera(player.getPosition().x, player.getPosition().y);
moveBackground1Camera(player.getPosition().x, player.getPosition().y);
cameraMatrixCopy.set(foregroundCamera.combined);
rayHandler.setCombinedMatrix(cameraMatrixCopy.scale(Globals.BOX_TO_WORLD, Globals.BOX_TO_WORLD, 1.0f), foregroundCamera.position.x,
foregroundCamera.position.y, foregroundCamera.viewportWidth * camera.zoom * Globals.BOX_TO_WORLD,
foregroundCamera.viewportHeight * foregroundCamera.zoom * Globals.BOX_TO_WORLD);
rayHandler.update();
rayHandler.render();
lightMap = rayHandler.getLightMapTexture();
fbo.begin();
{
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
/* Draw the second background (affected by lights), the player, the enemies and all the objects */
batch.enableBlending();
batch.setProjectionMatrix(background1Camera.combined);
batch.begin();
background1.draw(batch);
batch.end();
batch.setProjectionMatrix(foregroundCamera.combined);
batch.begin();
// Draw stuff...
batch.end();
}
fbo.end();
/* Now let's pile things up: draw the bottom-most layer */
batch.setProjectionMatrix(background0Camera.combined);
batch.disableBlending();
batch.begin();
background0.draw(batch);
batch.end();
/* Blend the frame buffer's texture with the light map in a fancy way */
Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
fboRegion.getTexture().bind(); // fboRegion = new TextureRegion(fbo.getColorBufferTexture());
Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE1);
lightMap.bind();
Gdx.gl20.glEnable(Gdx.gl20.GL_BLEND);
Gdx.gl20.glBlendFunc(Gdx.gl20.GL_SRC_ALPHA, Gdx.gl20.GL_ONE_MINUS_SRC_ALPHA);
lightShader.begin();
lightShader.setUniformf("ambient_color", level.getAmbientLightColor());
lightShader.setUniformi("u_texture0", 0);
lightShader.setUniformi("u_texture1", 1);
fullScreenQuad.render(lightShader, GL20.GL_TRIANGLE_FAN, 0, 4);
lightShader.end();
Gdx.gl20.glDisable(Gdx.gl20.GL_BLEND);
Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0); // Bind again the default texture unit
/* Draw any top-most layers you might have */
hud.draw();
}
https://stackoverflow.com/questions/25047541
复制相似问题