0 votes

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 :)

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

public class WorldGeneration : MeshInstance
{
public Texture texture = (Texture) ResourceLoader.Load("res://Materials/prototype512x512blue2.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;
}

}

in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.