Is it possible to "chunk" blocks in Unity like Minecraft does?

Sorry in advance if this is in the completely wrong place.

So from what I understand, Minecraft takes a 16x16x256 area of blocks and then mashes it all together into one object that uses far fewer resources than thousands of individual blocks would. I’m trying to accomplish the same in Unity, but it seems that I’m at a halt, as the individuality for each physical representation of a block is lost when it gets meshed together with a bunch of other blocks. What I mean by this is that a grass block has three textures: top, side, and bottom; so how do I give it three different textures when the entire chunk that it is in only has one MeshRenderer? And what about all the other blocks like stone, and sand, and obsidian and ores among other things that can be in that chunk? This would work fine if the entire chunk was only composed of dirt or only stone or something, but gets way more complicated when I could have several hundred different blocks needing several hundred different textures in a single chunk. Any way around this?

You got huge misconception how minecraft works.
Minecraft don’t put individaul blocks in single mesh. Still keep each block mesh separately.
Regarding textures again, not many texture is used, but atlas. Or certain number of atlases, for group of blocks.
This reduces amount of needed textures and solves part of your concerns.

I suggest that you get yourself more familiar with minecraft design. And with voxel term in general. Also, check out, what is texture atlas.

Did you try use mods on minecraft? Or at least texture pack? Try it if didn’t. And look inside, to get at least basics idea. Then also, you can watch youtube videos, where people made minecraft basics mechanics in Unity. I know there is few relevant out there. And keep looking for relevant information. Almost decade of existence, don’t leave the internet, without range of literature and resources.

1 Like

You need to learn how textures and materials and mesh renderers work. Otherwise you’ll never understand anything people tell you. You can learn it all by doing game asset creation 101 tutorials with blender.

You are trying to understand something complicated, but you don’t know the ABC’s yet. You’ve got to get started at square one.

1 Like

Check out the following thread.

2 Likes

I did, I’ve read almost two full pages of it, that’s where I got the idea for chunking a large amount of blocks in a single mesh in the first place.

I’ve figured out how to make a triangle from scratch at runtime in Unity, as well as a square (made with two triangles) and a cube (made with six squares), I think that’s the most complicated it gets, besides deciding how many sides of a block to render based on adjacent blocks, which I’ve already done.

Ugh, I already made a reply but something went wrong with it so I deleted it. Anyways, I was told that’s how chunking worked and it seems like it would be really efficient to have just one mesh and one renderer for an entire chunk rather than thousands of meshes and renderers. I’m doing it the second way right now, and I’m not able to have more than 10,000 blocks onscreen at once without my game lagging.

Each chunk has its own mesh and renderer.

By using a custom material with a custom shader that you can feed all the texture atlases into. Just as an example check out the following image that I hastily threw together using Unity’s Shader Graph, a blank material, and a cube. I can feed it atlases to use for rendering.

Mesh don’t need it for chunk. Unless you are not doing classic voxel.

I managed to see your initial post with quotation but could open references. Anyway, you don’t need to have 10k blocks active at given time. You pointed out already at few tricks.

I suggest you place 10k blocks and experience it by your self. make sure, they are not rigid body. Voxel can use lots of tricks, to optimize rendering. You don’t need render every block exists.

You said about single mesh in chunk, instead of blocks. How you imagine having caves, canyons, mountains and trees in same chunk? Try make a mesh of the landscape, which is changing dynamically.

I would’t recommend you at this stage looking into ECS, but ECS is one of efficient ways, to render many objects like voxels, as an example.

I don’t understand how it is more efficient to tell the engine that it needs to go dive into an image to find certain other images rather than just give it the specific image in the first place. So that has to do with having multiple textures in one mesh?

You’re not having the engine do it. You’re having the graphics card do it. A card that has anywhere from hundreds to thousands of individual high performance cores made to do literally just that. A shader is literally just a tiny program running on the GPU.

@Antypodish As @Ryiah just said, I’m pretty sure that each chunk has its own mesh, because of how expensive individual meshes, renderers, and colliders are rather than just one per a chunk. Also, I’d never even think of rendering every single block, Unity says that I have 278k blocks internally stored, which is only a few chunks worth.

You will be getting into hardware design, how it operates. Calling to memory and processing cost GPU/CPU cycles, like fetching textures, or processing vertices etc. Interestingly, is cheaper to call big texture, rather than multiple smaller one. Then you just need xy offset position to get right texture. Which is deadly cheap. Atlases.

@Ryiah Yeah, regardless of what software or hardware is doing the work, how hard would it be to make a shader to accomplish my goal? (having a chunk that can hold hundreds of different materials and apply them to each block)

@Antypodish I don’t really care how much performance it costs fetching the material, as that is nothing compared to displaying the material among other things, I’m more of just mad that Unity doesn’t have better support for meshes with multiple different materials.

Or is it? Do you have good reference to backup? There is good reason behind that people try reduce number of draw calls.

You don’t need make explicit shaders to render each textures. Unity provides already what you need. Question is, how much hundereds of draw calls will affect your game. Don’t be surprised, when you soon hit bottle neck, because so many textures get loaded. Just saying. Depends on hardware of course. Desktop are more likely to handle than Mobile.

If you explain me (just conceptually), how you process effectively complex shapes, into single mesh, then I can agree. And what makes you so sure? I would like see references if you don’t mind, so I can learn too. As I suggested, get familiar on voxel case study. And what are implication on generating single mesh, from 6464256 blocks for example, randomly placed and some disjoint. Providing you want have dynamic environment, where you can destroy and place blocks.

Unity’s experimental graphics pipelines have a node-based shader editor. I have very limited experience with it but it’s a lot easier than the alternative of having to write shader code. Below is a very basic shader I built in a few minutes of fiddling.

3609078--293321--TextureShader.png

At the same time it’s not the only aspect of rendering you need to pay attention to. Some people go overboard trying to reduce them when optimizing a different aspect may have made a larger difference in performance.

@Antypodish The chunk only needs to be updated when it is altered, which happens relatively rarely, compared to the constant stress of having a ton of components attached to every single visible block. I think the link below might explain some stuff about it, and if you read any of the thread that BIGTIMEMASTER posted, there is pages and pages where they talk about meshing blocks together as chunks.

1 Like

@Ryiah I think the problem is that this sort of thing is relatively undocumented, I couldn’t find one tutorial that actually showed how to efficiently make a Minecraft-clone down to the detail of creating a custom shader for rendering the blocks.

EDIT: I looked into the Unity sprite packer, but it said it was disabled and legacy or something. Does Unity no longer support it?

sounds like a great business opportunity for a problem solver like yourself.

Ryiah posted that link btw, not me.