engine icon indicating copy to clipboard operation
engine copied to clipboard

camera.screenToWorld calculates incorrectly for orthographic camera until first update is done

Open OlegGedzjuns opened this issue 2 years ago • 1 comments

Description

Example: https://playcanvas.com/project/1163442/overview/incorrect-screentoworld (projection and orthographic cameras. Both calculate the left center screen border screenToWorld position 4 times. During initialize, postInitialize, first and second update)

  1. launch scene
  2. open dev tools
  3. reload page
  4. check logs

camera with perspective projection calculates world position correctly all 4 times

camera with orthographic projection calculates incorrectly during initialize and postInitialize on the first update only y is calculated correctly on the second and all subsequent updates x and y are calculated correctly

image

Platform: Windows Browser: Chrome

OlegGedzjuns avatar Nov 15 '23 09:11 OlegGedzjuns

Okay so I forked it and it's due to when the aspect ratio and the client bounding rect are set.

  • The client rect is set in the application update method before the script component update method.
  • The aspect ratio is set in the application render method when updating the camera and happens in the tick handler after the application update is called.
  • Both these occur after initialize and postInitialize since they are called in the application start method which creates the ticking process.

So the order is:

  1. initialize - Aspect ratio = DEFAULT, Rect = DEFAULT
  2. postInitialize - Aspect ratio = DEFAULT, Rect = DEFAULT
  3. first update- Aspect ratio = DEFAULT, Rect = VALUE
  4. second update - Aspect ratio = VALUE, Rect = VALUE

The reason why perspective is all correct but orthographic isn't is because your near plane is set to 0. This completely zeros out the halfSize vector when calculating the screenToWorld conversion (see src/scene/camera.js).

kpal81xd avatar Nov 15 '23 23:11 kpal81xd