Esempio n. 1
0
        // FixedUpdate is called before any Update
        public void Update()
        {
            // if we are in capture we freeze the cursor plane
            if (Scene.InCapture == false)
            {
                Vector3 camPos  = camera.gameObject.transform.position;
                Vector3 forward = camera.gameObject.transform.forward;

                // orient Y-up plane so that it is in front of eye, perp to camera direction
                xformObject.transform.position = camPos + 10 * forward;
                xformObject.transform.LookAt(camera.gameObject.transform);
                xformObject.transform.RotateAround(xformObject.transform.position, xformObject.transform.right, 90);

                // that plane is the plane the mouse cursor moves on
                this.vCursorPlaneOrigin  = xformObject.transform.position;
                this.vCursorPlaneRight   = xformObject.transform.right;
                this.vCursorPlaneForward = xformObject.transform.forward;
                this.vRaySourcePosition  = camera.transform.position;
            }

            Vector3 curPos = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0);

            dx -= 0.3f * curPos.x;
            dy -= 0.3f * curPos.y;

            vPlaneCursorPos =
                vCursorPlaneOrigin + dx * vCursorPlaneRight + dy * vCursorPlaneForward;
            vSceneCursorPos = vPlaneCursorPos;


            bool bHit = false;

            if (Scene != null)
            {
                Ray       r   = new Ray(camera.transform.position, (vPlaneCursorPos - camera.transform.position).normalized);
                AnyRayHit hit = null;
                if (Scene.FindAnyRayIntersection(r, out hit))
                {
                    vSceneCursorPos = hit.hitPos;
                    bHit            = true;
                }
                else
                {
                    GameObjectRayHit ghit = null;
                    if (Scene.GetScene().FindWorldBoundsHit(r, out ghit))
                    {
                        vSceneCursorPos = ghit.hitPos;
                    }
                }
            }

            this.CurrentCursorPosWorld       = vPlaneCursorPos;
            this.CurrentCursorRaySourceWorld = this.vRaySourcePosition;

            Cursor.transform.position = vSceneCursorPos;
            if (Scene.InCapture)
            {
                Cursor.GetComponent <MeshRenderer> ().material = CursorCapturingMaterial;
            }
            else if (bHit)
            {
                Cursor.GetComponent <MeshRenderer> ().material = CursorHitMaterial;
            }
            else
            {
                Cursor.GetComponent <MeshRenderer> ().material = CursorDefaultMaterial;
            }
            Cursor.layer = (bHit || Scene.InCapture) ? LayerMask.NameToLayer(SceneGraphConfig.WidgetOverlayLayerName) : 0;

            // maintain a consistent visual size for 3D cursor sphere
            float fScaling = MathUtil.GetVRRadiusForVisualAngle(vSceneCursorPos, camera.transform.position, CursorVisualAngleInDegrees);

            Cursor.transform.localScale = new Vector3(fScaling, fScaling, fScaling);
        }