Exemple #1
0
            public void ProcessToFloor(Inlet <SplineSys> splineIn, Outlet <SplineSys> splineOut, TileData data, StopToken stop)
            {
                SplineSys   src     = data.ReadInletProduct(splineIn);
                MatrixWorld heights = data.ReadInletProduct(heightIn);

                if (src == null)
                {
                    return;
                }
                if (!enabled || heights == null)
                {
                    data.StoreProduct(splineOut, src); return;
                }

                if (stop != null && stop.stop)
                {
                    return;
                }
                SplineSys dst = new SplineSys(src);

                FloorSplines(dst, heights);
                dst.Update();

                if (stop != null && stop.stop)
                {
                    return;
                }
                data.StoreProduct(splineOut, dst);

                DebugGizmos.Clear("Spline");
                foreach (Segment segment in dst.lines[0].segments)
                {
                    DebugGizmos.DrawLine("Spline", segment.start.pos, segment.end.pos, Color.white, additive: true);
                }
            }
Exemple #2
0
        public void Push(Vector3[] points, float[] ranges, float intensity = 1, float nodesPointsRatio = 0, bool horizontalOnly = true)
        /// Moves points away from the segment so that they lay no closer than range
        /// If nodesPointsRatio = 0 pushing nodes only, if 1 - pushing points only
        /// Changes the points array (if nodesPointsRatio!=0)
        /// DistFactor multiplies the range to move point (for iteration push it should be less than 1)
        {
            for (int n = 0; n < nodes.Length - 1; n++)
            {
                Vector3 min    = Vector3.Min(nodes[n], nodes[n + 1]);
                Vector3 max    = Vector3.Max(nodes[n], nodes[n + 1]);
                float   length = (nodes[n] - nodes[n + 1]).magnitude;

                Vector3 start = nodes[n];                 //will change the nodes position, thus keeping original values
                Vector3 end   = nodes[n + 1];

                for (int p = 0; p < points.Length; p++)
                {
                    Vector3 point = points[p];
                    float   range = ranges[p];

                    if (min.x > point.x + range || max.x < point.x - range ||
                        min.z > point.z + range || max.z < point.z - range ||
                        (!horizontalOnly && (min.y > point.y + range || max.y < point.y - range)))
                    {
                        continue;
                    }

                    Vector3 closestPoint = PointNearestToPos(point, start, end);
                    closestPoint = ClampPointToSegment(closestPoint, start, end);
                    float percent = (closestPoint - start).magnitude / length;
                    percent = 2 * percent * percent * percent - 3 * percent * percent + 2 * percent;

                    DebugGizmos.DrawLine("Push1", start, end);
                    DebugGizmos.DrawDot("Push", closestPoint, 6);

                    Vector3 pushVector = PushVector(point, closestPoint, range, horizontalOnly) * intensity;

                    points[p] += pushVector * nodesPointsRatio;

                    float pStart = (1 - percent) * 2;  if (pStart > 1)
                    {
                        pStart = 1;
                    }
                    nodes[n] -= pushVector * (1 - nodesPointsRatio) * pStart;

                    float pEnd = percent * 2;  if (pEnd > 1)
                    {
                        pEnd = 1;
                    }
                    nodes[n + 1] -= pushVector * (1 - nodesPointsRatio) * pEnd;
                }
            }
        }
Exemple #3
0
        private static float GetNodeWeight(Vector3 prev, Vector3 node, Vector3 next, SerpentineFactors factors, float maxHeight)
        /// returns node candidate rating 0-infinity, lower is better
        {
            //incline
            Vector3 prevDelta     = prev - node;
            float   prevHorDist   = ((Vector2D)prevDelta).Magnitude;
            float   prevElevation = prevDelta.y > 0 ? prevDelta.y : -prevDelta.y;
            float   prevIncline   = prevElevation / prevHorDist;

            Vector3 nextDelta     = node - next;
            float   nextHorDist   = ((Vector2D)nextDelta).Magnitude;
            float   nextElevation = nextDelta.y > 0 ? nextDelta.y : -nextDelta.y;
            float   nextIncline   = nextElevation / nextHorDist;

            float inclineRating = prevIncline > nextIncline ? prevIncline : nextIncline;        //0 (planar), 1 (45 degree), to infinity (90 degree)

            inclineRating = 1 - Mathf.Atan(inclineRating) / (Mathf.PI / 2f);                    //1 (planar), 0.5 (45 degree), 0 (90 degree)
            //inclineRating *= inclineRating;

            //length
            Vector2D vec     = ((Vector2D)(prev - next)).Normalized;
            float    prevDot = Vector2D.Dot(vec, ((Vector2D)(node - prev)).Normalized);
            float    nextDot = Vector2D.Dot(vec, ((Vector2D)(node - next)).Normalized);

            float length  = ((Vector2D)(prev - next)).Magnitude;
            float nLength = ((Vector2D)(prev - node)).Magnitude + ((Vector2D)(node - next)).Magnitude;

            float lengthRating = length / nLength;                      //1 (straight), 0.7071 (90 degree), 0 (sharp corner)
            //lengthRating *= lengthRating;

            //height
            float heightRating = node.y / maxHeight;

            if (factors.height < 0)
            {
                heightRating = 1 - heightRating;
            }

            //	if (lengthFactor < 1) lengthFactor = 1; //now 1 to 2 only
            //	lengthFactor -= 1; //now 0 to 1;
            //	if (lengthFactor == 1) lengthFactor = float.MaxValue;
            //	else lengthFactor = 1 / (1-lengthFactor); //now 0 to infinity

            float rating = inclineRating * factors.incline + lengthRating * factors.length + heightRating * Mathf.Abs(factors.height);

            //Mathf.Pow(inclineRating, (1-factors.incline)) * Mathf.Pow(lengthRating, (1-factors.length));

            if (tmpIteration == tmpHighlightIteration && tmpNode == tmpHighlightNode)
            {
                DebugGizmos.DrawLabel("Serp", node,
                                      "Node:" + tmpNode + " var:" + tmpVariant +
                                      "\nInc:" + inclineRating.ToString() + "(" + prevIncline.ToString() + ", " + nextIncline.ToString() + ")" +
                                      "\nLen:" + lengthRating.ToString() +           //"(" + length + ", " + nLength + ")" +
                                      "\nSum:" + rating.ToString(),
                                      additive: true);

                DebugGizmos.DrawLine("Serp", node, prev, Color.red, additive: true);

                DebugGizmos.DrawDot("Serp", node, 6, Color.red, additive: true);
            }

            return(rating);
        }