phaser
phaser copied to clipboard
light2d pipeline ignoring container rotation for child sprites
Version
- Phaser Version: 3.55.2
- Operating system: Windows 10 latest
- Browser: Firefox latest,
Description
Expected: Adding lit sprites to containers should use the cumulative/world transform during lighting calculations Actual Behavior: Lit sprites in rotating containers display fixed lighting
Details: Adding a sprite + normal map and setting the sprite to the light2d pipeline works as expected - when rotating/moving the sprite, the angle to the light determines lighting intensity.
However, if you take the same sprite + normal map and add it as the child of a container, the container's rotation is ignored when light calculation occurs, resulting in fixed lighting.
Example Test Code
Codepen here: https://codepen.io/zaphnod/pen/OJzGKZB
Relevant setup:
function create() {
// Create ship container
ship = this.add.container(400, 450);
// Add ship body to container
shipBody = this.add.sprite(0, 0, 'ship');
shipBody.setPipeline('Light2D');
ship.add(shipBody);
// Create second ship at scene root
ship2 = this.add.sprite(1200, 450, 'ship');
ship2.setPipeline('Light2D');
this.lights.enable().setAmbientColor(0x333333);
// Add light at center of scene
sun = this.lights.addLight(800, 450, 2000, 0xffAA00);
}
// Rotate container + ship2
var timer = 0;
function update(time, delta) {
timer += delta;
ship.setAngle(timer / 20 % 360);
ship2.setAngle(timer / 20 % 360);
}
Additional Information
Seems likely that the lighting engine is passing in the object's local rotation to the shader fragment, rather than the full computed transform. I'll try to dig into the internals and get a better theory but I'm new to Phaser so would appreciate any pointers/assistance!
OK, so the issue as expected is that the lighting pipeline is naively calling LightPipeline.setNormalMapRotation()
with the object's parent-relative rotation, rather than its world-space transform rotation.
The quick and dirty solution would be to instead use object.getWorldTransformMatrix().rotation
instead, but as there doesn't seem to be any caching of the cumulative transform (e.g. during scene graph update), running that calc on a per-object basis would likely have a negative performance impact. At this point, I'm going to need some guidance from the core team as to how best approach a solution in the Phaser Way.
If there's a better forum for discussing this issue, please point the way...
Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master
branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.