private Quaternion calculateRotation(LeapTransform trs)
        {
            Vector3 up      = trs.yBasis.ToVector3();
            Vector3 forward = trs.zBasis.ToVector3();

            return(Quaternion.LookRotation(forward, up));
        }
Exemple #2
0
        private void GenerateFrame()
        {
            hands.Clear();

            LeapTransform leapTransform = new LeapTransform(Vector.Zero, LeapQuaternion.Identity, Vector.Ones);

            /*if (rightControllerHand != null && rightControllerHand.IsActive) hands.Add(rightControllerHand.GenerateHandData(frameID).Transform(leapTransform));
             * if (leftControllerHand != null && leftControllerHand.IsActive) hands.Add(leftControllerHand.GenerateHandData(frameID).Transform(leapTransform));*/

            if (rightControllerHand != null && rightControllerHand.IsActive)
            {
                hands.Add(rightControllerHand.LeapHand.Transform(leapTransform));
            }
            if (leftControllerHand != null && leftControllerHand.IsActive)
            {
                hands.Add(leftControllerHand.LeapHand.Transform(leapTransform));
            }

            //currentFrame = new Leap.Frame(frameID, timeStamp, 60, hands);
            currentFrame.Id        = frameID;
            currentFrame.Timestamp = timeStamp;
            //frame.Hands = hands;

            frameID++;
            timeStamp++;
        }
        protected void transformFrame(Frame source, Frame dest, bool resampleTemporalWarping = true)
        {
            LeapTransform leapTransform;

            if (_temporalWarping != null)
            {
                if (resampleTemporalWarping)
                {
                    _temporalWarping.TryGetWarpedTransform(LeapVRTemporalWarping.WarpedAnchor.CENTER, out warpedPosition, out warpedRotation, source.Timestamp);
                    warpedRotation = warpedRotation * transform.localRotation;
                }

                leapTransform = new LeapTransform(warpedPosition.ToVector(), warpedRotation.ToLeapQuaternion(), transform.lossyScale.ToVector() * 1e-3f);
                leapTransform.MirrorZ();
            }
            else
            {
                leapTransform = transform.GetLeapMatrix();
            }

            // My Modifications Start--->
            // leapTransform.MirrorX();
            //leapTransform.translation = new Vector(0.5f,0.6f,0.7f);
            // My Modifications End--->

            dest.CopyFrom(source).Transform(leapTransform);
        }
        /**
         * Converts a LeapTransform representing a rotation to a Unity Quaternion without
         * depending on the LeapTransform having a valid Quaternion.
         *
         * In previous version prior 4.0.0 this function performed a conversion to Unity's left-handed coordinate system, and now does not.
         *
         * @returns A Unity Quaternion representing the rotation.
         */
        public static Quaternion CalculateRotation(this LeapTransform trs)
        {
            Vector3 up      = trs.yBasis.ToVector3();
            Vector3 forward = -trs.zBasis.ToVector3();

            return(Quaternion.LookRotation(forward, up));
        }
Exemple #5
0
        void Update()
        {
            if (!EditorApplication.isPlaying && SupportsEditorPersistence())
            {
                Transform           editorPoseSpace;
                LeapServiceProvider leapServiceProvider = FindObjectOfType <LeapServiceProvider>();
                LeapTransform       poseTransform       = LeapTransform.Identity;
                if (leapServiceProvider != null)
                {
                    editorPoseSpace = leapServiceProvider.transform;
                    poseTransform   = TestHandFactory.GetTestPoseLeftHandTransform(leapServiceProvider.editTimePose);
                }
                else
                {
                    editorPoseSpace = transform;
                }

                Hand hand = TestHandFactory.MakeTestHand(Handedness == Chirality.Left, poseTransform).TransformedCopy(UnityMatrixExtension.GetLeapMatrix(editorPoseSpace));
                //Hand hand = TestHandFactory.MakeTestHand(0, 0, Handedness == Chirality.Left).TransformedCopy(UnityMatrixExtension.GetLeapMatrix(editorPoseSpace));

                if (GetLeapHand() == null)
                {
                    SetLeapHand(hand);
                    InitHand();
                    BeginHand();
                    UpdateHand();
                }
                else
                {
                    SetLeapHand(hand);
                    UpdateHand();
                }
            }
        }
        protected void updateIfTransformMoved(Frame source, ref Frame toUpdate)
        {
            if (transform.hasChanged)
            {
                _transformedFixedFrame  = null;
                _transformedUpdateFrame = null;
                transform.hasChanged    = false;
            }

            if (toUpdate == null)
            {
                LeapTransform leapTransform;
                if (_temporalWarping != null)
                {
                    Vector3    warpedPosition;
                    Quaternion warpedRotation;
                    _temporalWarping.TryGetWarpedTransform(LeapVRTemporalWarping.WarpedAnchor.CENTER, out warpedPosition, out warpedRotation, source.Timestamp);

                    warpedRotation = warpedRotation * transform.localRotation;

                    leapTransform = new LeapTransform(warpedPosition.ToVector(), warpedRotation.ToLeapQuaternion(), transform.lossyScale.ToVector() * 1e-3f);
                    leapTransform.MirrorZ();
                }
                else
                {
                    leapTransform = transform.GetLeapMatrix();
                }

                toUpdate = source.TransformedCopy(leapTransform);
            }
        }
        protected override Frame createFrame()
        {
            _originalFrame = TestHandFactory.MakeTestFrame(0, true, true);
            LeapTransform forwardTransform = new LeapTransform(translation, LeapQuaternion.Identity);

            return(_originalFrame.TransformedCopy(forwardTransform));
        }
        public void transformFrame(Frame source, Frame dest)
        {
            LeapTransform leapTransform;

            _temporalWarping = null;
            //////////////////////////////////////////////
            // 细细研究这个问题  弄清楚是为什么
            /////////////////////////////////////////////
            if (_temporalWarping != null)
            {
                Vector3    warpedPosition;
                Quaternion warpedRotation;
                _temporalWarping.TryGetWarpedTransform(LeapVRTemporalWarping.WarpedAnchor.CENTER, out warpedPosition, out warpedRotation, source.Timestamp);

                warpedRotation = warpedRotation * transform.localRotation;

                leapTransform = new LeapTransform(warpedPosition.ToVector(), warpedRotation.ToLeapQuaternion(), transform.lossyScale.ToVector() * 1e-3f);
                leapTransform.MirrorZ();
            }
            else
            {
                leapTransform = transform.GetLeapMatrix();
            }

            dest.CopyFrom(source).Transform(leapTransform);
        }
