public RenderStereoTextureCommand(int width, int height, StereoRenderer stereoRenderer)
            : base(width, height, stereoRenderer.Scene)
        {
            this.CommandAttributes |= CommandAttributes.StereoRendering;
            this.sceneManager = stereoRenderer.Scene;
            this.stereoRenderer = stereoRenderer;
            this.stereoCamera = (StereoCamera)stereoRenderer.Camera;
            this.stereoCamera.StereoParametersChanged += RequestUpdate;

            Texture2DDescription StereoTextureDesc = new Texture2DDescription
            {
                ArraySize = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.R8G8B8A8_UNorm,
                Width = 2 * width,
                Height = height,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            };

            Texture = new Texture2D(Game.Context.Device, StereoTextureDesc);
        }
Beispiel #2
0
        private void SetupStereoCamera()
        {
            StereoCamera camera = new StereoCamera();

            camera.Translation = new Vector3(0, 0, 0);

            CameraNode cameraNode = new CameraNode(camera);

            scene.RootNode.AddChild(cameraNode);
            scene.CameraNode = cameraNode;
        }
Beispiel #3
0
    void OnEnable()
    {
#if !UNITY_EDITOR
        m_cam = GetComponent <StereoCamera>();
#endif

        int code = NvStereo.NV_InitializeApi();
        if (code != 0)
        {
            string msg = Marshal.PtrToStringAnsi(NvStereo.NV_GetApiErrorMessage(code));
            Debug.LogWarning(string.Format("NV Initialize: {0} ({1})" + (code == -144? "\nStereoscopic 3D must be enabled at nVidia Control Panel!  Restart Unity 3D after modifying nVidia settings for them to take effect." : ""), code, msg));
        }
    }
Beispiel #4
0
        private void SetupStereoCamera()
        {
            StereoCamera camera = new StereoCamera();

            // Load the right eye view matrix from a calibration file created in StereoCameraCalibration tool
            Matrix cameraRightView = Matrix.Identity;

            MatrixHelper.LoadMatrixFromXML("Wrap920_Stereo_Millimeter.xml", ref cameraRightView);

            camera.LeftView  = Matrix.CreateLookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
            camera.RightView = Matrix.Invert(cameraRightView);

            CameraNode cameraNode = new CameraNode(camera);

            scene.RootNode.AddChild(cameraNode);
            scene.CameraNode = cameraNode;
        }
Beispiel #5
0
        protected StereoRenderer(IDeviceContext deviceContext)
            : base(deviceContext)
        {
            DeviceSettings settings = Game.Context.Settings;
            Camera = new StereoCamera();
            Camera.Reset();

            stereoSourceBox = new ResourceRegion
            {
                Front = 0,
                Back = 1,
                Top = 0,
                Bottom = settings.ScreenHeight,
                Left = 0,
                Right = settings.ScreenWidth
            };

            backBuffer = Game.Context.GetBackBuffer();
            cStereoRenderer = new RenderStereoTextureCommand(settings.ScreenWidth, settings.ScreenHeight, this);
        }
Beispiel #6
0
 void Awake()
 {
     instance   = this;
     controller = GetComponent <StereoController>();
 }
