- Home /
How to destroy terrain using Sebastian Lague's Marching Cubes implementation
I have created a voxel engine in the past but have never been able to get marching cubes quite right, and Sebastian Lague recently created his own using marching cubes. The problem is that he is useing compute shaders in order to improve performance and I am not sure how to access the "Array" to edit it. The goal is to be able to remove a single cube/voxel like in minecraft or 7 days to die (more like 7 days to die because I want it smoothened). I believe that the voxels are being set here in the NoiseDensity.compute
int index = indexFromCoord(id.x,id.y,id.z);
points[index] = float4(pos, finalVal); //-----------------------> Value of Point is set!!!
The cubes are marched in the MarchingCubes.compute and I believe that the values of the voxels are being stored in the pointsBuffer compute buffer so as a test I attempted to extract these values to an aray where they could later be edited and then set as the new buffer. p.s. i don't know what a compute buffer is / does, but I believe it is similar to an array. This is my attempt to extract the buffer, in order to do this I made it public in the MeshGenerator script. When I execute the mine function it logs a System.Single[]. I tried making the float array 2D but that caused an error saying that their is not enough memory.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dig : MonoBehaviour
{
private MeshGenerator meshGen;
public float chunkSize = 10f;
public float range = 3f;
// Start is called before the first frame update
void Start()
{
meshGen = GameObject.FindGameObjectWithTag("MeshGenerator").GetComponent<MeshGenerator>();
}
// Update is called once per frame
void Update()
{
}
public void Mine()
{
Transform myChunk;
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
//if (hit.transform.tag == "Chunk")
//{
myChunk = hit.transform;
float[] voxels = new float[meshGen.pointsBuffer.count];
meshGen.pointsBuffer.GetData(voxels);
//meshGen.UpdateChunkMesh(myChunk.GetComponent<Chunk>());
Debug.Log(voxels);
//}
}
}
}
video: https://www.youtube.com/watch?v=M3iI2l0ltbE
His code can be found here: https://github.com/SebLague/Marching-Cubes
Hi there, please post any updates, I've been looking for a way to pass a separate noise generator to his code, and finding it tough, was looking for the classic: for (int x = 0; x < Chunk.SIZE; x++) { for (int y = 0; y < Chunk.SIZE; y++) { for (int z = 0; z < Chunk.SIZE; z++) { ChunkFillUpdate(chunk, voxelData[index++] = new Voxel(x, y, z, density))); } } } } but couldn't get it anywhere, a custom Density Generator is needed perhaps? PS not sure if there's code formatting in comments. Sorry
Answer by faiblis · Oct 17, 2019 at 03:28 AM
Hi, you might try a Vector4[] array instead of float, since the shader is setting points as float4(pos, final value). I was able to extract the positions this way.
Hope that helps.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How can I smoothen my cubic mesh using marching cubes? 0 Answers
Using noise to generate Cube World like terrain 0 Answers
Where should I start on creating a procedural generated terrain that can be deformed? 0 Answers
How to make a voxel terrain generate all around the start point 1 Answer