Jump to content

Convert space to screen coordinates


Recommended Posts

Hi guys,

I'm trying to make 2D-shapes / 2D-sprites in my Canvas2D (ScreenSpaceCanvas) follow some sprites which are placed in my 3d world. 

For this, I'm using the BABYLON.Vector3.Project function as followed: 


BABYLON.Vector3.Project(space.position, BABYLON.Matrix.Identity(), scene.getTransformMatrix(), scene.activeCamera.viewport)

However, this is the result: 


Vector3 {x: NaN, y: NaN, z: NaN}

I've checked each of the arguments and they all contain valid values at the time of being called.

I've also stumbled across this topic, which uses another 4th argument in the call, but I've had no luck trying it with that one either. 


Any ideas? Thanks in advance!

Link to comment
Share on other sites

For whatever reason, it works now. I can't say why; I changed a couple of things in between and lost track of the things I've altered; been coding for too long non-stop now. ;)

I'm accessing the viewport via scene.activeCamera.viewport, which results in screen coordinates between 0 and 1. 

To get pixel values in order to place my Text2D or Shape2Ds, I do


mytext2D.x = canvas2D.width * projectVec.x; 

mytext2D.y = canvas2D.height - canvas2D.height * projectVec.y;   

for anyone who's interested. 


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...