Example #1
0
        public override TwistChainStep1Job Create(Animator animator, ref TwistChainStep1Data 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 TwistChainStep1Job();

            job.chain      = new NativeArray <ReadWriteTransformHandle>(chain.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            job.steps      = new NativeArray <float>(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];
            }

            return(job);
        }
        /// <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);
        }