Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
5.9 kB
1
Indexable
Never
public void StartAnimation(GPUICrowdRuntimeData runtimeData, int arrayIndex, GPUIAnimationClipData clipData, float startTime = -1.0f, float speed = 1.0f, float transitionTime = 0)
        {
            float currentTime = Time.time;

            bool hasTransition = transitionTime > 0;

            int previousClipIndex = GetCurrentClipIndex(clipData);

            if (speed <= 0)
                speed = 0.000001f;

            if (startTime >= 0)
                newClipStartTimes.x = currentTime - startTime;
            else if (previousClipIndex >= 0)
            {
                if (speed == currentClipSpeeds[previousClipIndex])
                    newClipStartTimes.x = currentClipStartTimes[previousClipIndex];
                else
                    newClipStartTimes.x = GetSpeedRelativeStartTime(currentTime, currentClipStartTimes[previousClipIndex], currentClipSpeeds[previousClipIndex], clipData.length, speed);
            }
            else
                newClipStartTimes.x = currentTime;
            newClipStartTimes.y = 0;
            newClipStartTimes.z = 0;
            newClipStartTimes.w = 0;

            if (hasTransition)
            {
                currentAnimationClipData[3] = currentAnimationClipData[2];
                currentAnimationClipData[2] = currentAnimationClipData[1];
                currentAnimationClipData[1] = currentAnimationClipData[0];
                currentAnimationClipData[0] = clipData;

                currentAnimationClipDataWeights.w = currentAnimationClipDataWeights.z;
                currentAnimationClipDataWeights.z = currentAnimationClipDataWeights.y;
                currentAnimationClipDataWeights.y = currentAnimationClipDataWeights.x;
                currentAnimationClipDataWeights.x = 0.01f;

                currentClipSpeeds[3] = currentClipSpeeds[2];
                currentClipSpeeds[2] = currentClipSpeeds[1];
                currentClipSpeeds[1] = currentClipSpeeds[0];
                currentClipSpeeds[0] = speed;

                newClipStartTimes[3] = currentClipStartTimes[2];
                newClipStartTimes[2] = currentClipStartTimes[1];
                newClipStartTimes[1] = currentClipStartTimes[0];

                if (transition == null)
                    transition = new GPUICrowdTransition();
                transition.SetData(arrayIndex, currentTime, transitionTime, activeClipCount, currentAnimationClipDataWeights, new Vector4(1, 0, 0, 0), 1);
                if (transitionIndex < 0)
                {
                    transitionIndex = runtimeData.transitioningAnimators.Count;
                    runtimeData.transitioningAnimators.Add(this);
                }

                activeClipCount += 1;
                if (activeClipCount > 4)
                    activeClipCount = 4;
            }
            else
            {
                currentAnimationClipData[3] = default;
                currentAnimationClipData[2] = default;
                currentAnimationClipData[1] = default;
                currentAnimationClipData[0] = clipData;

                currentAnimationClipDataWeights.w = 0;
                currentAnimationClipDataWeights.z = 0;
                currentAnimationClipDataWeights.y = 0;
                currentAnimationClipDataWeights.x = 1;

                currentClipSpeeds[0] = speed;

                activeClipCount = 1;
            }


            BlendAnimations(runtimeData, arrayIndex, hasTransition);
        }

private void BlendAnimations(GPUICrowdRuntimeData runtimeData, int arrayIndex, bool hasTransition = false)
        {
            if (!hasTransition && transitionIndex >= 0)
            {
                RemoveFromTransitioningAnimatorsSwapBack(runtimeData);
            }

            int animationIndex = arrayIndex * 2;
            int crowdAnimIndex = arrayIndex * 4;
            currentClipStartTimes = newClipStartTimes;

            GPUIAnimationClipData clipData;
            Vector4 animationFrames = Vector4.zero;
            for (int i = 0; i < 4; i++)
            {
                if (i < activeClipCount)
                {
                    clipData = currentAnimationClipData[i];

                    // set min-max frames and speed
                    runtimeData.crowdAnimatorControllerData[crowdAnimIndex + i] = new Vector4(
                        clipData.clipStartFrame,
                        // If loop is disabled send max frame as negative (trick to conserve memory)
                        clipData.IsLoopDisabled() ? -(clipData.clipStartFrame + clipData.clipFrameCount - 1) : clipData.clipStartFrame + clipData.clipFrameCount - 1,
                        currentClipSpeeds[i],
                        currentClipStartTimes[i]);

                    // set current clip frame
                    animationFrames[i] = clipData.clipStartFrame + GetClipFrame(i, Time.time, clipData.length, clipData.clipFrameCount, clipData.IsLoopDisabled());
                }
                else
                {
                    runtimeData.crowdAnimatorControllerData[crowdAnimIndex + i] = Vector4.zero;
                    animationFrames[i] = -1.0f;
                }
            }
            runtimeData.animationData[animationIndex] = animationFrames;
            // set weights
            runtimeData.animationData[animationIndex + 1] = currentAnimationClipDataWeights;

            // set data to buffers
            //runtimeData.animationDataBuffer.SetData(runtimeData.animationData, animationIndex, animationIndex, 2);
            //runtimeData.crowdAnimatorControllerBuffer.SetData(runtimeData.crowdAnimatorControllerData, crowdAnimIndex, crowdAnimIndex, activeClipCount);
            runtimeData.animationDataModified = true;
            runtimeData.crowdAnimatorDataModified = true;

            newClipStartTimes = Vector4.zero;
            runtimeData.disableFrameLerp = true;
        }