Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Block shapes #2

Open
machinetown44 opened this issue Jul 31, 2020 · 6 comments
Open

Block shapes #2

machinetown44 opened this issue Jul 31, 2020 · 6 comments

Comments

@machinetown44
Copy link
Contributor

@Hertzole hey, i really like this framework and want to use it for a project. I want to make use of texture arrays and possibly add texture blending shaders to it, but I need to get a better handle on how the block building process is done. Would be really great if you could give me some directions. Shoot me an email at machinetown44@gmail.com if you don't mind some direct communication.

@Hertzole
Copy link
Owner

Hi there!
I'm glad you like it! :)
I'll do my best to explain how it works. I'll also do it publicly here as it may benefit someone else too. But if you have any questions you'd like answered privately, you can send me an email at contact@hertzole.se and I'll get back to you!

So like the readme says, the only shape available is the block/cube shape and they are hardcoded. The blocks are built within a job that you can find in BuildChunkJob. It works in a relatively simple fashion. I'll go over how it builds the north face, as it is the first in the process, and then I think you can figure out how all the other faces are built.

It starts with looping over the chunk size for each axis, x, y, and z. For each loop on the Z-axis, as it is the last, it increases an index by one since you can only a one-dimensional array in jobs. It uses said index to find a block in the "block map" which is the map of blocks within a chunk.
If the block ID then is 0 it continues as it's an air block and it doesn't need a mesh.
Then it starts to actually make the faces.
It first checks if the block isn't on the edge and checks if the block in the Z+ direction is transparent [1]. If that doesn't apply, it checks if it is on the edge and then if the block in the neighboring chunk is transparent. If any of those passes, it starts actually making the vertices and such. I'm not going to explain how the vertices, triangles, or normals work as its just simple mesh math.
Now comes the interesting part: the textures! Textures almost work on an index system, just a 2D index. A UV is added as a Vector4 where X and Y are just a plain UV like any vertex but the Z and W are indexes to where on an atlas it should be located. This works together with the shader to return a texture at the given coordinates. [2]
Then alongside the triangles (indicies) and normals it also adds vertex colors. You need to support vertex colors in your shader for these to show up.

And that is basically how it creates the blocks. I hope this helped and if you have any more questions, don't hesitate to ask them either here or by email! :)

[1] The IsTransparent method checks for more than just transparency. First of all, it checks if it's an air block, which is always transparent. Then it checks if the next block is transparent but it shouldn't connect to itself (Think like glass in Minecraft. You don't see the glass faces from other glass blocks). Then it checks if it is transparent and it should connect to itself. Then it needs to make sure the next block has the same ID as the current block. It's useful to see if it should connect to itself or not. Blocks like leaves in Minecraft are transparent but don't connect to each other, while glass is also transparent and it connects to each other. (And with connecting I mean hiding faces you normally won't see)

[2] The texture coordinates are calculated in the GenerateAtlas method in TextureProvider. It takes all the unique textures, packs them all into an atlas and then maps each texture to a coordinate on the atlas. Since the Unity method PackTextures returns a rect, we can use this to figure out where each texture is on the map.
Each block then get it's texture ID assigned in the editor from the block collection. It collects all the textures and makes sure it's unique and assigns an ID to each block texture. There should be a more foolproof way to do that but it's how it is now. If something is wrong with your textures, just edit something in the inspector so it calls the OnValidate function.

@machinetown44
Copy link
Contributor Author

Thanks for the quick response!
Didn't notice unity already having a built in method for generating atlases.

I actually already started working on a method to author custom mesh shapes, just need to add some management system for it. Could probably make a push after I'm done if you're interested. For the shader though, it's going to be a bit more difficult since texture arrays require a custom editor, but the possibility of having textures tiled over multiple blocks seems too nice to pass on. I'll try to figure something out.

Anyway, you can leave this open to serve as a Q&A of sorts for any possible future inquirers or just close it. I'll send you an email once I have something to show.

@Hertzole
Copy link
Owner

Any contribution would be awesome!
Regarding the texture atlas stuff: I get where you're coming from and I suspect you want something like greedy meshing. The shader I use actually supports that with a texture atlas already. It's just that I haven't been able to make greedy meshing work properly. The only thing that would need changing is instead of just setting the UV coordinates to 0 and 1 you would input the repeat amount there. If you have the code downloaded you can change the 1 to something like 5 to see how it repeats.

The reason I decided to go with a texture atlas was because it's more backward-compatible than texture arrays. Textures arrays aren't really something new either but you never know. If anything, it's a good backup system if one could implement them side by side. I hope this clears things up a bit!

@Hertzole
Copy link
Owner

Hertzole commented Aug 1, 2020

Hi
I don't know if you got my email as it shows as sent by I got a message back from my email provider it couldn't be sent.

@machinetown44
Copy link
Contributor Author

Hey, was just about to check it. No emails.
Do you use discord maybe?

@Hertzole
Copy link
Owner

Hertzole commented Aug 1, 2020

I don't use Discord much but you can still contact me there if you'd like more direct communication. (Response time may vary) Hertzole#5580

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants