Exemplo n.º 1
0
 public void OnBodyScaledCallback(Body body)
 {
   GameObject gameObject = UnityUtil.BodyMapper.FirstOrDefault(x => x.Value.BodyId.ptr == body.BodyId.ptr).Key;
   LeapInteraction properties = gameObject.GetComponent<LeapInteraction>();
   float bodyScale = body.Scale;
   float scaleRatio = bodyScale / properties.scale;
   gameObject.transform.localScale *= scaleRatio;
   properties.scale = bodyScale;
 }
Exemplo n.º 2
0
 public static GameObject GetGameObject(Body body)
 {
     foreach(KeyValuePair<GameObject, Body> pair in UnityUtil.BodyMapper)
       {
     if (pair.Value == body)
     {
       return pair.Key;
     }
       }
       return null;
 }
Exemplo n.º 3
0
        // IntPtr bodyHandle)
        public void OnBodyAddedCallback(Body body)
        {
            // Create the game object
              bool isFinger = false;
              GameObject gameObject;
              Shape.ShapeType type = body.Shape.Type;
              if (type == Shape.ShapeType.Capsule)
              {
            float capsuleRadius = body.Shape.CapsuleRadius;
            float capsuleSegmentLength = body.Shape.CapsuleSegmentLength;

            gameObject = GameObject.Instantiate(Resources.Load(UnityUtil.ResourcesFolder + UnityUtil.FingerBoneTemplateName) as GameObject) as GameObject;

            // Adjust dimensions
            float capsuleHeight = capsuleRadius * 2f + capsuleSegmentLength;
            float yScale = capsuleHeight / 2f;
            float zxScale = capsuleRadius / 0.5f;
            gameObject.transform.localScale = new Vector3(zxScale, yScale, zxScale);
            gameObject.name = UnityUtil.FingerBoneName;

            LeapTransform t = new LeapTransform();
            t = new LeapTransform(Native.AccessPropertyAsTransform(body, Native.Property.BodyTransform, Native.Mode.Get, t.ToNative()));
            gameObject.transform.position = t.Position;
            gameObject.transform.rotation = t.Rotation;

            gameObject.rigidbody.maxAngularVelocity = 100.0f;

            isFinger = true;
            fingerBoneCount++;
              }
              else
              {
            gameObject = GameObject.Instantiate(Resources.Load(UnityUtil.ResourcesFolder + UnityUtil.HandPalmTemplateName) as GameObject) as GameObject;
            gameObject.name = UnityUtil.HandPalmName;
            LeapTransform t = new LeapTransform();
            t = new LeapTransform(Native.AccessPropertyAsTransform(body, Native.Property.BodyTransform, Native.Mode.Get, t.ToNative()));
            gameObject.transform.position = t.Position;
            gameObject.transform.rotation = t.Rotation;

            gameObject.rigidbody.maxAngularVelocity = 100.0f;

            latestHandPalmAdded = gameObject;
            fingerBoneCount = 0;
              }

              BodyMapper.Add(gameObject, body);

              GameObject container = isFinger && latestHandPalmAdded != null ? latestHandPalmAdded : GameObject.Find(UnityUtil.DynamicObjectContainerName) as GameObject;
              gameObject.transform.parent = container.transform;

              gameObject.layer = UnityUtil.LayerForHands;

              if (fingerBoneCount%3 == 0 && fingerBoneCount > 0)
              {
            gameObject.name = UnityUtil.FingerTipBoneName;
            if (fingerBoneCount == 3)
            {
              gameObject.name = UnityUtil.ThumbTipBoneName;
            }
              }

              if (fingerBoneCount == 15)
              {
            latestHandPalmAdded = null;
            fingerBoneCount = 0;

            if (UnityUtil.FilterHandCollisionPerColliderExplicitly) { DisableHandSelfCollisions(gameObject.transform.parent); }
              }
        }