Exemple #9
0
        protected void transformHands(ref LeapTransform LeftHand, ref LeapTransform RightHand)
        {
            LeapTransform leapTransform = transform.GetLeapMatrix();

            LeftHand  = new LeapTransform(leapTransform.TransformPoint(LeftHand.translation), leapTransform.TransformQuaternion(LeftHand.rotation));
            RightHand = new LeapTransform(leapTransform.TransformPoint(RightHand.translation), leapTransform.TransformQuaternion(RightHand.rotation));
        }
        /**
         * Extracts a transform matrix containing translation, rotation, and scale from a Unity Transform object and
         * returns a Leap Motion LeapTransform object.
         * Use this matrix to transform Leap Motion tracking data to the Unity world relative to the
         * specified transform.
         *
         * In addition to applying the translation, rotation, and scale from the Transform object, the returned
         * transformation changes the coordinate system from right- to left-handed and converts units from millimeters to meters
         * by scaling.
         * @returns A Leap.LeapTransform object representing the specified transform from Leap Motion into Unity space.
         */
        public static LeapTransform GetLeapMatrix(this Transform t)
        {
            Vector        scale     = new Vector(t.lossyScale.x * MM_TO_M, t.lossyScale.y * MM_TO_M, t.lossyScale.z * MM_TO_M);
            LeapTransform transform = new LeapTransform(t.position.ToVector(), t.rotation.ToLeapQuaternion(), scale);

            transform.MirrorZ(); // Unity is left handed.
            return(transform);
        }
Exemple #11
0
    public static LeapTransform GetLeapTransform(Vector3 position, Quaternion rotation)
    {
        Vector        scale     = new Vector(MM_TO_M, MM_TO_M, MM_TO_M); // Leap units -> Unity units.
        LeapTransform transform = new LeapTransform(position.ToVector(), rotation.ToLeapQuaternion(), scale);

        transform.MirrorZ(); // Unity is left handed.
        return(transform);
    }
Exemple #12
0
        public void DrawBasis(Vector position, LeapTransform basis, float scale)
        {
            Vector3 origin = position.ToVector3();

            Debug.DrawLine(origin, origin + basis.xBasis.ToVector3() * scale, Color.red);
            Debug.DrawLine(origin, origin + basis.yBasis.ToVector3() * scale, Color.green);
            Debug.DrawLine(origin, origin + basis.zBasis.ToVector3() * scale, Color.blue);
        }
Exemple #13
0
 public void DrawBasis(RuntimeGizmoDrawer gizmoDrawer, Vector3 origin, LeapTransform basis, float scale) {
   gizmoDrawer.color = Color.red;
   gizmoDrawer.DrawLine(origin, origin + basis.xBasis.ToVector3() * scale);
   gizmoDrawer.color = Color.green;
   gizmoDrawer.DrawLine(origin, origin + basis.yBasis.ToVector3() * scale);
   gizmoDrawer.color = Color.blue;
   gizmoDrawer.DrawLine(origin, origin + basis.zBasis.ToVector3() * scale);
 }
