guillaumechereau / goxel

Goxel: Free and Open Source 3D Voxel Editor
GNU General Public License v3.0
2.83k stars 226 forks source link

feat request / question : exporting optimized meshes with no redundant vertices (possible workaround via Blender) #387

Open sleeptightAnsiC opened 3 months ago

sleeptightAnsiC commented 3 months ago

Hey yet again,

Right away, I'm just gonna say this is most likely a dupe of https://github.com/guillaumechereau/goxel/issues/315 I opened a new ticket as the other one is pretty lengthy already and mine will be too.

I'm looking for a way to export meshes that are "kinda production-ready", with as little amount of vertices as possible and with colors included. As I understand the problem with colors is well known (e.g. https://github.com/guillaumechereau/goxel/issues/203) and solvable depending on the format of exported file. The problem with vertices is a bit bigger though.

There are two issues with vertices (mostly tested with .gltf and .obj):

  1. Duplicate vertices at the edge of each voxel - because of this voxels aren't connected
  2. Despite that voxels create a simple 2D plane in most places, there are still redundant vertices on them (mesh is simply not optimized)

https://github.com/user-attachments/assets/ae6cc18b-60ca-40b5-b346-40a88f998bf8

So, I solved both those issues via Blender. Pasting instructions here in case someone has a similar problem:

  1. Edit Mode > select whole mesh [A] > RMB at mesh > Merge Vertices > By Distance - https://docs.blender.org/manual/en/latest/modeling/meshes/editing/mesh/merge.html
  2. Object Mode > Modifiers > Add Modifier > Generate > Decimate > Planar > Apply - https://docs.blender.org/manual/en/latest/modeling/modifiers/generate/decimate.html

https://github.com/user-attachments/assets/0a27d877-2e69-40cf-a152-88741fa6a771

However, my solution isn't perfect. I already noticed that depending on which step is applied as the first one, this can either mess the colors (as seen on video above) or leave the vertices inside of geometry. I'm still looking for something better and will try to automate it at some point.

Is there a way to export meshes without those problems? Is there any better workaround to the one I use?

Thanks

guillaumechereau commented 3 months ago

Thanks for the detailed explanation.

Currently goxel uses meshoptimzer [1] to simplify the meshes before glTF export. Maybe there are better alternatives? Or the options I used are not optimal. I am not too sure about that.

[1] https://github.com/zeux/meshoptimizer

sleeptightAnsiC commented 3 months ago

NOTEs

the code making it happen is here https://github.com/guillaumechereau/goxel/blob/292588ca2ed8cff6d179762419eca6e5a7bad6ce/src/volume_to_vertices.c#L370-L422

it uses this struct https://github.com/guillaumechereau/goxel/blob/292588ca2ed8cff6d179762419eca6e5a7bad6ce/src/volume_utils.h#L74-L92

and is being called from there https://github.com/guillaumechereau/goxel/blob/292588ca2ed8cff6d179762419eca6e5a7bad6ce/src/volume_to_vertices.c#L422-L463

@guillaumechereau a question: Is meshoptimizer being used or will be used in any other file than volume_to_vertices.c ?

I have a feeling it might be easier to just implement the algorithm from scratch, instead of messing with the general purpose library. I may give it a try.

420noscope-exe commented 3 months ago

I'm having a problem with this too. Im trying to bring this rather large model into unity.

https://github.com/user-attachments/assets/b86c86a6-f538-46cd-b808-1cd537615c4c

It has way to many verticies.

I tried goxel's simplifiy gltf feature to 1.0. It doesn't simplifiy as much as blender's decimate option, and it turns smaller voxel features into triangles. I think this is the issue from #315 that wasn't reproducing.

Goxel gltf simplify: image image

Blender Decimate (No simplifiy on export): image image image

However, my solution isn't perfect. I already noticed that depending on which step is applied as the first one, this can either mess the colors (as seen on video above) or leave the vertices inside of geometry.

Yes, Blender seems to "decimate" the colors too lol. Jokes aside, thank you these videos are helpful, it gives me a starting point

420noscope-exe commented 3 months ago

So, I solved both those issues via Blender. Pasting instructions here in case someone has a similar problem:

  1. Edit Mode > select whole mesh [A] > RMB at mesh > Merge Vertices > By Distance - https://docs.blender.org/manual/en/latest/modeling/meshes/editing/mesh/merge.html
  2. Object Mode > Modifiers > Add Modifier > Generate > Decimate > Planar > Apply - https://docs.blender.org/manual/en/latest/modeling/modifiers/generate/decimate.html

I've figured it out! You were right on track! You actually need to select Delimit UVs!

  1. Edit Mode > select whole mesh [A] > RMB at mesh > Merge Vertices > By Distance - https://docs.blender.org/manual/en/latest/modeling/meshes/editing/mesh/merge.html
  2. Object Mode > Modifiers > Add Modifier > Generate > Decimate > Planar > Delimit UVs > Apply - https://docs.blender.org/manual/en/latest/modeling/modifiers/generate/decimate.html

https://github.com/user-attachments/assets/41c74dd5-c4f2-4194-a776-1df3dfe10eee

I don't know if I can be of any help in regards to re writing the algo. But it seems that it needs to focus on grouping voxels of like colors on surfaces and taking those into account before simplifying vertices. Maybe some code from the magic wand tool with the color option can be re-used?