Categories
Game Creation Mastering Development

Procedural Mesh UV Mapping

I wrote a script that Creates a mesh procedurally

        mr = gameObject.AddComponent<MeshRenderer>();
        mf = gameObject.AddComponent<MeshFilter>();
        //
        mf.mesh = new Mesh
            {
                vertices = GetVertices(),
                triangles = GetTriangles()
                uvs = UpdateUVMap();
            };

        //assign material + shader.
        mr.sharedMaterial = new Material( Shader.Find("Shader Graphs/surface") );

(the mesh is a sphere for this example)
i created my Shader in shader-graph (any shader, has same result?) when played, all seems good except that the object is black. whats happening?

//basic uv map example from docs

    public Vector2[] UpdateUVMap()
    {
        Vector2[] uv = new Vector2[this.vertices.Count];

        for (int i = 0; i < uv.Length; i++)
        {
            uv[i] = new Vector2(vertices[i].x, vertices[i].z);
        }

        return uvs.ToArray();

        //uvs.Clear();
        //uvs.AddRange(uv);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *