Beispiel #1
0
    public void GenerateWallPoints()
    {
        Vector3 p0, p1, m0, m1;
        float   width1, width2;

        //Debug.Log("This is a test " + Points[0]);
        lWallPoints.Clear();
        rWallPoints.Clear();

        // First for loop goes through each individual control point and connects it to the next, so 0-1, 1-2, 2-3 and so on
        for (int i = 0; i < Points.Count - 1; i++)
        {
            //if (Points[i] == null || Points[i + 1] == null || (i > 0 && Points[i - 1] == null) || (i < Points.Count - 2 && Points[i + 2] == null))
            //{
            //    return;
            //}
            //Debug.Log(GameManager.segmentPos);
            p0     = Points[i].transform.position;
            p1     = Points[i + 1].transform.position;
            width1 = Points[i].GetComponent <PointControl>().width;
            width2 = Points[i + 1].GetComponent <PointControl>().width;

            // Tangent calculation for each control point
            // Tangent M[k] = (P[k+1] - P[k-1]) / 2
            // With [] indicating subscript

            // m0
            if (i == 0)
            {
                m0 = p1 - p0;
            }
            else
            {
                m0 = 0.5f * (p1 - Points[i - 1].transform.position);
            }

            // m1
            if (i < Points.Count - 2)
            {
                m1 = 0.5f * (Points[(i + 2) % Points.Count].transform.position - p0);
            }
            else
            {
                m1 = p1 - p0;
            }

            Vector3    normal      = CatmullRom.CalculateNormal(m0, Vector3.up);
            Quaternion orientation = Quaternion.LookRotation(m0, normal);
            Vector3    leftOffset  = orientation * new Vector3(-width1 / 2.0f, 0, 0);
            Vector3    rightOffset = orientation * new Vector3(width1 / 2.0f, 0, 0);
            if (i == 0)
            {
                lWallPoints.Add(p0 + leftOffset);
                rWallPoints.Add(p0 + rightOffset);
            }

            normal      = CatmullRom.CalculateNormal(m1, Vector3.up);
            orientation = Quaternion.LookRotation(m1, normal);
            leftOffset  = orientation * new Vector3(-width2 / 2.0f, 0, 0);
            rightOffset = orientation * new Vector3(width2 / 2.0f, 0, 0);

            lWallPoints.Add(p1 + leftOffset);
            rWallPoints.Add(p1 + rightOffset);
        }
    }