/// <summary>
        /// Setup binding between character joints and animator stream, that will be written by Kinematica job
        /// </summary>
        /// <param name="animator">Unity animator associated with the animation job</param>
        /// <param name="transforms">Character joint transforms</param>
        /// <param name="synthesizer">Reference to motion synthesizer</param>
        /// <param name="deltaTimePropertyHandle">Property handle for the deltaTime</param>
        /// <returns></returns>
        public bool Setup(Animator animator, Transform[] transforms, ref MotionSynthesizer synthesizer, PropertySceneHandle deltaTimePropertyHandle)
        {
            this.synthesizer = MemoryRef <MotionSynthesizer> .Create(ref synthesizer);

            int numJoints = synthesizer.Binary.numJoints;

            this.transforms = new NativeArray <TransformStreamHandle>(numJoints, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            boundJoints = new NativeArray <bool>(numJoints, Allocator.Persistent, NativeArrayOptions.ClearMemory);

            // Root joint is always first transform, and names don't need to match contrary to other joints
            this.transforms[0] = animator.BindStreamTransform(transforms[0]);
            boundJoints[0]     = true;

            for (int i = 1; i < transforms.Length; i++)
            {
                int jointNameIndex = synthesizer.Binary.GetStringIndex(transforms[i].name);
                int jointIndex     = (jointNameIndex >= 0) ? synthesizer.Binary.animationRig.GetJointIndexForNameIndex(jointNameIndex) : -1;
                if (jointIndex >= 0)
                {
                    this.transforms[jointIndex] = animator.BindStreamTransform(transforms[i]);
                    boundJoints[jointIndex]     = true;
                }
            }

            deltaTime = deltaTimePropertyHandle;

            return(true);
        }
            public void Setup(Animator animator, RotationConstraint[] constraints)
            {
                int count = constraints.Length;

                referenceTransforms = new NativeArray <TransformStreamHandle>(count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
                currentTransforms   = new NativeArray <TransformStreamHandle>(count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

                minMaxSpeeds = new NativeArray <Vector2>(count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
                minMaxAngles = new NativeArray <Vector2>(count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

                lastPositions    = new NativeArray <Vector3>(count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
                initialRotations = new NativeArray <Quaternion>(count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
                lastAngles       = new NativeArray <float>(count, Allocator.Persistent);

                for (int i = 0; i < count; ++i)
                {
                    Assert.IsTrue(constraints[i].referenceTransform != null);
                    Assert.IsTrue(constraints[i].transform != null);

                    referenceTransforms[i] = animator.BindStreamTransform(constraints[i].referenceTransform);
                    currentTransforms[i]   = animator.BindStreamTransform(constraints[i].transform);

                    minMaxSpeeds[i] = new Vector2(constraints[i].minimumSpeed, constraints[i].maximumSpeed);
                    minMaxAngles[i] = new Vector2(constraints[i].minimumAngle, constraints[i].maximumAngle);

                    lastPositions[i]    = constraints[i].referenceTransform.position;
                    initialRotations[i] = constraints[i].transform.localRotation;
                }

                deltaTime = animator.BindSceneProperty(animator.gameObject.transform, typeof(Unit), "m_deltaTime");
            }
示例#3
0
        public void Setup(Animator animator, Transform[] bones, Transform target)
        {
            this.bones       = new NativeArray <TransformStreamHandle>(bones.Length, Allocator.Persistent);
            this.boneWeights = new NativeArray <PropertySceneHandle>(bones.Length - 1, Allocator.Persistent);
            this.boneSqrMags = new NativeArray <float>(bones.Length - 1, Allocator.Persistent);

            for (int i = 0; i < this.bones.Length; i++)
            {
                this.bones[i] = animator.BindStreamTransform(bones[i]);
            }

            for (int i = 0; i < this.bones.Length - 1; i++)
            {
                var boneParams = bones[i].gameObject.GetComponent <IKJBoneParams>();
                if (boneParams == null)
                {
                    boneParams = bones[i].gameObject.AddComponent <IKJBoneParams>();
                }

                this.boneWeights[i] = animator.BindSceneProperty(bones[i].transform, typeof(IKJBoneParams), "weight");
            }

            // Rotation Limits
            SetUpRotationLimits(animator, bones);

            _target           = animator.BindSceneTransform(target);
            _IKPositionWeight = animator.BindSceneProperty(animator.transform, typeof(CCDIKJ), "weight");
            _maxIterations    = animator.BindSceneProperty(animator.transform, typeof(CCDIKJ), "maxIterations");
            _tolerance        = animator.BindSceneProperty(animator.transform, typeof(CCDIKJ), "tolerance");
            _XY = animator.BindSceneProperty(animator.transform, typeof(CCDIKJ), "XY");
            _useRotationLimits = animator.BindSceneProperty(animator.transform, typeof(CCDIKJ), "useRotationLimits");
        }
示例#4
0
    public bool Setup(Animator animator, IkChain chain, Type componentType, string weightProperty, string weightOffsetProperty, string targetOffsetProperty)
    {
        if (!chain.HasValidData())
        {
            return(false);
        }

        // Target
        m_TargetOffset = chain.target.offset;

        if (chain.target.readFrom == TargetType.Stream)
        {
            m_EffectorStreamHandle = animator.BindStreamTransform(chain.target.target);
            m_UseStreamEffector    = true;
        }
        else
        {
            m_EffectorSceneHandle = animator.BindSceneTransform(chain.target.target);
            m_UseStreamEffector   = false;
        }


        // Weight
        if (chain.weight.useAnimatorProperty && chain.weight.propertyName != "")
        {
            m_AnimatorWeight      = animator.BindStreamProperty(animator.transform, typeof(Animator), chain.weight.propertyName);
            m_UseAnimatorProperty = true;
        }

        m_WeightHandle         = animator.BindSceneProperty(animator.transform, componentType, weightProperty);
        m_AnimatorWeightOffset = animator.BindSceneProperty(animator.transform, componentType, weightOffsetProperty);


        // Driven
        m_IkType = chain.driven.type;

        if (m_IkType == IkType.Generic)
        {
            var end   = chain.driven.genericEndJoint;
            var mid   = end.parent;
            var start = mid.parent;

            m_StartHandle = animator.BindStreamTransform(start);
            m_MidHandle   = animator.BindStreamTransform(mid);
            m_EndHandle   = animator.BindStreamTransform(end);
        }
        else
        {
            m_HumanLimb = chain.driven.humanoidLimb;
        }

        return(true);
    }
示例#5
0
        public void Setup(Animator animator, Transform[] bones, Transform target, Transform poleTarget, Transform aimTransform)
        {
            this.bones       = new NativeArray <TransformStreamHandle>(bones.Length, Allocator.Persistent);
            this.boneWeights = new NativeArray <PropertySceneHandle>(bones.Length, Allocator.Persistent);

            for (int i = 0; i < this.bones.Length; i++)
            {
                this.bones[i] = animator.BindStreamTransform(bones[i]);
            }


            for (int i = 0; i < this.bones.Length; i++)
            {
                var boneParams = bones[i].gameObject.GetComponent <IKJBoneParams>();
                if (boneParams == null)
                {
                    boneParams = bones[i].gameObject.AddComponent <IKJBoneParams>();
                }

                this.boneWeights[i] = animator.BindSceneProperty(bones[i].transform, typeof(IKJBoneParams), "weight");
            }

            // Rotation Limits
            SetUpRotationLimits(animator, bones);

            _target           = animator.BindSceneTransform(target);
            _poleTarget       = animator.BindSceneTransform(poleTarget);
            _transform        = animator.BindStreamTransform(aimTransform);
            _IKPositionWeight = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "weight");
            _poleWeight       = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "poleWeight");
            _axisX            = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "axisX");
            _axisY            = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "axisY");
            _axisZ            = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "axisZ");
            _poleAxisX        = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "poleAxisX");
            _poleAxisY        = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "poleAxisY");
            _poleAxisZ        = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "poleAxisZ");
            _clampWeight      = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "clampWeight");
            _clampSmoothing   = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "clampSmoothing");
            _maxIterations    = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "maxIterations");
            _tolerance        = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "tolerance");
            _XY = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "XY");
            _useRotationLimits = animator.BindSceneProperty(animator.transform, typeof(AimIKJ), "useRotationLimits");

            step = 1f / (float)bones.Length;
        }