Jump to content

Any way to dynamically get canvas coordinates of a sprite?


mpwassler
 Share

Recommended Posts

I have a sprite in my scene which the user clicks on and then the camera moves. The problem is I need to overlay an html element on that sprite after the camera moves. Is there any way to project the sprite position to x, y canvas coordinates? The Vector3.Project function doesn't seem to work for me beacuse I can't compute the worldMatrix from the sprite ( at least not that I know of ).

Here is what I tried. The this context here is the sprite.

var transformationMatrix = camera.getViewMatrix().multiply(camera.getProjectionMatrix());
var position = this.position
var projectedPosition = BABYLON.Vector3.Project(position, this.computeWorldMatrix(true), transformationMatrix, camera.viewport.toGlobal(camera.getEngine()));

Thanks!

Link to comment
Share on other sites

Never mind I figured this out my use of the project method was simply wrong. This works!


var position = this.position
var projectedPosition = BABYLON.Vector3.Project(
    position,  
    BABYLON.Matrix.Identity(), 
    this.scene.getTransformMatrix(), 
    camera.viewport.toGlobal(990, 450) // size of my canvas
);

 

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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