Ejemplo n.º 1
0
    /// <summary>
    /// Updates the transforms.
    /// </summary>
    private void UpdateTransforms()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        MiTransform hmdLeftEye  = VrManager.Instance.Hmd.GetEyeTransform(Eyes.Left);
        MiTransform hmdRightEye = VrManager.Instance.Hmd.GetEyeTransform(Eyes.Right);

        //this.CenterEyeTransform.localRotation = hmdLeftEye.Rotation;
        //this.LeftEyeTransform.localRotation = hmdLeftEye.Rotation;
        //this.RightEyeTransform.localRotation = hmdRightEye.Rotation;
        //this.ReticleTransform.localRotation = hmdLeftEye.Rotation;
        //Debug.Log(string.Format("hmdLeftEye rotation is {0}", hmdLeftEye.Rotation));
        if (!LockRotation)
        {
            this.transform.localRotation = hmdLeftEye.Rotation;
        }

        this.CenterEyeTransform.localPosition = 0.5f * (hmdLeftEye.Position + hmdRightEye.Position);
        this.LeftEyeTransform.localPosition   = hmdLeftEye.Position;
        this.RightEyeTransform.localPosition  = hmdRightEye.Position;
        this.ReticleTransform.localPosition   = hmdLeftEye.Position;
#elif UNITY_EDITOR || UNITY_STANDALONE_WIN
        MiEmulation emulation = MiEmulation.Instance;
        transform.rotation = Quaternion.Euler(emulation.headEulerAngles.y, emulation.headEulerAngles.x, emulation.headEulerAngles.z);
        this.CenterEyeTransform.localPosition = Vector3.zero;
        this.LeftEyeTransform.localPosition   = new Vector3(-emulation.ipd * 0.5f, 0.0f, 0.0f);
        this.RightEyeTransform.localPosition  = new Vector3(emulation.ipd * 0.5f, 0.0f, 0.0f);

        this.CenterEyeTransform.localRotation = Quaternion.identity;
        this.LeftEyeTransform.localRotation   = Quaternion.identity;
        this.RightEyeTransform.localRotation  = Quaternion.identity;
#endif
    }
Ejemplo n.º 2
0
    /// <summary>
    /// OnPreCull is called before a camera culls the scene.
    /// </summary>
    private void OnPreCull()
    {
        MiEmulation emulation    = MiEmulation.Instance;
        float       realAspect   = (float)Screen.width / Screen.height;
        float       deviceAspect = emulation.screenSize.x / emulation.screenSize.y;

        this.cam.orthographicSize = 0.5f * Mathf.Max(1.0f, deviceAspect / realAspect);
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Calculate the distortion mesh position and uv
    /// </summary>
    /// <param name="vertices">mesh vertex positions.</param>
    /// <param name="texcoords">mesh vertex texcoords.</param>
    private void CalcMeshVertices(out Vector3[] vertices, out Vector2[] texcoords)
    {
        MiEmulation emulation = MiEmulation.Instance;

        float[] frustumFromLens = new float[4];
        float[] bareFrustum     = new float[4];
        emulation.GetFrustum(frustumFromLens, true);
        emulation.GetFrustum(bareFrustum, false);
        Rect viewport;

        viewport  = emulation.GetViewport(bareFrustum);
        vertices  = new Vector3[2 * distortionMeshWidth * distortionMeshHeight];
        texcoords = new Vector2[2 * distortionMeshWidth * distortionMeshHeight];
        int vertexIndex = 0;

        // Caculate left, then right
        for (int side = 0; side < 2; side++)
        {
            for (int row = 0; row < distortionMeshHeight; row++)
            {
                for (int col = 0; col < distortionMeshWidth; col++)
                {
                    float u = (float)col / (distortionMeshWidth - 1.0f);
                    float v = (float)row / (distortionMeshHeight - 1.0f);

                    // do barrel distortion - calc the vertex position in normalized way
                    float tanAngleX   = Mathf.Lerp(frustumFromLens[0], frustumFromLens[2], u);
                    float tanAngleY   = Mathf.Lerp(frustumFromLens[3], frustumFromLens[1], v);
                    float distorted   = Mathf.Sqrt(tanAngleX * tanAngleX + tanAngleY * tanAngleY);
                    float undistorted = emulation.DistortInverse(distorted);
                    float x           = tanAngleX * undistorted / distorted;
                    float y           = tanAngleY * undistorted / distorted;
                    x = (x - bareFrustum[0]) / (bareFrustum[2] - bareFrustum[0]);
                    y = (y - bareFrustum[3]) / (bareFrustum[1] - bareFrustum[3]);

                    // Convert to screen space
                    x = (viewport.x + x * viewport.width - 0.5f) * emulation.screenSize.x / emulation.screenSize.y;
                    y = viewport.y + y * viewport.height - 0.5f;
                    vertices[vertexIndex] = new Vector3(x, y, 1.0f);

                    // Modify 's' according the the left or the right side of the mesh.
                    u = (u + side) * 0.5f;
                    texcoords[vertexIndex] = new Vector2(u, v);
                    vertexIndex++;
                }
            }

            // switch to the right side
            float w = frustumFromLens[2] - frustumFromLens[0];
            frustumFromLens[0] = -(w + frustumFromLens[0]);
            frustumFromLens[2] = w - frustumFromLens[2];
            w = bareFrustum[2] - bareFrustum[0];
            bareFrustum[0] = -(w + bareFrustum[0]);
            bareFrustum[2] = w - bareFrustum[2];
            viewport.x     = 1.0f - (viewport.x + viewport.width);
        }
    }