public void OnPreCull()
    {
        if (!Mojing.SDK.NeedDistortion && Mojing.SDK.VRModeEnabled)
        {
            GetComponent <Camera>().projectionMatrix = CreateMatrix();
        }
        else
        {
            GetComponent <Camera>().ResetProjectionMatrix();
        }
        if (Mojing.SDK.bWaitForMojingWord)
        {
            EnableEye(false);
            return;
        }
        if (GetComponent <Camera>() != null)
        {
            SetUpEye();
#if !UNITY_EDITOR && UNITY_ANDROID
            //mojing2 render directly
            if (!Mojing.SDK.NeedDistortion)
            {
                return;
            }

            if (MojingSDK.Unity_IsEnableATW())
            {
                int iFrameIndex = 0;
                if (MojingSDK.Unity_IsATW_ON())
                {
                    // Unity_ATW_GetModelFrameIndex 接口中自带睡眠代码
                    iFrameIndex = MojingSDK.Unity_ATW_GetModelFrameIndex();
                }
                switch (eye)
                {
                case Mojing.Eye.Left:
                    GetComponent <Camera>().targetTexture = MojingRender.StereoScreen[iFrameIndex * 2];
                    break;

                case Mojing.Eye.Right:
                    GetComponent <Camera>().targetTexture = MojingRender.StereoScreen[iFrameIndex * 2 + 1];
                    break;
                }
            }
#endif
        }
        else
        {
            MojingLog.LogError(eye.ToString() + ": no camera found.");
        }
    }
    void SetTargetTex(Mojing.Eye eye)
    {
        int iFrameIndex = 0;

        if (MojingSDK.Unity_IsATW_ON())
        {
            // Unity_ATW_GetModelFrameIndex 接口中自带睡眠代码
            iFrameIndex = MojingSDK.Unity_ATW_GetModelFrameIndex();
            MojingLog.LogTrace("Unity get iFrameIndex = " + iFrameIndex);
        }
        switch (eye)
        {
        case Mojing.Eye.Left:
            GetComponent <Camera>().targetTexture = MojingRender.StereoScreen[iFrameIndex * 2];
            MojingLog.LogTrace("Use Texture " + (iFrameIndex * 2) + " id = " + MojingRender.StereoScreen[iFrameIndex * 2].GetNativeTexturePtr());

            break;

        case Mojing.Eye.Right:
            GetComponent <Camera>().targetTexture = MojingRender.StereoScreen[iFrameIndex * 2 + 1];
            MojingLog.LogTrace("Use Texture " + (iFrameIndex * 2 + 1) + " id = " + MojingRender.StereoScreen[iFrameIndex * 2 + 1].GetNativeTexturePtr());
            break;
        }
    }
Beispiel #3
0
    //private long frameCount = 0;
    IEnumerator EndOfFrame()
    {
        //frameCount = 0;
        MojingLog.LogTrace("Enter Mojing.EndOfFrame thread");
        while (true)
        {
            yield return(new WaitForEndOfFrame());

            if ((!bWaitForMojingWord) && VRModeEnabled && NeedDistortion)
            {
                LCamera.Render();
                RCamera.Render();

                if (MojingSDK.Unity_IsATW_ON() || m_bResetTextureId)
                {
                    int iFrameIndex = 0;

                    if (MojingSDK.Unity_IsATW_ON())
                    {
                        iFrameIndex = MojingSDK.Unity_ATW_GetModelFrameIndex();
                        MojingLog.LogTrace("ATW: get iFrameIndex = " + iFrameIndex);
                    }
                    int    tID       = (int)MojingRender.StereoScreen[iFrameIndex * 2].GetNativeTexturePtr();
                    int    sID       = (int)MojingRender.StereoScreen[iFrameIndex * 2 + 1].GetNativeTexturePtr();
                    IntPtr tID_Metal = MojingRender.StereoScreen[iFrameIndex * 2].colorBuffer.GetNativeRenderBufferPtr();
                    IntPtr sID_Metal = MojingRender.StereoScreen[iFrameIndex * 2 + 1].colorBuffer.GetNativeRenderBufferPtr();
                    if (tID == 0 || sID == 0 || tID_Metal == IntPtr.Zero || sID_Metal == IntPtr.Zero)
                    {
                        // Skip a frame, the old texture id is invalid and no new texture id to use
                        //MojingLog.LogTrace("Skip a frame since texture id is zero: " + tID + "," + sID);
                        m_bResetTextureId = true;
                    }
                    else
                    {
                        if (_Last_left_TextureID != tID || _Last_right_TextureID != sID)
                        {
                            _Last_left_TextureID  = tID;
                            _Last_right_TextureID = sID;
                        }
                        if (_Last_left_TextureID_Metal != tID_Metal || _Last_right_TextureID_Metal != sID_Metal)
                        {
                            _Last_left_TextureID_Metal  = tID_Metal;
                            _Last_right_TextureID_Metal = sID_Metal;
                        }
                        //MojingLog.LogTrace("Set Texture ID: " + _Last_left_TextureID + "," + _Last_right_TextureID);

#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_ANDROID
                        if (MojingSDK.Unity_IsATW_ON() || MojingSDK.Unity_IsInterFBRPlatform())
                        {
                            MojingSDK.ATW_SetTextureID(_Last_left_TextureID, _Last_right_TextureID, headView);
                        }
                        else
                        {
                            MojingSDK.SetTextureID(_Last_left_TextureID, _Last_right_TextureID);
                        }
#elif UNITY_IOS
                        if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
                        {
                            MojingSDK.SetTextureID_Metal(tID_Metal, sID_Metal);
                        }
                        else
                        {
                            MojingSDK.SetTextureID(tID, sID);
                        }
#endif
                        MojingSDK.Unity_DistortFrame();
                        if (!MojingSDK.Unity_IsATW_ON() && --m_ResetCount == 0)
                        {
                            m_bResetTextureId = false;
                        }
                    }
                }
                else
                {
                    //MojingLog.LogTrace("Read Texture ID = " + tID + ", " + sID + " while set Texture ID: " + _Last_left_TextureID + "," + _Last_right_TextureID);

#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_ANDROID
                    if (MojingSDK.Unity_IsATW_ON() || MojingSDK.Unity_IsInterFBRPlatform())
                    {
                        MojingSDK.ATW_SetTextureID(_Last_left_TextureID, _Last_right_TextureID, headView);
                    }
                    else
                    {
                        MojingSDK.SetTextureID(_Last_left_TextureID, _Last_right_TextureID);
                    }
#elif UNITY_IOS
                    if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
                    {
                        MojingSDK.SetTextureID_Metal(_Last_left_TextureID_Metal, _Last_right_TextureID_Metal);
                    }
                    else
                    {
                        MojingSDK.SetTextureID(_Last_left_TextureID, _Last_right_TextureID);
                    }
#endif

                    MojingSDK.Unity_DistortFrame();
                }
            }
        }
        //MojingLog.LogTrace("Leave Mojing.EndOfFrame thread");
    }