Exemple #1
0
        public void ProcessAnimation(AnimationStream stream)
        {
            float jobWeight = stream.GetInputWeight(0);

            if (jobWeight > 0f)
            {
                float sumWeights = AnimationRuntimeUtils.Sum(cache, sourceWeightStartIdx, sources.Length);
                if (sumWeights < k_Epsilon)
                {
                    return;
                }

                float weightScale = sumWeights > 1f ? 1f / sumWeights : 1f;

                Vector3    currentWPos = driven.GetPosition(stream);
                Quaternion currentWRot = driven.GetRotation(stream);
                var        accumTx     = new AffineTransform(currentWPos, currentWRot);
                for (int i = 0; i < sources.Length; ++i)
                {
                    var normalizedWeight = cache.GetRaw(sourceWeightStartIdx, i) * weightScale;
                    if (normalizedWeight < k_Epsilon)
                    {
                        continue;
                    }

                    var sourceTx = new AffineTransform(sources[i].GetPosition(stream), sources[i].GetRotation(stream));
                    sourceTx *= sourceOffsets[i];

                    accumTx.rotation     = Quaternion.Lerp(accumTx.rotation, sourceTx.rotation, normalizedWeight);
                    accumTx.translation += (sourceTx.translation - currentWPos) * normalizedWeight;
                }

                // Convert accumTx to local space
                if (drivenParent.IsValid(stream))
                {
                    var parentTx = new AffineTransform(drivenParent.GetPosition(stream), drivenParent.GetRotation(stream));
                    accumTx = parentTx.InverseMul(accumTx);
                }

                Vector3    currentLPos = driven.GetLocalPosition(stream);
                Quaternion currentLRot = driven.GetLocalRotation(stream);
                if (Vector3.Dot(positionAxesMask, positionAxesMask) < 3f)
                {
                    accumTx.translation = AnimationRuntimeUtils.Lerp(currentLPos, accumTx.translation, positionAxesMask);
                }
                if (Vector3.Dot(rotationAxesMask, rotationAxesMask) < 3f)
                {
                    accumTx.rotation = Quaternion.Euler(AnimationRuntimeUtils.Lerp(currentLRot.eulerAngles, accumTx.rotation.eulerAngles, rotationAxesMask));
                }

                driven.SetLocalPosition(stream, Vector3.Lerp(currentLPos, accumTx.translation, jobWeight));
                driven.SetLocalRotation(stream, Quaternion.Lerp(currentLRot, accumTx.rotation, jobWeight));
            }
            else
            {
                AnimationRuntimeUtils.PassThrough(stream, driven);
            }
        }
Exemple #2
0
        public void ProcessAnimation(AnimationStream stream)
        {
            float jobWeight = stream.GetInputWeight(0);

            if (jobWeight > 0f)
            {
                float sumWeights = AnimationRuntimeUtils.Sum(cache, sourceWeightStartIdx, sources.Length);
                if (sumWeights < k_Epsilon)
                {
                    return;
                }

                float weightScale = sumWeights > 1f ? 1f / sumWeights : 1f;

                Quaternion currentWRot = driven.GetRotation(stream);
                Quaternion accumRot    = currentWRot;
                for (int i = 0; i < sources.Length; ++i)
                {
                    var normalizedWeight = cache.GetRaw(sourceWeightStartIdx, i) * weightScale;
                    if (normalizedWeight < k_Epsilon)
                    {
                        continue;
                    }

                    accumRot = Quaternion.Lerp(accumRot, sources[i].GetRotation(stream) * sourceOffsets[i], normalizedWeight);
                }

                // Convert accumRot to local space
                if (drivenParent.IsValid(stream))
                {
                    accumRot = Quaternion.Inverse(drivenParent.GetRotation(stream)) * accumRot;
                }

                Quaternion currentLRot = driven.GetLocalRotation(stream);
                if (Vector3.Dot(axesMask, axesMask) < 3f)
                {
                    accumRot = Quaternion.Euler(AnimationRuntimeUtils.Lerp(currentLRot.eulerAngles, accumRot.eulerAngles, axesMask));
                }

                var offset = cache.Get <Vector3>(drivenOffsetIdx);
                if (Vector3.Dot(offset, offset) > 0f)
                {
                    accumRot *= Quaternion.Euler(offset);
                }

                driven.SetLocalRotation(stream, Quaternion.Lerp(currentLRot, accumRot, jobWeight));
            }
            else
            {
                AnimationRuntimeUtils.PassThrough(stream, driven);
            }
        }
        public void ProcessAnimation(AnimationStream stream)
        {
            float jobWeight = stream.GetInputWeight(0);

            if (jobWeight > 0f)
            {
                AffineTransform overrideTx;
                if (source.IsValid(stream))
                {
                    var sourceLocalTx    = new AffineTransform(source.GetLocalPosition(stream), source.GetLocalRotation(stream));
                    var sourceToSpaceRot = cache.Get <Quaternion>(sourceToCurrSpaceRotIdx);
                    overrideTx = Quaternion.Inverse(sourceToSpaceRot) * (sourceInvLocalBindTx * sourceLocalTx) * sourceToSpaceRot;
                }
                else
                {
                    overrideTx = new AffineTransform(cache.Get <Vector3>(positionIdx), Quaternion.Euler(cache.Get <Vector3>(rotationIdx)));
                }

                Space overrideSpace = (Space)cache.GetRaw(spaceIdx);
                var   posW          = cache.GetRaw(positionWeightIdx) * jobWeight;
                var   rotW          = cache.GetRaw(rotationWeightIdx) * jobWeight;
                switch (overrideSpace)
                {
                case Space.World:
                    driven.SetPosition(stream, Vector3.Lerp(driven.GetPosition(stream), overrideTx.translation, posW));
                    driven.SetRotation(stream, Quaternion.Lerp(driven.GetRotation(stream), overrideTx.rotation, rotW));
                    break;

                case Space.Local:
                    driven.SetLocalPosition(stream, Vector3.Lerp(driven.GetLocalPosition(stream), overrideTx.translation, posW));
                    driven.SetLocalRotation(stream, Quaternion.Lerp(driven.GetLocalRotation(stream), overrideTx.rotation, rotW));
                    break;

                case Space.Pivot:
                    var drivenLocalTx = new AffineTransform(driven.GetLocalPosition(stream), driven.GetLocalRotation(stream));
                    overrideTx = drivenLocalTx * overrideTx;
                    driven.SetLocalPosition(stream, Vector3.Lerp(drivenLocalTx.translation, overrideTx.translation, posW));
                    driven.SetLocalRotation(stream, Quaternion.Lerp(drivenLocalTx.rotation, overrideTx.rotation, rotW));
                    break;

                default:
                    break;
                }
            }
            else
            {
                AnimationRuntimeUtils.PassThrough(stream, driven);
            }
        }
