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();
}
// 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());
Debug.Log(voxels);
//}
}
}
}
video: https://www.youtube.com/watch?v=M3iI2l0ltbE
His code can be found here: https://github.com/SebLague/Marching-Cubes
↧