Wednesday, December 25, 2024
spot_img

translate from World to Display screen pos? – Cocos Creator


Regardless of the completely different assets and examples, I’ve not been capable of perceive find out how to translate a world pos to a display pos.

I’ve a easy scene with a dice positioned in [0, 0, 0] and a perspective digital camera positioned in [0, 0, 10], wanting in the direction of the dice.

That is how I translate the dice place:

        const cubePos = this.dice.place;
        const screenPos = this.Camera3D.worldToScreen(cubePos);

        console.log("Dice display place X is " + screenPos.x);

Now, after I place the dice on the left nook of the display, the X worth being logged in 0, which I suppose is smart as a result of the operate makes use of a bottom-left anchor.

But when I place the dice on the suitable aspect of the display, the worth turns into one thing near 280. Why 280? I’m taking part in in 1080×1920.

Can somebody give me some perception of how that is speculated to work?

Please observe that worldToScreen has two params you must enter.

The consequence is identical regarless of what I do:

This:

        const screenPos = this.Camera3D.worldToScreen(cubePos);

This:

        let pos = new Vec3();
        this.Camera3D.worldToScreen(cubePos, pos);

And even this:

        let pos = new Vec3();
        this.Camera3D.digital camera.worldToScreen(pos, cubePos);

give the identical consequence.

What I don’t perceive is how is the worldToScreen calculated and what’s this “280” referring to.

Okay I’ve lastly understood. The worldToScreen() operate calculates the place in response to the ACTUAL window measurement. I’m testing the sport within the editor preview. That implies that if I zoom in/out the sport preview, the calculated display pos will change as properly.

So I’ve to compensate with a ratio between the seen view measurement and the display measurement:


        let pos = new Vec3();
        this.Camera3D.worldToScreen(new Vec3(cubePos.x, cubePos.y, cubePos.z), pos);


        console.log(pos.x * (view.getVisibleSize().x / display.windowSize.x)); // This logs a price between 0 and my precise view measurement, 1080 in my case.

Is that this conduct actually supposed? :slight_smile:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles