Ejemplo n.º 1
0
        public bool ComputeIntersection(Vector3 source, Vector3 direction, ref IntersectionResults results)
        {
            var overlay = OpenVR.Overlay;

            if (overlay == null)
            {
                return(false);
            }

            var input = new VROverlayIntersectionParams_t();

            input.eOrigin       = SteamVR_Render.instance.trackingSpace;
            input.vSource.v0    = source.x;
            input.vSource.v1    = source.y;
            input.vSource.v2    = -source.z;
            input.vDirection.v0 = direction.x;
            input.vDirection.v1 = direction.y;
            input.vDirection.v2 = -direction.z;

            var output = new VROverlayIntersectionResults_t();

            if (!overlay.ComputeOverlayIntersection(handle, ref input, ref output))
            {
                return(false);
            }

            results.point    = new Vector3(output.vPoint.v0, output.vPoint.v1, -output.vPoint.v2);
            results.normal   = new Vector3(output.vNormal.v0, output.vNormal.v1, -output.vNormal.v2);
            results.UVs      = new Vector2(output.vUVs.v0, output.vUVs.v1);
            results.distance = output.fDistance;
            return(true);
        }
Ejemplo n.º 2
0
    public bool ComputeIntersection(Vector3 source, Vector3 direction, ref SteamVR_Overlay.IntersectionResults results)
    {
        CVROverlay overlay = OpenVR.Overlay;

        if (overlay == null)
        {
            return(false);
        }
        VROverlayIntersectionParams_t vroverlayIntersectionParams_t = default(VROverlayIntersectionParams_t);

        vroverlayIntersectionParams_t.eOrigin       = SteamVR_Render.instance.trackingSpace;
        vroverlayIntersectionParams_t.vSource.v0    = source.x;
        vroverlayIntersectionParams_t.vSource.v1    = source.y;
        vroverlayIntersectionParams_t.vSource.v2    = -source.z;
        vroverlayIntersectionParams_t.vDirection.v0 = direction.x;
        vroverlayIntersectionParams_t.vDirection.v1 = direction.y;
        vroverlayIntersectionParams_t.vDirection.v2 = -direction.z;
        VROverlayIntersectionResults_t vroverlayIntersectionResults_t = default(VROverlayIntersectionResults_t);

        if (!overlay.ComputeOverlayIntersection(this.handle, ref vroverlayIntersectionParams_t, ref vroverlayIntersectionResults_t))
        {
            return(false);
        }
        results.point    = new Vector3(vroverlayIntersectionResults_t.vPoint.v0, vroverlayIntersectionResults_t.vPoint.v1, -vroverlayIntersectionResults_t.vPoint.v2);
        results.normal   = new Vector3(vroverlayIntersectionResults_t.vNormal.v0, vroverlayIntersectionResults_t.vNormal.v1, -vroverlayIntersectionResults_t.vNormal.v2);
        results.UVs      = new Vector2(vroverlayIntersectionResults_t.vUVs.v0, vroverlayIntersectionResults_t.vUVs.v1);
        results.distance = vroverlayIntersectionResults_t.fDistance;
        return(true);
    }
Ejemplo n.º 3
0
    //オーバーレイと交点を持つかチェック
    private bool ComputeOverlayIntersection(Vector3 pos, Vector3 rotvect, ref VROverlayIntersectionResults_t results)
    {
#pragma warning disable 0219
        string Tag = "[" + this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(); //クラス名とメソッド名を自動取得
#pragma warning restore 0219

        //レイ照射情報
        VROverlayIntersectionParams_t param = new VROverlayIntersectionParams_t();
        //レイ発射元位置
        param.vSource = new HmdVector3_t
        {
            v0 = pos.x,
            v1 = pos.y,
            v2 = -pos.z //右手系 to 左手系
        };
        //レイ発射単位方向ベクトル
        param.vDirection = new HmdVector3_t
        {
            v0 = rotvect.x,
            v1 = rotvect.y,
            v2 = -rotvect.z //右手系 to 左手系
        };
        //ルーム空間座標系で照射
        param.eOrigin = ETrackingUniverseOrigin.TrackingUniverseStanding;

        //Overlayと交差していればtrue、していなければfalseで、詳細情報がresultsに入る
        return(overlay.ComputeOverlayIntersection(overlayHandle, ref param, ref results));
    }
Ejemplo n.º 4
0
    //タップされているかどうかを調べる
    private void CheckTapping(VROverlayIntersectionResults_t results, LeftOrRight lr, ref bool tapped)
    {
#pragma warning disable 0219
        string Tag = "[" + this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(); //クラス名とメソッド名を自動取得
#pragma warning restore 0219

        //コントローラとオーバーレイの距離が一定以下なら
        if (results.fDistance < config.TapOnDistance && !tapped)
        {
            //タップされた
            tapped = true;
            haptic(lr);

            //クリック処理(完全ロックでない場合)
            if (tapEnable)
            {
                uGUIclick(results);
            }
        }
        //コントローラとオーバーレイの距離が一定以上なら
        if (results.fDistance > config.TapOffDistance && tapped)
        {
            //離れた
            tapped = false;
            haptic(lr);
        }
    }
Ejemplo n.º 5
0
    //Canvas上の要素を特定してクリックする
    private void uGUIclick(VROverlayIntersectionResults_t results)
    {
#pragma warning disable 0219
        string Tag = "[" + this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(); //クラス名とメソッド名を自動取得
#pragma warning restore 0219

        //クリック用uv座標を計算
        float u = results.vUVs.v0 * renderTexture.width;
        float v = renderTexture.height - results.vUVs.v1 * renderTexture.height;

        //Canvas上のレイキャストのために座標をセット
        Vector2          ScreenPoint = new Vector2(u, v);
        PointerEventData pointer     = new PointerEventData(EventSystem.current)
        {
            position = ScreenPoint
        };

        //レイキャスト結果格納用リストを確保
        List <RaycastResult> result = new List <RaycastResult>();

        //RaycastAllはレイキャスターを叩く。
        //CanvasについていたりCameraについていたりするすべてのレイキャスターを叩く(要らないものは切っておくとよい)
        EventSystem.current.RaycastAll(pointer, result);

        //検出した要素の数と座標

        Debug.Log(Tag + "count:" + result.Count + " u:" + u + " / v:" + v);

        //一番最初に見つけた要素にクリック処理を行う
        for (int i = 0; i < result.Count; i++)
        {
            var res = result[i];

            //一番最初に引っ掛けたものを叩く(target以外はcheckを外しておく)
            if (res.isValid)
            {
                Debug.Log(Tag + res.gameObject.name + " at " + res.gameObject.transform.root.name);
                //対象にしたいルートオブジェクトの子かを調べる
                if (res.gameObject.transform.root.name == LaycastRootObject.name)
                {
                    ExecuteEvents.Execute(res.gameObject, pointer, ExecuteEvents.pointerClickHandler);
                    break;
                }
            }
        }
    }
Ejemplo n.º 6
0
    public static bool ComputeIntersection(ulong handle, Vector3 source, Vector3 direction, ETrackingUniverseOrigin trackingUniverseOrigin, ref IntersectionResults results)
    {
        var input = new VROverlayIntersectionParams_t {
            eOrigin    = trackingUniverseOrigin,
            vSource    = { v0 = source.x, v1 = source.y, v2 = -source.z },
            vDirection = { v0 = direction.x, v1 = direction.y, v2 = -direction.z }
        };

        var output = new VROverlayIntersectionResults_t();

        if (!OpenVR.Overlay.ComputeOverlayIntersection(handle, ref input, ref output))
        {
            return(false);
        }

        results.point    = new Vector3(output.vPoint.v0, output.vPoint.v1, -output.vPoint.v2);
        results.normal   = new Vector3(output.vNormal.v0, output.vNormal.v1, -output.vNormal.v2);
        results.UVs      = new Vector2(output.vUVs.v0, output.vUVs.v1);
        results.distance = output.fDistance;
        return(true);
    }
Ejemplo n.º 7
0
    /// <summary>
    /// Compute a given Ray and determine if it hit an Overlay
    /// </summary>
    /// <param name="source"></param>
    /// <param name="direction"></param>
    /// <param name="results"></param>
    /// <returns></returns>
    private bool ComputeIntersection(HOTK_OverlayBase hotkOverlay, Vector3 source, Vector3 direction, ref SteamVR_Overlay.IntersectionResults results)
    {
        var overlay = OpenVR.Overlay;

        if (overlay == null)
        {
            return(false);
        }

        var input = new VROverlayIntersectionParams_t
        {
            eOrigin = SteamVR_Render.instance.trackingSpace,
            vSource =
            {
                v0 = source.x,
                v1 = source.y,
                v2 = -source.z
            },
            vDirection =
            {
                v0 = direction.x,
                v1 = direction.y,
                v2 = -direction.z
            }
        };

        var output = new VROverlayIntersectionResults_t();

        if (!overlay.ComputeOverlayIntersection(hotkOverlay.Handle, ref input, ref output))
        {
            return(false);
        }

        results.point    = new Vector3(output.vPoint.v0, output.vPoint.v1, -output.vPoint.v2);
        results.normal   = new Vector3(output.vNormal.v0, output.vNormal.v1, -output.vNormal.v2);
        results.UVs      = new Vector2(output.vUVs.v0, output.vUVs.v1);
        results.distance = output.fDistance;
        return(true);
    }
