Exemple #1
0
    /* this method is adapted from http://www.jakecaspick.com/post/endlessroad/?fbclid=IwAR3z_UHz15fQohIkY6tSgBI6VLrnCgbXhqNJS6u6vl8Rl6A8P3y2pC-mDmQ
     */
    public void GenerateMesh()
    {
        GetComponent <MeshFilter>().mesh = mesh = new Mesh();
        vertices = new Vector3[(arcDivisions + 1) * (widthDivisions + 1)];
        uv       = new Vector2[vertices.Length];
        float      arcStep    = angle / arcDivisions;
        float      widthStep  = width / widthDivisions;
        GameObject obj1       = new GameObject();
        ArcPoint   nextVertex = obj1.AddComponent <ArcPoint>();

        nextVertex.Arcarcpoint(0, 0);
        Vector2 nextUV = Vector2.zero;

        if (!flipped)
        {
            for (int t = 0; t <= arcDivisions; t++)
            {
                for (int w = 0; w <= widthDivisions; w++)
                {
                    nextVertex.Set(t * arcStep + startAngle, (w * widthStep - width / 2) + radius);
                    vertices[t * (widthDivisions + 1) + w] = ArcToLocal(nextVertex);
                    nextUV.Set((float)w / widthDivisions / textureScale, (float)t / arcDivisions * totalDistance / textureScale + uvOffset);
                    uv[t * (widthDivisions + 1) + w] = nextUV;
                }
            }
        }
        else
        {
            for (int t = 0; t <= arcDivisions; t++)
            {
                for (int w = 0; w <= widthDivisions; w++)
                {
                    nextVertex.Set(t * arcStep + startAngle, (w * -widthStep + width / 2) + radius);
                    vertices[t * (widthDivisions + 1) + w] = ArcToLocal(nextVertex);
                    nextUV.Set((float)w / widthDivisions / textureScale, (float)t / arcDivisions * totalDistance / textureScale + uvOffset);
                    uv[t * (widthDivisions + 1) + w] = nextUV;
                }
            }
        }
        mesh.vertices = vertices;
        mesh.uv       = uv;
        triangles     = new int[arcDivisions * widthDivisions * 6];
        for (int ti = 0, vi = 0, t = 0; t < arcDivisions; t++, vi++)
        {
            for (int w = 0; w < widthDivisions; w++, ti += 6, vi++)
            {
                triangles[ti]     = vi;
                triangles[ti + 3] = triangles[ti + 2] = vi + 1;
                triangles[ti + 4] = triangles[ti + 1] = vi + widthDivisions + 1;
                triangles[ti + 5] = vi + widthDivisions + 2;
                mesh.triangles    = triangles;
            }
        }
        mesh.RecalculateNormals();


        //Mesh collider needs  to be added after creating mesh or else
        //    collider will be in the shape of the initial plane
        gameObject.AddComponent <MeshCollider>();
        gameObject.GetComponent <MeshCollider>().sharedMesh = mesh;
    }
Exemple #2
0
    public void GenerateMesh()
    {
        GetComponent <MeshFilter>().mesh = mesh = new Mesh();

        vertices = new Vector3[(arcDivisions + 1) * (widthDivisions + 1)];
        uv       = new Vector2[vertices.Length];

        float    arcStep    = angle / arcDivisions;
        float    widthStep  = width / widthDivisions;
        ArcPoint nextVertex = new ArcPoint(0, 0);
        Vector2  nextUV     = Vector2.zero;

        if (!flipped)
        {
            for (int t = 0; t <= arcDivisions; t++)
            {
                for (int w = 0; w <= widthDivisions; w++)
                {
                    nextVertex.Set(t * arcStep + startAngle, (w * widthStep - width / 2) + radius);
                    vertices[t * (widthDivisions + 1) + w] = ArcToLocal(nextVertex);

                    nextUV.Set((float)w / widthDivisions / textureScale, (float)t / arcDivisions * totalDistance / textureScale + uvOffset);
                    uv[t * (widthDivisions + 1) + w] = nextUV;
                }
            }
        }
        else
        {
            for (int t = 0; t <= arcDivisions; t++)
            {
                for (int w = 0; w <= widthDivisions; w++)
                {
                    nextVertex.Set(t * arcStep + startAngle, (w * -widthStep + width / 2) + radius);
                    vertices[t * (widthDivisions + 1) + w] = ArcToLocal(nextVertex);

                    nextUV.Set((float)w / widthDivisions / textureScale, (float)t / arcDivisions * totalDistance / textureScale + uvOffset);
                    uv[t * (widthDivisions + 1) + w] = nextUV;
                }
            }
        }

        mesh.vertices = vertices;
        mesh.uv       = uv;

        triangles = new int[arcDivisions * widthDivisions * 6];

        for (int ti = 0, vi = 0, t = 0; t < arcDivisions; t++, vi++)
        {
            for (int w = 0; w < widthDivisions; w++, ti += 6, vi++)
            {
                triangles[ti]     = vi;
                triangles[ti + 3] = triangles[ti + 2] = vi + 1;
                triangles[ti + 4] = triangles[ti + 1] = vi + widthDivisions + 1;
                triangles[ti + 5] = vi + widthDivisions + 2;

                mesh.triangles = triangles;
            }
        }

        mesh.RecalculateNormals();
    }