Exemple #4
0
        public void ProcessAnimation(AnimationStream stream)
        {
            float jobWeight = stream.GetInputWeight(0);

            if (jobWeight > 0f)
            {
                float sumWeights = AnimationRuntimeUtils.Sum(cache, sourceWeightStartIdx, sources.Length);
                if (sumWeights < k_Epsilon)
                {
                    return;
                }

                float weightScale = sumWeights > 1f ? 1f / sumWeights : 1f;

                Vector3 currentWPos = driven.GetPosition(stream);
                Vector3 accumPos    = currentWPos;
                for (int i = 0; i < sources.Length; ++i)
                {
                    var normalizedWeight = cache.GetRaw(sourceWeightStartIdx, i) * weightScale;
                    if (normalizedWeight < k_Epsilon)
                    {
                        continue;
                    }

                    accumPos += (sources[i].GetPosition(stream) + sourceOffsets[i] - currentWPos) * normalizedWeight;
                }

                // Convert accumPos to local space
                if (drivenParent.IsValid(stream))
                {
                    var parentTx = new AffineTransform(drivenParent.GetPosition(stream), drivenParent.GetRotation(stream));
                    accumPos = parentTx.InverseTransform(accumPos);
                }

                Vector3 currentLPos = driven.GetLocalPosition(stream);
                if (Vector3.Dot(axesMask, axesMask) < 3f)
                {
                    accumPos = AnimationRuntimeUtils.Lerp(currentLPos, accumPos, axesMask);
                }

                driven.SetLocalPosition(stream, Vector3.Lerp(currentLPos, accumPos + cache.Get <Vector3>(drivenOffsetIdx), jobWeight));
            }
            else
            {
                AnimationRuntimeUtils.PassThrough(stream, driven);
            }
        }
        public static void SolveTwoBoneIK(
            AnimationStream stream,
            TransformHandle root,
            TransformHandle mid,
            TransformHandle tip,
            TransformHandle target,
            TransformHandle hint,
            float posWeight,
            float rotWeight,
            float hintWeight,
            Vector2 limbLengths,
            AffineTransform targetOffset
            )
        {
            Vector3    aPosition = root.GetPosition(stream);
            Vector3    bPosition = mid.GetPosition(stream);
            Vector3    cPosition = tip.GetPosition(stream);
            Vector3    tPosition = Vector3.Lerp(cPosition, target.GetPosition(stream) + targetOffset.translation, posWeight);
            Quaternion tRotation = Quaternion.Lerp(tip.GetRotation(stream), target.GetRotation(stream) * targetOffset.rotation, rotWeight);
            bool       hasHint   = hint.IsValid(stream) && hintWeight > 0f;

            Vector3 ab = bPosition - aPosition;
            Vector3 bc = cPosition - bPosition;
            Vector3 ac = cPosition - aPosition;
            Vector3 at = tPosition - aPosition;

            float oldAbcAngle = TriangleAngle(ac.magnitude, limbLengths[0], limbLengths[1]);
            float newAbcAngle = TriangleAngle(at.magnitude, limbLengths[0], limbLengths[1]);

            // Bend normal strategy is to take whatever has been provided in the animation
            // stream to minimize configuration changes, however if this is collinear
            // try computing a bend normal given the desired target position.
            // If this also fails, try resolving axis using hint if provided.
            Vector3 axis = Vector3.Cross(ab, bc);

            if (axis.sqrMagnitude < k_SqrEpsilon)
            {
                axis = Vector3.Cross(at, bc);
                if (axis.sqrMagnitude < k_SqrEpsilon)
                {
                    axis = hasHint ? Vector3.Cross(hint.GetPosition(stream) - aPosition, bc) : Vector3.up;
                }
            }
            axis = Vector3.Normalize(axis);

            float      a      = 0.5f * (oldAbcAngle - newAbcAngle);
            float      sin    = Mathf.Sin(a);
            float      cos    = Mathf.Cos(a);
            Quaternion deltaR = new Quaternion(axis.x * sin, axis.y * sin, axis.z * sin, cos);

            mid.SetRotation(stream, deltaR * mid.GetRotation(stream));

            cPosition = tip.GetPosition(stream);
            ac        = cPosition - aPosition;
            root.SetRotation(stream, QuaternionExt.FromToRotation(ac, at) * root.GetRotation(stream));

            if (hasHint)
            {
                float acSqrMag = ac.sqrMagnitude;
                if (acSqrMag > 0f)
                {
                    bPosition = mid.GetPosition(stream);
                    cPosition = tip.GetPosition(stream);
                    ab        = bPosition - aPosition;
                    ac        = cPosition - aPosition;

                    Vector3 acNorm = ac / Mathf.Sqrt(acSqrMag);
                    Vector3 ah     = hint.GetPosition(stream) - aPosition;
                    Vector3 abProj = ab - acNorm * Vector3.Dot(ab, acNorm);
                    Vector3 ahProj = ah - acNorm * Vector3.Dot(ah, acNorm);

                    float maxReach = limbLengths[0] + limbLengths[1];
                    if (abProj.sqrMagnitude > (maxReach * maxReach * 0.001f) && ahProj.sqrMagnitude > 0f)
                    {
                        Quaternion hintR = QuaternionExt.FromToRotation(abProj, ahProj);
                        hintR.x *= hintWeight;
                        hintR.y *= hintWeight;
                        hintR.z *= hintWeight;
                        root.SetRotation(stream, hintR * root.GetRotation(stream));
                    }
                }
            }

            tip.SetRotation(stream, tRotation);
        }
