im working on a voxel based game in unity. the terrain(sphere-->planet) should be out of voxels and at first i built a minecraft like voxel plugin for unity. this means that i had a sphere out of cubes, but as you can imagine, a sphere out of cubes just doesn't look good :P (i made a level array which stored the material at [x,y,z]. --> so when "1" stands for stone, it means that level[x,y,z] = 1; was stone. but then i decided to use the marching cubes algorithm for my voxel planet. so i learned the marching cubes algorithm and i realised that now i must have a density function, and not a level array with material information.
my density function looks like this:
x * x + y * y + z * z - rad - (noise.FractalNoise3D (x, y, z, oct, frec, amp) * scale);
The noise is made by a simplex noise generator script. so far so good. i get a good sphere rendered with a little bit of noise. but when i try to edit the sphere (terrain editing) i run into problems.
i made a function which gets the collision coordinates(x,y,z) of the mouse and the sphere, and changes the density in the level array at these coordinates.:
void dig(){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
if (Physics.Raycast (ray, out hit)) { Debug.Log ("Dig..."); Debug.DrawLine(ray.origin,ray.origin+( ray.direction*hit.distance), Color.green,2); editTerrain(hit); } } void editTerrain(RaycastHit hit){ int xEn = (int) hit.point.x; int yEn = (int) hit.point.y; int zEn = (int) hit.point.z; level [xEn, yEn, zEn] += 1000; mesh = MarchingCubes.CreateMesh (level); mesh.uv = new Vector2[mesh.vertices.Length]; mesh.RecalculateNormals (); GetComponent<MeshFilter> ().mesh = mesh; GetComponent<MeshCollider>().sharedMesh=mesh; }
my problems/questions now:
-is the density function alone enough for my level array? did i understand terrain creation right so far?
-when i try to edit the terrain (add something to the density at levelArray[x,y,z], it just flats the surface, but doesn't dig holes.
-when i want to add materials to my voxel planet (stone, dirt, sand, etc...), can i make another array, which holds the material data at [x,y,z]? are there better possibilities?
-btw...i know i didn't apply chunks to my voxel planet yet...this comes later...
i hope anybody can help me because i don't know what to do.