[C#] How to I generate a Mesh using the SurfaceTool?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mtaethefarmer

I am having trouble generating a mesh onscreen using the SurfaceTool, would someone take a look at my code and let me know what I am doing wrong?

public override void _Ready()
{
    material.AlbedoColor = new Color(0.8f, 0.0f, 0.0f);
    SurfaceTool tool = new SurfaceTool();

    tool.Begin(Mesh.PrimitiveType.Triangles);
    tool.SetMaterial(material);

    tool.AddUv(new Vector2(0, 0));
    tool.AddVertex(new Vector3(5, 0, 0));

    tool.AddUv(new Vector2(0.5f, 1));
    tool.AddVertex(new Vector3(0, 5, 0));

    tool.AddUv(new Vector2(1, 0));
    tool.AddVertex(new Vector3(5, 5, 0));

    tool.GenerateNormals();
    tool.Index();

    mesh = tool.Commit();

    Instance.SetMesh(mesh);
    AddChild(Instance, true);
}

Where do you create the Material? I just see that you’re assigning the albedo color to some unknown variable “material”.

Also try to turn culling off in the material (for testing) to avoid the tri being invisible because you look at it from the wrong side (normal-wise).

Also make sure that you’ve a light set up (or an environment with ambient lighting) and have the camera set up to look at the right place.

Apart from that the code looks OK.
I assume that “Instance” ist a MeshInstance that is created somewhere.

wombatstampede | 2019-04-25 10:48