camera.screenToWorld calculates incorrectly for orthographic camera until first update is done
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)
- launch scene
- open dev tools
- reload page
- 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
Platform: Windows Browser: Chrome
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
updatemethod before the script componentupdatemethod. - The aspect ratio is set in the application
rendermethod when updating the camera and happens in the tick handler after the applicationupdateis called. - Both these occur after
initializeandpostInitializesince they are called in the applicationstartmethod which creates the ticking process.
So the order is:
- initialize - Aspect ratio = DEFAULT, Rect = DEFAULT
- postInitialize - Aspect ratio = DEFAULT, Rect = DEFAULT
- first update- Aspect ratio = DEFAULT, Rect = VALUE
- 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).