Marching-Cubes
Marching-Cubes copied to clipboard
Realtime Editing
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.
If you want to generate a voxel shape in the CPU, you could use
`
// 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);
Thanks I will try that when I get home
Have edited some code and made https://youtu.be/403JCvVUn4A via CPU
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!
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
https://github.com/VincentLagerros/RealTimeTerrainEdit
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.
Did a inf terrain realtime edit, https://youtu.be/_1c_wpa-d50
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.
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
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!
@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?