示例#1
0
        void AddConnection(SplineComputer computer, int pointIndex)
        {
            Node node = (Node)target;

            Node.Connection[] connections = node.GetConnections();
            if (EditorUtility.DisplayDialog("Link point?", "Add point " + pointIndex + " to connections?", "Yes", "No"))
            {
                Undo.RecordObject(addComp, "Add connection");
                Undo.RecordObject(node, "Add Connection");
                if (connections.Length == 0)
                {
                    switch (EditorUtility.DisplayDialogComplex("Align node to point?", "This is the first connection for the node, would you like to snap or align the node's Transform the spline point.", "No", "Snap", "Snap and Align"))
                    {
                    case 1: SplinePoint point = addComp.GetPoint(pointIndex);
                        node.transform.position = point.position;
                        break;

                    case 2:
                        SplineResult result = addComp.Evaluate((double)pointIndex / (addComp.pointCount - 1));
                        node.transform.position = result.position;
                        node.transform.rotation = result.rotation;
                        break;
                    }
                }
                node.AddConnection(computer, pointIndex);
                addComp  = null;
                addPoint = 0;
                SceneView.RepaintAll();
                Repaint();
            }
        }
示例#2
0
        public SplineResult Evaluate(double percent)
        {
            if (root == null)
            {
                return(null);
            }
            if (_elements.Length == 1)
            {
                return(_elements[0].computer.Evaluate(percent));
            }
            SplineComputer comp;

            Spline.Direction dir;
            double           eval = 0.0;

            GetEvaluationValues(percent, out comp, out eval, out dir);
            if (comp == null)
            {
                return(null);
            }
            SplineResult result = comp.Evaluate(eval);

            result.percent = percent;
            return(result);
        }
示例#3
0
        public static double ScreenPointToSplinePercent(SplineComputer computer, Vector2 screenPoint)
        {
            SplinePoint[] points          = computer.GetPoints();
            float         closestDistance = (screenPoint - HandleUtility.WorldToGUIPoint(points[0].position)).sqrMagnitude;
            double        closestPercent  = 0.0;
            double        add             = computer.moveStep;

            if (computer.type == Spline.Type.Linear)
            {
                add /= 2f;
            }
            int count = 0;

            for (double i = add; i < 1.0; i += add)
            {
                SplineResult result = computer.Evaluate(i);
                Vector2      point  = HandleUtility.WorldToGUIPoint(result.position);
                float        dist   = (point - screenPoint).sqrMagnitude;
                if (dist < closestDistance)
                {
                    closestDistance = dist;
                    closestPercent  = i;
                }
                count++;
            }
            return(closestPercent);
        }
示例#4
0
        public static SplineResult Lerp(SplineResult a, SplineResult b, float t)
        {
            SplineResult splineResult = new SplineResult();

            Lerp(a, b, t, splineResult);
            return(splineResult);
        }
示例#5
0
 public void SetPercent(double percent)
 {
     percentSet    = true;
     percent       = DMath.Clamp01(percent);
     _followResult = Evaluate(percent);
     ApplyTransformation();
 }
        protected override void OnSceneGUI()
        {
            base.OnSceneGUI();
            SplineFollower user = (SplineFollower)target;

            if (Application.isPlaying)
            {
                if (!user.autoFollow)
                {
                    DrawResult(user.offsettedResult);
                }
                return;
            }
            if (user.computer == null)
            {
                return;
            }
            if (user.autoStartPosition)
            {
                SplineResult result = user.address.Evaluate(user.address.Project(user.transform.position, 4, user.clipFrom, user.clipTo));
                DrawResult(result);
            }
            else if (!user.autoFollow)
            {
                SplineResult result = user.address.Evaluate(user.result.percent);
                DrawResult(result);
            }
        }
示例#7
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 virtual SplineResult Project(Vector3 point, double from = 0.0, double to = 1.0)
        {
            SplineResult result = new SplineResult();

            Project(result, point, from, to);
            return(result);
        }
