首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Box2dlights -分层灯

Box2dlights -分层灯
EN

Stack Overflow用户
提问于 2014-07-30 21:45:58
回答 1查看 1.5K关注 0票数 4

如何使box2dlights在环境照明中忽略纹理和精灵?例如,我有一个舞台,周围的灯光设置为黑暗。我希望我的灯照亮一个平台直接在灯光下,但后面的背景图像应该保持黑暗,而不是点燃。目前,灯光是最上面的渲染层,灯光下的所有东西都被照亮。

EN

回答 1

Stack Overflow用户

发布于 2015-06-06 11:02:07

实现这一目标的正确途径如下:

  1. 更新你的物理和相机。
  2. 渲染光图,以便稍后可以从RayHandlerFrameBuffer中获取纹理。
  3. 按照所需的顺序,将顶层呈现为透明的FrameBuffer对象,但不要在其中呈现光映射。不要渲染您的HUD或任何最顶层您不希望受到您的照明影响。
  4. 完成对FBO的呈现并开始呈现到屏幕上。
  5. 渲染不受灯光影响的背景。
  6. 绑定到Texture Units 0和1,光地图和顶层的FBO Texture
  7. 开始一个Shader,您将使用它将您的光地图与您的FBO Texture混合。混合非常简单(发生在Fragment Shader中):glFragColor = tex0.rgb * tex1.rgb,并保持tex1.a不受影响(tex0 =光图纹理,tex1 = fbo纹理)。RayHandler的环境光由于这种渲染方法而丢失,所以你可以将环境光色传递给阴影,并将它添加到光地图通道中。
  8. 将纹理单元绑定到着色器并执行呈现。此呈现必须在启用alpha混合(SRC_ALPHA、ONE_MINUS_SRC_ALPHA)的情况下完成。
  9. 再次绑定默认的Texture Unit,以便正确地完成剩余的呈现(TEXTURE_0):呈现任何剩余的最顶层层和HUD (如果有的话)。

一些示例代码:

代码语言:javascript
复制
@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();

    }

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25047541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档