Example #1
0
    // Reduces the size of the given shape by the given factor.
    static VoxelSet <bool> ReduceShape(VoxelSet <bool> shape, int factor)
    {
        Vec3i size = Vec3i.Max(shape.Size / factor, new Vec3i(1));

        Debug.Log("Old size: " + shape.Size + "     New size: " + size);

        VoxelSet <bool> reducedShape = new VoxelSet <bool>(size);

        shape.Apply((v, idx) => {
            Vec3i targetIdx         = Vec3i.Min(size - 1, idx / factor);
            reducedShape[targetIdx] = reducedShape[targetIdx] || v;
        });

        return(reducedShape);
    }
Example #2
0
        public static List <Quad> VoxelsToQuads(VoxelSet <Vec4b> voxels)
        {
            List <Quad> quads = new List <Quad>();

            Vec3f[] normals =
            {
                new Vec3f(1,   0, 0),
                new Vec3f(-1,  0, 0),
                new Vec3f(0,   1, 0),
                new Vec3f(0,  -1, 0),
                new Vec3f(0,   0, 1),
                new Vec3f(0,   0, -1)
            };

            Vec3f[] dxs =
            {
                new Vec3f(0, 1, 0),
                new Vec3f(0, 0, 1),
                new Vec3f(0, 0, 1),
                new Vec3f(1, 0, 0),
                new Vec3f(1, 0, 0),
                new Vec3f(0, 1, 0)
            };

            Vec3f[] dys =
            {
                new Vec3f(0, 0, 1),
                new Vec3f(0, 1, 0),
                new Vec3f(1, 0, 0),
                new Vec3f(0, 0, 1),
                new Vec3f(0, 1, 0),
                new Vec3f(1, 0, 0)
            };

            voxels.Apply((Vec4b c, Vec3i idx) => {
                if (c.w == 0)
                {
                    // Voxel is empty
                    //return;
                }

                // Check each face
                for (int i = 0; i < 6; ++i)
                {
                    Quad quad   = new Quad();
                    quad.normal = normals[i];
                    quad.dx     = dxs[i];
                    quad.dy     = dys[i];
                    quad.offset = new Vec3f(idx.x, idx.y, idx.z);
                    quad.color  = c;

                    Vec3i neighbor = idx + new Vec3i(new Vec3f(quad.normal.x, quad.normal.y, quad.normal.z));
                    if (c.w == 0 || voxels.IsValid(neighbor) && voxels[neighbor].w > 0)
                    {
                        // Voxel face is covered
                        continue;
                    }

                    if (quad.normal.x > 0 || quad.normal.y > 0 || quad.normal.z > 0)
                    {
                        quad.offset += quad.normal;
                    }

                    quads.Add(quad);
                }
            });

            return(quads);
        }