Hi, I'm new to Godot and have trouble to find some informations. I want to make a Minecraft like graphic with GDScript. I know that I can create a mesh with the SurfaceTool, but I don't know how to place textures on it.

Especially: let's assume that I have a mesh with the size of 10x10, but built only out of two triangles, but I want to put different texture images with the size of 1x1 onto this mesh.

Is this possible at all and if yes, how can I do this from script (without Editor)

I hope you understand what I mean

Regads DotGo

Well, for starters while generating your mesh you should also add it UV's, but before that you will want to create your material then once you have created your mesh with UV's and you are instancing it as a meshinstance you then assign the material to it. Note that while you can create a material from code, you don't have to. The material can be created through the editor beforehand.

The scale of the texture on the mesh will be determined by the UV coordinates.

https://docs.godotengine.org/en/3.1/classes/class_surfacetool.html#class-surfacetool-method-add-uv https://docs.godotengine.org/en/3.1/classes/class_meshinstance.html#class-meshinstance-method-set-surface-material

http://wiki.polycount.com/wiki/Texture_Coordinates

edit: In case you are using ArrayMesh instead of MeshInstance: https://docs.godotengine.org/en/3.1/classes/class_arraymesh.html#class-arraymesh-method-surface-set-material

I noticed right now that I can scale the texture to repeat it multiple times. For the first time I can create materials in the editor, but later I maybe have to create them in a script

Thanks for the links. Will continue reading.

As @Megalomaniak said, you will need the UV and material in order of anything to show up on the mesh. The material tells the Shader drawing the mesh what textures to use, while the UV coordinates of each vertex tells shader what part of the texture should be drawn on each triangle.

If you are trying to generate voxel terrain, I’ve written a two part tutorial series on exactly that. The tutorial was written for Godot 3.0.6 and uses the SurfaceTool, though the project should work with Godot 3.1 without any changes.

The tutorial might provide a helpful starting point on how to generate a terrain mesh with the correct UV coordinates.


The key with working with UV coordinates is you have to remember to convert your positions to UV space. Where the texture is written with pixels, in UV space the entire texture is mapped from zero to one.

For example, if you have a texture that is 256x256 pixels in size and you want to get the center, in pixel space it would be X:128 Y:128, while in UV space it would be X:0.5 Y:0.5.

So long as you are okay with the textures being in a grid, it isn’t too hard to convert the grid coordinates to UV space, thought off the top of my head I don’t remember the algorithm.

@TwistedTwigleg I already found your tutorial. I will go to the code, but I found a problem with the shadows in your tutorial. You can see it in the screenshot with the castle and I can reproduce it on my PC. If I place a single block on the ground it looks like he is floating

And even with this simple the framerate drops to 30 if I maximize the Window

@DotGo said: @TwistedTwigleg I already found your tutorial. I will go to the code, but I found a problem with the shadows in your tutorial. You can see it in the screenshot with the castle and I can reproduce it on my PC. If I place a single block on the ground it looks like he is floating

Yeah, the shadow issue is because the solid squares that make up the terrain are disconnected from each other, which is necessary for using the correct UV positions.

You can mostly fix the issue by setting the Normal Bias setting in the DirectionalLight node to 0.2, then the shadows will be much closer to each individual voxel, removing the 'floating' appearance.

And even with this simple the framerate drops to 30 if I maximize the Window

This is probably due to the SSAO in the WorldEnvironment node. If you select the WorldEnvironment node, you should be able to turn it off and the framerate should be much better. Turning off Glow might help too.


Hopefully this helps!

Even with Normal Bias set to 0 it is not completely removed. Is there any chance to fix this without effecting performance (e.g. using cube primitive)?

Another issue are the black dots appear on the green surface. I assume, it is caused by the same problem (disconnected squares)?

@DotGo said: Even with Normal Bias set to 0 it is not completely removed. Is there any chance to fix this without effecting performance (e.g. using cube primitive)?

I do not know of any solution without playing around with it some more. From a rendering performance point of view, using something like a cube would result in a bunch of hidden faces for the majority of voxels, which could decrease performance, though it might fix the shadow issue).

You might be able to get the shadows looking closer by tweaking the shadow settings the DirectionalLight node, but other than, I cannot think of anything that would fix the issue while still allowing for UV mapped voxel faces.

There is a Godot module for making voxel terrain that uses C++ for improved performance that does not seem to suffer this issue. Maybe look and see how it is done there? You'll need to recompile Godot with the module installed to use the module though.

Another issue are the black dots appear on the green surface. I assume, it is caused by the same problem (disconnected squares)?

Probably. It could be the skybox leaking through the extremely small gap between each voxel, or the shadow settings in the directional light are causing the dots.


Edit: Setting the Bias to 0.1 and the Contact to 0.2 on the DirectionalLight node helps a lot. The shadows look much better and appear to be connected with only slight artifacts at the edges.

@TwistedTwigleg said:

Edit: Setting the Bias to 0.1 and the Contact to 0.2 on the DirectionalLight node helps a lot. The shadows look much better and appear to be connected with only slight artifacts at the edges.

Thanks, it is better, but still not perfect. I will try to analyse the Zylann module, but I guess this will become difficult. My C++ is not the best and I need to learn more about Godot and how it works before I go to this module.

3 years later