Zylann / godot_voxel

Voxel module for Godot Engine
MIT License
2.61k stars 245 forks source link

Editable voxels #6

Closed toger5 closed 7 years ago

toger5 commented 7 years ago

random stuff:

hey, just found time to look at your code. (I don't know if you remember but I also once did a voxel approach in gdscript and than was blown away by your results when you finished (or started... ;) ) your voxel module.) I still really like it and probably will do a project with it one day ;)

actual topic:

I saw that u use the surface tool for the actual mesh generation. how would editable voxels than be implemented? do you have to recreate the whole chunk? or can u find vertices inside a surface tool and change them? (delete replace...) How does godot perform when using a huge amount of surface tools (for each voxel) I saw some discussions with reduz where he said that this should perform pretty well. I didn't try that for now. but I know that in other engines you get a huge decrease in performance when splitting to multiple meshes... (although keeping the same vertex count)

Just am super curious if you already know how to implement it

Zylann commented 7 years ago

Voxels should be editable but currently the module doesn't update the mesh automatically, you have to do it manually. It's a bit more tedious than it sounds because meshes are split in cubic chunks, and editing a voxel lying at a boundary requires neighbors to be updated as well. But I plan to support it indeed (will work on it when Godot 3 will be more stable).

The idea of editable voxels is that you don't modify the mesh, but the voxel data. Them the mesh has to update according to that, and here it means updating between 1 or 8 chunks depending on the location. That process should be fast enough so framerate doesn't drop. In a game context, I plan to add a simple voxel raycaster that doesn't need physics to work. It just needs an origin and direction, then gives which voxel you are pointing at. https://github.com/Zylann/zCraft/blob/master/src/engine/core/raycast.cpp#L14

Individual voxel meshes give awful performance, even with same material so the main purpose of the mesher that uses SurfaceTool is to batch cubes into one mesh by also discarding occluded faces (and adding free AO as an option :D). Then I use chunks so that I can make it of any size while taking advantage of frustum culling. It's currently not the fastest implementation, because SurfaceTool constrains me to repeat vertices I don't need, while I could output indexed vertices straight away. But before changing that I need to see how the Mesh class will change in 3.0 :p

Reduz told me a bit about instancing so I should test this as well to see if some parts of voxel meshes can use it instead of batching.

toger5 commented 7 years ago

Thank you for your detailed reply!! how can a block affect 8 chunks or do you mean neighbour blocks? So you regenerate a whole chunk for removing/editing one block? or do you keep the mesh and than search for the verts based on the voxel coordinates which got added/removed (how would that work? do you have a lookup table with pointers to the accruing vests? so you find the right vertices really fast and can modify them?). and than just modify the small part of the mesh which got affected?

I guess you already have some kind of voxel ray caster working? Because I also have made one for my gdScript implementation I could translate to c++. No idea if it's good performance I usually don't make any reasearch and just try what I can come up with so it could be a bad approach...

Zylann commented 7 years ago

Modifying a voxel in a block can affect 8 blocks in case you change a corner whose sides are shared with other blocks, because one single mesh is generated for every block. I don't attempt to modify only the vertices belonging to that voxel because it would consume a lot of memory for little gain.

The raycaster I have comes from one of my old voxel projects, itself implemented by reusing an existing one. I didn't tested it but I assume it still works :p

toger5 commented 7 years ago

oh you have only one megs for each voxel? this is what was confusing me I thought it would be one mesh for each chunk. like 16*16 blocks this is how I had implemented it. OKay this makes sense than! shouldn't even be that hard than?

Zylann commented 7 years ago

We have a terminology misunderstanding:

So no, I'm not creating a mesh for each voxel, I create a mesh for every block^^

toger5 commented 7 years ago

yea we had a terminology problem ;) (I'm sorry... was asking stupid things than...) But at least everything makes sense to me now.

Zylann commented 7 years ago

The last updates make it easier to edit the terrain voxel by voxel, see the demo ;)

toger5 commented 7 years ago

Nice! I didnt checked thw repo you did a huge amount of work !! :)

Corruptinator commented 7 years ago

Sorry about that, I didn't mean to reference that. What happened is that I was referring to a Youtube video that had the pound number and Six in the "Smooth Voxel" comments, instead I'd inadvertedly caused the reference to occur. It was an accident.

Zylann commented 7 years ago

Voxels are editable as shown in the voxel game demo project. It will be made a bit easier over time, as I also plan to make this module usable in editor, not just with script.