Exemple #14
0
        public void GetInterpolatedLeftRightTransform(Int64 time,
                                                      Int64 sourceTime,
                                                      Int64 leftId,
                                                      Int64 rightId,
                                                      out LeapTransform leftTransform,
                                                      out LeapTransform rightTransform)
        {
            leftTransform  = LeapTransform.Identity;
            rightTransform = LeapTransform.Identity;

            UInt64  size           = GetInterpolatedFrameSize(time);
            IntPtr  trackingBuffer = Marshal.AllocHGlobal((Int32)size);
            eLeapRS result         = LeapC.InterpolateFrameFromTime(_leapConnection, time, sourceTime, trackingBuffer, size);

            reportAbnormalResults("LeapC get interpolated frame from time call was ", result);

            if (result == eLeapRS.eLeapRS_Success)
            {
                LEAP_TRACKING_EVENT tracking_evt;
                StructMarshal <LEAP_TRACKING_EVENT> .PtrToStruct(trackingBuffer, out tracking_evt);

                int             id;
                LEAP_VECTOR     position;
                LEAP_QUATERNION orientation;

                long handPtr = tracking_evt.pHands.ToInt64();
                long idPtr   = handPtr + _handIdOffset;
                long posPtr  = handPtr + _handPositionOffset;
                long rotPtr  = handPtr + _handOrientationOffset;
                int  stride  = StructMarshal <LEAP_HAND> .Size;

                for (uint i = tracking_evt.nHands; i-- != 0; idPtr += stride, posPtr += stride, rotPtr += stride)
                {
                    id = Marshal.ReadInt32(new IntPtr(idPtr));
                    StructMarshal <LEAP_VECTOR> .PtrToStruct(new IntPtr(posPtr), out position);

                    StructMarshal <LEAP_QUATERNION> .PtrToStruct(new IntPtr(rotPtr), out orientation);

                    LeapTransform transform = new LeapTransform(position.ToLeapVector(), orientation.ToLeapQuaternion());
                    if (id == leftId)
                    {
                        leftTransform = transform;
                    }
                    else if (id == rightId)
                    {
                        rightTransform = transform;
                    }
                }
            }

            Marshal.FreeHGlobal(trackingBuffer);
        }
 // These versions of GetPalmRotation & CalculateRotation return the opposite
 // vector compared to LeapUnityExtension.CalculateRotation.
 // This will be deprecated once LeapUnityExtension.CalculateRotation is
 // flipped in the next release of LeapMotion Core Assets.
 private Quaternion getRiggedPalmRotation()
 {
     if (hand_ != null)
     {
         LeapTransform trs = hand_.Basis;
         return(calculateRotation(trs));
     }
     if (palm)
     {
         return(palm.rotation);
     }
     return(Quaternion.identity);
 }
        protected void transformHands(ref LeapTransform LeftHand, ref LeapTransform RightHand)
        {
            LeapTransform leapTransform;

            /*if (_temporalWarping != null) {
             * leapTransform = new LeapTransform(warpedPosition.ToVector(), warpedRotation.ToLeapQuaternion(), transform.lossyScale.ToVector() * 1e-3f);
             * leapTransform.MirrorZ();
             * } else*/{
                leapTransform = transform.GetLeapMatrix();
            }

            LeftHand  = new LeapTransform(leapTransform.TransformPoint(LeftHand.translation), leapTransform.TransformQuaternion(LeftHand.rotation));
            RightHand = new LeapTransform(leapTransform.TransformPoint(RightHand.translation), leapTransform.TransformQuaternion(RightHand.rotation));
        }
        protected void updateIfTransformMoved(Frame source, ref Frame toUpdate)
        {
            if (transform.hasChanged)
            {
                _transformedFixedFrame  = null;
                _transformedUpdateFrame = null;
                transform.hasChanged    = false;
                //Debug.Log("has changed ");
            }

            if (toUpdate == null)
            {
                LeapTransform leapTransform;
                if (_temporalWarping != null)
                {
                    Vector3    warpedPosition;
                    Quaternion warpedRotation;
                    _temporalWarping.TryGetWarpedTransform(LeapVRTemporalWarping.WarpedAnchor.CENTER, out warpedPosition, out warpedRotation, source.Timestamp);

                    warpedRotation = warpedRotation * transform.localRotation;

                    leapTransform = new LeapTransform(warpedPosition.ToVector(), warpedRotation.ToLeapQuaternion(), transform.lossyScale.ToVector() * 1e-3f);
                    leapTransform.MirrorZ();
                }
                else
                {
                    leapTransform = transform.GetLeapMatrix();
                    //Debug.Log("This is the transforme" + leapTransform.translation);
                }
                //     Debug.Log("This is the source" + source);

                //toUpdate = source.TransformedCopy(leapTransform);
                try
                {
                    toUpdate = source.TransformedCopy(leapTransform);

                    //Debug.Log(webController.Frame());
                    //toUpdate = source.TransformedCopy(leapTransform);
                }
                catch (Exception e)
                {
                    //Debug.Log("leaptransform   " + transform);
                    Debug.Log("Type:     " + e + "Message:    " + e.Message + "    stacktrace:" + e.StackTrace);
                }

                //toUpdate = webController.Frame().TransformedCopy(leapTransform);
            }
        }