Exemple #6
0
        public void ProcessAnimation(AnimationStream stream)
        {
            float jobWeight = stream.GetInputWeight(0);

            if (jobWeight > 0f)
            {
                float sumWeights = AnimationRuntimeUtils.Sum(cache, sourceWeightStartIdx, sources.Length);
                if (sumWeights < k_Epsilon)
                {
                    return;
                }

                float weightScale = sumWeights > 1f ? 1f / sumWeights : 1f;

                Vector2    minMaxAngles  = cache.Get <Vector2>(limitsIdx);
                Vector3    currentWPos   = driven.GetPosition(stream);
                Quaternion currentWRot   = driven.GetRotation(stream);
                Vector3    currentDir    = currentWRot * aimAxis;
                Quaternion accumDeltaRot = Quaternion.identity;
                for (int i = 0; i < sources.Length; ++i)
                {
                    var normalizedWeight = cache.GetRaw(sourceWeightStartIdx, i) * weightScale;
                    if (normalizedWeight < k_Epsilon)
                    {
                        continue;
                    }

                    var toDir       = sources[i].GetPosition(stream) - currentWPos;
                    var rotToSource = Quaternion.AngleAxis(
                        Mathf.Clamp(Vector3.Angle(currentDir, toDir), minMaxAngles.x, minMaxAngles.y),
                        Vector3.Cross(currentDir, toDir).normalized
                        );

                    accumDeltaRot = Quaternion.Lerp(accumDeltaRot, sourceOffsets[i] * rotToSource, normalizedWeight);
                }
                Quaternion newRot = accumDeltaRot * currentWRot;

                // Convert newRot to local space
                if (drivenParent.IsValid(stream))
                {
                    newRot = Quaternion.Inverse(drivenParent.GetRotation(stream)) * newRot;
                }

                Quaternion currentLRot = driven.GetLocalRotation(stream);
                if (Vector3.Dot(axesMask, axesMask) < 3f)
                {
                    newRot = Quaternion.Euler(AnimationRuntimeUtils.Lerp(currentLRot.eulerAngles, newRot.eulerAngles, axesMask));
                }

                var offset = cache.Get <Vector3>(drivenOffset);
                if (Vector3.Dot(offset, offset) > 0f)
                {
                    newRot *= Quaternion.Euler(offset);
                }

                driven.SetLocalRotation(stream, Quaternion.Lerp(currentLRot, newRot, jobWeight));
            }
            else
            {
                AnimationRuntimeUtils.PassThrough(stream, driven);
            }
        }