private void GenerateRoad()
    {
        if (mMesh == null)
        {
            mMesh      = new Mesh();
            mMesh.name = "RoadSegment";
            m_MeshFilter.sharedMesh = mMesh;
        }

        mMesh.Clear();

        //vertices
        List <Vector3> allVertsAlongRoad = new List <Vector3> ();
        List <Vector3> allNormals        = new List <Vector3> ();

        for (int ring = 0; ring < m_VerticalSliceCount; ring++)
        {
            float t = ring / (m_VerticalSliceCount - 1f);

            OrientedPoint op = GetBezierOrientation(t);
            for (int i = 0; i < m_Shape2D.vertices.Length; i++)
            {
                allVertsAlongRoad.Add(op.LocalToWorld(m_Shape2D.vertices[i].points));
                allNormals.Add(op.LocalToWorldVec(m_Shape2D.vertices[i].normals));
                //allNormals.Add (m_Shape2D.vertices[i].normals);//without rotation
            }
        }

        //Trianlges
        List <int> allTriangles = new List <int> ();

        for (int ring = 0; ring < m_VerticalSliceCount - 1; ring++)
        {
            int rootIndex        = ring * m_Shape2D.GetVertexCount();
            int rootIndexForward = (ring + 1) * m_Shape2D.GetVertexCount();

            for (int line = 0; line < m_Shape2D.GetLineCount(); line += 2)
            {
                int lineA = m_Shape2D.lineIndices[line];
                int lineB = m_Shape2D.lineIndices[line + 1];

                int currentA = rootIndex + lineA;
                int currentB = rootIndex + lineB;
                int nextA    = rootIndexForward + lineA;
                int nextB    = rootIndexForward + lineB;

                allTriangles.Add(currentA);
                allTriangles.Add(nextA);
                allTriangles.Add(nextB);

                allTriangles.Add(nextB);
                allTriangles.Add(currentB);
                allTriangles.Add(currentA);
            }
        }
        mMesh.SetVertices(allVertsAlongRoad);
        mMesh.SetTriangles(allTriangles, 0);
        mMesh.SetNormals(allNormals);
    }
Esempio n. 2
0
    void GenerateMesh()
    {
        mesh.Clear();

        // Vertices
        List <Vector3> verts   = new List <Vector3>();
        List <Vector3> normals = new List <Vector3>();

        for (int ring = 0; ring < edgeRingCount; ring++)
        {
            float         t  = ring / (edgeRingCount - 1f);
            OrientedPoint op = GetBezierOP(t);
            for (int i = 0; i < shape2D.VertexCount; i++)
            {
                verts.Add(op.LocalToWorldPos(shape2D.vertices[i].point));
                normals.Add(op.LocalToWorldVec(shape2D.vertices[i].normal));
            }
        }

        // Triangles
        List <int> triIndices = new List <int>();

        for (int ring = 0; ring < edgeRingCount - 1; ring++)
        {
            int rootIndex     = ring * shape2D.VertexCount;
            int rootIndexNext = (ring + 1) * shape2D.VertexCount;

            for (int line = 0; line < shape2D.LineCount; line += 2)
            {
                int lineIndexA = shape2D.lineIndices[line];
                int lineIndexB = shape2D.lineIndices[line + 1];

                int currentA = rootIndex + lineIndexA;
                int currentB = rootIndex + lineIndexB;

                int nextA = rootIndexNext + lineIndexA;
                int nextB = rootIndexNext + lineIndexB;

                triIndices.Add(currentA);
                triIndices.Add(nextA);
                triIndices.Add(nextB);

                triIndices.Add(currentA);
                triIndices.Add(nextB);
                triIndices.Add(currentB);
            }
        }

        mesh.SetVertices(verts);
        mesh.SetNormals(normals);
        mesh.SetTriangles(triIndices, 0);
    }
Esempio n. 3
0
    /// <summary>
    /// Smesh goes brrr
    /// </summary>
    /// <param name="uv"></param>
    private void GenerateMesh(Vector2 uv)
    {
        // On vide nos listes
        Mesh.Clear();
        vertices.Clear();
        triangles.Clear();
        normals.Clear();
        uv0.Clear();
        uv1.Clear();

        CubicBezierCurve bezier = GetBezierRepresentation(Space.Self);

        float curveArcLegth = bezier.GetArcLength();
        float tiling        = GetTextureAspectRatio();
        int   edgeCount     = Mathf.Max(2, Mathf.RoundToInt(curveArcLegth * RoadChain.trianglesPerSegment)); // 2 triangles minimum sinon kaboom

        // Generation vertices normales & map texture
        for (int i = 0; i < edgeCount; ++i)
        {
            float         t  = i / (edgeCount - 1f);
            OrientedPoint op = bezier.GetOrientedPoint(t);

            float uv0V = t * tiling;
            float uv1U = Mathf.Lerp(uv.x, uv.y, t);

            for (int j = 0; j < RoadChain.mesh2D.VertexCount; ++j)
            {
                vertices.Add(op.LocalToWorldPos(RoadChain.mesh2D.vertices[j].point));
                normals.Add(op.LocalToWorldVec(RoadChain.mesh2D.vertices[j].normal));
                uv0.Add(new Vector2(RoadChain.mesh2D.vertices[j].u, uv0V));
                uv1.Add(new Vector2(uv1U, 0));
            }
        }

        // Generation des triangles
        for (int i = 0; i < edgeCount - 1; ++i)
        {
            int ifv   = i * RoadChain.mesh2D.VertexCount;
            int ipofv = (i + 1) * RoadChain.mesh2D.VertexCount;

            for (int j = 0; j < RoadChain.mesh2D.LineCount; j += 2)
            {
                int t   = RoadChain.mesh2D.triangles[j];
                int tpo = RoadChain.mesh2D.triangles[j + 1];
                triangles.Add(ifv + t);
                triangles.Add(ipofv + t);
                triangles.Add(ipofv + tpo);
                triangles.Add(ifv + t);
                triangles.Add(ipofv + tpo);
                triangles.Add(ifv + tpo);
            }
        }

        Mesh.SetVertices(vertices);
        Mesh.SetTriangles(triangles, 0);
        Mesh.SetNormals(normals);
        Mesh.SetUVs(0, uv0);
        Mesh.SetUVs(1, uv1);

        Mesh.RecalculateBounds();
        MeshCollider collider = GetComponent <MeshCollider>();

        collider.sharedMesh = Mesh;
    }