Exemple #18
0
        public void GetInterpolatedLeftRightTransform(long time, long sourceTime, long leftId, long rightId, out LeapTransform leftTransform, out LeapTransform rightTransform)
        {
            leftTransform  = LeapTransform.Identity;
            rightTransform = LeapTransform.Identity;
            ulong   interpolatedFrameSize = this.GetInterpolatedFrameSize(time);
            IntPtr  pEvent = Marshal.AllocHGlobal((int)interpolatedFrameSize);
            eLeapRS result = LeapC.InterpolateFrameFromTime(this._leapConnection, time, sourceTime, pEvent, interpolatedFrameSize);

            this.reportAbnormalResults("LeapC get interpolated frame from time call was ", result);
            if (result == eLeapRS.eLeapRS_Success)
            {
                LEAP_TRACKING_EVENT leap_tracking_event;
                StructMarshal <LEAP_TRACKING_EVENT> .PtrToStruct(pEvent, out leap_tracking_event);

                int  num3   = leap_tracking_event.pHands.ToInt32();
                int  num4   = num3 + _handIdOffset;
                int  num5   = num3 + _handPositionOffset;
                int  num6   = num3 + _handOrientationOffset;
                int  size   = StructMarshal <LEAP_HAND> .Size;
                uint nHands = leap_tracking_event.nHands;
                while (nHands-- != 0)
                {
                    LEAP_VECTOR     leap_vector;
                    LEAP_QUATERNION leap_quaternion;
                    int             num2 = Marshal.ReadInt32(new IntPtr(num4));
                    StructMarshal <LEAP_VECTOR> .PtrToStruct(new IntPtr(num5), out leap_vector);

                    StructMarshal <LEAP_QUATERNION> .PtrToStruct(new IntPtr(num6), out leap_quaternion);

                    LeapTransform transform = new LeapTransform(leap_vector.ToLeapVector(), leap_quaternion.ToLeapQuaternion());
                    if (num2 == leftId)
                    {
                        leftTransform = transform;
                    }
                    else if (num2 == rightId)
                    {
                        rightTransform = transform;
                    }
                    num4 += size;
                    num5 += size;
                    num6 += size;
                }
            }
            Marshal.FreeHGlobal(pEvent);
        }
        public void Start()
        {
            _settings = LeapSettings.Get(DataDir);
            _xform    = new LeapTransform(Vector.Zero, LeapQuaternion.Identity, new Vector(MillimetersToMeters, MillimetersToMeters, MillimetersToMeters));
            _xform.MirrorZ();
            _controller             = new Controller();
            _controller.Connect    += HandleLeapConnected;
            _controller.Disconnect += HandleLeapDisconnected;
            _controller.StartConnection();

            var tickRate = _settings.UpdateRate_ms;

            if (tickRate <= 0)
            {
                tickRate = LeapSettings.Defaults().UpdateRate_ms;
            }
            _timer = new Timer(HandleTimerTick, null, 0, tickRate);
        }
    IEnumerator UpdateTrackingAfterLeapInit()
    {
        while (((LeapServiceProvider)Hands.Provider).GetLeapController() == null)
        {
            yield return(null);
        }

        // To simplify the configuration values, positive X angles tilt the Leap towards the screen no matter how its mounted.
        // Therefore, we must convert to the real values before using them.
        // If top mounted, the X rotation should be negative if tilted towards the screen so we must negate the X rotation in this instance.
        var   isTopMounted = Mathf.Approximately(PhysicalConfigurable.Config.LeapRotationD.z, 180f);
        float xAngleDegree = isTopMounted ? -PhysicalConfigurable.Config.LeapRotationD.x : PhysicalConfigurable.Config.LeapRotationD.x;

        SetLeapTrackingMode();
        TrackingTransform = new LeapTransform(
            PhysicalConfigurable.Config.LeapPositionRelativeToScreenBottomM.ToVector(),
            Quaternion.Euler(xAngleDegree, PhysicalConfigurable.Config.LeapRotationD.y, PhysicalConfigurable.Config.LeapRotationD.z).ToLeapQuaternion()
            );
    }
        protected override void transformFrame(Frame source, Frame dest)
        {
            LeapTransform leapTransform = LeapTransform.Identity;

            if (mainCamera != null)
            {
                //By default, use the camera transform matrix to transform the frame into
                leapTransform       = mainCamera.transform.GetLeapMatrix();
                leapTransform.scale = Vector.Ones * 1e-3f;

                //If the application is playing then we can try to use temporal warping
                if (Application.isPlaying)
                {
                    leapTransform = GetWarpedMatrix(source.Timestamp);
                }
            }

            dest.CopyFrom(source).Transform(leapTransform);
        }
        private void GenerateFrame()
        {
            hands.Clear();

            LeapTransform leapTransform = new LeapTransform(Vector.Zero, LeapQuaternion.Identity, Vector.Ones);

            if (rightControllerHand != null && rightControllerHand.IsActive)
            {
                hands.Add(rightControllerHand.LeapHand.Transform(leapTransform));
            }
            if (leftControllerHand != null && leftControllerHand.IsActive)
            {
                hands.Add(leftControllerHand.LeapHand.Transform(leapTransform));
            }

            currentFrame.Id        = frameID;
            currentFrame.Timestamp = timeStamp;

            frameID++;
            timeStamp++;
        }
        protected Frame getTransformedFrame(Frame source)
        {
            LeapTransform leapTransform;

            if (_temporalWarping != null)
            {
                Vector3    warpedPosition;
                Quaternion warpedRotation;
                _temporalWarping.TryGetWarpedTransform(LeapVRTemporalWarping.WarpedAnchor.CENTER, out warpedPosition, out warpedRotation, source.Timestamp);

                warpedRotation = warpedRotation * transform.localRotation;

                leapTransform = new LeapTransform(warpedPosition.ToVector(), warpedRotation.ToLeapQuaternion(), transform.lossyScale.ToVector() * 1e-3f);
                leapTransform.MirrorZ();
            }
            else
            {
                leapTransform = transform.GetLeapMatrix();
            }

            return(source.TransformedCopy(leapTransform));
        }
    public void PositionCamera()
    {
        Transform referenceObject = transform;

        if (referenceObject == null)
        {
            HandController tmp = GameObject.FindObjectOfType <HandController>();
            if (tmp)
            {
                referenceObject = tmp.transform;
            }
        }
        if (referenceObject == null)
        {
            GameObject tmp = GameObject.Find("Main Camera");
            if (tmp)
            {
                referenceObject = tmp.transform;
            }
        }
        if (referenceObject == null)
        {
            return;
        }

        LeapTransform referenceTransform = new LeapTransform(referenceObject.position, referenceObject.rotation);

        referenceTransform.Position = referenceObject.position;
        referenceTransform.Rotation = referenceObject.rotation;

        Vector3 offsetCameraToLeap = OffsetCameraToLeap * 1.0f / clientUnitLengthInMillimeters;

        offsetCameraToLeap = referenceObject.rotation * offsetCameraToLeap;

        referenceTransform.Position = referenceObject.position + offsetCameraToLeap;

        Scene.LeapOriginInClient = referenceTransform;
    }
 /**
  * Extracts a transform matrix containing translation, rotation, and scale from a Unity Transform object and
  * returns a Leap Motion LeapTransform object.
  * Use this matrix to transform Leap Motion tracking data to the Unity world relative to the
  * specified transform.
  *
  * In addition to applying the translation, rotation, and scale from the Transform object, the returned
  * transformation changes the coordinate system from right- to left-handed and converts units from millimeters to meters
  * by scaling.
  * @returns A Leap.LeapTransform object representing the specified transform from Leap Motion into Unity space.
  */
 public static LeapTransform GetLeapMatrix(this Transform t) {
   Vector scale = new Vector(t.lossyScale.x * MM_TO_M, t.lossyScale.y * MM_TO_M, t.lossyScale.z * MM_TO_M);
   LeapTransform transform = new LeapTransform(t.position.ToVector(), t.rotation.ToLeapQuaternion(), scale);
   transform.MirrorZ(); // Unity is left handed.
   return transform;
 }
 protected override Frame createFrame() {
   _originalFrame = TestHandFactory.MakeTestFrame(0, true, true);
   LeapTransform forwardTransform = new LeapTransform(translation, LeapQuaternion.Identity);
   return _originalFrame.TransformedCopy(forwardTransform);
 }
    protected void transformFrame(Frame source, Frame dest) {
      LeapTransform leapTransform;
      if (_temporalWarping != null) {
        Vector3 warpedPosition;
        Quaternion warpedRotation;
        _temporalWarping.TryGetWarpedTransform(LeapVRTemporalWarping.WarpedAnchor.CENTER, out warpedPosition, out warpedRotation, source.Timestamp);

        warpedRotation = warpedRotation * transform.localRotation;

        leapTransform = new LeapTransform(warpedPosition.ToVector(), warpedRotation.ToLeapQuaternion(), transform.lossyScale.ToVector() * 1e-3f);
        leapTransform.MirrorZ();
      } else {
        leapTransform = transform.GetLeapMatrix();
      }

      dest.CopyFrom(source).Transform(leapTransform);
    }