示例#8
0
        public static SplineResult Lerp(SplineResult a, SplineResult b, double t)
        {
            SplineResult result = new SplineResult();

            Lerp(a, b, t, result);
            return(result);
        }
示例#9
0
        private double ProjectMouse()
        {
            if (computer.pointCount == 0)
            {
                return(0.0);
            }
            float  closestDistance = (Event.current.mousePosition - HandleUtility.WorldToGUIPoint(computer.GetPointPosition(0))).sqrMagnitude;
            double closestPercent  = 0.0;
            double add             = computer.moveStep;

            if (computer.type == Spline.Type.Linear)
            {
                add /= 2.0;
            }
            int count = 0;

            for (double i = add; i < 1.0; i += add)
            {
                SplineResult result = computer.Evaluate(i);
                Vector2      point  = HandleUtility.WorldToGUIPoint(result.position);
                float        dist   = (point - Event.current.mousePosition).sqrMagnitude;
                if (dist < closestDistance)
                {
                    closestDistance = dist;
                    closestPercent  = i;
                }
                count++;
            }
            return(closestPercent);
        }
示例#10
0
        /// <summary>
        /// Evaluate the spline at the position of a given point and return a SplineResult
        /// </summary>
        /// <param name="pointIndex">Point index</param>
        public SplineResult Evaluate(int pointIndex)
        {
            SplineResult result = new SplineResult();

            Evaluate(result, GetPointPercent(pointIndex));
            return(result);
        }
示例#11
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]);
            }
        }
示例#12
0
        /// <summary>
        /// Evaluates the spline segment based on the spline's precision.
        /// </summary>
        /// <param name="from">Start position [0-1]</param>
        /// <param name="to">Target position [from-1]</param>
        /// <returns></returns>
        public void Evaluate(ref SplineResult[] samples, double from = 0.0, double to = 1.0)
        {
            from = DMath.Clamp01(from);
            to   = DMath.Clamp(to, from, 1.0);
            double fromValue         = from * (iterations - 1);
            double toValue           = to * (iterations - 1);
            int    clippedIterations = DMath.CeilInt(toValue) - DMath.FloorInt(fromValue) + 1;

            if (samples == null)
            {
                samples = new SplineResult[clippedIterations];
            }
            if (samples.Length != clippedIterations)
            {
                samples = new SplineResult[clippedIterations];
            }
            double percent = from;
            double ms      = moveStep;
            int    index   = 0;

            while (true)
            {
                samples[index] = Evaluate(percent);
                index++;
                if (index >= samples.Length)
                {
                    break;
                }
                percent = DMath.Move(percent, to, ms);
            }
        }
示例#13
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;
        }
        public SplineResult Evaluate(double percent)
        {
            SplineResult result = new SplineResult();

            Evaluate(result, percent);
            return(result);
        }
示例#15
0
        public SplineResult GetFollowResult(SplineFollower follower, double percent)
        {
            SplineResult result = follower.Evaluate(percent);
            Vector3      right  = Vector3.Cross(result.direction, result.normal);

            result.position += -right * follower.offset.x * result.size + result.normal * follower.offset.y * result.size;
            return(result);
        }
示例#16
0
 public void SetDistance(float distance)
 {
     _followResult = Evaluate(0.0);
     Spline.Direction dir = direction;
     direction = Spline.Direction.Forward;
     Move(distance);
     direction = dir;
 }
示例#17
0
 public void CopyFrom(SplineResult input)
 {
     position  = input.position;
     direction = input.direction;
     normal    = input.normal;
     color     = input.color;
     size      = input.size;
     percent   = input.percent;
 }
示例#18
0
 public static void Lerp(SplineResult a, SplineResult b, float t, SplineResult target)
 {
     target.position  = DMath.LerpVector3(a.position, b.position, t);
     target.direction = Vector3.Slerp(a.direction, b.direction, t);
     target.normal    = Vector3.Slerp(a.normal, b.normal, t);
     target.color     = Color.Lerp(a.color, b.color, t);
     target.size      = Mathf.Lerp(a.size, b.size, t);
     target.percent   = DMath.Lerp(a.percent, b.percent, t);
 }
