Exemplo n.º 1
0
    // store the matrix values, any updates will happen on the update loop
    public void UpdateCameraMatrices(Spatial4x4 transform, Spatial4x4 projection)
    {
        // store matrix information from the sample
        if (cameraTransform != Spatial4x4.Zero)
        {
            // always track the last known good transform
            lastGoodCameraTransform = cameraTransform;
        }
        cameraTransform = transform;

        if (cameraProjection != Spatial4x4.Zero)
        {
            lastGoodCameraProjection = cameraProjection;
        }
        cameraProjection = projection;

        // get last known good transform
        Matrix4x4?transformMatrix = null;

        if (cameraTransform != Spatial4x4.Zero)
        {
            transformMatrix = cameraTransform.ToUnityTransform();
        }
        else if (lastGoodCameraTransform != Spatial4x4.Zero)
        {
            transformMatrix = lastGoodCameraTransform.ToUnityTransform();
        }

        // swap the projection for the one sent to us
        UnityEngine.Matrix4x4?projectionMatrix = null;
        if (cameraProjection != Spatial4x4.Zero)
        {
            projectionMatrix = cameraProjection.ToUnity();
        }
        else if (lastGoodCameraProjection != Spatial4x4.Zero)
        {
            projectionMatrix = cameraProjection.ToUnity();
        }

        if (transformMatrix == null || projectionMatrix == null)
        {
            return;
        }

        // set the real worl position to PV camera pose
        if (transformMatrix.Value.ValidTRS())
        {
            gameObject.transform.position = transformMatrix.Value.GetColumn(3);
            gameObject.transform.rotation = Quaternion.LookRotation(transformMatrix.Value.GetColumn(2), transformMatrix.Value.GetColumn(1));
        }

        if (ImageCorners == null)
        {
            ImageCorners = new Vector3[4];
        }

        ImageCenter = WorldPoint(new Vector2(.5f, .5f), transformMatrix.Value, projectionMatrix.Value);

        for (int i = 0; i < viewport.Length; i++)
        {
            ImageCorners[i] = WorldPoint(viewport[i], transformMatrix.Value, projectionMatrix.Value);
        }
    }