3D - Making a mesh in code, How do I add a texture to the created mesh.

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

Hello all, I am making a minecraft inspired game but I am currently making the cube/block I have the block done but now I want to add a texture to it, like in minecraft having a grass block but when I try to add a texture it just renders in a single colour, how do I add a texture to my mesh, I think I need to use UVs but I have no clue if I done it correct or not!

here is the code:Legobet88 🀄 Trustworthy Agen Situs Slot Online Bet88 Terpercaya Ter- Gacor Hari Ini 2024

:bust_in_silhouette: Reply From: Kurotsuki

Games like minecraft uses what is called a texture atlas, a texture atlas is a single image file that contains all the textures needed in the game and stitches them together, uv values are mapped as 0 and 1 so in order to use a specific part of the image the uv values for each vertex needs to differ

For example, say your texture atlas contains 4 textures in a 2x2 matrix and you want to use the top right texture for your blocks, the uv values for the top right texture needs to be (0.5, 0.0), (1.0, 0.0), (0.5, 0.5), and (1.0, 0.5) as no matter what size the image is the uv coordinates are mapped into 0 and 1