sahil-1035 / minecraft-clone

A small Minecraft clone written using C++ and OpenGL
MIT License
2 stars 1 forks source link

Generate texture atlas at runtime #1

Open ajh123 opened 1 month ago

ajh123 commented 1 month ago

When a game like Minecraft have many blocks it becomes impossible to keep track of the images if they are all inside one. That's why Minecraft stores each texture as a single image (and some extra JSON files).

One way to generate the texture atlas is to create a Frame buffer object, iterate all the textures, render them insider the frame buffer, then use the frame buffer as if it was the texture atlas.

In Minecraft each block has an (or multiple) image file and a JSON file. For example, the Oak Log has two images (one at assets/minecraft/textures/blocks/oak_log.png and assets/minecraft/textures/blocks/oak_log_top.png). Then there is a JSON file at assets/minecraft/models/blocks/oak_log.json which looks like:

{
  "parent": "minecraft:block/cube_column",
  "textures": {
    "end": "minecraft:block/oak_log_top",
    "side": "minecraft:block/oak_log"
  }
}

A simpler JSON file (like the one for stone) looks like:

{
  "parent": "minecraft:block/cube_all",
  "textures": {
    "all": "minecraft:block/stone"
  }
}
ajh123 commented 1 month ago

In modern Minecraft version (like 1.20.6) there is a debug key to "dump dynamic textures"

For example this is the "dynamic texture" for blocks (and items apparently):

minecraft_textures_atlas_blocks png_0

That's 588 KB!