libgdx / box2dlights

Fork of box2dlights by Kalle Hamalainen
Apache License 2.0
257 stars 82 forks source link

Port pseudo3d branch into current implementation and refactor RayHandler configuration #124

Closed fgnm closed 8 months ago

fgnm commented 3 years ago

This is a huge update of box2dLights as I've ported all the works made by @rinold and @kdenzel about pseudo3d lights into current box2dLights implementation. I've also fixed some minor issue with RayHandler configuration by introducing RayHandlerOptions object to initialize diffuse, pseudo3d and gammaCorrection. This means breaking change as I've removed static setters and static public variables.

The result is however much more clean and easy to understand, this is an example how to use new configuration:

RayHandlerOptions options = new RayHandlerOptions();
options.setDiffuse(true);
options.setGammaCorrection(true);
options.setPseudo3d(true);

rayHandler = new RayHandler(world, options);
rayHandler.setAmbientLight(0f, 0f, 0f, 0.5f);
rayHandler.setBlurNum(3);
rayHandler.setShadows(true);

Removing static functions makes also possible to switch diffuse, gammaCorrection and pseudo3d at runtime without re-create FBOs (as point out in #123). To do that I've add the following functions to RayHandler:

rayHandler.setDiffuseLight(boolean);
rayHandler.applyGammaCorrection(boolean);
rayHandler.setPseudo3dLight(boolean, boolean);

What's working in pseudo3d

Chain Lights are not supported yet.

Test pseudo3d

This works quite different from base lights, as each shadow is mesh and rendered after the main light mesh. Lights and objects now has the height parameter needed to get the pseudo3d effect. Light's height can be easily set with light.setHeight(lightHeight), while ANY fixture that has to be recognized with a pseudo3d light must have a custom user data attached: body.createFixture(fixtureDef).setUserData(new LightData(objectHeight));.

A working example could be found in Box2dLightTest2 class.

Tested on desktop and Android without notable issue.

immagine

I know that this is a big PR and actually no one is working actively on box2dLights, but merging this might encourage others to contribute and extend support to pseudo3d features. A side from RayHandlerOptions, pseudo3d functionality does not create disturb to normal box2dLights as it continue working as usual.

earthduders commented 2 years ago

Works flawlessly to implement in my LibGDX liftoff project. I am so thankful.

fgnm commented 2 years ago

@n1cken you can find a more up to date fork here where I ported normal maps too :) https://github.com/rednblackgames/hyperlap2d-runtime-libgdx/tree/master/src/main/java/games/rednblack/editor/renderer/box2dLight