Exemple #28
0
        protected void OnPreCullHandTransforms(Camera camera)
        {
            if (updateHandInPrecull)
            {
                //Don't update pre cull for preview, reflection, or scene view cameras
                if (camera == null)
                {
                    camera = preCullCamera;
                }
                switch (camera.cameraType)
                {
                case CameraType.Preview:
#if UNITY_2017_1_OR_NEWER
                case CameraType.Reflection:
#endif
                case CameraType.SceneView:
                    return;
                }

                if (Application.isPlaying &&
                    !manualUpdateHasBeenCalledSinceUpdate &&
                    _leapController != null)
                {
                    manualUpdateHasBeenCalledSinceUpdate = true;

                    //Find the left and/or right hand(s) to latch
                    Hand          leftHand = null, rightHand = null;
                    LeapTransform precullLeftHand  = LeapTransform.Identity;
                    LeapTransform precullRightHand = LeapTransform.Identity;
                    for (int i = 0; i < CurrentFrame.Hands.Count; i++)
                    {
                        Hand updateHand = CurrentFrame.Hands[i];
                        if (updateHand.IsLeft && leftHand == null)
                        {
                            leftHand = updateHand;
                        }
                        else if (updateHand.IsRight && rightHand == null)
                        {
                            rightHand = updateHand;
                        }
                    }

                    //Determine their new Transforms
                    var interpolationTime = CalculateInterpolationTime();
                    _leapController.GetInterpolatedLeftRightTransform(
                        interpolationTime + (ExtrapolationAmount * 1000),
                        interpolationTime - (BounceAmount * 1000),
                        (leftHand != null ? leftHand.Id : 0),
                        (rightHand != null ? rightHand.Id : 0),
                        out precullLeftHand,
                        out precullRightHand);
                    bool leftValid  = precullLeftHand.translation != Vector.Zero;
                    bool rightValid = precullRightHand.translation != Vector.Zero;
                    transformHands(ref precullLeftHand, ref precullRightHand);

                    //Calculate the delta Transforms
                    if (rightHand != null && rightValid)
                    {
                        _transformArray[0] =
                            Matrix4x4.TRS(precullRightHand.translation.ToVector3(),
                                          precullRightHand.rotation.ToQuaternion(),
                                          Vector3.one)
                            * Matrix4x4.Inverse(Matrix4x4.TRS(rightHand.PalmPosition.ToVector3(),
                                                              rightHand.Rotation.ToQuaternion(),
                                                              Vector3.one));
                    }
                    if (leftHand != null && leftValid)
                    {
                        _transformArray[1] =
                            Matrix4x4.TRS(precullLeftHand.translation.ToVector3(),
                                          precullLeftHand.rotation.ToQuaternion(),
                                          Vector3.one)
                            * Matrix4x4.Inverse(Matrix4x4.TRS(leftHand.PalmPosition.ToVector3(),
                                                              leftHand.Rotation.ToQuaternion(),
                                                              Vector3.one));
                    }

                    //Apply inside of the vertex shader
                    Shader.SetGlobalMatrixArray(HAND_ARRAY_GLOBAL_NAME, _transformArray);
                }
            }
        }
