screenToWorld weird output

Hi all,

So in my project I ran in to some screenToWorld problem.
It is not working like I am expecting.

I made a new empty project to test out a few simple lines of code.
There is a camera located at the world origin (0,0,0) and has no rotation (0,0,0).
This is the code attached to the camera entity:

// initialize code called once per entity
Camera.prototype.initialize = function () {

    var center = this.entity.camera.screenToWorld(window.innerWidth / 2, window.innerHeight / 2, 10);

    console.log(center);
    // logs:
    // "x": 7.820499677654496,
    // "y": -5.661549388473711,
    // "z": -2.6051186755460614
};

Could somebody explain why it logs those values and not:

// "x": 0,
// "y": 0,
// "z": -10

Maybe my understanding of screenToWorld() function is just all wrong.

Here is the project if you want to look around.
https://playcanvas.com/project/1021903/

Looks like it’s an issue where the clientRect on graphicsDevice is much smaller size for the first frame on initialize :thinking:

Doing it in update on the first frame is fine as the rect is updated before the scripts update are called.

2 Likes