Beispiel #7
0
        // 将更新眼镜和笔数据放在FixedUpdate里面是为了保证数据更新速度大于画线的速度 ,防止线跳动情况发生。但是这样会牺牲一定的帧数
        void Update()
        {
            ///if there is no line renderer of stylus then search for it
            if (stylusObject != null && stylusRenderer == null)
            {
                stylusRenderer = stylusObject.transform.GetComponent <LineRenderer>();
            }

            //编辑器状态执行逻辑
            if (Application.isEditor && !Application.isPlaying)
            {
                ///Update Stylus length here
                if (stylusRenderer != null)
                {
                    if (stylusRenderer.startWidth != stylusWidth || (stylusRenderer.GetPosition(1) != stylusRenderer.transform.position + (stylusRenderer.transform.forward * 0.1f * stylusLength)))
                    {
                        stylusRenderer.startWidth = stylusWidth;
                        stylusRenderer.endWidth   = stylusWidth;
                        stylusRenderer.SetPosition(0, stylusRenderer.transform.position);
                        stylusRenderer.SetPosition(1, stylusRenderer.transform.position + (stylusRenderer.transform.forward * 0.1f * stylusLength));                        //(we are reciveing direction in 0.1f scale and forward transform is 1)
                    }
                }
            }

            if (Application.isPlaying && voxelData.isRecievedData)
            {
                //获取按钮数据
                UpdateButton(voxelData.keyType);

                //获取视觉计算后的两个点的数据
                if (leftCamera != null && rightCamera != null)
                {
                    if (stylusObject != null)
                    {
                        stylusHead        = stylusObject.transform.GetChild(0).gameObject;
                        stylusScaleInital = stylusObject.transform.localScale;
                    }

                    if (isHeadTrackingEnabled)
                    {
                        int isTrackingGlasses = voxelData.isTrackingGlasses;

                        glassPosition = new double[] { voxelData.headPosition[0], voxelData.headPosition[1], voxelData.headPosition[2] };

                        if (currentScreenMode == ScreenMode.ScreenTilt)
                        {
                            WireDraw.transform.localRotation = Quaternion.Euler(voxelData.screenAngle, 0, 0);
                        }

                        if (stereoCameraInstance == null)
                        {
                            stereoCameraInstance = GetComponentInChildren <StereoCamera>();
                        }

                        if (!isFirstRun)
                        {
                            if (isTrackingGlasses == 1)
                            {
                                stereoCameraInstance.stereoMode = StereoCamera.StereoModes.Active;
                            }
                            else
                            {
                                stereoCameraInstance.stereoMode = StereoCamera.StereoModes.Disabled;
                            }
                        }
                        else
                        {
                            isFirstRun = false;
                        }

                        UpdateDataToMeter(ref glassPosition);                                                                                      //将单位从毫米换算成米
                        UpdateCameraPose(new Vector3((float)glassPosition[0] - 0.26f, -(float)glassPosition[1] + 0.05f, (float)glassPosition[2])); //更新眼镜位置
                    }

                    if (currentScreenMode == ScreenMode.LookAt)
                    {
                        sceneCamera.LookAt(centerObject);
                    }
                }

                stylusPositionOne = new double[] { voxelData.penAtPt1[0], voxelData.penAtPt1[1], voxelData.penAtPt1[2], voxelData.penAtPt1[3] };
                stylusPositionTwo = new double[] { voxelData.penAtPt2[0], voxelData.penAtPt2[1], voxelData.penAtPt2[2], voxelData.penAtPt2[3] };

                Vector3 stylusFinalPointOne, stylusFinalPointTwo;

                bool isTherePoint =
                    VoxelStationMathSdk.Math.UpdatePenPose(stylusPositionOne, stylusPositionTwo, leftCamera, rightCamera, out stylusFinalPointOne, out stylusFinalPointTwo);//两条射线是否有交点

                if (isTherePoint)
                {
                    UpdatePose(stylusFinalPointOne, stylusFinalPointTwo);//更新笔的位置
                }
                //Note: Idle to hover state can be ignored for assigning delegates
                //but hover to grab is a must if a user want to pick a object
                RaycastHit hit = new RaycastHit();
                switch (stylusState)
                {
                case StylusState.Idle:
                {
                    // Perform a raycast on the entire scene to determine what the
                    // stylus is currently colliding with.
                    STYLUS_BEAM_WIDTH = stylusWidth * viewSize;

                    if (Physics.Raycast(pose.Position, pose.Direction, out hit))
                    {
                        // Update the stylus beam length.
                        STYLUS_BEAM_LENGTH = hit.distance;

                        switch (hit.collider.tag)
                        {
                        case "UI":
                            stylusState = StylusState.Hover;
                            if (this.onStylusHoverUIBegin != null)
                            {
                                //Calls on hover function
                                this.onStylusHoverUIBegin(new ActionEventInfo(hit.collider.gameObject));
                                _UISelection = hit.collider.gameObject;
                            }
                            break;

                        case "Grab":
                            stylusState = StylusState.Hover;
                            if (this.onStylusHoverObjectBegin != null)
                            {
                                previousModelSelection = hit.collider.gameObject;

                                this.onStylusHoverObjectBegin(new ActionEventInfo(hit.collider.gameObject));
                                previousModelSelection = hit.collider.gameObject;
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        STYLUS_BEAM_LENGTH = stylusLength;
                    }
                }
                break;

                case StylusState.Hover:
                    //PreviousSelection
                    if (Physics.Raycast(pose.Position, pose.Direction, out hit))
                    {
                        // Update the stylus beam length.
                        STYLUS_BEAM_LENGTH = hit.distance;

                        switch (hit.collider.tag)
                        {
                        case "UI":
                            //If user pointer came directly from model to UI
                            //then call object hover end and reset to idle
                            if (previousModelSelection != null && this.onStylusHoverObjectEnd != null)
                            {
                                this.onStylusHoverObjectEnd(new ActionEventInfo(previousModelSelection));
                                previousModelSelection = null;
                                stylusState            = StylusState.Idle;
                            }

                            if (isButtonOnePressed && !wasButtonOnePressed && this.onStylusButtonOnePressedUI != null)
                            {
                                this.onStylusButtonOnePressedUI(new ActionEventInfo(hit.collider.gameObject));
                            }
                            break;

                        case "Grab":
                            //if user pointer came from UI to model directly
                            //then all end on hover of UI and reset to idle
                            if (_UISelection != null && this.onStylusHoverUIEnd != null)
                            {
                                //Call the function to say the UI hover event is over
                                this.onStylusHoverUIEnd(new ActionEventInfo(_UISelection));
                                _UISelection = null;
                                stylusState  = StylusState.Idle;
                            }

                            //Call the grab function
                            if (isButtonOnePressed && !wasButtonOnePressed && this.onStylusGrabObjectBegin != null)
                            {
                                grabObject = hit.collider.gameObject;
                                this.onStylusGrabObjectBegin(new ActionEventInfo(grabObject, pose, STYLUS_BEAM_LENGTH));
                                stylusState = StylusState.Grab;
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        if (this.onStylusHoverObjectEnd != null)
                        {
                            this.onStylusHoverObjectEnd(new ActionEventInfo(previousModelSelection));
                        }
                        stylusState            = StylusState.Idle;
                        previousModelSelection = null;
                        STYLUS_BEAM_LENGTH     = stylusLength * viewSize;
                    }
                    break;

                case StylusState.Grab:
                {
                    if (grabObject.tag.Equals("Grab") && this.onStylusGrabObjectUpdate != null)
                    {
                        ///Update the grab.
                        ///this.UpdateGrab(pose.Position, pose.Rotation);
                        ///calls the update for model
                        this.onStylusGrabObjectUpdate(new ActionEventInfo(grabObject, pose, STYLUS_BEAM_LENGTH));
                    }

                    /// End the grab if the front stylus button was released.
                    if (!isButtonOnePressed && wasButtonOnePressed)
                    {
                        ///Calls the end grab delegate to stop movin model
                        if (this.onStylusGrabObjectEnd != null)
                        {
                            this.onStylusGrabObjectEnd(new ActionEventInfo(grabObject));
                        }
                        stylusState = StylusState.Hover;
                        grabObject  = null;
                    }
                }
                break;

                default:
                    break;
                }

                //if we have previously hovered over UI and now the hit.collider is giving empty response then we need to set slection as Idle
                //and notify the call back function
                if (hit.collider == null)
                {
                    if (_UISelection != null && this.onStylusHoverUIEnd != null)
                    {
                        //Call the function to say the UI hover event is over
                        this.onStylusHoverUIEnd(new ActionEventInfo(_UISelection));
                        _UISelection = null;
                        stylusState  = StylusState.Idle;
                    }

                    if (previousModelSelection != null && (stylusState != StylusState.Grab && stylusState != StylusState.Hover))
                    {
                        if (this.onStylusHoverObjectEnd != null)
                        {
                            this.onStylusHoverObjectEnd(new ActionEventInfo(previousModelSelection));
                        }
                        previousModelSelection = null;
                        stylusState            = StylusState.Idle;
                    }
                }

                // Update the stylus beam.
                this.UpdateStylus(pose.Position, pose.Direction);

                // Cache state for next frame.
                wasButtonOnePressed = isButtonOnePressed;
            }
#if !UNITY_EDITOR
            //鼠标隐藏函数,通过操作inspector面板执行
            UpdateMouseAutoHide();
#endif
            //键盘热键相关逻辑
            HotKey();
        }
 void Awake()
 {
     instance = this;
     controller = GetComponent<StereoController>();
 }