Troubles with SurfaceTool and textures

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

Hello everyone what I’m trying to do is some procedurally generate a plane and place a texture on it. I have the plane generating and I can put a material on it, I just for the life of me cant figure out how to put a texture on the material so that it shows the texture. I guess I should also put this here… Once I get this working, my plane will have tiles with each tile being able to have a separate texture. Are my uvs setup for that and how would I go about doing that?

I am using C# btw. But gdscript answers are welcome since its fairly easy to convert between them. Thanks :slight_smile:

using Godot;
using System;
using System.Collections.Generic;

public class WorldGeneration : MeshInstance
{
public Texture texture = (Texture) ResourceLoader.Load(“res://Materials/prototype_512x512_blue2.png”);

private float BlockSize = 5f;   

private int ChunkSizeX = 2;
private int ChunkSizeY = 2;

private List<Chunk> SpawnedChunks = new List<Chunk>();

public override void _Ready() {
    
    SpatialMaterial material = new SpatialMaterial();

    material.AlbedoTexture = texture;

    Chunk chunk = CreateChunk(0);
    this.Mesh = chunk.arrayMesh;
    this.MaterialOverride = material;
}

private Chunk CreateChunk(int position) {
    Chunk chunk = new Chunk();
    chunk.ChunkPosition = position;
    chunk.ChunkData = new int[ChunkSizeX, ChunkSizeY];
    
    chunk = CreateChunkData(chunk);
    chunk = CreateVertData(chunk);

    return chunk;
}

private Chunk CreateChunkData(Chunk chunk) {
    for(int y = 0; y < ChunkSizeY; y++) {
        for(int x = 0; x < ChunkSizeX; x++) {
            if(y < 16) {
                chunk.ChunkData[x,y] = 1;
            }
        }
    }

    return chunk;
}

private Chunk CreateVertData(Chunk chunk) {
    SurfaceTool surfaceTool = new SurfaceTool();
    surfaceTool.Begin(Mesh.PrimitiveType.Triangles);

    surfaceTool.AddNormal(new Vector3(0,0,1));

    for(int y = 0; y < ChunkSizeY; y++) {
        for(int x = 0; x < ChunkSizeX; x++) {
            if(chunk.ChunkData[x,y] == 1) {
               
                surfaceTool.AddVertex(new Vector3(-BlockSize * x, -BlockSize * y, 0));
                surfaceTool.AddVertex(new Vector3(BlockSize * x, BlockSize * y, 0));
                surfaceTool.AddVertex(new Vector3(BlockSize * x, -BlockSize * y, 0));

                surfaceTool.AddVertex(new Vector3(-BlockSize * x, -BlockSize * y, 0));
                surfaceTool.AddVertex(new Vector3(-BlockSize * x, BlockSize * y, 0));
                surfaceTool.AddVertex(new Vector3(BlockSize * x, BlockSize * y, 0));
            }
        }
    }

    for(int y = 0; y < ChunkSizeY; y++) {
        for(int x = 0; x < ChunkSizeX; x++) {
            surfaceTool.AddUv(new Vector2((float)x/ChunkSizeX, (float) y/ChunkSizeY));
        }
    }

    var mesh = surfaceTool.Commit();
    chunk.arrayMesh = mesh;
    return chunk;
}

}