예제 #1
0
        public IVoxelModel CalcVoxelCruncher(VoxelData <VoxelMaterial> voxels)
        {
            var map = new VoxelMaterial[voxels.Bound.x, voxels.Bound.y, voxels.Bound.z];

            for (int i = 0; i < voxels.Bound.x; ++i)
            {
                for (int j = 0; j < voxels.Bound.y; ++j)
                {
                    for (int k = 0; k < voxels.Bound.z; ++k)
                    {
                        map[i, j, k] = null;
                    }
                }
            }

            var crunchers = new List <VoxelPrimitive>();
            var faces     = new VoxelVisiableFaces();

            Bounds bound = new Bounds();

            foreach (var it in voxels.GetEnumerator())
            {
                if (it.value.canMerge)
                {
                    bound.Encapsulate(new Vector3(it.position.x, it.position.y, it.position.z));
                    map[it.position.x, it.position.y, it.position.z] = it.value;
                }
                else
                {
                    crunchers.Add(new VoxelPrimitive(it.position.x, it.position.x, it.position.y, it.position.y, it.position.z, it.position.z, faces, it.value));
                }
            }

            var max = bound.max;

            max.x     = System.Math.Min(max.x + 1, voxels.Bound.x);
            max.y     = System.Math.Min(max.y + 2, voxels.Bound.y);
            max.z     = System.Math.Min(max.z + 1, voxels.Bound.z);
            bound.max = max;

            CalcVoxelCruncher(map, bound, ref crunchers);

            return(new VoxelModelList(crunchers));
        }
예제 #2
0
        public bool GetVisiableFaces(VoxelMaterial[,,] map, Vector3Int bound, int x, int y, int z, VoxelMaterial material, out VoxelVisiableFaces faces)
        {
            for (int i = 0; i < 6; i++)
            {
                instanceID[i] = null;
            }

            if (x >= 1)
            {
                instanceID[0] = map[(byte)(x - 1), y, z];
            }
            if (y >= 1)
            {
                instanceID[2] = map[x, (byte)(y - 1), z];
            }
            if (z >= 1)
            {
                instanceID[4] = map[x, y, (byte)(z - 1)];
            }
            if (x < bound.x - 1)
            {
                instanceID[1] = map[(byte)(x + 1), y, z];
            }
            if (y < bound.y - 1)
            {
                instanceID[3] = map[x, (byte)(y + 1), z];
            }
            if (z < bound.z - 1)
            {
                instanceID[5] = map[x, y, (byte)(z + 1)];
            }

            if (material.is_transparent)
            {
                var name = material.Name;

                faces.left   = (instanceID[0] == null) ? true : instanceID[0].Name != name ? true : false;
                faces.right  = (instanceID[1] == null) ? true : instanceID[1].Name != name ? true : false;
                faces.bottom = (instanceID[2] == null) ? true : instanceID[2].Name != name ? true : false;
                faces.top    = (instanceID[3] == null) ? true : instanceID[3].Name != name ? true : false;
                faces.front  = (instanceID[4] == null) ? true : instanceID[4].Name != name ? true : false;
                faces.back   = (instanceID[5] == null) ? true : instanceID[5].Name != name ? true : false;

                if (material.canMerge)
                {
                    if (x == 0)
                    {
                        faces.left = false;
                    }
                    if (z == 0)
                    {
                        faces.front = false;
                    }
                    if (x + 1 == bound.x)
                    {
                        faces.right = false;
                    }
                    if (z + 1 == bound.z)
                    {
                        faces.back = false;
                    }
                }
            }
            else
            {
                faces.left   = (instanceID[0] == null) ? true : instanceID[0].is_transparent ? true : false;
                faces.right  = (instanceID[1] == null) ? true : instanceID[1].is_transparent ? true : false;
                faces.bottom = (instanceID[2] == null) ? true : instanceID[2].is_transparent ? true : false;
                faces.top    = (instanceID[3] == null) ? true : instanceID[3].is_transparent ? true : false;
                faces.front  = (instanceID[4] == null) ? true : instanceID[4].is_transparent ? true : false;
                faces.back   = (instanceID[5] == null) ? true : instanceID[5].is_transparent ? true : false;
            }

            if (!material.canMerge)
            {
                faces = new VoxelVisiableFaces(faces.Any);
            }

            return(faces.Any);
        }
예제 #3
0
        public void OnBuildBlock(ref LiveMesh mesh, ref int index, Vector3 pos, Vector3 scale, VoxelVisiableFaces faces)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int n = index * 4, k = 0; k < 4; k++, n++)
                {
                    Vector3 v = _vertices[i, k];
                    v.x *= scale.x;
                    v.y *= scale.y;
                    v.z *= scale.z;
                    v.x += pos.x;
                    v.y += pos.y;
                    v.z += pos.z;

                    mesh.vertices[n] = v;
                    mesh.normals[n]  = _normals[i];
                    mesh.uv[n]       = _uvs[i, k];
                }

                for (int j = index * 6, k = 0; k < 6; k++, j++)
                {
                    mesh.indices[j] = index * 4 + _indices[i, k];
                }

                index++;
            }
        }