internal static void Unregister(BoingBones bones) { if (OnBonesUnregister != null) { OnBonesUnregister(bones); } s_bonesMap.Remove(bones.GetInstanceID()); PostUnregisterBones(); }
internal static void Register(BoingBones bones) { PreRegisterBones(); s_bonesMap.Add(bones.GetInstanceID(), bones); if (OnBonesRegister != null) { OnBonesRegister(bones); } }
public void PullResults(BoingBones bones) { Instance.PullResults(bones); }
public void Execute(BoingBones bones, float dt) { float maxCollisionResolutionPushLen = bones.MaxCollisionResolutionSpeed * dt; for (int iChain = 0; iChain < bones.BoneData.Length; ++iChain) { var chain = bones.BoneChains[iChain]; var aBone = bones.BoneData[iChain]; if (aBone == null) { continue; } Vector3 gravityDt = chain.Gravity * dt; // execute boing work for (int iBone = 0; iBone < aBone.Length; ++iBone) // skip root { var bone = aBone[iBone]; // no gravity on root if (iBone > 0) { bone.Instance.PositionSpring.Velocity += gravityDt; } if (chain.ParamsOverride == null) { bone.Instance.Execute(ref bones.Params, dt); } else { bone.Instance.Execute(ref chain.ParamsOverride.Params, dt); } } var rootBone = aBone[0]; rootBone.ScaleWs = rootBone.BlendedScaleLs = rootBone.CachedScaleLs; rootBone.UpdateBounds(); chain.Bounds = rootBone.Bounds; Vector3 rootAnimPos = rootBone.Transform.position; // apply length stiffness & volume preservation for (int iBone = 1; iBone < aBone.Length; ++iBone) // skip root { var bone = aBone[iBone]; var parentBone = aBone[bone.ParentIndex]; float tLengthStiffness = 1.0f - Mathf.Pow(1.0f - bone.LengthStiffness, 30.0f * dt); // a factor of 30.0f is what makes 0.5 length stiffness looks like 50% stiffness Vector3 toParentVec = parentBone.Instance.PositionSpring.Value - bone.Instance.PositionSpring.Value; Vector3 toParentDir = VectorUtil.NormalizeSafe(toParentVec, Vector3.zero); float fullyStiffToParentLen = (parentBone.Transform.position - bone.Transform.position).magnitude; float toParentLen = toParentVec.magnitude; float fullyStiffLenDelta = toParentLen - fullyStiffToParentLen; float toParentAdjustLen = tLengthStiffness * fullyStiffLenDelta; // length stiffness { bone.Instance.PositionSpring.Value += toParentAdjustLen * toParentDir; Vector3 velocityInParentAdjustDir = Vector3.Project(bone.Instance.PositionSpring.Velocity, toParentDir); bone.Instance.PositionSpring.Velocity -= tLengthStiffness * velocityInParentAdjustDir; } // bend angle cap if (bone.BendAngleCap < MathUtil.Pi - MathUtil.Epsilon) { Vector3 animPos = bone.Transform.position; Vector3 posDelta = bone.Instance.PositionSpring.Value - rootAnimPos; posDelta = VectorUtil.ClampBend(posDelta, animPos - rootAnimPos, bone.BendAngleCap); bone.Instance.PositionSpring.Value = rootAnimPos + posDelta; } // volume preservation if (bone.SquashAndStretch > 0.0f) { float toParentLenRatio = toParentLen * MathUtil.InvSafe(fullyStiffToParentLen); float volumePreservationScale = Mathf.Sqrt(1.0f / toParentLenRatio); volumePreservationScale = Mathf.Clamp(volumePreservationScale, 1.0f / Mathf.Max(1.0f, chain.MaxStretch), Mathf.Max(1.0f, chain.MaxSquash)); Vector3 volumePreservationScaleVec = VectorUtil.ComponentWiseDivSafe(volumePreservationScale * Vector3.one, parentBone.ScaleWs); bone.BlendedScaleLs = Vector3.Lerp ( Vector3.Lerp ( bone.CachedScaleLs, volumePreservationScaleVec, bone.SquashAndStretch ), bone.CachedScaleLs, bone.AnimationBlend ); } else { bone.BlendedScaleLs = bone.CachedScaleLs; } bone.ScaleWs = VectorUtil.ComponentWiseMult(parentBone.ScaleWs, bone.BlendedScaleLs); bone.UpdateBounds(); chain.Bounds.Encapsulate(bone.Bounds); } chain.Bounds.Expand(0.2f * Vector3.one); // Boing Kit colliders if (chain.EnableBoingKitCollision) { foreach (var collider in bones.BoingColliders) { if (collider == null) { continue; } if (!chain.Bounds.Intersects(collider.Bounds)) { continue; } foreach (var bone in aBone) { if (!bone.Bounds.Intersects(collider.Bounds)) { continue; } Vector3 push; bool collided = collider.Collide(bone.Instance.PositionSpring.Value, bones.MinScale * bone.CollisionRadius, out push); if (!collided) { continue; } bone.Instance.PositionSpring.Value += VectorUtil.ClampLength(push, 0.0f, maxCollisionResolutionPushLen); bone.Instance.PositionSpring.Velocity -= Vector3.Project(bone.Instance.PositionSpring.Velocity, push); } } } // Unity colliders var sharedSphereCollider = BoingManager.SharedSphereCollider; if (chain.EnableUnityCollision && sharedSphereCollider != null) { sharedSphereCollider.enabled = true; foreach (var collider in bones.UnityColliders) { if (collider == null) { continue; } if (!chain.Bounds.Intersects(collider.bounds)) { continue; } foreach (var bone in aBone) { if (!bone.Bounds.Intersects(collider.bounds)) { continue; } sharedSphereCollider.center = bone.Instance.PositionSpring.Value; sharedSphereCollider.radius = bone.CollisionRadius; Vector3 pushDir; float pushDist; bool collided = Physics.ComputePenetration ( sharedSphereCollider, Vector3.zero, Quaternion.identity, collider, collider.transform.position, collider.transform.rotation, out pushDir, out pushDist ); if (!collided) { continue; } bone.Instance.PositionSpring.Value += VectorUtil.ClampLength(pushDir * pushDist, 0.0f, maxCollisionResolutionPushLen); bone.Instance.PositionSpring.Velocity -= Vector3.Project(bone.Instance.PositionSpring.Velocity, pushDir); } } sharedSphereCollider.enabled = false; } // self collision if (chain.EnableInterChainCollision) { foreach (var bone in aBone) { for (int iOtherChain = iChain + 1; iOtherChain < bones.BoneData.Length; ++iOtherChain) { var otherChain = bones.BoneChains[iOtherChain]; var aOtherBone = bones.BoneData[iOtherChain]; if (aOtherBone == null) { continue; } if (!otherChain.EnableInterChainCollision) { continue; } if (!chain.Bounds.Intersects(otherChain.Bounds)) { continue; } foreach (var otherBone in aOtherBone) { Vector3 push; bool collided = Collision.SphereSphere ( bone.Instance.PositionSpring.Value, bones.MinScale * bone.CollisionRadius, otherBone.Instance.PositionSpring.Value, bones.MinScale * otherBone.CollisionRadius, out push ); if (!collided) { continue; } push = VectorUtil.ClampLength(push, 0.0f, maxCollisionResolutionPushLen); float pushRatio = otherBone.CollisionRadius * MathUtil.InvSafe(bone.CollisionRadius + otherBone.CollisionRadius); bone.Instance.PositionSpring.Value += pushRatio * push; otherBone.Instance.PositionSpring.Value -= (1.0f - pushRatio) * push; bone.Instance.PositionSpring.Velocity -= Vector3.Project(bone.Instance.PositionSpring.Velocity, push); otherBone.Instance.PositionSpring.Velocity -= Vector3.Project(otherBone.Instance.PositionSpring.Velocity, push); } } } } } // end: foreach bone chain }
public void PullResults(BoingBones bones) { for (int iChain = 0; iChain < bones.BoneData.Length; ++iChain) { var chain = bones.BoneChains[iChain]; var aBone = bones.BoneData[iChain]; if (aBone == null) { continue; } // must cache before manipulating bone transforms // otherwise, we'd cache delta propagated down from parent bones foreach (var bone in aBone) { bone.CachedPositionWs = bone.Transform.position; bone.CachedPositionLs = bone.Transform.localPosition; bone.CachedRotationWs = bone.Transform.rotation; bone.CachedRotationLs = bone.Transform.localRotation; } // blend bone position for (int iBone = 0; iBone < aBone.Length; ++iBone) { var bone = aBone[iBone]; // skip fixed root if (iBone == 0 && !chain.LooseRoot) { bone.BlendedPositionWs = bone.CachedPositionWs; continue; } bone.BlendedPositionWs = Vector3.Lerp ( bone.Instance.PositionSpring.Value, bone.CachedPositionWs, bone.AnimationBlend ); } // rotation delta back-propagation if (bones.EnableRotationEffect) { for (int iBone = 0; iBone < aBone.Length; ++iBone) { var bone = aBone[iBone]; // skip fixed root if (iBone == 0 && !chain.LooseRoot) { bone.BlendedRotationWs = bone.CachedRotationWs; continue; } if (bone.ChildIndices == null) { if (bone.ParentIndex >= 0) { var parentBone = aBone[bone.ParentIndex]; bone.BlendedRotationWs = parentBone.BlendedRotationWs * (parentBone.RotationInverseWs * bone.CachedRotationWs); } continue; } Vector3 bonePos = bone.CachedPositionWs; Vector3 boneBlendedPos = bone.BlendedPositionWs; Quaternion boneRot = bone.CachedRotationWs; Quaternion boneRotInv = Quaternion.Inverse(boneRot); Vector4 childRotDeltaPsVecAccumulator = Vector3.zero; float totalWeight = 0.0f; foreach (int iChild in bone.ChildIndices) { var childBone = aBone[iChild]; Vector3 childPos = childBone.CachedPositionWs; Vector3 childPosDelta = childPos - bonePos; Vector3 childPosDeltaDir = VectorUtil.NormalizeSafe(childPosDelta, Vector3.zero); Vector3 childBlendedPos = childBone.BlendedPositionWs; Vector3 childBlendedPosDelta = childBlendedPos - boneBlendedPos; Vector3 childBlendedPosDeltaDir = VectorUtil.NormalizeSafe(childBlendedPosDelta, Vector3.zero); Quaternion childRotDelta = Quaternion.FromToRotation(childPosDeltaDir, childBlendedPosDeltaDir); Quaternion childRotDeltaPs = boneRotInv * childRotDelta; Vector4 childRotDeltaPsVec = QuaternionUtil.ToVector4(childRotDeltaPs); float weight = Mathf.Max(MathUtil.Epsilon, chain.MaxLengthFromRoot - childBone.LengthFromRoot); childRotDeltaPsVecAccumulator += weight * childRotDeltaPsVec; totalWeight += weight; } if (totalWeight > 0.0f) { Vector4 avgChildRotDeltaPsVec = childRotDeltaPsVecAccumulator / totalWeight; bone.RotationBackPropDeltaPs = QuaternionUtil.FromVector4(avgChildRotDeltaPsVec); bone.BlendedRotationWs = (boneRot * bone.RotationBackPropDeltaPs) * boneRot; } else if (bone.ParentIndex >= 0) { var parentBone = aBone[bone.ParentIndex]; bone.BlendedRotationWs = parentBone.BlendedRotationWs * (parentBone.RotationInverseWs * bone.CachedRotationWs); } } } // write blended position & adjusted rotation into final transforms for (int iBone = 0; iBone < aBone.Length; ++iBone) { var bone = aBone[iBone]; // skip fixed root if (iBone == 0 && !chain.LooseRoot) { bone.Instance.PositionSpring.Reset(bone.CachedPositionWs); bone.Instance.RotationSpring.Reset(bone.CachedRotationWs); continue; } bone.Transform.position = bone.BlendedPositionWs; bone.Transform.rotation = bone.BlendedRotationWs; bone.Transform.localScale = bone.BlendedScaleLs; } } }