Ejemplo n.º 1
0
        /// <summary>
        /// Animates a Vector3 value along a specified curve
        /// </summary>
        /// <param name="internalProperties"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        protected virtual Vector3 AnimateCurveValue(ref InternalWiggleProperties internalProperties, WiggleProperties properties)
        {
            internalProperties.timeSinceLastPause  += properties.GetDeltaTime();
            internalProperties.timeSinceLastChange += properties.GetDeltaTime();

            // handle pause
            if (internalProperties.timeSinceLastPause < internalProperties.pauseDuration)
            {
                float curveProgress = (internalProperties.curveDirection == 1f) ? 1f : 0f;

                EvaluateCurve(properties.Curve, curveProgress, internalProperties.remapZero, internalProperties.remapOne, ref internalProperties.newValue);
                if (properties.RelativeCurveAmplitude)
                {
                    internalProperties.newValue += internalProperties.initialValue;
                }
            }

            // if we're just out of a pause
            if (internalProperties.timeSinceLastPause == internalProperties.timeSinceLastChange)
            {
                internalProperties.timeSinceLastChange = 0f;
            }

            // if we've reached the end
            if (internalProperties.randomFrequency > 0)
            {
                float curveProgress = (internalProperties.timeSinceLastChange) / internalProperties.randomFrequency;
                if (internalProperties.curveDirection < 0f)
                {
                    curveProgress = 1 - curveProgress;
                }

                EvaluateCurve(properties.Curve, curveProgress, internalProperties.remapZero, internalProperties.remapOne, ref internalProperties.newValue);

                if (internalProperties.timeSinceLastChange > internalProperties.randomFrequency)
                {
                    internalProperties.timeSinceLastChange = 0f;
                    internalProperties.timeSinceLastPause  = 0f;
                    if (properties.CurvePingPong)
                    {
                        internalProperties.curveDirection = -internalProperties.curveDirection;
                    }

                    RandomizeFloat(ref internalProperties.randomFrequency, properties.FrequencyMin, properties.FrequencyMax);
                }
            }

            if (properties.RelativeCurveAmplitude)
            {
                internalProperties.newValue = internalProperties.initialValue + internalProperties.newValue;
            }

            return(internalProperties.newValue);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Moves a vector3's values towards a target
        /// </summary>
        /// <param name="movedValue"></param>
        /// <param name="properties"></param>
        /// <param name="startValue"></param>
        /// <param name="initialValue"></param>
        /// <param name="destinationValue"></param>
        /// <param name="timeSinceLastPause"></param>
        /// <param name="timeSinceLastValueChange"></param>
        /// <param name="randomAmplitude"></param>
        /// <param name="randomFrequency"></param>
        /// <param name="pauseDuration"></param>
        /// <param name="frequency"></param>
        /// <returns></returns>
        protected virtual bool MoveVector3TowardsTarget(ref Vector3 movedValue, WiggleProperties properties, ref Vector3 startValue, Vector3 initialValue,
                                                        ref Vector3 destinationValue, ref float timeSinceLastPause, ref float timeSinceLastValueChange,
                                                        ref Vector3 randomAmplitude, ref float randomFrequency,
                                                        ref float pauseDuration, float frequency)
        {
            timeSinceLastPause       += properties.GetDeltaTime();
            timeSinceLastValueChange += properties.GetDeltaTime();

            // handle pause
            if (timeSinceLastPause < pauseDuration)
            {
                return(false);
            }

            // if we're just out of a pause
            if (timeSinceLastPause == timeSinceLastValueChange)
            {
                timeSinceLastValueChange = 0f;
            }

            // if we've reached the end
            if (frequency > 0)
            {
                float curveProgress = (timeSinceLastValueChange) / frequency;

                if (!properties.UseSpeedCurve)
                {
                    movedValue = Vector3.Lerp(startValue, destinationValue, curveProgress);
                }
                else
                {
                    float curvePercent = properties.SpeedCurve.Evaluate(curveProgress);
                    movedValue = Vector3.LerpUnclamped(startValue, destinationValue, curvePercent);
                }


                if (timeSinceLastValueChange > frequency)
                {
                    timeSinceLastValueChange = 0f;
                    timeSinceLastPause       = 0f;
                    movedValue       = destinationValue;
                    destinationValue = DetermineNewValue(properties, movedValue, initialValue, ref startValue,
                                                         ref randomAmplitude, ref randomFrequency, ref pauseDuration);
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Computes the next Vector3 value for the specified property
        /// </summary>
        /// <param name="valueActive"></param>
        /// <param name="properties"></param>
        /// <param name="internalProperties"></param>
        /// <returns></returns>
        protected virtual bool UpdateValue(bool valueActive, WiggleProperties properties, ref InternalWiggleProperties internalProperties)
        {
            if (!valueActive)
            {
                return(false);
            }
            if (!properties.WigglePermitted)
            {
                return(false);
            }

            // handle limited time
            if ((properties.LimitedTime) && (properties.LimitedTimeTotal > 0f))
            {
                float timeSave = properties.LimitedTimeLeft;
                properties.LimitedTimeLeft -= properties.GetDeltaTime();
                if (properties.LimitedTimeLeft <= 0)
                {
                    if (timeSave > 0f)
                    {
                        if (properties.LimitedTimeResetValue)
                        {
                            internalProperties.returnVector = internalProperties.limitedTimeValueSave;
                            properties.LimitedTimeLeft      = 0;
                            properties.WigglePermitted      = false;
                            return(true);
                        }
                    }
                    return(false);
                }
            }

            switch (properties.WiggleType)
            {
            case WiggleTypes.PingPong:
                return(MoveVector3TowardsTarget(ref internalProperties.returnVector, properties, ref internalProperties.startValue, internalProperties.initialValue,
                                                ref internalProperties.newValue, ref internalProperties.timeSinceLastPause,
                                                ref internalProperties.timeSinceLastChange, ref internalProperties.randomAmplitude,
                                                ref internalProperties.randomFrequency,
                                                ref internalProperties.pauseDuration, internalProperties.randomFrequency));


            case WiggleTypes.Random:
                return(MoveVector3TowardsTarget(ref internalProperties.returnVector, properties, ref internalProperties.startValue, internalProperties.initialValue,
                                                ref internalProperties.newValue, ref internalProperties.timeSinceLastPause,
                                                ref internalProperties.timeSinceLastChange, ref internalProperties.randomAmplitude,
                                                ref internalProperties.randomFrequency,
                                                ref internalProperties.pauseDuration, internalProperties.randomFrequency));

            case WiggleTypes.Noise:
                internalProperties.returnVector = AnimateNoiseValue(ref internalProperties, properties);
                return(true);

            case WiggleTypes.Curve:
                internalProperties.returnVector = AnimateCurveValue(ref internalProperties, properties);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Animates a Vector3 value along a perlin noise
        /// </summary>
        /// <param name="internalProperties"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        protected virtual Vector3 AnimateNoiseValue(ref InternalWiggleProperties internalProperties, WiggleProperties properties)
        {
            internalProperties.noiseElapsedTime += properties.GetDeltaTime();

            internalProperties.newValue.x = (Mathf.PerlinNoise(internalProperties.randomNoiseFrequency.x * internalProperties.noiseElapsedTime, internalProperties.randomNoiseShift.x) * 2.0f - 1.0f) * internalProperties.randomAmplitude.x;
            internalProperties.newValue.y = (Mathf.PerlinNoise(internalProperties.randomNoiseFrequency.y * internalProperties.noiseElapsedTime, internalProperties.randomNoiseShift.y) * 2.0f - 1.0f) * internalProperties.randomAmplitude.y;
            internalProperties.newValue.z = (Mathf.PerlinNoise(internalProperties.randomNoiseFrequency.z * internalProperties.noiseElapsedTime, internalProperties.randomNoiseShift.z) * 2.0f - 1.0f) * internalProperties.randomAmplitude.z;

            if (properties.RelativeAmplitude)
            {
                internalProperties.newValue += internalProperties.initialValue;
            }

            return(internalProperties.newValue);
        }