示例#19
0
 public void Lerp(SplineResult b, float t)
 {
     this.position  = Vector3.Lerp(this.position, b.position, t);
     this.direction = Vector3.Slerp(this.direction, b.direction, t);
     this.normal    = Vector3.Slerp(this.normal, b.normal, t);
     this.color     = Color.Lerp(this.color, b.color, t);
     this.size      = Mathf.Lerp(this.size, b.size, t);
     this.percent   = DMath.Lerp(this.percent, b.percent, t);
 }
示例#20
0
 public SplineResult(SplineResult input)
 {
     position  = input.position;
     normal    = input.normal;
     direction = input.direction;
     color     = input.color;
     size      = input.size;
     percent   = input.percent;
 }
示例#21
0
        /// <summary>
        /// Same as Spline.Evaluate but the result is transformed by the computer's transform
        /// </summary>
        /// <param name="percent">Evaluation percent</param>
        /// <param name="address">Optional address if junctions should be used</param>
        /// <returns></returns>
        public SplineResult Evaluate(double percent)
        {
            SplineResult result = spline.Evaluate(percent);

            if (_space == SplineComputer.Space.Local)
            {
                TransformResult(result);
            }
            return(result);
        }
示例#22
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);
            }
        }
示例#23
0
        public void Lerp(SplineResult b, double t)
        {
            float ft = (float)t;

            this.position  = DMath.LerpVector3(this.position, b.position, t);
            this.direction = Vector3.Slerp(this.direction, b.direction, ft);
            this.normal    = Vector3.Slerp(this.normal, b.normal, ft);
            this.color     = Color.Lerp(this.color, b.color, ft);
            this.size      = Mathf.Lerp(this.size, b.size, ft);
            this.percent   = DMath.Lerp(this.percent, b.percent, t);
        }
示例#24
0
        public static SplineResult Lerp(SplineResult a, SplineResult b, float t)
        {
            SplineResult result = new SplineResult();

            result.position  = Vector3.Lerp(a.position, b.position, t);
            result.direction = Vector3.Slerp(a.direction, b.direction, t);
            result.normal    = Vector3.Slerp(a.normal, b.normal, t);
            result.color     = Color.Lerp(a.color, b.color, t);
            result.size      = Mathf.Lerp(a.size, b.size, t);
            result.percent   = DMath.Lerp(a.percent, b.percent, t);
            return(result);
        }
示例#25
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]);
         }
     }
 }
示例#26
0
 void PaintHeightMap(Terrain terrain, SplineComputer computer, ref float[,] drawLayer, ref float[,] alphaLayer)
 {
     if (heights == null)
     {
         GetBase();
     }
     SplineResult[] results = new SplineResult[computer.iterations];
     for (int i = 0; i < results.Length; i++)
     {
         float percent = (float)i / (results.Length - 1);
         results[i] = computer.Evaluate(percent);
     }
     Draw(results, ref drawLayer, ref alphaLayer);
 }
示例#27
0
        void Draw(SplineResult[] points, ref float[,] drawLayer, ref float[,] alphaLayer)
        {
            List <SplineResult> selectedPoints = new List <SplineResult>();
            Point last = new Point();

            //Filter out points that are too close to each other
            for (int i = 0; i < points.Length; i++)
            {
                Point current = ToHeightmapCoords(points[i].position + points[i].normal * offset);
                if (i == 0 || i == points.Length - 1)
                {
                    last = new Point(current.x, current.y);
                    selectedPoints.Add(points[i]);
                }
                else if (Vector2.Distance(new Vector2(current.x, current.y), new Vector2(last.x, last.y)) >= 1.5f)
                {
                    selectedPoints.Add(points[i]);
                    last = new Point(current.x, current.y);
                }
            }
            if (selectedPoints.Count <= 1)
            {
                return;
            }
            TerrainPaintPoint[] paintPoints = new TerrainPaintPoint[selectedPoints.Count];
            for (int i = 0; i < selectedPoints.Count; i++)
            {
                ConvertToPaintPoint(selectedPoints[i], ref paintPoints[i]);
            }
            //Paint the points
            for (int i = 0; i < paintPoints.Length - 1; i++)
            {
                hasChanges = true;
                PaintSegment(paintPoints[i], paintPoints[i + 1], ref drawLayer, ref alphaLayer);
            }

            SplineResult exResult = selectedPoints[0];

            exResult.position += exResult.position - selectedPoints[1].position;
            TerrainPaintPoint exPoint = null;

            ConvertToPaintPoint(exResult, ref exPoint);
            PaintSegment(paintPoints[0], exPoint, ref drawLayer, ref alphaLayer, false, false);

            exResult           = selectedPoints[selectedPoints.Count - 1];
            exResult.position += exResult.position - selectedPoints[selectedPoints.Count - 2].position;
            ConvertToPaintPoint(exResult, ref exPoint);
            PaintSegment(paintPoints[paintPoints.Length - 1], exPoint, ref drawLayer, ref alphaLayer, false, false);
            //Extrapolate the ending and the begining
        }
        protected void DrawResult(SplineResult result)
        {
            SplineTracer tracer = (SplineTracer)target;

            Handles.color = Color.white;
            Handles.DrawLine(tracer.transform.position, result.position);
            SplineEditorHandles.DrawSolidSphere(result.position, HandleUtility.GetHandleSize(result.position) * 0.2f);
            Handles.color = Color.blue;
            Handles.DrawLine(result.position, result.position + result.direction * HandleUtility.GetHandleSize(result.position) * 0.5f);
            Handles.color = Color.green;
            Handles.DrawLine(result.position, result.position + result.normal * HandleUtility.GetHandleSize(result.position) * 0.5f);
            Handles.color = Color.red;
            Handles.DrawLine(result.position, result.position + result.right * HandleUtility.GetHandleSize(result.position) * 0.5f);
            Handles.color = Color.white;
        }
示例#29
0
        public void SetNormals(ref SplinePoint[] points, ref List <int> selected)
        {
            Vector3 avg = Vector3.zero;

            for (int i = 0; i < selected.Count; i++)
            {
                avg += points[selected[i]].position;
            }
            if (selected.Count > 1)
            {
                avg /= selected.Count;
            }
            Camera editorCamera = SceneView.lastActiveSceneView.camera;

            for (int i = 0; i < selected.Count; i++)
            {
                switch (setNormalMode)
                {
                case 0: points[selected[i]].normal = Vector3.Normalize(editorCamera.transform.position - points[selected[i]].position); break;

                case 1: points[selected[i]].normal = editorCamera.transform.forward; break;

                case 2: points[selected[i]].normal = CalculatePointNormal(ref points, selected[i]); break;

                case 3: points[selected[i]].normal = Vector3.left; break;

                case 4: points[selected[i]].normal = Vector3.right; break;

                case 5: points[selected[i]].normal = Vector3.up; break;

                case 6: points[selected[i]].normal = Vector3.down; break;

                case 7: points[selected[i]].normal = Vector3.forward; break;

                case 8: points[selected[i]].normal = Vector3.back; break;

                case 9: points[selected[i]].normal *= -1; break;

                case 10: points[selected[i]].normal = Vector3.Normalize(avg - points[selected[i]].position); break;

                case 11:
                    double       percent = (double)selected[i] / (points.Length - 1);
                    SplineResult result  = computer.Evaluate(percent);
                    points[selected[i]].normal = Vector3.Cross(result.direction, result.right).normalized;
                    break;
                }
            }
        }
示例#30
0
        private void TRS(ExtrudableMesh source, TS_Mesh target, double percent)
        {
            CreateTSFromExtrudableMesh(source, ref target);
            SplineResult splineResult = Evaluate(percent);
            Quaternion   rhs          = Quaternion.identity;

            switch (axis)
            {
            case Axis.X:
                rhs = Quaternion.LookRotation(Vector3.right);
                break;

            case Axis.Y:
                rhs = Quaternion.LookRotation(Vector3.up, Vector3.back);
                break;
            }
            ref Matrix4x4 reference = ref vertexMatrix;