Пример #1
0
        /// <summary>
        /// Evaluate the sampled samples
        /// </summary>
        /// <param name="percent">Percent [0-1] of evaulation</param>
        /// <returns></returns>
        public virtual void Evaluate(SplineResult result, double percent)
        {
            if (samples.Length == 0)
            {
                result = new SplineResult();
                return;
            }
            if (samples.Length == 1)
            {
                result.CopyFrom(samples[0]);
                return;
            }
            percent = DMath.Clamp01(percent);
            int    index         = GetSampleIndex(percent);
            double percentExcess = (samples.Length - 1) * percent - index;

            if (percentExcess > 0.0 && index < samples.Length - 1)
            {
                SplineResult.Lerp(samples[index], samples[index + 1], percentExcess, result);
            }
            else
            {
                result.CopyFrom(samples[index]);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the clipped _samples defined by clipFrom and clipTo
        /// </summary>
        private void GetClippedSamples()
        {
            double clipFromValue     = clipFrom * (samples.Length - 1);
            double clipToValue       = clipTo * (samples.Length - 1);
            int    clippedIterations = DMath.CeilInt(clipToValue) - DMath.FloorInt(clipFromValue) + 1;

            if (span == 1.0)
            {
                //if (_clippedSamples.Length != samples.Length) _clippedSamples = new SplineResult[samples.Length];
                //samples.CopyTo(_clippedSamples, 0);
                _clippedSamples = samples;
                return;
            }
            else if (_clippedSamples.Length != clippedIterations)
            {
                _clippedSamples = new SplineResult[clippedIterations];
            }
            int clipFromIndex = DMath.FloorInt(clipFromValue);
            int clipToIndex   = DMath.CeilInt(clipToValue);

            if (clipFromIndex + 1 < samples.Length)
            {
                _clippedSamples[0] = SplineResult.Lerp(samples[clipFromIndex], samples[clipFromIndex + 1], clipFromValue - clipFromIndex);
            }
            for (int i = 1; i < _clippedSamples.Length - 1; i++)
            {
                _clippedSamples[i] = samples[clipFromIndex + i];
            }
            if (clipToIndex - 1 >= 0)
            {
                _clippedSamples[_clippedSamples.Length - 1] = SplineResult.Lerp(samples[clipToIndex], samples[clipToIndex - 1], clipToIndex - clipToValue);
            }
            getClippedSamples = false;
        }
Пример #3
0
        /// <summary>
        /// Gets the clipped samples defined by clipFrom and clipTo
        /// </summary>
        private void GetClippedSamples()
        {
            getClippedSamples = false;
            if (span == 1.0 && !samplesAreLooped)
            {
                _clippedSamples = samples;
                return;
            }

            double clipFromValue = clipFrom * (samples.Length - 1);
            double clipToValue   = clipTo * (samples.Length - 1);

            int clipFromIndex = DMath.FloorInt(clipFromValue);
            int clipToIndex   = DMath.CeilInt(clipToValue);

            if (samplesAreLooped) //Handle looping segments
            {
                int toSamples   = DMath.CeilInt(clipToValue) + 1;
                int fromSamples = samples.Length - DMath.FloorInt(clipFromValue) - 1;
                if (_clippedSamples.Length != toSamples + fromSamples)
                {
                    _clippedSamples = new SplineResult[toSamples + fromSamples];
                }
                _clippedSamples[0] = Evaluate(_clipFrom);
                for (int i = 1; i < fromSamples; i++)
                {
                    _clippedSamples[i] = samples[samples.Length - fromSamples + i - 1];
                }
                for (int i = 0; i < toSamples - 1; i++)
                {
                    _clippedSamples[fromSamples + i] = new SplineResult(samples[i]);
                }
                _clippedSamples[_clippedSamples.Length - 1] = Evaluate(_clipTo);
                return;
            }

            int clippedIterations = DMath.CeilInt(clipToValue) - DMath.FloorInt(clipFromValue) + 1;

            if (_clippedSamples.Length != clippedIterations)
            {
                _clippedSamples = new SplineResult[clippedIterations];
            }
            if (clipFromIndex + 1 < samples.Length)
            {
                _clippedSamples[0] = SplineResult.Lerp(samples[clipFromIndex], samples[clipFromIndex + 1], clipFromValue - clipFromIndex);
            }
            for (int i = 1; i < _clippedSamples.Length - 1; i++)
            {
                _clippedSamples[i] = samples[clipFromIndex + i];
            }
            if (clipToIndex - 1 >= 0)
            {
                _clippedSamples[_clippedSamples.Length - 1] = SplineResult.Lerp(samples[clipToIndex], samples[clipToIndex - 1], clipToIndex - clipToValue);
            }
        }
Пример #4
0
 public virtual void Evaluate(SplineResult result, double percent)
 {
     if (samples.Length == 0)
     {
         result = new SplineResult();
     }
     else if (samples.Length == 1)
     {
         result.CopyFrom(samples[0]);
     }
     else if (_uniformSample && _uniformPreserveClipRange)
     {
         double num  = 1.0;
         int    num2 = 0;
         for (int i = 0; i < samples.Length; i++)
         {
             double num3 = DMath.Abs(percent - samples[i].percent);
             if (num3 < num)
             {
                 num  = num3;
                 num2 = i;
             }
         }
         if (percent > samples[num2].percent)
         {
             SplineResult.Lerp(samples[num2], samples[num2 + 1], Mathf.InverseLerp((float)samples[num2].percent, (float)samples[num2 + 1].percent, (float)percent), result);
         }
         else if (percent < samples[num2].percent)
         {
             SplineResult.Lerp(samples[num2 - 1], samples[num2], Mathf.InverseLerp((float)samples[num2 - 1].percent, (float)samples[num2].percent, (float)percent), result);
         }
         else
         {
             result.CopyFrom(samples[num2]);
         }
     }
     else
     {
         percent = DMath.Clamp01(percent);
         int    sampleIndex = GetSampleIndex(percent);
         double num4        = (double)(samples.Length - 1) * percent - (double)sampleIndex;
         if (num4 > 0.0 && sampleIndex < samples.Length - 1)
         {
             SplineResult.Lerp(samples[sampleIndex], samples[sampleIndex + 1], num4, result);
         }
         else
         {
             result.CopyFrom(samples[sampleIndex]);
         }
     }
 }
Пример #5
0
        /// <summary>
        /// Evaluate the sampled samples
        /// </summary>
        /// <param name="percent">Percent [0-1] of evaulation</param>
        /// <returns></returns>
        public SplineResult Evaluate(double percent)
        {
            if (samples.Length == 0)
            {
                return(null);
            }
            percent = DMath.Clamp01(percent);
            int    index         = GetSampleIndex(percent);
            double percentExcess = (samples.Length - 1) * percent - index;

            if (percentExcess > 0.0 && index < samples.Length - 1)
            {
                return(SplineResult.Lerp(samples[index], samples[index + 1], percentExcess));
            }
            else
            {
                return(new SplineResult(samples[index]));
            }
        }
Пример #6
0
        public void Move(float distance)
        {
            if (distance < 0f)
            {
                distance = 0f;
            }
            if (distance == 0f)
            {
                return;
            }
            if (samples.Length <= 1)
            {
                if (samples.Length == 1)
                {
                    _result.CopyFrom(samples[0]);
                    ApplyMotion();
                }
                return;
            }
            bool callOnEndReached = false, callOnBeginningReached = false;

            float  moved          = 0f;
            int    nextIndex      = 0;
            double clippedPercent = ClipPercent(_result.percent);

            if (clippedPercent == 0.0 && lastClippedPercent == 1.0 && clipFrom == clipTo)
            {
                clippedPercent = 1.0;
            }
            from.CopyFrom(_result);
            to.CopyFrom(_result);

            if (_direction == Spline.Direction.Forward)
            {
                nextIndex = DMath.FloorInt(clippedPercent * (clippedSamples.Length - 1)) - 1;
                if (nextIndex < 0)
                {
                    nextIndex = 0;
                }
                for (int i = nextIndex; i < clippedSamples.Length - 1; i++)
                {
                    if (ClipPercent(clippedSamples[i].percent) > clippedPercent)
                    {
                        break;
                    }
                    nextIndex = i;
                }
            }
            else
            {
                nextIndex = DMath.CeilInt(clippedPercent * (clippedSamples.Length - 1)) + 1;
                if (nextIndex >= clippedSamples.Length)
                {
                    nextIndex = clippedSamples.Length - 1;
                }
                for (int i = nextIndex; i >= 1; i--)
                {
                    double percent = ClipPercent(clippedSamples[i].percent);
                    if (i == clippedSamples.Length - 1)
                    {
                        percent = 1.0;
                    }
                    if (percent < clippedPercent)
                    {
                        break;
                    }
                    nextIndex = i;
                }
            }
            while (moved < distance)
            {
                from.CopyFrom(to); //Get the current sample
                if (_direction == Spline.Direction.Forward)
                {
                    nextIndex++;
                    if ((samplesAreLooped && _result.percent >= clippedSamples[clippedSamples.Length - 1].percent && _result.percent < clippedSamples[0].percent) || (!samplesAreLooped && _result.percent >= clippedSamples[clippedSamples.Length - 1].percent) || nextIndex >= clippedSamples.Length)
                    {
                        if (onEndReached != null && lastClippedPercent < 1.0)
                        {
                            callOnEndReached = true;
                        }
                        if (wrapMode == Wrap.Default)
                        {
                            lastClippedPercent = 1.0;
                            _result.CopyFrom(clippedSamples[clippedSamples.Length - 1]);
                            CheckTriggersClipped(from.percent, _result.percent);
                            break;
                        }
                        else if (wrapMode == Wrap.Loop)
                        {
                            CheckTriggersClipped(from.percent, clippedSamples[clippedSamples.Length - 1].percent);
                            from.CopyFrom(clippedSamples[0]);
                            nextIndex = 1;
                        }
                        else if (wrapMode == Wrap.PingPong)
                        {
                            _direction = Spline.Direction.Backward;
                            CheckTriggersClipped(from.percent, clippedSamples[clippedSamples.Length - 1].percent);
                            from.CopyFrom(clippedSamples[clippedSamples.Length - 1]);
                            nextIndex = clippedSamples.Length - 2;
                        }
                    }
                }
                else
                {
                    nextIndex--;
                    if ((samplesAreLooped && _result.percent <= clippedSamples[0].percent && _result.percent > clippedSamples[clippedSamples.Length - 1].percent) || (!samplesAreLooped && _result.percent <= clippedSamples[0].percent) || nextIndex < 0)
                    {
                        if (onBeginningReached != null && lastClippedPercent > 0.0)
                        {
                            callOnBeginningReached = true;
                        }
                        if (wrapMode == Wrap.Default)
                        {
                            lastClippedPercent = 0.0;
                            _result.CopyFrom(clippedSamples[0]);
                            CheckTriggersClipped(from.percent, _result.percent);
                            break;
                        }
                        else if (wrapMode == Wrap.Loop)
                        {
                            CheckTriggersClipped(from.percent, clippedSamples[0].percent);
                            from.CopyFrom(clippedSamples[clippedSamples.Length - 1]);
                            nextIndex = clippedSamples.Length - 2;
                        }
                        else if (wrapMode == Wrap.PingPong)
                        {
                            _direction = Spline.Direction.Forward;
                            CheckTriggersClipped(from.percent, clippedSamples[0].percent);
                            from.CopyFrom(clippedSamples[0]);
                            nextIndex = 1;
                        }
                    }
                }
                lastClippedPercent = ClipPercent(_result.percent);
                to.CopyFrom(clippedSamples[nextIndex]); //Get the next sample
                float traveled = Vector3.Distance(to.position, from.position);
                moved += traveled;
                if (moved >= distance)
                {
                    float  excess      = moved - distance;
                    double lerpPercent = 1.0 - excess / traveled;
                    SplineResult.Lerp(from, to, lerpPercent, _result);
                    if (samplesAreLooped)
                    {
                        if (_direction == Spline.Direction.Forward && from.percent > to.percent)
                        {
                            _result.percent = DMath.Lerp(from.percent, 1.0, lerpPercent);
                        }
                        else if (_direction == Spline.Direction.Backward && from.percent < to.percent)
                        {
                            _result.percent = DMath.Lerp(1.0, to.percent, lerpPercent);
                        }
                    }
                    CheckTriggersClipped(from.percent, _result.percent);
                    break;
                }
                CheckTriggersClipped(from.percent, to.percent);
            }

            ApplyMotion();
            if (callOnEndReached)
            {
                onEndReached();
            }
            else if (callOnBeginningReached)
            {
                onBeginningReached();
            }
            InvokeTriggers();
        }
Пример #7
0
        public virtual void Project(SplineResult result, Vector3 point, double from = 0.0, double to = 1.0)
        {
            if (samples.Length == 0)
            {
                return;
            }
            if (samples.Length == 1)
            {
                if (result == null)
                {
                    result = new SplineResult(samples[0]);
                }
                else
                {
                    result.CopyFrom(samples[0]);
                }
                return;
            }
            if (computer == null)
            {
                result = new SplineResult();
                return;
            }
            //First make a very rough sample of the from-to region
            int steps = (computer.pointCount - 1) * 6; //Sampling six points per segment is enough to find the closest point range
            int step  = samples.Length / steps;

            if (step < 1)
            {
                step = 1;
            }
            float minDist   = (point - samples[0].position).sqrMagnitude;
            int   fromIndex = 0;
            int   toIndex   = samples.Length - 1;

            if (from != 0.0)
            {
                fromIndex = GetSampleIndex(from);
            }
            if (to != 1.0)
            {
                toIndex = Mathf.CeilToInt((float)to * (samples.Length - 1));
            }
            int checkFrom = fromIndex;
            int checkTo   = toIndex;

            //Find the closest point range which will be checked in detail later
            for (int i = fromIndex; i <= toIndex; i += step)
            {
                if (i > toIndex)
                {
                    i = toIndex;
                }
                float dist = (point - samples[i].position).sqrMagnitude;
                if (dist < minDist)
                {
                    minDist   = dist;
                    checkFrom = Mathf.Max(i - step, 0);
                    checkTo   = Mathf.Min(i + step, samples.Length - 1);
                }
                if (i == toIndex)
                {
                    break;
                }
            }
            minDist = (point - samples[checkFrom].position).sqrMagnitude;

            int index = checkFrom;

            //Find the closest result within the range
            for (int i = checkFrom + 1; i <= checkTo; i++)
            {
                float dist = (point - samples[i].position).sqrMagnitude;
                if (dist < minDist)
                {
                    minDist = dist;
                    index   = i;
                }
            }
            //Project the point on the line between the two closest samples
            int backIndex = index - 1;

            if (backIndex < 0)
            {
                backIndex = 0;
            }
            int frontIndex = index + 1;

            if (frontIndex > samples.Length - 1)
            {
                frontIndex = samples.Length - 1;
            }
            Vector3 back             = LinearAlgebraUtility.ProjectOnLine(samples[backIndex].position, samples[index].position, point);
            Vector3 front            = LinearAlgebraUtility.ProjectOnLine(samples[index].position, samples[frontIndex].position, point);
            float   backLength       = (samples[index].position - samples[backIndex].position).magnitude;
            float   frontLength      = (samples[index].position - samples[frontIndex].position).magnitude;
            float   backProjectDist  = (back - samples[backIndex].position).magnitude;
            float   frontProjectDist = (front - samples[frontIndex].position).magnitude;

            if (backIndex < index && index < frontIndex)
            {
                if ((point - back).sqrMagnitude < (point - front).sqrMagnitude)
                {
                    SplineResult.Lerp(samples[backIndex], samples[index], backProjectDist / backLength, result);
                }
                else
                {
                    SplineResult.Lerp(samples[frontIndex], samples[index], frontProjectDist / frontLength, result);
                }
            }
            else if (backIndex < index)
            {
                SplineResult.Lerp(samples[backIndex], samples[index], backProjectDist / backLength, result);
            }
            else
            {
                SplineResult.Lerp(samples[frontIndex], samples[index], frontProjectDist / frontLength, result);
            }
            if (from == 0.0 && to == 1.0 && result.percent < _address.moveStep / _resolution) //Handle looped splines
            {
                Vector3 projected = LinearAlgebraUtility.ProjectOnLine(samples[samples.Length - 1].position, samples[samples.Length - 2].position, point);
                if ((point - projected).sqrMagnitude < (point - result.position).sqrMagnitude)
                {
                    SplineResult.Lerp(samples[samples.Length - 1], samples[samples.Length - 2], LinearAlgebraUtility.InverseLerp(samples[samples.Length - 1].position, samples[samples.Length - 2].position, projected), result);
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Project a point onto the sampled SplineComputer
        /// </summary>
        /// <param name="point">Point in space</param>
        /// <param name="from">Start check from</param>
        /// <param name="to">End check at</param>
        /// <returns></returns>
        public SplineResult Project(Vector3 point, double from = 0.0, double to = 1.0)
        {
            if (samples.Length == 0)
            {
                return(new SplineResult());
            }
            if (computer == null)
            {
                return(new SplineResult());
            }
            //First make a very rough sample of the from-to region
            int steps = (computer.pointCount - 1) * 6; //Sampling six points per segment is enough to find the closest point range
            int step  = samples.Length / steps;

            if (step < 1)
            {
                step = 1;
            }
            float minDist   = (point - samples[0].position).sqrMagnitude;
            int   fromIndex = 0;
            int   toIndex   = samples.Length - 1;

            if (from != 0.0)
            {
                fromIndex = GetSampleIndex(from);
            }
            if (to != 1.0)
            {
                toIndex = Mathf.CeilToInt((float)to * (samples.Length - 1));
            }
            int checkFrom = fromIndex;
            int checkTo   = toIndex;

            //Find the closest point range which will be checked in detail later
            for (int i = fromIndex; i <= toIndex; i += step)
            {
                if (i > toIndex)
                {
                    i = toIndex;
                }
                float dist = (point - samples[i].position).sqrMagnitude;
                if (dist < minDist)
                {
                    minDist   = dist;
                    checkFrom = Mathf.Max(i - step, 0);
                    checkTo   = Mathf.Min(i + step, samples.Length - 1);
                }
                if (i == toIndex)
                {
                    break;
                }
            }
            minDist = (point - samples[checkFrom].position).sqrMagnitude;

            int index = checkFrom;

            //Find the closest result within the range
            for (int i = checkFrom + 1; i <= checkTo; i++)
            {
                float dist = (point - samples[i].position).sqrMagnitude;
                if (dist < minDist)
                {
                    minDist = dist;
                    index   = i;
                }
            }
            //Project the point on the line between the two closest samples
            int backIndex = index - 1;

            if (backIndex < 0)
            {
                backIndex = 0;
            }
            int frontIndex = index + 1;

            if (frontIndex > samples.Length - 1)
            {
                frontIndex = samples.Length - 1;
            }
            Vector3 back             = Dreamteck.Utils.ProjectOnLine(samples[backIndex].position, samples[index].position, point);
            Vector3 front            = Dreamteck.Utils.ProjectOnLine(samples[index].position, samples[frontIndex].position, point);
            float   backLength       = (samples[index].position - samples[backIndex].position).magnitude;
            float   frontLength      = (samples[index].position - samples[frontIndex].position).magnitude;
            float   backProjectDist  = (back - samples[backIndex].position).magnitude;
            float   frontProjectDist = (front - samples[frontIndex].position).magnitude;

            if (backIndex < index && index < frontIndex)
            {
                if ((point - back).sqrMagnitude < (point - front).sqrMagnitude)
                {
                    return(SplineResult.Lerp(samples[backIndex], samples[index], backProjectDist / backLength));
                }
                else
                {
                    return(SplineResult.Lerp(samples[frontIndex], samples[index], frontProjectDist / frontLength));
                }
            }
            else if (backIndex < index)
            {
                return(SplineResult.Lerp(samples[backIndex], samples[index], backProjectDist / backLength));
            }
            else
            {
                return(SplineResult.Lerp(samples[frontIndex], samples[index], frontProjectDist / frontLength));
            }
        }
Пример #9
0
        private void GetClippedSamples()
        {
            getClippedSamples = false;
            if (span == 1.0 && !samplesAreLooped)
            {
                _clippedSamples = samples;
                return;
            }
            double num  = clipFrom * (double)(samples.Length - 1);
            double num2 = clipTo * (double)(samples.Length - 1);
            int    num3 = DMath.FloorInt(num);
            int    num4 = DMath.CeilInt(num2);

            if (samplesAreLooped)
            {
                if (_uniformSample)
                {
                    int num5 = 0;
                    int num6 = 0;
                    int num7 = -1;
                    for (int i = 0; i < samples.Length - 1; i++)
                    {
                        if (samples[i].percent > clipFrom)
                        {
                            num5++;
                            if (num7 < 0)
                            {
                                num7 = i - 1;
                            }
                        }
                        else if (samples[i].percent < clipTo)
                        {
                            num6++;
                        }
                    }
                    num5++;
                    if (_clippedSamples.Length != num5 + num6 || _clippedSamples == samples)
                    {
                        _clippedSamples = new SplineResult[num5 + num6];
                    }
                    for (int j = 1; j <= num5; j++)
                    {
                        _clippedSamples[j] = _samples[num7 + j];
                    }
                    for (int k = 0; k < num6 - 1; k++)
                    {
                        _clippedSamples[k + num5 + 1] = _samples[k];
                    }
                    _clippedSamples[0] = Evaluate(clipFrom);
                    _clippedSamples[_clippedSamples.Length - 1] = Evaluate(clipTo);
                }
                else
                {
                    int num8 = DMath.CeilInt(num2) + 1;
                    int num9 = samples.Length - DMath.FloorInt(num) - 1;
                    if (_clippedSamples.Length != num8 + num9)
                    {
                        _clippedSamples = new SplineResult[num8 + num9];
                    }
                    _clippedSamples[0] = Evaluate(_clipFrom);
                    for (int l = 1; l < num9; l++)
                    {
                        _clippedSamples[l] = samples[samples.Length - num9 + l - 1];
                    }
                    for (int m = 0; m < num8 - 1; m++)
                    {
                        _clippedSamples[num9 + m] = new SplineResult(samples[m]);
                    }
                    _clippedSamples[_clippedSamples.Length - 1] = Evaluate(_clipTo);
                }
            }
            else if (_uniformSample)
            {
                int num10 = 0;
                int num11 = -1;
                for (int n = 0; n < samples.Length; n++)
                {
                    if (samples[n].percent > clipFrom && samples[n].percent < clipTo)
                    {
                        num10++;
                        if (num11 < 0)
                        {
                            num11 = n - 1;
                        }
                    }
                }
                num10 += 2;
                if (_clippedSamples.Length != num10 || _clippedSamples == samples)
                {
                    _clippedSamples = new SplineResult[num10];
                }
                for (int num12 = 1; num12 < _clippedSamples.Length - 1; num12++)
                {
                    _clippedSamples[num12] = samples[num11 + num12];
                }
                _clippedSamples[0] = Evaluate(clipFrom);
                _clippedSamples[_clippedSamples.Length - 1] = Evaluate(clipTo);
            }
            else
            {
                int num13 = DMath.CeilInt(num2) - DMath.FloorInt(num) + 1;
                if (_clippedSamples.Length != num13 || _clippedSamples == samples)
                {
                    _clippedSamples = new SplineResult[num13];
                }
                if (num3 + 1 < samples.Length)
                {
                    _clippedSamples[0] = SplineResult.Lerp(samples[num3], samples[num3 + 1], num - (double)num3);
                }
                for (int num14 = 1; num14 < _clippedSamples.Length - 1; num14++)
                {
                    _clippedSamples[num14] = samples[num3 + num14];
                }
                if (num4 - 1 >= 0)
                {
                    _clippedSamples[_clippedSamples.Length - 1] = SplineResult.Lerp(samples[num4], samples[num4 - 1], (double)num4 - num2);
                }
            }
        }
Пример #10
0
        public virtual void Project(SplineResult result, Vector3 point, double from = 0.0, double to = 1.0)
        {
            if (samples.Length == 0)
            {
                return;
            }
            if (samples.Length == 1)
            {
                if (result == null)
                {
                    result = new SplineResult(samples[0]);
                }
                else
                {
                    result.CopyFrom(samples[0]);
                }
                return;
            }
            if (computer == null)
            {
                result = new SplineResult();
                return;
            }
            int num  = (computer.pointCount - 1) * 6;
            int num2 = samples.Length / num;

            if (num2 < 1)
            {
                num2 = 1;
            }
            float num3 = (point - samples[0].position).sqrMagnitude;
            int   num4 = 0;
            int   num5 = samples.Length - 1;

            if (from != 0.0)
            {
                num4 = GetSampleIndex(from);
            }
            if (to != 1.0)
            {
                num5 = Mathf.CeilToInt((float)to * (float)(samples.Length - 1));
            }
            int num6 = num4;
            int num7 = num5;

            for (int i = num4; i <= num5; i += num2)
            {
                if (i > num5)
                {
                    i = num5;
                }
                float sqrMagnitude = (point - samples[i].position).sqrMagnitude;
                if (sqrMagnitude < num3)
                {
                    num3 = sqrMagnitude;
                    num6 = Mathf.Max(i - num2, 0);
                    num7 = Mathf.Min(i + num2, samples.Length - 1);
                }
                if (i == num5)
                {
                    break;
                }
            }
            num3 = (point - samples[num6].position).sqrMagnitude;
            int num8 = num6;

            for (int j = num6 + 1; j <= num7; j++)
            {
                float sqrMagnitude2 = (point - samples[j].position).sqrMagnitude;
                if (sqrMagnitude2 < num3)
                {
                    num3 = sqrMagnitude2;
                    num8 = j;
                }
            }
            int num9 = num8 - 1;

            if (num9 < 0)
            {
                num9 = 0;
            }
            int num10 = num8 + 1;

            if (num10 > samples.Length - 1)
            {
                num10 = samples.Length - 1;
            }
            Vector3 vector     = LinearAlgebraUtility.ProjectOnLine(samples[num9].position, samples[num8].position, point);
            Vector3 vector2    = LinearAlgebraUtility.ProjectOnLine(samples[num8].position, samples[num10].position, point);
            float   magnitude  = (samples[num8].position - samples[num9].position).magnitude;
            float   magnitude2 = (samples[num8].position - samples[num10].position).magnitude;
            float   magnitude3 = (vector - samples[num9].position).magnitude;
            float   magnitude4 = (vector2 - samples[num10].position).magnitude;

            if (num9 < num8 && num8 < num10)
            {
                if ((point - vector).sqrMagnitude < (point - vector2).sqrMagnitude)
                {
                    SplineResult.Lerp(samples[num9], samples[num8], magnitude3 / magnitude, result);
                }
                else
                {
                    SplineResult.Lerp(samples[num10], samples[num8], magnitude4 / magnitude2, result);
                }
            }
            else if (num9 < num8)
            {
                SplineResult.Lerp(samples[num9], samples[num8], magnitude3 / magnitude, result);
            }
            else
            {
                SplineResult.Lerp(samples[num10], samples[num8], magnitude4 / magnitude2, result);
            }
            if (from == 0.0 && to == 1.0 && result.percent < _address.moveStep / _resolution)
            {
                Vector3 vector3 = LinearAlgebraUtility.ProjectOnLine(samples[samples.Length - 1].position, samples[samples.Length - 2].position, point);
                if ((point - vector3).sqrMagnitude < (point - result.position).sqrMagnitude)
                {
                    SplineResult.Lerp(samples[samples.Length - 1], samples[samples.Length - 2], LinearAlgebraUtility.InverseLerp(samples[samples.Length - 1].position, samples[samples.Length - 2].position, vector3), result);
                }
            }
        }
Пример #11
0
        public void Move(float distance)
        {
            if (distance < 0f)
            {
                distance = 0f;
            }
            if (distance == 0f)
            {
                return;
            }
            _followResult = Evaluate(followResult.percent);
            SplineResult lastResult        = _followResult;
            double       tracePercent      = _followResult.percent;
            SplineResult traceResult       = _followResult;
            double       traceTriggersFrom = traceResult.percent;
            float        moved             = 0f;

            while (moved < distance)
            {
                int resultIndex = GetSampleIndex(traceResult.percent);
                if (direction == Spline.Direction.Forward)
                {
                    if (tracePercent == clipTo)
                    {
                        CheckTriggers((float)traceTriggersFrom, traceResult.percent);
                        if (wrapMode == Wrap.Default)
                        {
                            if (onEndReached != null && !Mathf.Approximately((float)_followResult.percent, (float)clipTo))
                            {
                                onEndReached();
                            }
                            _followResult = traceResult;
                            break;
                        }
                        if (wrapMode == Wrap.Loop)
                        {
                            if (onEndReached != null && !Mathf.Approximately((float)_followResult.percent, (float)clipTo))
                            {
                                onEndReached();
                            }
                            traceResult       = Evaluate(clipFrom);
                            traceTriggersFrom = traceResult.percent;
                            resultIndex       = GetSampleIndex(clipFrom);
                        }
                        if (wrapMode == Wrap.PingPong)
                        {
                            if (onEndReached != null && !Mathf.Approximately((float)_followResult.percent, (float)clipTo))
                            {
                                onEndReached();
                            }
                            direction         = Spline.Direction.Backward;
                            lastResult        = traceResult;
                            traceTriggersFrom = traceResult.percent;
                            continue;
                        }
                    }
                    double nextPercent = (double)(resultIndex + 1) / (samples.Length - 1);
                    if (nextPercent <= tracePercent)
                    {
                        nextPercent = (double)(resultIndex + 2) / (samples.Length - 1);
                    }
                    if (nextPercent > clipTo)
                    {
                        nextPercent = clipTo;
                    }
                    tracePercent = nextPercent;
                }
                else
                {
                    if (tracePercent == clipFrom)
                    {
                        CheckTriggers((float)traceTriggersFrom, traceResult.percent);
                        if (wrapMode == Wrap.Default)
                        {
                            if (onBeginningReached != null && !Mathf.Approximately((float)_followResult.percent, (float)clipFrom))
                            {
                                onBeginningReached();
                            }
                            _followResult = traceResult;
                            break;
                        }
                        if (wrapMode == Wrap.Loop)
                        {
                            if (onBeginningReached != null && !Mathf.Approximately((float)_followResult.percent, (float)clipFrom))
                            {
                                onBeginningReached();
                            }
                            traceResult       = Evaluate(clipTo);
                            traceTriggersFrom = traceResult.percent;
                            resultIndex       = GetSampleIndex(clipTo);
                        }
                        if (wrapMode == Wrap.PingPong)
                        {
                            if (onBeginningReached != null && !Mathf.Approximately((float)_followResult.percent, (float)clipFrom))
                            {
                                onBeginningReached();
                            }
                            direction         = Spline.Direction.Forward;
                            lastResult        = traceResult;
                            traceTriggersFrom = traceResult.percent;
                            continue;
                        }
                    }
                    double nextPercent = (double)resultIndex / (samples.Length - 1);
                    if (nextPercent >= tracePercent)
                    {
                        nextPercent = (double)(resultIndex - 1) / (samples.Length - 1);
                    }
                    if (nextPercent < clipFrom)
                    {
                        nextPercent = clipFrom;
                    }
                    tracePercent = nextPercent;
                }
                lastResult  = traceResult;
                traceResult = Evaluate(tracePercent);
                float traveled = (traceResult.position - lastResult.position).magnitude;
                moved += traveled;
                if (moved >= distance)
                {
                    float  excess      = moved - distance;
                    double lerpPercent = 1.0 - excess / traveled;
                    if (direction == Spline.Direction.Backward && !averageResultVectors)
                    {
                        traceResult.direction = samples[Mathf.Max(resultIndex - 1, 0)].direction;
                        float directionLerp = (float)lastResult.percent * (samples.Length - 1) - resultIndex;
                        lastResult.direction = Vector3.Slerp(samples[resultIndex].direction, traceResult.direction, 1f - directionLerp);
                    }
                    _followResult = SplineResult.Lerp(lastResult, traceResult, lerpPercent);
                    CheckTriggers((float)traceTriggersFrom, _followResult.percent);
                    break;
                }
            }
            ApplyTransformation();
            white = !white;
        }
Пример #12
0
        public void Move(float distance)
        {
            if (distance < 0f)
            {
                distance = 0f;
            }
            if (distance == 0f)
            {
                return;
            }
            if (base.samples.Length <= 1)
            {
                if (base.samples.Length == 1)
                {
                    _result.CopyFrom(base.samples[0]);
                    ApplyMotion();
                }
                return;
            }
            bool   flag  = false;
            bool   flag2 = false;
            float  num   = 0f;
            int    num2  = 0;
            double num3  = ClipPercent(_result.percent);

            if (num3 == 0.0 && lastClippedPercent == 1.0 && base.clipFrom == base.clipTo)
            {
                num3 = 1.0;
            }
            SplineResult splineResult  = new SplineResult(_result);
            SplineResult splineResult2 = new SplineResult(_result);

            if (_direction == Spline.Direction.Forward)
            {
                num2 = DMath.FloorInt(num3 * (double)(base.clippedSamples.Length - 1)) - 1;
                if (num2 < 0)
                {
                    num2 = 0;
                }
                for (int i = num2; i < base.clippedSamples.Length - 1 && !(ClipPercent(base.clippedSamples[i].percent) > num3); i++)
                {
                    num2 = i;
                }
            }
            else
            {
                num2 = DMath.CeilInt(num3 * (double)(base.clippedSamples.Length - 1)) + 1;
                if (num2 >= base.clippedSamples.Length)
                {
                    num2 = base.clippedSamples.Length - 1;
                }
                for (int num4 = num2; num4 >= 1; num4--)
                {
                    double num5 = ClipPercent(base.clippedSamples[num4].percent);
                    if (num4 == base.clippedSamples.Length - 1)
                    {
                        num5 = 1.0;
                    }
                    if (num5 < num3)
                    {
                        break;
                    }
                    num2 = num4;
                }
            }
            while (num < distance)
            {
                splineResult.CopyFrom(splineResult2);
                if (_direction == Spline.Direction.Forward)
                {
                    num2++;
                    if ((base.samplesAreLooped && _result.percent >= base.clippedSamples[base.clippedSamples.Length - 1].percent && _result.percent < base.clippedSamples[0].percent) || (!base.samplesAreLooped && _result.percent >= base.clippedSamples[base.clippedSamples.Length - 1].percent) || num2 >= base.clippedSamples.Length)
                    {
                        if (this.onEndReached != null)
                        {
                            flag = true;
                        }
                        if (wrapMode == Wrap.Default)
                        {
                            lastClippedPercent = 1.0;
                            _result.CopyFrom(base.clippedSamples[base.clippedSamples.Length - 1]);
                            CheckTriggersClipped(splineResult.percent, _result.percent);
                            break;
                        }
                        if (wrapMode == Wrap.Loop)
                        {
                            CheckTriggersClipped(splineResult.percent, base.clippedSamples[base.clippedSamples.Length - 1].percent);
                            splineResult.CopyFrom(base.clippedSamples[0]);
                            num2 = 1;
                        }
                        else if (wrapMode == Wrap.PingPong)
                        {
                            _direction = Spline.Direction.Backward;
                            CheckTriggersClipped(splineResult.percent, base.clippedSamples[base.clippedSamples.Length - 1].percent);
                            splineResult.CopyFrom(base.clippedSamples[base.clippedSamples.Length - 1]);
                            num2 = base.clippedSamples.Length - 2;
                        }
                    }
                }
                else
                {
                    num2--;
                    if ((base.samplesAreLooped && _result.percent <= base.clippedSamples[0].percent && _result.percent > base.clippedSamples[base.clippedSamples.Length - 1].percent) || (!base.samplesAreLooped && _result.percent <= base.clippedSamples[0].percent) || num2 < 0)
                    {
                        if (this.onBeginningReached != null)
                        {
                            flag2 = true;
                        }
                        if (wrapMode == Wrap.Default)
                        {
                            lastClippedPercent = 0.0;
                            _result.CopyFrom(base.clippedSamples[0]);
                            CheckTriggersClipped(splineResult.percent, _result.percent);
                            break;
                        }
                        if (wrapMode == Wrap.Loop)
                        {
                            CheckTriggersClipped(splineResult.percent, base.clippedSamples[0].percent);
                            splineResult.CopyFrom(base.clippedSamples[base.clippedSamples.Length - 1]);
                            num2 = base.clippedSamples.Length - 2;
                        }
                        else if (wrapMode == Wrap.PingPong)
                        {
                            _direction = Spline.Direction.Forward;
                            CheckTriggersClipped(splineResult.percent, base.clippedSamples[0].percent);
                            splineResult.CopyFrom(base.clippedSamples[0]);
                            num2 = 1;
                        }
                    }
                }
                lastClippedPercent = ClipPercent(_result.percent);
                splineResult2.CopyFrom(base.clippedSamples[num2]);
                float num6 = Vector3.Distance(splineResult2.position, splineResult.position);
                num += num6;
                if (num >= distance)
                {
                    float  num7 = num - distance;
                    double t    = 1.0 - (double)(num7 / num6);
                    SplineResult.Lerp(splineResult, splineResult2, t, _result);
                    if (base.samplesAreLooped)
                    {
                        if (_direction == Spline.Direction.Forward && splineResult.percent > splineResult2.percent)
                        {
                            _result.percent = DMath.Lerp(splineResult.percent, 1.0, t);
                        }
                        else if (_direction == Spline.Direction.Backward && splineResult.percent < splineResult2.percent)
                        {
                            _result.percent = DMath.Lerp(1.0, splineResult2.percent, t);
                        }
                    }
                    CheckTriggersClipped(splineResult.percent, _result.percent);
                    break;
                }
                CheckTriggersClipped(splineResult.percent, splineResult2.percent);
            }
            ApplyMotion();
            if (flag)
            {
                this.onEndReached();
            }
            else if (flag2)
            {
                this.onBeginningReached();
            }
            InvokeTriggers();
        }