示例#1
0
        //!
        //! Update is called once per frame
        //!
        void Update()
        {
            //Camera.main.nearClipPlane = Mathf.Max(0.1f, Vector3.Distance(Camera.main.transform.position, scene.transform.position) - VPETSettings.Instance.maxExtend * VPETSettings.Instance.sceneScale);
            //Camera.main.farClipPlane = Mathf.Max(100f,Mathf.Min(100000f, Vector3.Distance(Camera.main.transform.position, scene.transform.position) + VPETSettings.Instance.maxExtend * VPETSettings.Instance.sceneScale));
            mainController.UpdatePropertiesSecondaryCameras();

            //forward changes of the fov to the secondary (render in front) camera
            Camera frontCamera = this.transform.GetChild(0).GetComponent <Camera>();

            frontCamera.fieldOfView = this.GetComponent <Camera>().fieldOfView;

            //forward changes of the fov to the third (overlay) camera
            if (frontCamera.transform.childCount > 0)
            {
                Camera outlineCamera = frontCamera.transform.GetChild(0).GetComponent <Camera>();
                outlineCamera.fieldOfView = this.GetComponent <Camera>().fieldOfView;
            }

            //get sensor data from native Plugin on Windows
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
            Marshal.Copy(getOrientationSensorData(), orientationSensorData, 0, 4);
#endif

            if (move)
            {
                //cache rotation of attached gameObjects
                if (this.transform.childCount > 1)
                {
                    childrotationBuffer = this.transform.GetChild(1).rotation;
                }

#if !UNITY_EDITOR
#if (UNITY_ANDROID || UNITY_IOS)
#if USE_TANGO || USE_ARKIT
                newRotation = trackingTransform.rotation;
                newPosition = trackingTransform.position;
#else
                newRotation = gyroAdapter.Rotation;
#endif
#elif UNITY_STANDALONE_WIN
                newRotation = Quaternion.Euler(90, 90, 0) * convertRotation(new Quaternion(orientationSensorData[0], orientationSensorData[1], orientationSensorData[2], orientationSensorData[3]));
#endif
#endif
                if (doApplyRotation)
                {
                    if (!firstApplyTransform)
                    {
                        if (!mainController.arMode)
                        {
                            calibrate(rotationFirst);
                            positionOffset = positionFirst - newPosition;
                        }
                        firstApplyTransform = true;
                    }
                    //grab sensor reading on current platform
#if !UNITY_EDITOR
        #if (UNITY_ANDROID) && !(USE_TANGO || UNITY_IOS)
                    transform.localRotation = rotationOffset * Quaternion.Euler(0, 0, 55) * newRotation;
        #elif UNITY_STANDALONE_WIN
                    transform.rotation = rotationOffset * newRotation;
        #else
                    if (TangoBuild4LenovoPhab2)
                    {
                        transform.rotation = rotationOffset * new Quaternion(-newRotation.y, newRotation.x, newRotation.z, newRotation.w);
                    }
                    else
                    {
                        transform.rotation = rotationOffset * newRotation;
                    }
                    // HACK: to block roll
                    if (!mainController.arMode)
                    {
                        transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, 0);
                    }
                    //transform.rotation *= newRotation * Quaternion.Inverse(oldRotation);
                #if USE_TANGO || USE_ARKIT
                    cameraParent.position += rotationOffset * (newPosition - oldPosition);
                #endif
        #endif
#endif
                }
                else if (firstApplyTransform)
                {
                    rotationFirst       = rotationOffset * newRotation;
                    positionFirst       = positionOffset + newPosition;
                    firstApplyTransform = false;
                }

#if USE_TANGO || USE_ARKIT
                oldPosition = trackingTransform.position;
                oldRotation = trackingTransform.rotation;
#endif
                //reset rotation of attached gameObjects and send update to server if neccessary
                if (this.transform.childCount > 1)
                {
                    this.transform.GetChild(1).rotation = childrotationBuffer;
                    if (this.transform.GetChild(1).position != lastPosition)
                    {
                        //only sends updates every 30 times per second (at most)
                        if ((Time.time - lastUpdateTime) >= updateIntervall)
                        {
                            lastUpdateTime = Time.time;
                            lastPosition   = this.transform.GetChild(1).position;
                            // serverAdapter.sendTranslation(this.transform.GetChild(1) );
                            serverAdapter.SendObjectUpdate(this.transform.GetChild(1));
                        }
                    }
                }

                if (joystickAdapter)
                {
                    Vector3 val = joystickAdapter.getTranslation();
                    if (val.magnitude > 0.01)
                    {
                        if (joystickAdapter.moveCameraActive && !mainController.arMode)
                        {
                            mainController.moveCameraObject(val);
                        }
                        if (joystickAdapter.moveObjectActive)
                        {
                            mainController.translateSelectionJoystick(val);
                        }
                        if (joystickAdapter.rotateObjectActive)
                        {
                            mainController.rotateSelectionJoystick(val);
                        }
                        if (joystickAdapter.scaleObjectActive)
                        {
                            mainController.scaleSelectionJoystick(val);
                        }
                        if (mainController.UIAdapter.LayoutUI == layouts.ANIMATION)
                        {
                            mainController.AnimationController.setKeyFrame();
                        }
                    }
                    joystickAdapter.getButtonUpdates();
                }
            }

            //smoothly "fly" the camera to a given position
            if (smoothTranslationActive)
            {
                transform.position = Vector3.Lerp(transform.position, targetTranslation, Time.deltaTime * translationDamping);
                //if the position is nearly reached, stop
                if (Vector3.Distance(transform.position, targetTranslation) < 0.0001f)
                {
                    transform.position      = targetTranslation;
                    smoothTranslationActive = false;
                }
                //if 3 seconds have past, stop (avoids infinit translation for unreachable points)
                if ((Time.time - smoothTranslateTime) > 3.0f)
                {
                    smoothTranslationActive = false;
                }
            }

            //calculate & display frames per second
            if (VPETSettings.Instance.debugMsg)
            {
                timeleft -= Time.deltaTime;
                accum    += Time.timeScale / Time.deltaTime;
                ++frames;

                // Interval ended - update GUI text and start new interval
                if (timeleft <= 0.0)
                {
                    // display two digits
                    float  fps    = accum / frames;
                    string format = System.String.Format("{0:F2} FPS", fps);
                    fpsText  = format;
                    fpsText += " LiveView IP: " + VPETSettings.Instance.serverIP;
                    fpsText += " State: " + mainController.ActiveMode.ToString();
                    fpsText += " DeviceType: " + SystemInfo.deviceType.ToString();
                    fpsText += " DeviceName: " + SystemInfo.deviceName.ToString();
                    fpsText += " DeviceModel: " + SystemInfo.deviceModel.ToString();
                    fpsText += " SupportGyro: " + SystemInfo.supportsGyroscope.ToString();
                    fpsText += " DataPath: " + Application.dataPath;
                    fpsText += " PersistPath: " + Application.persistentDataPath;
                    fpsText += " Config1: " + Application.dataPath + "/VPET/editing_tool.cfg";
                    fpsText += " Config2: " + Application.persistentDataPath + "/editing_tool.cfg";
                    fpsText += " Mouse Active: " + mainController.MouseInputActive;
                    fpsText += " Touch Active: " + mainController.TouchInputActive;
                    fpsText += " Renderpath:" + Camera.main.renderingPath;
                    fpsText += " ActualRenderpath:" + Camera.main.actualRenderingPath;
                    fpsText += " Msg:" + VPETSettings.Instance.msg;
                    accum    = 0.0f;
                    frames   = 0;
                    timeleft = updateInterval;
                }
            }
        }
