Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

light2d pipeline ignoring container rotation for child sprites #6086

Closed
irongaze opened this issue Apr 23, 2022 · 3 comments
Closed

light2d pipeline ignoring container rotation for child sprites #6086

irongaze opened this issue Apr 23, 2022 · 3 comments

Comments

@irongaze
Copy link

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!

@irongaze
Copy link
Author

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.

@irongaze
Copy link
Author

irongaze commented May 6, 2022

If there's a better forum for discussing this issue, please point the way...

@photonstorm
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants