internal Vector3 GetSegmentMidPoint(ushort segmentId)
        {
            NetSegment segment = netMan.m_segments.m_buffer[segmentId];

            NetLane[] lanes = netMan.m_lanes.m_buffer;
            NetNode[] nodes = netMan.m_nodes.m_buffer;


            NetNode start = nodes[segment.m_startNode];
            NetNode end   = nodes[segment.m_endNode];

            //return Vector3.Lerp(start.m_position, end.m_position, 0.5f);
            return(Beizer.CalculateBezierPoint(0.5f, start.m_position, start.m_position + segment.m_startDirection / 3.0f, end.m_position + segment.m_endDirection / 3.0f, end.m_position));
        }
		public void AddPoints(PathPoint[] _points){

            List<PathPoint> points = FilterPoints(_points);

            lastPoint = points[0].pos;


            for (int i = 0; i < points.Count - 1; i++)
            {

                Debug.DrawLine(points[i].pos, points[i].pos + Vector3.up, points[i].guessed ? Color.cyan : Color.blue, 20000);

                Debug.DrawLine(points[i].pos, points[i].pos + points[i].forwards, Color.green, 20000);
                Debug.DrawLine(points[i].pos, points[i].pos + points[i].backwards, Color.grey, 20000);

                

                PathPoint thisPoint = points[i];
                PathPoint nextPoint = points[i + 1];

                
               // Log.debug(i + ":" + "Angle:" + Vector3.Angle(thisPoint.normal, nextPoint.normal) + " Magnitude: " + (thisPoint.pos - nextPoint.pos).magnitude);
                                

                Vector3 thisPointPos = thisPoint.pos;
                Vector3 nextPointPos = nextPoint.pos;

                float angle = Vector3.Angle(thisPoint.forwards, nextPoint.forwards);
                float segementLength = (thisPointPos - nextPointPos).magnitude;

                if (thisPoint.forwards.magnitude > maxNormalSize)
                    thisPoint.forwards = thisPoint.forwards.normalized * maxNormalSize;

                if (thisPoint.backwards.magnitude > maxNormalSize)
                    thisPoint.backwards = thisPoint.backwards.normalized * maxNormalSize;

                Vector3 p0 = thisPointPos;
                Vector3 p1 = thisPointPos + thisPoint.forwards * normalScaleFactor;
                Vector3 p2 = nextPointPos + nextPoint.backwards * normalScaleFactor;

                //Seperate logic for tight turns
                if (segementLength < 30.0f)
                {
                    p1 = thisPointPos + thisPoint.forwards.normalized * segementLength * tightNormalScaleFactor;
                    p2 = nextPointPos + nextPoint.backwards.normalized * segementLength * tightNormalScaleFactor;
                }

                

                Vector3 p3 = nextPointPos;

             //   Debug.DrawLine(p0, p1, Color.cyan, 2000);
             //   Debug.DrawLine(p2, p3, Color.cyan, 2000);
                                              
                
                //First calculate the real length of the spline with a rough calulation
                float realLength = 0;
                float maxAngle = 0;
                for (float a = 0; a < 1.0f; a += 0.1f)
                {
                    Vector3 pointA = Beizer.CalculateBezierPoint(a, p0, p1, p2, p3);
                    Vector3 pointB = Beizer.CalculateBezierPoint(a + 0.2f, p0, p1, p2, p3);

                    float ang = Vector3.Angle(pointB-p0, p1-p0);
                    if (ang > maxAngle) {
                        maxAngle = ang;
                    }

                    realLength += (pointA - pointB).magnitude;
                }

                if (realLength > pathBreakThreshold)
                {
                    AddVertexPair(thisPoint.pos, thisPoint.forwards.normalized, false);
                    AddEndStop(thisPoint.pos, thisPoint.forwards.normalized, true);
                    AddEndStop(nextPoint.pos, nextPoint.forwards.normalized, false);
                    AddVertexPair(nextPoint.pos, nextPoint.forwards.normalized);

                    continue;
                }


                // Make each segment tile a whole number of texture repeats so that
                // overlaping paths line up
                int textureRepeats = (int)Math.Floor(realLength / (width * 4));

                textureOffset = (float)Math.Round(textureOffset);

                //Aim to keep a fixed size for each quad, increaing the number the more it curves
                int steps = (int)Math.Floor(realLength / 20.0f + maxAngle / 3.0f) + 1;
                float step = 1.0f / steps;
                for (float a = 0; a < 1.0f; a += step)
                {
                    Vector3 point = Beizer.CalculateBezierPoint(a, p0, p1, p2, p3);
                    Vector3 pointB = Beizer.CalculateBezierPoint(a+step, p0, p1, p2, p3);
                    Vector3 fwd = (pointB - point).normalized;

                    Debug.DrawLine(point+ Vector3.up, point + Vector3.up + fwd, Color.green, 2000);

                    AddVertexPair(point, fwd);

                    textureOffset += step * textureRepeats; 

                }

            }


            //End the last segement
            AddVertexPair(points[points.Count - 1].pos, points[points.Count - 1].forwards.normalized, false);

			//GenerateIndiciesAsLineStrip ();

            AddEndStop(points[0].pos, points[0].forwards.normalized, false);
            AddEndStop(points[points.Count - 1].pos, points[points.Count - 1].forwards.normalized, true);
		}
Example #3
0
        public void AddPoints(Vector3[] points)
        {
            lastPoint = points[0];

            for (int i = 0; i < points.Length - 1; i++)
            {
                Vector3 start = points [i];
                Vector3 end   = points [i + 1];

                if ((end - start).magnitude < width * 3)
                {
                    continue;
                }

                if (i != 0)
                {
                    start += (end - start).normalized * (width * curveRetractionFactor);
                }

                if (i != points.Length - 2)
                {
                    end += (start - end).normalized * (width * curveRetractionFactor);
                }


                AddSegment(start, end);


                if (i < points.Length - 2)
                {
                    Vector3 cornerPoint = points[i + 1];

                    Vector3 nextStart = cornerPoint;
                    Vector3 nextEnd   = points[i + 2];
                    nextStart -= (nextStart - nextEnd).normalized * (width * curveRetractionFactor);


                    Vector3 p0 = end;
                    Vector3 p1 = Vector3.Lerp(end, cornerPoint, 0.5f);
                    Vector3 p2 = Vector3.Lerp(nextStart, cornerPoint, 0.5f);
                    Vector3 p3 = nextStart;

                    Vector3 startDir = (end - start).normalized;
                    Vector3 endDir   = (nextEnd - nextStart).normalized;

                    float step = 0.2f;
                    for (float a = 0; a <= 1.0f; a += step)
                    {
                        Vector3 point = Beizer.CalculateBezierPoint(a, p0, p1, p2, p3);
                        Vector3 fwd   = Vector3.Lerp(startDir, endDir, a);

                        textureOffset = (a * width * curveRetractionFactor * lineScale) / 2.0f;
                        AddVertexPair(point, fwd);
                    }
                }
            }

            GenerateIndiciesAsLineStrip();

            Vector3 startFwd = (points[0] - points[1]);

            //			if (startFwd.magnitude < width * 3.0f) {
            //				startFwd = (points[0] - points [2]);
            //			}
            AddEndStop(points[0], startFwd.normalized, false);

            Vector3 endFwd = (points[points.Length - 1] - points[points.Length - 2]);

            if (endFwd.magnitude < width * 3.0f)
            {
                endFwd = (points[points.Length - 1] - points[points.Length - 3]);
            }
            AddEndStop(points[points.Length - 1], endFwd.normalized, true);
        }