Unity camera.ViewportToWorldPoint for Zero Parallax

Hey zSpace,

Can you please explain how I can get the 3D world coordinates of the Zero Parallax?

More specifically, I would like to use the method camera.ViewportToWorldPoint() to get the coordinates on the zero parallax plane in 3D world space.

Thanks in advance.

Hi there,

I dug up a script called canvasToViewportAlign.cs that has been used to match a world space UI canvas to the zero parallax plane.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using zSpace.Core;

public class canvasToViewportAlign : MonoBehaviour {

    private ZCore core;

    void Start(){
        core = GameObject.FindObjectOfType<ZCore>();
        var viewportCenter   = core.GetViewportWorldCenter();
        var viewportRotation = core.GetViewportWorldRotation();
        var viewportSizePixels = core.GetViewportSize();
        var viewportSizeMeters = core.GetViewportWorldSize();

        this.transform.position = viewportCenter;
        this.transform.rotation = viewportRotation;

        // Based on Unity document,
        // passing the meter to pixel value as scale for the canvas to fit in current screen.
        var scale = new Vector2(viewportSizeMeters.x / viewportSizePixels.x,
                                viewportSizeMeters.y / viewportSizePixels.y);

        var resolution = new Vector2(viewportSizePixels.x, viewportSizePixels.y);

        const float SmallScaleZ = 0.0002f;

        Vector3 localScale = new Vector3(scale.x, scale.y, SmallScaleZ);
        this.transform.localScale = localScale;

        var rect = this.GetComponent<RectTransform>();
        rect.sizeDelta = resolution;
        Vector2 center = new Vector2(0.5f, 0.5f);
        rect.pivot = center;
    }

	void Update () {
        transform.position = core.GetViewportWorldCenter();
        transform.rotation = core.GetViewportWorldRotation();
	}
}

Feel free to use this as reference.

From here, you could use RectTransformUtility.ScreenPointToWorldPointInRectangle() to translate from screen coordinates to world zero parallax coordinates.