Giter Site home page Giter Site logo

Block shapes about hertzvox-2 HOT 6 OPEN

hertzole avatar hertzole commented on July 21, 2024
Block shapes

from hertzvox-2.

Comments (6)

Hertzole avatar Hertzole commented on July 21, 2024

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 [email protected] 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.

from hertzvox-2.

machinetown44 avatar machinetown44 commented on July 21, 2024

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.

from hertzvox-2.

Hertzole avatar Hertzole commented on July 21, 2024

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!

from hertzvox-2.

Hertzole avatar Hertzole commented on July 21, 2024

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.

from hertzvox-2.

machinetown44 avatar machinetown44 commented on July 21, 2024

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

from hertzvox-2.

Hertzole avatar Hertzole commented on July 21, 2024

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

from hertzvox-2.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.