Marching-Cubes icon indicating copy to clipboard operation
Marching-Cubes copied to clipboard

Realtime Editing

Open WireWolf opened this issue 5 years ago • 12 comments

Hi I was wondering if you could shed some light on how to edit single voxels in realtime similar to 7 days to die or minecraft, I have created a voxel engine in the past but could not get marching cubes working and I have no experience with compute shaders. I cannot figure out how to edit the "voxel array" and update the chunk. I believe that the pointsBuffer holds the information I need however I don't know where it is safe to edit it and also what type of "Array" it is. I have tried

float[] voxels = new float[meshGen.pointsBuffer.count]; meshGen.pointsBuffer.GetData(voxels);

however it returns system.single[] and i do not know how to fix this. I tried making a 2d array but i threw a memory exception error. Any help would be greatly appreciated thank you for your time.

WireWolf avatar Jun 20 '19 20:06 WireWolf

If you want to generate a voxel shape in the CPU, you could use

` voxelCPU

// densityGenerator.Generate (pointsBuffer, numPointsPerAxis, boundsSize, worldBounds, centre, offset, pointSpacing); Vector4[] data = new Vector4[30 * 30 * 30]; //Vector4[] readData = new Vector4[30 * 30 * 30]; //pointsBuffer.GetData(readData); for (int y = 0; y < 30; y++) { for (int x = 0; x < 30; x++) { for (int z = 0; z < 30; z++) { int i = x * 30 * 30 + z * 30 + y; data[i] = new Vector4(x, y, z, Vector3.Distance(Vector3.zero, new Vector3(x - 15, y - 15, z - 15)) > 10 ? -15 : 15); //Vector4 _data = readData[i]; //print(readData[i]); } } }

pointsBuffer.SetData(data);

`

in MeshGenerator (fixed size = true, bounds size = 30, num points per axis = 30) at line ~200 in UpdateChunkMesh to generate a voxel ball, To be more exact the arraydata is a Vector4 with 4 floats where xyz is the pos and w is the density, I use 15 and -15 as on/off but 1 and -1 also works

Code taken from shader (.compute): int index = indexFromCoord(id.x,id.y,id.z); // same as z3030 + y*30 + x points[index] = float4(pos, density);

VincentLagerros avatar Jul 21 '19 10:07 VincentLagerros

Thanks I will try that when I get home

WireWolf avatar Jul 21 '19 18:07 WireWolf

Have edited some code and made https://youtu.be/403JCvVUn4A via CPU

VincentLagerros avatar Jul 22 '19 22:07 VincentLagerros

I'm interested also in making the terrain modified at runtime, do we have any information how we can do this? I have tried so far to set the density manually to some points and recompute the mesh but it doesnt affect anything, I'm close to dead end so any information is much appreciated!

MagokSama avatar Sep 20 '19 10:09 MagokSama

Made this in ~2h, the code is a bit messy and size is limited to 30, but works: https://youtu.be/S_oqkZgjbms will post project on github soon

VincentLagerros avatar Sep 20 '19 14:09 VincentLagerros

https://github.com/VincentLagerros/RealTimeTerrainEdit

VincentLagerros avatar Sep 20 '19 15:09 VincentLagerros

I made the most rookie mistake of all when trying to make my own custom density generator, it drove me nuts.

Make sure MeshGenerator is using the MarchingCubes shader, not whatever density shader you're using. My brain went full mush trying to figure out what was wrong.

ChiefBreakeverything avatar Oct 30 '19 08:10 ChiefBreakeverything

Did a inf terrain realtime edit, https://youtu.be/_1c_wpa-d50

VincentLagerros avatar Dec 02 '19 10:12 VincentLagerros

Did a inf terrain realtime edit, https://youtu.be/_1c_wpa-d50

Amazing, do you have this in repository? I'd like to take a peek in your code if thats okay with you.

MagokSama avatar Dec 02 '19 12:12 MagokSama

Did a inf terrain realtime edit, https://youtu.be/_1c_wpa-d50

Amazing, do you have this in repository? I'd like to take a peek in your code if thats okay with you.

Updated it a bit: https://youtu.be/sj0n86d7Hbg

Drive Folder: https://drive.google.com/drive/folders/1t28wldbP_nkK5qRB9NOzSZMk7RtrONLK?usp=sharing

Build: https://drive.google.com/file/d/1yV5CajOLG-xGRrqDpFYoaDhaRSUqf5RL/view?usp=sharing

Source: https://drive.google.com/file/d/1DsDLje8piv0SlN87bSzAmThhJaU-pSMy/view?usp=sharing

VincentLagerros avatar Dec 03 '19 16:12 VincentLagerros

Nice work Vincent, I'm looking to make basically the same thing when I get a spare moment to breath. Your solution is looking awesome!

ChiefBreakeverything avatar Dec 09 '19 06:12 ChiefBreakeverything

@VincentLagerros I am desperately seeking your help!

Can you help me out? I have added you on facebook in the hope we can have a chat about what I see?

The problem seems to stem whenever I click on a border chunk in a 3x3x3 set of chunks.

This bit of code appears to cause the issue :

 if (addChunks.Count > 0)
                {
                    for (int i = 0; i < addChunks.Count; i++)
                    {
                        GameObject g;
                        if ((g = GameObject.Find(mainHolder.GetNameFromCoord(addChunks[i]))) == null)
                        {
                            mainHolder.AddChuck(addChunks[i]);
                        }
                        else
                        {
                            Debug.Log("HOT RELOAD");
                            mainHolder.HotReloadChunk(addChunks[i], g.GetComponent<MeshChunk>());
                        }
                    }

                }

We check if the Chunk == null, and if it is, we add it to the main holder, which then causes a nullreferenceexception?

renegadeandy avatar Jul 01 '20 21:07 renegadeandy