public void work()
 {
     MarchingCubes.SetTarget (0.0f);
     MarchingCubes.SetWindingOrder (0, 1, 2);
     MarchingCubes.SetModeToCubes ();
     proxy = MarchingCubes.MakeMeshProxy(voxels());
 }
    void OnChunkGeometryReady(MeshProxy proxy)
    {
        Mesh mesh = proxy.toMesh ();
        MeshCollider collider = gameObject.AddComponent<MeshCollider> ();
        collider.sharedMesh = mesh;
        MeshFilter meshFilter = gameObject.AddComponent<MeshFilter> ();
        mesh.RecalculateNormals ();
        meshFilter.mesh = mesh;

        MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer> ();
        meshRenderer.material = mat;
    }
Example #3
0
 public MeshProxyDebugView(MeshProxy mesh)
 {
     _proxy = mesh;
 }
    public static MeshProxy MakeMeshProxy(float[,,] voxels)
    {
        List<Vector3> verts = new List<Vector3>();
        List<int> index = new List<int>();

        float[] cube = new float[8];

        for(int x = 0; x < voxels.GetLength(0)-1; x++)
        {
            for(int y = 0; y < voxels.GetLength(1)-1; y++)
            {
                for(int z = 0; z < voxels.GetLength(2)-1; z++)
                {
                    //Get the values in the 8 neighbours which make up a cube
                    FillCube(x,y,z,voxels,cube);
                    //Perform algorithm
                    Mode_Func(new Vector3(x,y,z), cube, verts, index);
                }
            }
        }

        /*Vector3[] normals = new Vector3[verts.Count];
        List<Vector3>[] vertexNormals = new List<Vector3>[normals.Length]; //array of lists, so each element stores a list of normals for that vertex, to be averaged later

        for (int i = 0; i < index.Count; i += 3) {
            Vector3 currNormal = Vector3.Cross(verts[index[i]]- verts[index[i + 1]], verts[index[i]]- verts[index[i + 2]]);
            if(vertexNormals[index[i]] == null) vertexNormals[index[i]] = new List<Vector3>();
            if(vertexNormals[index[i+1]] == null) vertexNormals[index[i+1]] = new List<Vector3>();
            if(vertexNormals[index[i+2]] == null) vertexNormals[index[i+2]] = new List<Vector3>();

            vertexNormals[index[i]].Add(currNormal);
            vertexNormals[index[i+1]].Add(currNormal);
            vertexNormals[index[i+2]].Add(currNormal);
        }

        for (int i = 0; i < vertexNormals.Length; i++) {
            normals[i] = Vector3.zero;

            float numNormals = vertexNormals[i].Count;
            for (int j=0; j < numNormals; j++) {
                normals[i] += vertexNormals[i][j];
            }

            normals[i].Scale(new Vector3(1f/numNormals,1f/numNormals,1f/numNormals));
        }*/

        MeshProxy mesh = new MeshProxy ();
        mesh.vertices = verts.ToArray();
        mesh.triangles = index.ToArray();
        //mesh.normals = normals;

        return mesh;
    }