Пример #1
0
    public void ApplyCurb(GeneratedCurb curb, TerrainModifier terrainModifier)
    {
        // Todo height of TPP apply

        if (meshFilter == null)
        {
            meshFilter   = GetComponent <MeshFilter>();
            meshCollider = GetComponent <MeshCollider>();
        }

        Vector3[] trVerts = new Vector3[curb.RenderVertices.Length];
        for (int i = 0; i < trVerts.Length; i++)
        {
            trVerts[i] = curb.RenderVertices[i] + new Vector3(0f, terrainModifier.GetTensorHeight(curb.RenderVertices[i].x, curb.RenderVertices[i].z), 0f);
        }



        meshFilter.mesh.Clear();

        meshFilter.mesh.vertices = trVerts;
        meshFilter.mesh.uv       = curb.RenderUVs;

        meshFilter.mesh.subMeshCount = 1;

        meshFilter.mesh.SetTriangles(curb.RenderTriangles, 0);

        meshFilter.mesh.RecalculateNormals();

        meshCollider.sharedMesh = meshFilter.mesh;
    }
Пример #2
0
 public void AddCurb(GeneratedCurb curb)
 {
     curbs.Add(curb);
 }
Пример #3
0
    public void CalculateCurbs(DiscreteTrack track)
    {
        bool[] leftCurbs  = new bool[track.idealLineMesh.Length];
        bool[] rightCurbs = new bool[track.idealLineMesh.Length];
        for (int i = 0; i < leftCurbs.Length; i++)
        {
            leftCurbs[i]  = false;
            rightCurbs[i] = false;
        }


        float threshhold = 0.23f;
        int   addbefaft  = 8;

        bool inv        = false;
        int  startIndex = -1;
        int  endIndex   = -1;

        for (int i = 0; i < track.idealLineMesh.Length; i++)
        {
            if (track.idealLineMesh[i] < threshhold && startIndex == -1)
            {
                inv        = false;
                startIndex = i;
            }
            else if (track.idealLineMesh[i] >= threshhold && startIndex != -1)
            {
                endIndex = i;
            }

            if (track.idealLineMesh[i] > (1f - threshhold) && startIndex == -1)
            {
                inv        = true;
                startIndex = i;
            }
            else if (track.idealLineMesh[i] <= (1f - threshhold) && startIndex != -1)
            {
                endIndex = i;
            }


            if (startIndex != -1 && endIndex != -1)
            {
                for (int j = (startIndex - addbefaft); j < endIndex + addbefaft; j++)
                {
                    int jR = j < 0 ? j + track.idealLineMesh.Length : (j % track.idealLineMesh.Length);

                    if (inv)
                    {
                        leftCurbs[jR] = true;
                    }
                    else
                    {
                        rightCurbs[jR] = true;
                    }
                }

                startIndex = -1;
                endIndex   = -1;
            }
        }

        startIndex = -1;

        for (int i = 0; i < leftCurbs.Length; i++)
        {
            if (leftCurbs[i] && startIndex == -1)
            {
                startIndex = i;
            }
            else if (leftCurbs[i] == false && startIndex != -1)
            {
                Vector3[] curbVertices   = new Vector3[i - startIndex];
                Vector3[] curbDirections = new Vector3[i - startIndex];
                for (int j = 0; j < curbVertices.Length; j++)
                {
                    curbVertices[j]   = new Vector3(leftV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)].x, 0f, leftV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)].y);
                    curbDirections[j] = new Vector3((leftV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)] - rightV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)]).x, 0f, (leftV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)] - rightV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)]).y);

                    leftWidths[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)] = 3f;
                }

                int prevIndex = startIndex - 1 - offset < 0 ? startIndex - 1 - offset + track.idealLineMesh.Length : startIndex - 1 - offset;
                int afteIndex = i - offset < 0 ? i - offset + track.idealLineMesh.Length : i - offset;


                GeneratedCurb curb = new GeneratedCurb(curbVertices, curbDirections, 3f, 0f, 0f, new Vector3(leftV[prevIndex].x, 0f, leftV[prevIndex].y), new Vector3(leftV[afteIndex].x, 0f, leftV[afteIndex].y));

                genCurbs.Add(curb);

                startIndex = -1;
            }
        }

        startIndex = -1;

        for (int i = 0; i < rightCurbs.Length; i++)
        {
            if (rightCurbs[i] && startIndex == -1)
            {
                startIndex = i;
            }
            else if (rightCurbs[i] == false && startIndex != -1)
            {
                Vector3[] curbVertices   = new Vector3[i - startIndex];
                Vector3[] curbDirections = new Vector3[i - startIndex];
                for (int j = 0; j < curbVertices.Length; j++)
                {
                    curbVertices[j]   = new Vector3(rightV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)].x, 0f, rightV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)].y);
                    curbDirections[j] = new Vector3((rightV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)] - leftV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)]).x, 0f, (rightV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)] - leftV[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)]).y);

                    rightWidths[(startIndex + j - offset) < 0 ? (startIndex + j - offset + leftV.Length) : (startIndex + j - offset)] = 3f;
                }

                int prevIndex = startIndex - 1 - offset < 0 ? startIndex - 1 - offset + track.idealLineMesh.Length : startIndex - 1 - offset;
                int afteIndex = i - offset < 0 ? i - offset + track.idealLineMesh.Length : i - offset;


                GeneratedCurb curb = new GeneratedCurb(curbVertices, curbDirections, 3f, 0f, 0f, new Vector3(rightV[prevIndex].x, 0f, rightV[prevIndex].y), new Vector3(rightV[afteIndex].x, 0f, rightV[afteIndex].y));

                genCurbs.Add(curb);

                startIndex = -1;
            }
        }
    }