Exemple #29
0
        protected virtual LeapTransform GetWarpedMatrix(long timestamp,
                                                        bool updateTemporalCompensation = true)
        {
            LeapTransform leapTransform;

            //Calculate a Temporally Warped Pose
            if (Application.isPlaying &&
                updateTemporalCompensation &&
                transformHistory.history.IsFull &&
                _temporalWarpingMode != TemporalWarpingMode.Off)
            {
                transformHistory.SampleTransform(timestamp
                                                 - (long)(warpingAdjustment * 1000f)
                                                 - (_temporalWarpingMode ==
                                                    TemporalWarpingMode.Images ? -20000 : 0),
                                                 out warpedPosition, out warpedRotation);
            }

            // Normalize the rotation Quaternion.
            warpedRotation = Quaternion.Lerp(warpedRotation, Quaternion.identity, 0f);

            //Calculate the Current Pose
            Pose currentPose = Pose.identity;

            if (_deviceOffsetMode == DeviceOffsetMode.Transform && deviceOrigin != null &&
                (!Application.isPlaying || _temporalWarpingMode == TemporalWarpingMode.Off))
            {
                // Transform mode at edit-time -- just use the transform pose.
                currentPose = deviceOrigin.ToPose();
            }
            else if (!Application.isPlaying)
            {
                currentPose.position =
                    currentPose.rotation * Vector3.up * deviceOffsetYAxis
                    + currentPose.rotation * Vector3.forward * deviceOffsetZAxis;
                currentPose.rotation = Quaternion.Euler(deviceTiltXAxis, 0f, 0f);
                currentPose          = transform.ToLocalPose().Then(currentPose);
            }
            else
            {
                transformHistory.SampleTransform(timestamp, out currentPose.position,
                                                 out currentPose.rotation);
            }

            // Choose between Warped and Current Pose
            bool useCurrentPosition =
                _temporalWarpingMode == TemporalWarpingMode.Off ||
                !Application.isPlaying;

            warpedPosition = useCurrentPosition ? currentPose.position : warpedPosition;
            warpedRotation = useCurrentPosition ? currentPose.rotation : warpedRotation;

            // Apply offsets (when applicable)
            if (Application.isPlaying)
            {
                if (_deviceOffsetMode != DeviceOffsetMode.Transform)
                {
                    warpedPosition += warpedRotation * Vector3.up * deviceOffsetYAxis
                                      + warpedRotation * Vector3.forward * deviceOffsetZAxis;
                    warpedRotation *= Quaternion.Euler(deviceTiltXAxis, 0f, 0f);

                    warpedRotation *= Quaternion.Euler(-90f, 180f, 0f);
                }
                else
                {
                    warpedRotation *= Quaternion.Euler(-90f, 90f, 90f);
                }
            }

            if (this == null)
            {
                // We are being destroyed, get outta here.
                return(LeapTransform.Identity);
            }
            if (transform.parent != null &&
                _deviceOffsetMode != DeviceOffsetMode.Transform)
            {
                leapTransform = new LeapTransform(
                    transform.parent.TransformPoint(warpedPosition).ToVector(),
                    (transform.parent.rotation * warpedRotation).ToLeapQuaternion(),
                    transform.lossyScale.ToVector() * 1e-3f
                    );
            }
            else
            {
                leapTransform = new LeapTransform(
                    warpedPosition.ToVector(),
                    warpedRotation.ToLeapQuaternion(),
                    transform.lossyScale.ToVector() * 1e-3f
                    );
            }

            leapTransform.MirrorZ();

            return(leapTransform);
        }