Exemplo n.º 4
0
 public void OnBodyRemovedCallback(Body body)
 {
     // find gameobject
       GameObject gameObject = BodyMapper.FirstOrDefault(x => x.Value.BodyId.ptr == body.BodyId.ptr).Key;
       GameObject.Destroy(gameObject);
       BodyMapper.Remove(gameObject);
 }
    public void ApplyToBody(Body body)
    {
      switch(HandlingMode)
      {
      case HandlingModeEnum.Fixed:
        EnableAnchorRotation = false;
        EnableScaling = false;
        EnableHandCollision = false;
        AverageTransforms = false;
        break;
      case HandlingModeEnum.RotateWithOtherHand:
        EnableAnchorRotation = false;
        EnableScaling = false;
        EnableHandCollision = true;
        AverageTransforms = false;
        break;
      case HandlingModeEnum.Rotate:
        EnableAnchorRotation = true;
        EnableScaling = false;
        EnableHandCollision = true;
        AverageTransforms = false;
        break;
      case HandlingModeEnum.RotateAndScale:
        EnableAnchorRotation = true;
        EnableScaling = true;
        EnableHandCollision = true;
        AverageTransforms = false;
        break;
      case HandlingModeEnum.Averaged:
        EnableAnchorRotation = false;
        EnableScaling = false;
        EnableHandCollision = true;
        AverageTransforms = true;
        break;
      }

      MinStrengthToLockRotation = LockRotation ? 0.0f : 1.0f;

      body.MotionType = Body.MotionTypeEnum.Dynamic;

      body.EnableCollisionShapeHolding = EnableCollisionShapeHolding;
      body.EnableAnchorBasedHolding = EnableAnchorBasedHolding;
      
      body.UsePalmPositionForHoldings = PositionFromPalm;
      body.UsePalmRotationForHoldings = RotationFromPalm;
      
      body.EnableReorientationOnMultiHolding = EnableAnchorRotation;
      body.EnableScalingOnMultiHolding = EnableScaling;
      body.EnableHandCollisionWhileHolding = EnableHandCollision;
      body.EnableRotationWithFingersWhileHolding = RotateWithFingers;
      body.OvertakeControlWithNewerHoldings = ToggleMainHolding;
      
      body.EnableAveragingTransformsOnMultiHolding = AverageTransforms;
      
      body.MaxMagneticGrabDistance = MagneticDistance;

      body.EnableClicking = EnableClicking;

      if (GenerateAnchors) { body.GenerateDefaultAnchors(); }
      if (GenerateDefaultClickAnchors) { body.GenerateDefaultClickAnchors(); }
      if (EnableSmoothGrabbing) { body.EnableSmoothGrabbing(); }

      body.UseCurrentRelativeRotationWhenLockingRotation = !PredefinedRotation;
      body.LockRotationAboveThisStrength = MinStrengthToLockRotation;

      body.PinchDistanceThresholdForMinStrength = MinPinchDistnace;
      body.PinchDistanceThresholdForMaxStrength = FullPinchDistance;

      body.SetPalmAnchor(HandAnchorType);

      body.SetMagneticStrength(StrengthFactor);
    }
Exemplo n.º 6
0
 public void RemoveBody(Body body)
 {
   Native.RemoveBody(this, body); // invalidates body
   body.Invalidate();
 }
Exemplo n.º 7
0
 /// <summary>
 /// Adds a body to the scene.
 /// </summary>
 /// <param name="body"></param>
 public void AddBody(Body body)
 {
   Native.AddBody(this, body);
 }
Exemplo n.º 8
0
    static protected void RepositionHandBone(GameObject gameObject, Body leapBody)
    {
      //RepositionFreeBody(gameObject, leapBody);

      Transform unityTransform = gameObject.transform;
      LeapTransform leapTransform = leapBody.Transform;
      LeapTransform targetUnityTransform = LeapTransformToUnity(leapTransform, gameObject);

      if (gameObject.name == UnityUtil.FingerBoneName || gameObject.name == UnityUtil.FingerTipBoneName || gameObject.name == UnityUtil.ThumbTipBoneName)
      {
        // reorient the capsule.
        Quaternion flipAxes = Quaternion.Euler(90.0f, 0f, 0f);
        targetUnityTransform.Rotation = leapTransform.Rotation * flipAxes;

        // shift the capsule along it's local y-direction
        float halfHeight = unityTransform.localScale.y;
        float radius = unityTransform.localScale.x / 2f;
        Vector3 displacement = (Quaternion)targetUnityTransform.Rotation * new Vector3(0, -halfHeight+radius, 0);
        // if mirroring, reverse displacment
        if (UnityUtil.MirrorAlongZ) { displacement *= -1.0f; }
        targetUnityTransform.Position += displacement;
      }

      // either do a hard keyframe
      bool useHardKeyframe = false;
      if (useHardKeyframe || gameObject.rigidbody.isKinematic) {
        unityTransform.position = targetUnityTransform.Position;
        unityTransform.rotation = targetUnityTransform.Rotation;
        gameObject.rigidbody.velocity = Vector3.zero;
        gameObject.rigidbody.angularVelocity = Vector3.zero;
      } else {
        // or assigne velocities
        ApplyTargetUnityTransformAsVelocities(targetUnityTransform, gameObject);
      }
    }
