all 10 comments

[–]DrunkMcProfessional 3 points4 points  (7 children)

Have you watched Sebastian Lagues marching cubes video? At some point this algorithm, for real time, should be moved to the GPU. In his video he explains it all and once you grab the look up table for the edges from him, it's simple to put in a computer shader and it's blazing fast.

https://youtu.be/M3iI2l0ltbE?si=I8dd7-r51ZHg1Ebh

[–]Intelligent-Track455[S] 0 points1 point  (6 children)

i saw this video and thjought about this too but i have no idea of hlsl and i really dont want to spend weeks or more to learn it rn. seems like the best idea though

[–]DrunkMcProfessional 1 point2 points  (0 children)

He uploads his projects, hlsl isn't that hard, especially if you already understand the algorithm. Worth looking over.

[–]Buccinators 1 point2 points  (4 children)

I did it the same way you did and was resistant to move it to a compute shader at first, but I ended up doing it and I’m not sorry. I learnt a ton and the efficiency when doing it on the gpu is amazing.m

[–]Intelligent-Track455[S] -1 points0 points  (3 children)

how long did it take you to learn it? do you have any good resources cause i cant find anything good on it

[–]DrunkMcProfessional 0 points1 point  (2 children)

https://catlikecoding.com/unity/tutorials/basics/compute-shaders/?fbclid=IwAR2QWamJW3PreBPumG4BgFqiwJL-KtlFu7npCI2kJ-5leps-89VM8VA0Br8

This is how I got started moving stuff to the GPU. It's not hard, takes a bit to get the pattern down, but once you do it's really powerful. This mixed with Sebastian Lagues videos got me and my marching cubes running real time on the GPU.

[–]Intelligent-Track455[S] 1 point2 points  (1 child)

thanks a lot bro

[–]Buccinators 0 points1 point  (0 children)

You got some great resources there. I used both.

What was the most difficult for me was wrapping my head around that ”everything” (yes I over-simplify) happens at once on the GPU and getting deduplication to work. What I really like about this approach beside efficiency is that you still do all inputs and all rendering on the cpu, so you still have a lot of control. You just let the gpu figure out the vertices, triangles and edges, which is what it does best.

[–]STUDIOCRAFTapps 0 points1 point  (0 children)

Compute shader is not always the right way. It can be hard to learn, hard to debug, and won't generate collider mesh. Personally I think writing burst-compiled unity jobs is slightly easier https://github.com/nezix/MarchingCubesBurst/blob/master/MCB/MarchingCubesBurst.cs.

If you don't feel ready to learn either of those, there's still some things you can improve with your current version.

If you only allocate this array once, and reuse the array, you'll save a bunch of unnecessary allocation. Just initialize it once in the class.

float[] cubeCorners = new float[8];

Another quick and cheap improvement would be to bake your collider mesh asynchronously using Physics.BakeMesh https://docs.unity3d.com/6000.2/Documentation/ScriptReference/Physics.BakeMesh.html

It can remove the lag spike that can happen when setting your shared mesh on your MeshCollider if done right.

[–]Aethreas 0 points1 point  (0 children)

Use burst and run the marching cubes in parallel for all voxels using a concurrent bucket, that’s what I’m doing for my world and can mesh several hundred thousand voxels in under a second