static protected LeapTransform CalcUnityTransformForNextFrame(GameObject gameObject) { //return gameObject.transform; // skip velocities -- return this frame Transform unityTransform = gameObject.transform; LeapTransform targetTransform = new LeapTransform(); // apply angular velcocit - rotation Quaternion extraRotation = Quaternion.identity; Vector3 angularDisplacement = gameObject.GetComponent <Rigidbody>().angularVelocity *Time.deltaTime; float angularMagnitude = angularDisplacement.magnitude; if (angularMagnitude > 0.0001f) { extraRotation = Quaternion.AngleAxis(angularMagnitude, angularDisplacement / angularMagnitude); } targetTransform.Rotation = extraRotation * unityTransform.rotation; // apply angular & linear velocities to body Vector3 oldCom = unityTransform.position + unityTransform.rotation * unityTransform.GetComponent <Rigidbody>().centerOfMass; Vector3 newCom = oldCom + gameObject.GetComponent <Rigidbody>().velocity *Time.deltaTime; targetTransform.Position = newCom - (Quaternion)targetTransform.Rotation * unityTransform.GetComponent <Rigidbody>().centerOfMass; return(targetTransform); }
static protected void ApplyTargetUnityTransformAsVelocities(LeapTransform targetUnityTransform, GameObject gameObject) { Vector3 angularVelocity = CalcAngularVelocityToTarget(gameObject, targetUnityTransform.Rotation); gameObject.GetComponent <Rigidbody>().velocity = CalcLinearVelocityToTarget(gameObject, targetUnityTransform.Position, targetUnityTransform.Rotation, angularVelocity); gameObject.GetComponent <Rigidbody>().angularVelocity = angularVelocity; }
static protected LeapTransform UnityTransformToLeap(LeapTransform unityTransform, GameObject gameObject) { LeapTransform leapTransform = new LeapTransform(); float scale = gameObject.transform.lossyScale.x; leapTransform.Position = unityTransform.Position + (Quaternion)unityTransform.Rotation * GetCenterFromCollider(gameObject) * scale; leapTransform.Rotation = unityTransform.Rotation; return leapTransform; }
static protected LeapTransform UnityTransformToLeap(LeapTransform unityTransform, GameObject gameObject) { LeapTransform leapTransform = new LeapTransform(); float scale = gameObject.transform.lossyScale.x; leapTransform.Position = unityTransform.Position + (Quaternion)unityTransform.Rotation * GetCenterFromCollider(gameObject) * scale; leapTransform.Rotation = unityTransform.Rotation; return(leapTransform); }
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.GetComponent <Rigidbody>().isKinematic) { unityTransform.position = targetUnityTransform.Position; unityTransform.rotation = targetUnityTransform.Rotation; gameObject.GetComponent <Rigidbody>().velocity = Vector3.zero; gameObject.GetComponent <Rigidbody>().angularVelocity = Vector3.zero; } else { // or assigne velocities ApplyTargetUnityTransformAsVelocities(targetUnityTransform, gameObject); } }
static protected void RepositionHeldBody(GameObject gameObject, Body leapBody) { LeapTransform leapTransform = leapBody.Transform; LeapTransform targetUnityTransform = LeapTransformToUnity(leapTransform, gameObject); LeapInteraction leapInteraction = gameObject.GetComponent <LeapInteraction>(); if (gameObject.GetComponent <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.GetComponent <Rigidbody>().velocity; props.tmpAngularVelocity = gameObject.GetComponent <Rigidbody>().angularVelocity; props.velocityToTransfer = true; gameObject.GetComponent <Rigidbody>().velocity = Vector3.zero; gameObject.GetComponent <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); } }
/// <summary> /// Updates Leap information from your scene. This is usually done after your Unity physics is stepped, /// and actions are applied. /// </summary> static public void UpdateLeapFromUnity(Dictionary <UnityEngine.GameObject, Body> BodyMapper) { foreach (KeyValuePair <GameObject, Body> pair in BodyMapper) { GameObject gameObject = pair.Key; if (gameObject == null) { // Remove the body from mapping & // TODO: remove from leap interact continue; } if (gameObject.name != UnityUtil.FingerBoneName && gameObject.name != UnityUtil.FingerTipBoneName && gameObject.name != UnityUtil.ThumbTipBoneName && gameObject.name != UnityUtil.HandPalmName) { LeapTransform nextFrameUnityTransform = CalcUnityTransformForNextFrame(gameObject); LeapTransform targetLeapTransform = UnityTransformToLeap(nextFrameUnityTransform, gameObject); Body body = pair.Value; body.Transform = targetLeapTransform; } } }
static protected LeapTransform CalcUnityTransformForNextFrame(GameObject gameObject) { //return gameObject.transform; // skip velocities -- return this frame Transform unityTransform = gameObject.transform; LeapTransform targetTransform = new LeapTransform(); // apply angular velcocit - rotation Quaternion extraRotation = Quaternion.identity; Vector3 angularDisplacement = gameObject.rigidbody.angularVelocity * Time.deltaTime; float angularMagnitude = angularDisplacement.magnitude; if (angularMagnitude > 0.0001f) { extraRotation = Quaternion.AngleAxis(angularMagnitude, angularDisplacement / angularMagnitude); } targetTransform.Rotation = extraRotation * unityTransform.rotation; // apply angular & linear velocities to body Vector3 oldCom = unityTransform.position + unityTransform.rotation * unityTransform.rigidbody.centerOfMass; Vector3 newCom = oldCom + gameObject.rigidbody.velocity * Time.deltaTime; targetTransform.Position = newCom - (Quaternion)targetTransform.Rotation * unityTransform.rigidbody.centerOfMass; return targetTransform; }
static protected void ApplyTargetUnityTransformAsVelocities(LeapTransform targetUnityTransform, GameObject gameObject) { Vector3 angularVelocity = CalcAngularVelocityToTarget(gameObject, targetUnityTransform.Rotation); gameObject.rigidbody.velocity = CalcLinearVelocityToTarget(gameObject, targetUnityTransform.Position, targetUnityTransform.Rotation, angularVelocity); gameObject.rigidbody.angularVelocity = angularVelocity; }
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); }
public void AddClickAnchor(LeapTransform t) { Native.AccessPropertyAsTransform(this, Native.Property.ShapeClickAnchors, Native.Mode.Add, t.ToNative()); }
public void OnBodyAddedCallback(Body body)// IntPtr bodyHandle) { // 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); } } }
protected static LeapTransform LeapTransformToUnity(LeapTransform leapTransform, GameObject gameObject) { LeapTransform unityTransform = new LeapTransform(); float scale = gameObject.transform.lossyScale.x; unityTransform.Position = leapTransform.Position - (Quaternion)leapTransform.Rotation * GetCenterFromCollider(gameObject) * scale; unityTransform.Rotation = leapTransform.Rotation; return unityTransform; }
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; }
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; }
// 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); } } }