/// <inheritdoc />
 public override void Destroy(TwistChainConstraintJob job)
 {
     job.chain.Dispose();
     job.weights.Dispose();
     job.steps.Dispose();
     job.rotations.Dispose();
 }
 /// <inheritdoc />
 public override void Update(TwistChainConstraintJob job, ref T data)
 {
     // Update weights based on curve.
     for (int i = 0; i < job.steps.Length; ++i)
     {
         job.weights[i] = Mathf.Clamp01(data.curve.Evaluate(job.steps[i]));
     }
 }
        /// <inheritdoc />
        public override TwistChainConstraintJob Create(Animator animator, ref T data, Component component)
        {
            // Retrieve chain in-between root and tip transforms.
            Transform[] chain = ConstraintsUtils.ExtractChain(data.root, data.tip);

            // Extract steps from chain.
            float[] steps = ConstraintsUtils.ExtractSteps(chain);

            // Build Job.
            var job = new TwistChainConstraintJob();

            job.chain      = new NativeArray <ReadWriteTransformHandle>(chain.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            job.steps      = new NativeArray <float>(chain.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            job.weights    = new NativeArray <float>(chain.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            job.rotations  = new NativeArray <Quaternion>(chain.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            job.rootTarget = ReadWriteTransformHandle.Bind(animator, data.rootTarget);
            job.tipTarget  = ReadWriteTransformHandle.Bind(animator, data.tipTarget);

            // Set values in NativeArray.
            for (int i = 0; i < chain.Length; ++i)
            {
                job.chain[i]   = ReadWriteTransformHandle.Bind(animator, chain[i]);
                job.steps[i]   = steps[i];
                job.weights[i] = Mathf.Clamp01(data.curve.Evaluate(steps[i]));
            }

            job.rotations[0] = Quaternion.identity;
            job.rotations[chain.Length - 1] = Quaternion.identity;
            for (int i = 1; i < chain.Length - 1; ++i)
            {
                job.rotations[i] = Quaternion.Inverse(Quaternion.Lerp(chain[0].rotation, chain[chain.Length - 1].rotation, job.weights[i])) * chain[i].rotation;
            }


            return(job);
        }