Exemple #30
0
        protected override void transformFrame(Frame source, Frame dest)
        {
            LeapTransform leapTransform = GetWarpedMatrix(source.Timestamp);

            dest.CopyFrom(source).Transform(leapTransform);
        }
Exemple #31
0
        public void LateUpdateHandTransforms(Camera camera)
        {
            if (_updateHandInPrecull)
            {
#if UNITY_EDITOR
                //Hard-coded name of the camera used to generate the pre-render view
                if (camera.gameObject.name == "PreRenderCamera")
                {
                    return;
                }

                bool isScenePreviewCamera = camera.gameObject.hideFlags == HideFlags.HideAndDontSave;
                if (isScenePreviewCamera)
                {
                    return;
                }
#endif

                if (Application.isPlaying && !manualUpdateHasBeenCalledSinceUpdate && leap_controller_ != null)
                {
                    manualUpdateHasBeenCalledSinceUpdate = true;
                    //Find the Left and/or Right Hand(s) to Latch
                    Hand          leftHand = null, rightHand = null;
                    LeapTransform PrecullLeftHand = LeapTransform.Identity, PrecullRightHand = LeapTransform.Identity;
                    for (int i = 0; i < CurrentFrame.Hands.Count; i++)
                    {
                        Hand updateHand = CurrentFrame.Hands[i];
                        if (updateHand.IsLeft && leftHand == null)
                        {
                            leftHand = updateHand;
                        }
                        else if (updateHand.IsRight && rightHand == null)
                        {
                            rightHand = updateHand;
                        }
                    }

                    //Determine their new Transforms
                    leap_controller_.GetInterpolatedLeftRightTransform(CalculateInterpolationTime() + (ExtrapolationAmount * 1000), CalculateInterpolationTime() - (BounceAmount * 1000), (leftHand != null ? leftHand.Id : 0), (rightHand != null ? rightHand.Id : 0), out PrecullLeftHand, out PrecullRightHand);
                    bool LeftValid = PrecullLeftHand.translation != Vector.Zero; bool RightValid = PrecullRightHand.translation != Vector.Zero;
                    transformHands(ref PrecullLeftHand, ref PrecullRightHand);

                    //Calculate the Delta Transforms
                    if (rightHand != null && RightValid)
                    {
                        _transformArray[0] =
                            Matrix4x4.TRS(PrecullRightHand.translation.ToVector3(), PrecullRightHand.rotation.ToQuaternion(), Vector3.one) *
                            Matrix4x4.Inverse(Matrix4x4.TRS(rightHand.PalmPosition.ToVector3(), rightHand.Rotation.ToQuaternion(), Vector3.one));
                    }
                    if (leftHand != null && LeftValid)
                    {
                        _transformArray[1] =
                            Matrix4x4.TRS(PrecullLeftHand.translation.ToVector3(), PrecullLeftHand.rotation.ToQuaternion(), Vector3.one) *
                            Matrix4x4.Inverse(Matrix4x4.TRS(leftHand.PalmPosition.ToVector3(), leftHand.Rotation.ToQuaternion(), Vector3.one));
                    }

                    //Apply inside of the vertex shader
                    Shader.SetGlobalMatrixArray(HAND_ARRAY, _transformArray);
                }
            }
        }
Exemple #32
0
        public override void OnInit(Controller arg0)
        {
            base.OnInit(arg0);

            LeapTransform = new LeapTransform(SensorOrientation.DesktopLeftward);
            //LeapTransform = new LeapTransform(SensorOrientation.MonitorSkyward);
        }