Exemplo n.º 9
0
    static protected void RepositionHeldBody(GameObject gameObject, Body leapBody)
    {
      LeapTransform leapTransform = leapBody.Transform;
      LeapTransform targetUnityTransform = LeapTransformToUnity(leapTransform, gameObject);

      LeapInteraction leapInteraction = gameObject.GetComponent<LeapInteraction>();

      if (gameObject.rigidbody.isKinematic || !leapInteraction.UseVelocity) {

        ApplyTargetUnityTransformAsVelocities(targetUnityTransform, gameObject);

        gameObject.transform.position = targetUnityTransform.Position;
        gameObject.transform.rotation = targetUnityTransform.Rotation;
        if (true)
        {
          LeapInteraction props = gameObject.GetComponent<LeapInteraction>();
          props.tmpVelocity = gameObject.rigidbody.velocity;
          props.tmpAngularVelocity = gameObject.rigidbody.angularVelocity;
          props.velocityToTransfer = true;

          gameObject.rigidbody.velocity = Vector3.zero;
          gameObject.rigidbody.angularVelocity = Vector3.zero;
        }
      } else {
        ApplyTargetUnityTransformAsVelocities(targetUnityTransform, gameObject);
      }

      if (UnityUtil.FilterHandCollisionPerColliderExplicitly && !leapInteraction.CollisionsWithHandFilteredOut )
      {
        HandViewer.Instance.DisableHandCollisionsWithGameObject(gameObject);
        leapInteraction.CollisionsWithHandFilteredOut = true;
      }

      if (UnityUtil.LayerForHeldObjects != UnityUtil.LayerForReleasedObjects) { SetLayerForHierarchy(gameObject, UnityUtil.LayerForHeldObjects); }
    }
Exemplo n.º 10
0
    static protected void RepositionFreeBody(GameObject gameObject, Body leapBody)
    {
      LeapInteraction props = gameObject.GetComponent<LeapInteraction>();
      if (props.velocityToTransfer)
      {
        gameObject.rigidbody.velocity = props.tmpVelocity;
        gameObject.rigidbody.angularVelocity = props.tmpAngularVelocity;
        props.velocityToTransfer = false;
      }

      LeapInteraction leapInteraction = gameObject.GetComponent<LeapInteraction>();
      if (UnityUtil.FilterHandCollisionPerColliderExplicitly && leapInteraction.CollisionsWithHandFilteredOut)
      {
        HandViewer.Instance.EnableHandCollisionsWithGameObject(gameObject);
        leapInteraction.CollisionsWithHandFilteredOut = false;
      }

      if (UnityUtil.LayerForReleasedObjects != UnityUtil.LayerForHeldObjects) { SetLayerForHierarchy(gameObject, UnityUtil.LayerForReleasedObjects); }
    }
