Question by
dmm_jbw · Apr 20, 2017 at 07:49 PM ·
canvasmathworldspace
World Space to Local Space on 3D Canvas
I've searched for the answer to this, but haven't quite found what I'm looking for. I'm terrible at math and not quite sure how to do what I want.
I have a 3D canvas in world space and I'm getting a raycast hit on to it (also world space obviously). What I need to do is get that hit position in the local space of the canvas it's hitting. Ideally, I'd like to put that into normalized x, y coordinates on the canvas.
Any help?
Comment
Answer by dmm_jbw · Apr 20, 2017 at 08:29 PM
I have it working. Not sure if this is the best way to do it, but it works for my purposes.
var localSpacePoint = RawImage.rectTransform.transform.InverseTransformPoint(worldPoint);
var corners = new Vector3[4];
RawImage.rectTransform.GetWorldCorners(corners);
var localSpaceCorner = RawImage.rectTransform.transform.InverseTransformPoint(corners[0]);
var delta = localSpacePoint - localSpaceCorner;
// Normalize points (0-1) within the canvas
var normalX = delta.x / RawImage.rectTransform.rect.width;
var normalY = delta.y / RawImage.rectTransform.rect.height;