Exemple #33
0
    // Update is called once per frame
    void Update()
    {
        //Frame frame = controller.Frame(); // controller is a Controller object
        //if (frame.Hands.Count > 0)
        //{

        //List<Hand> hands = frame.Hands;
        //Hand firstHand = hands[0];

        //print(firstHand.Fingers[1].TipPosition.ToVector3());
        //if (initHand == false)
        //{
        //    hand_model = GetComponent("HandModels") as HandModel;
        //    print(hand_model);
        //    //right_leap_hand = hand_model.GetLeapHand();
        //    //if (right_leap_hand == null) Debug.LogError("No right_leap_hand founded");
        //    //initHand = true;

        //}

        //if (initHand == true)
        //{

        //    for (int i = 0; i < HandModel.NUM_FINGERS; i++)
        //    {
        //        FingerModel finger = hand_model.fingers[i];
        //        // draw ray from finger tips (enable Gizmos in Game window to see)
        //        Debug.DrawRay(finger.GetTipPosition(), finger.GetRay().direction, Color.red);
        //    }
        //}
        //}


        //LeapRightHand.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));

        //readObject();

        Frame frame = controller.Frame(); // controller is a Controller object

        if (frame.Hands.Count > 0)
        {
            //if (initHand == true)
            //{
            //print(frame.Hands.Count);
            List <Hand> hands = frame.Hands;
            right_leap_hand = hands[0];
            if (right_leap_hand.IsRight)
            {
                //Leap.Unity.Pose pose = new Leap.Unity.Pose(right_leap_hand.PalmPosition.ToVector3(), right_leap_hand.Rotation.ToQuaternion());
                LeapTransform leapMatrix = transform.GetLeapMatrix();
                //palmPossition[0] = right_leap_hand.PalmPosition.ToVector3();
                //palmPossition[1] = pose.position;

                //Vector3 handPosition = pose.position;
                //print(palmPossition[0]);
                //float handYBasis = right_leap_hand.PalmNormal.Roll;

                //float pitch = right_leap_hand.Direction.Pitch;
                //float yaw = right_leap_hand.Direction.Yaw;
                //float x = right_leap_hand.PalmNormal.Pitch;

                //float roll = right_leap_hand.PalmNormal.Roll;
                ////Vector normalPalm = new Vector(pitch, yaw, roll);
                //LeapQuaternion handRotation = right_leap_hand.Basis.rotation;

                //handRotation.z = roll;
                //handRotation.y = yaw;
                //handRotation.x = pitch;

                //Vector handZBasis = -right_leap_hand.Direction;
                Vector handOrigin = right_leap_hand.PalmPosition;
                Vector normalPalm = right_leap_hand.PalmNormal;
                //print(normalPalm);
                // print(handOrigin.Normalized);

                //print(xBasis);
                //print(Matrix4x4.TRS(handOrigin,
                //     Quaternion.identity,
                //     new Vector3(transform.lossyScale.x, transform.lossyScale.x, transform.lossyScale.z)));
                //print(rayStart.transform.worldToLocalMatrix);
                //Matrix handTransform = new Matrix(handXBasis, handYBasis, handZBasis, handOrigin);
                //handTransform = handTransform.RigidInverse();
                //Vector transformedPosition = handTransform.TransformPoint(right_leap_hand.PalmPosition);
                //float locationPart = normalPalm.x
                Vector3 normalPalmUnity  = leapMatrix.TransformDirection(normalPalm).ToVector3();
                Vector3 leapHandPosition = leapMatrix.TransformPoint(handOrigin).ToVector3();
                //Quaternion leapHandDirection = leapMatrix.translation
                //Quaternion leapHandDirection = handRotation.ToQuaternion();

                leapHandPosition.y       -= 1.8f;
                leapHandPosition.x       -= 0.7f;
                LeapRightHandRay.position = leapHandPosition;


                //print(leapHandDirection);
                //Vector3 rotationToHand = new Vector3(leapHandDirection.eulerAngles.x, leapHandDirection.eulerAngles.y, leapHandDirection.eulerAngles.z);
                rayStart = Instantiate(emptyObj, LeapRightHandRay.position, Quaternion.identity, LeapRightHandRay);

                //print(leapHandDirection.eulerAngles);
                LeapRightHandRay.forward = normalPalmUnity;
                //LeapRightHandRay.localRotation = Quaternion.Euler(rotationToHand);
                //LeapRightHandRay.localRotation = Quaternion.FromToRotation(LeapRightHandRay.forward, normalPalmUnity);
                //print(LeapRightHandRay.forward);
                //float step = speed / 4 * Time.deltaTime;
                //readObject();

                //print(printMessage.isActivated);
                //cubeRubick.transform.position = Vector3.MoveTowards(cubeRubick.transform.position, LeapRightHandRay.position, step);

                //Finger leapFinger = firstHand.Fingers[1];
                //Vector transformedPosition = handTransform.TransformPoint(leapFinger.TipPosition);
                //Vector transformedDirection = handTransform.TransformDirection(leapFinger.Direction);
                //print(transformedPosition);
                // Do something with the transformed fingers


                //Leap.Vector palmPosition = firstHand.PalmPosition;
                //Leap.Vector palmDirection = firstHand.PalmNormal;
                //Vector3 vectpalmPosition = new Vector3(palmPosition.x,
                //                                        palmPosition.y,
                //                                        palmPosition.z);

                //print(vectpalmPosition);

                ////print("Trasform ray:");
                ////print(LeapRightHand.position);
                ////print("Trasform hand pos:");
                ////print(vectpalmPosition);

                //initHand = false;


                //GameObject rayStart = Instantiate(emptyObj, vectpalmPosition, Quaternion.identity, parentHand);


                ////Vector3 unityPosition = ToPositionVector3(palmDirection);

                ////LeapRightHand.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));

                //Vector3 ray = rayStart.transform.position;
                //RaycastHit hit;
                ////print(ray);
                ////Intersecteaza raza vreun obiect in layerMask ?
                //if (Physics.Raycast(ray, LeapRightHand.forward, out hit, Mathf.Infinity, layerMask))
                //{
                //    Debug.DrawRay(ray, LeapRightHand.forward * hit.distance, Color.yellow);
                //    print(hit.collider.gameObject);
                //    //print(hit.collider.gameObject.name);
                //}
                //else
                //{
                //    Debug.DrawRay(ray, LeapRightHand.forward * 1000, Color.green);
                //}

                //print(firstHand.PalmNormal);

                // }
                //}
                //}
            }
        }
    }
 public Frame GetTransformedFrame(LeapTransform trs, int history = 0)
 {
     return(processor.frame.TransformedCopy(trs));
 }