Exemplo n.º 11
0
    public Body AddBodyToLeapFromUnity(Rigidbody rigidbody)
    {
      LeapInteraction properties = rigidbody.GetComponent<LeapInteraction>();

      if (rigidbody.collider && properties)
      {
        Collider[] colliders = rigidbody.GetComponents<Collider>();
        
        Shape shape = new Shape();
        foreach(Collider collider in colliders)
        {
          if (collider is SphereCollider)
          {
            float scale = rigidbody.transform.lossyScale.x;
            SphereCollider sc = collider as SphereCollider;
            shape = Shape.CreateSphere(sc.radius * scale);
          }
          else if (collider is CapsuleCollider)
          {
            float scale = rigidbody.transform.lossyScale.x;
            CapsuleCollider cc = collider as CapsuleCollider;
            shape = Shape.CreateCapsule((Shape.CapsuleOrientation)cc.direction, Math.Max(0f, cc.height / 2f - cc.radius) * scale, cc.radius * scale);
          }
          else if (collider is BoxCollider)
          {
            BoxCollider bc = collider as BoxCollider;
            Vector3 scale = collider.transform.lossyScale;
            shape = Shape.CreateBox(Vector3.Scale(bc.size, scale) / 2f, 0f);
          }
        }
        
        if (shape != IntPtr.Zero)
        {
          Body body = new Body();//shape);
          body.Shape = shape;
          body.Mass = rigidbody.mass;
          
          // Add body anchors.
          for (int i = 0; i < rigidbody.transform.childCount; i++)
          {
            Transform child = rigidbody.transform.GetChild(i);
            if (child.name.StartsWith("Anchor") || child.name.StartsWith("ClickAnchor"))
            {
              LeapTransform anchor = new LeapTransform();
              anchor.Position = Vector3.Scale(child.localPosition - rigidbody.transform.rotation * TransformSyncUtil.GetCenterFromCollider(rigidbody.gameObject), rigidbody.transform.lossyScale);
              anchor.Rotation = child.localRotation;
              if (child.name.StartsWith("Anchor")) { body.Shape.AddAnchor(anchor); }
              if (child.name.StartsWith("ClickAnchor")) 
              {
                body.Shape.AddClickAnchor(anchor); 
              }
            }
          }
          
          // Apply BodyProperties
          properties.ApplyToBody(body);

          Scene.AddBody(body);
          BodyMapper.Add(rigidbody.gameObject, body);

          rigidbody.maxAngularVelocity = 100.0f;

          return body;
        }
      }
      return null;
    }
Exemplo n.º 12
0
        public Body AddBodyToLeapFromUnity(Rigidbody rigidbody)
        {
            LeapInteraction properties = rigidbody.GetComponent <LeapInteraction>();

            if (rigidbody.GetComponent <Collider>() && properties)
            {
                Collider[] colliders = rigidbody.GetComponents <Collider>();

                Shape shape = new Shape();
                foreach (Collider collider in colliders)
                {
                    if (collider is SphereCollider)
                    {
                        float          scale = rigidbody.transform.lossyScale.x;
                        SphereCollider sc    = collider as SphereCollider;
                        shape = Shape.CreateSphere(sc.radius * scale);
                    }
                    else if (collider is CapsuleCollider)
                    {
                        float           scale = rigidbody.transform.lossyScale.x;
                        CapsuleCollider cc    = collider as CapsuleCollider;
                        shape = Shape.CreateCapsule((Shape.CapsuleOrientation)cc.direction, Math.Max(0f, cc.height / 2f - cc.radius) * scale, cc.radius * scale);
                    }
                    else if (collider is BoxCollider)
                    {
                        BoxCollider bc    = collider as BoxCollider;
                        Vector3     scale = collider.transform.lossyScale;
                        shape = Shape.CreateBox(Vector3.Scale(bc.size, scale) / 2f, 0f);
                    }
                }

                if (shape != IntPtr.Zero)
                {
                    Body body = new Body();//shape);
                    body.Shape = shape;
                    body.Mass  = rigidbody.mass;

                    // Add body anchors.
                    for (int i = 0; i < rigidbody.transform.childCount; i++)
                    {
                        Transform child = rigidbody.transform.GetChild(i);
                        if (child.name.StartsWith("Anchor") || child.name.StartsWith("ClickAnchor"))
                        {
                            LeapTransform anchor = new LeapTransform();
                            anchor.Position = Vector3.Scale(child.localPosition - rigidbody.transform.rotation * TransformSyncUtil.GetCenterFromCollider(rigidbody.gameObject), rigidbody.transform.lossyScale);
                            anchor.Rotation = child.localRotation;
                            if (child.name.StartsWith("Anchor"))
                            {
                                body.Shape.AddAnchor(anchor);
                            }
                            if (child.name.StartsWith("ClickAnchor"))
                            {
                                body.Shape.AddClickAnchor(anchor);
                            }
                        }
                    }

                    // Apply BodyProperties
                    properties.ApplyToBody(body);

                    Scene.AddBody(body);
                    BodyMapper.Add(rigidbody.gameObject, body);

                    rigidbody.maxAngularVelocity = 100.0f;

                    return(body);
                }
            }
            return(null);
        }
 public void RemoveBody(Body body)
 {
     Native.RemoveBody(this, body); // invalidates body
     body.Invalidate();
 }
 /// <summary>
 /// Adds a body to the scene.
 /// </summary>
 /// <param name="body"></param>
 public void AddBody(Body body)
 {
     Native.AddBody(this, body);
 }