示例#2
0
文件: MoveCamera.cs 项目: iVerb/VPET
        //!
        //! Update is called once per frame
        //!
        void Update()
        {
            mainController.UpdatePropertiesSecondaryCameras();

            //get sensor data from native Plugin on Windows
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
            Marshal.Copy(getOrientationSensorData(), orientationSensorData, 0, 4);
#endif

            if (move)
            {
                //cache rotation of attached gameObjects
                if (this.transform.childCount > 1)
                {
                    childrotationBuffer = this.transform.GetChild(1).rotation;
                }

#if !UNITY_EDITOR
#if (UNITY_ANDROID || UNITY_IOS)
#if USE_TANGO || USE_ARKIT
                newRotation = trackingTransform.rotation;
                newPosition = trackingTransform.position;
#else
                newRotation = gyroAdapter.Rotation;
#endif
#elif UNITY_STANDALONE_WIN
                newRotation = Quaternion.Euler(90, 90, 0) * convertRotation(new Quaternion(orientationSensorData[0], orientationSensorData[1], orientationSensorData[2], orientationSensorData[3]));
#endif
#endif
                if (doApplyRotation)
                {
                    if (!firstApplyTransform)
                    {
                        if (!mainController.arMode)
                        {
                            calibrate(rotationFirst);
                            positionOffset = positionFirst - newPosition;
                        }
                        firstApplyTransform = true;
                    }
                    //grab sensor reading on current platform
#if !UNITY_EDITOR
#if (UNITY_ANDROID) && !(USE_TANGO || UNITY_IOS)
                    transform.localRotation = rotationOffset * Quaternion.Euler(0, 0, 55) * newRotation;
#elif UNITY_STANDALONE_WIN
                    transform.rotation = rotationOffset * newRotation;
#else
                    if (TangoBuild4LenovoPhab2)
                    {
                        transform.rotation = rotationOffset * new Quaternion(-newRotation.y, newRotation.x, newRotation.z, newRotation.w);
                    }
                    else
                    {
                        transform.rotation = rotationOffset * newRotation;
                    }
                    // HACK: to block roll
                    if (!mainController.arMode && mainController.ActiveMode != MainController.Mode.lookThroughLightMode)
                    {
                        transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, 0);
                    }
                    //transform.rotation *= newRotation * Quaternion.Inverse(oldRotation);
#if USE_TANGO || USE_ARKIT
                    cameraParent.position += rotationOffset * (newPosition - oldPosition);
#endif
#endif
#endif
                }
                else if (firstApplyTransform)
                {
                    rotationFirst       = rotationOffset * newRotation;
                    positionFirst       = positionOffset + newPosition;
                    firstApplyTransform = false;
                }

#if USE_TANGO || USE_ARKIT
                oldPosition = trackingTransform.position;
                oldRotation = trackingTransform.rotation;
#endif

                if (joystickAdapter)
                {
                    Vector3 val = joystickAdapter.getTranslation();
                    if (val.magnitude > 0.01)
                    {
                        if (joystickAdapter.moveCameraActive && !mainController.arMode)
                        {
                            mainController.moveCameraObject(val);
                        }
                        if (joystickAdapter.moveObjectActive)
                        {
                            mainController.translateSelectionJoystick(val);
                        }
                        if (joystickAdapter.rotateObjectActive)
                        {
                            mainController.rotateSelectionJoystick(val);
                        }
                        if (joystickAdapter.scaleObjectActive)
                        {
                            mainController.scaleSelectionJoystick(val);
                        }
                        if (mainController.UIAdapter.LayoutUI == layouts.ANIMATION)
                        {
                            mainController.AnimationController.setKeyFrame();
                        }
                    }
                    joystickAdapter.getButtonUpdates();
                }
            }

            //smoothly "fly" the camera to a given position
            if (smoothTranslationActive)
            {
                transform.position = Vector3.Lerp(transform.position, targetTranslation, Time.deltaTime * translationDamping);
                //if the position is nearly reached, stop
                if (Vector3.Distance(transform.position, targetTranslation) < 0.0001f)
                {
                    transform.position      = targetTranslation;
                    smoothTranslationActive = false;
                }
                //if 3 seconds have past, stop (avoids infinit translation for unreachable points)
                if ((Time.time - smoothTranslateTime) > 3.0f)
                {
                    smoothTranslationActive = false;
                }
            }

            if (mainController.ActiveMode == MainController.Mode.lookThroughCamMode ||
                mainController.ActiveMode == MainController.Mode.lookThroughLightMode)
            {
                Transform currentSelectedTransform = mainController.getCurrentSelection().transform;
                currentSelectedTransform.position = this.transform.position;
                currentSelectedTransform.rotation = this.transform.rotation;
            }

            //calculate & display frames per second
            if (VPETSettings.Instance.debugMsg)
            {
                timeleft -= Time.deltaTime;
                accum    += Time.timeScale / Time.deltaTime;
                ++frames;

                // Interval ended - update GUI text and start new interval
                if (timeleft <= 0.0)
                {
                    // display two digits
                    float  fps    = accum / frames;
                    string format = System.String.Format("{0:F2} FPS", fps);
                    fpsText  = format;
                    fpsText += " LiveView IP: " + VPETSettings.Instance.serverIP;
                    fpsText += " State: " + mainController.ActiveMode.ToString();
                    fpsText += " DeviceType: " + SystemInfo.deviceType.ToString();
                    fpsText += " DeviceName: " + SystemInfo.deviceName.ToString();
                    fpsText += " DeviceModel: " + SystemInfo.deviceModel.ToString();
                    fpsText += " SupportGyro: " + SystemInfo.supportsGyroscope.ToString();
                    fpsText += " DataPath: " + Application.dataPath;
                    fpsText += " PersistPath: " + Application.persistentDataPath;
                    fpsText += " Config1: " + Application.dataPath + "/VPET/editing_tool.cfg";
                    fpsText += " Config2: " + Application.persistentDataPath + "/editing_tool.cfg";
                    fpsText += " Mouse Active: " + mainController.MouseInputActive;
                    fpsText += " Touch Active: " + mainController.TouchInputActive;
                    fpsText += " Renderpath:" + Camera.main.renderingPath;
                    fpsText += " ActualRenderpath:" + Camera.main.actualRenderingPath;
                    fpsText += " Msg:" + VPETSettings.Instance.msg;
                    accum    = 0.0f;
                    frames   = 0;
                    timeleft = updateInterval;
                }
            }
            if (supervisedObjects.Count > 0)
            {
                for (int i = supervisedObjects.Count - 1; i >= 0; i--)
                {
                    if (!supervisedObjects[i])
                    {
                        supervisedObjects.RemoveAt(i);
                        continue;
                    }
                    if (Vector3.Distance(supervisedObjects[i].transform.position, this.transform.position) > 300.0f * VPETSettings.Instance.sceneScale)
                    {
                        supervisedObjects[i].SetActive(mainController.showCam);
                        BoxCollider[] col = supervisedObjects[i].GetComponentsInParent <BoxCollider>(true);
                        if (col.Length > 0)
                        {
                            col[0].enabled = mainController.showCam;
                        }
                        supervisedObjects.RemoveAt(i);
                    }
                }
            }
        }