Ejemplo n.º 8
0
    public bool ComputeIntersection(Vector3 source, Vector3 direction, ref IntersectionResults results)
    {
        var vr = SteamVR.instance;

        var input = new VROverlayIntersectionParams_t();

        input.eOrigin      = SteamVR_Render.instance.trackingSpace;
        input.vSource.v    = new float[] { source.x, source.y, -source.z };
        input.vDirection.v = new float[] { direction.x, direction.y, -direction.z };

        var output = new VROverlayIntersectionResults_t();

        if (!vr.overlay.ComputeOverlayIntersection(handle, ref input, ref output))
        {
            return(false);
        }

        results.point    = new Vector3(output.vPoint.v[0], output.vPoint.v[1], -output.vPoint.v[2]);
        results.normal   = new Vector3(output.vNormal.v[0], output.vNormal.v[1], -output.vNormal.v[2]);
        results.UVs      = new Vector2(output.vUVs.v[0], output.vUVs.v[1]);
        results.distance = output.fDistance;
        return(true);
    }
Ejemplo n.º 9
0
    //指定されたdeviceが有効かチェックした上で、オーバーレイと交点を持つかチェック
    private bool checkRay(uint idx, TrackedDevicePose_t[] allDevicePose, ref VROverlayIntersectionResults_t results)
    {
#pragma warning disable 0219
        string Tag = "[" + this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(); //クラス名とメソッド名を自動取得
#pragma warning restore 0219

        //device indexが有効
        if (idx != OpenVR.k_unTrackedDeviceIndexInvalid)
        {
            //接続されていて姿勢情報が有効
            if (allDevicePose[idx].bDeviceIsConnected && allDevicePose[idx].bPoseIsValid)
            {
                //姿勢情報などを変換してもらう
                TrackedDevicePose_t          Pose  = allDevicePose[idx];
                SteamVR_Utils.RigidTransform Trans = new SteamVR_Utils.RigidTransform(Pose.mDeviceToAbsoluteTracking);

                //コントローラー用に45度前方に傾けた方向ベクトルを計算
                Vector3 vect = (Trans.rot * Quaternion.AngleAxis(45, Vector3.right)) * Vector3.forward;

                return(ComputeOverlayIntersection(Trans.pos, vect, ref results));
            }
        }
        return(false);
    }
Ejemplo n.º 10
0
 public abstract bool ComputeOverlayIntersection(ulong ulOverlayHandle,ref VROverlayIntersectionParams_t pParams,ref VROverlayIntersectionResults_t pResults);
Ejemplo n.º 11
0
 public override bool ComputeOverlayIntersection(ulong ulOverlayHandle,ref VROverlayIntersectionParams_t pParams,ref VROverlayIntersectionResults_t pResults)
 {
     CheckIfUsable();
     bool result = VRNativeEntrypoints.VR_IVROverlay_ComputeOverlayIntersection(m_pVROverlay,ulOverlayHandle,ref pParams,ref pResults);
     return result;
 }
Ejemplo n.º 12
0
    //----------おまけ(コントローラーでOverlayを叩いてuGUIをクリックできるやつ)-------------

    //uGUIクリックを実現する
    private void updateVRTouch()
    {
#pragma warning disable 0219
        string Tag = "[" + this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(); //クラス名とメソッド名を自動取得
#pragma warning restore 0219

        //コントローラのindex

        //VR接続されているすべてのデバイスの情報を取得
        TrackedDevicePose_t[] allDevicePose = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
        openvr.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, 0f, allDevicePose);

        //Overlayのレイ走査結果を格納する変数
        VROverlayIntersectionResults_t results = new VROverlayIntersectionResults_t();

        /*
         * //視線による操作
         * uint Hmdidx = OpenVR.k_unTrackedDeviceIndex_Hmd;
         * if (checkRay(Hmdidx, allDevicePose, ref results))
         * {
         *  parent.setCursorPosition(results, LeftOrRight.Right, channel);
         *  Debug.Log(DEBUG_TAG + "HMD u:"+results.vUVs.v0+" v:"+ results.vUVs.v1+" d:"+results.fDistance);
         * }
         */
        //左手コントローラーの情報取得
        uint Leftidx = openvr.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
        if (checkRay(Leftidx, allDevicePose, ref results))
        {
            //線上にオーバーレイがある場合は続けて処理
            CheckTapping(results, LeftOrRight.Left, ref tappedLeft);

            //カーソル表示用に更新
            LeftHandU        = results.vUVs.v0 * renderTexture.width;
            LeftHandV        = renderTexture.height - results.vUVs.v1 * renderTexture.height;
            LeftHandDistance = results.fDistance;
        }
        else
        {
            LeftHandU        = -1f;
            LeftHandV        = -1f;
            LeftHandDistance = -1f;
        }
        //右手コントローラーの情報取得
        uint Rightidx = openvr.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
        if (checkRay(Rightidx, allDevicePose, ref results))
        {
            //線上にオーバーレイがある場合は続けて処理
            CheckTapping(results, LeftOrRight.Right, ref tappedRight);

            //カーソル表示用に更新
            RightHandU        = results.vUVs.v0 * renderTexture.width;
            RightHandV        = renderTexture.height - results.vUVs.v1 * renderTexture.height;
            RightHandDistance = results.fDistance;
        }
        else
        {
            RightHandU        = -1f;
            RightHandV        = -1f;
            RightHandDistance = -1f;
        }
    }