예제 #1
0
    // Construct an axis-aligned solid cuboid. Optional parameters are `center` and
    // `radius`, which default to `[0, 0, 0]` and `[1, 1, 1]`. The radius can be
    // specified using a single number or a list of three numbers, one for each axis.
    static public CSG cube(CSGVector3 center, CSGVector3 halfSize, CSGShared shared)
    {
        int[][][] data = new int[][][] {
            new int[][] { new int[] { 0, 4, 6, 2 }, new int[] { -1, 0, 0 } },
            new int[][] { new int[] { 1, 3, 7, 5 }, new int[] { +1, 0, 0 } },
            new int[][] { new int[] { 0, 1, 5, 4 }, new int[] { 0, -1, 0 } },
            new int[][] { new int[] { 2, 6, 7, 3 }, new int[] { 0, +1, 0 } },
            new int[][] { new int[] { 0, 2, 3, 1 }, new int[] { 0, 0, -1 } },
            new int[][] { new int[] { 4, 5, 7, 6 }, new int[] { 0, 0, +1 } }
        };

        List <CSGPolygon> polygons = new List <CSGPolygon>();

        foreach (int[][] info in data)
        {
            List <CSGVertex> vertices = new List <CSGVertex>();

            foreach (int i in info[0])
            {
                CSGVector3 pos = new CSGVector3(
                    center.x + halfSize.x * (2 * ((i & 1) != 0 ? 1 : 0) - 1),
                    center.y + halfSize.y * (2 * ((i & 2) != 0 ? 1 : 0) - 1),
                    center.z + halfSize.z * (2 * ((i & 4) != 0 ? 1 : 0) - 1)
                    );

                vertices.Add(new CSGVertex(pos, new CSGVector3(info[1][0], info[1][1], info[1][2])));
            }

            polygons.Add(new CSGPolygon(vertices, shared));
        }

        return(CSG.fromPolygons(polygons));
    }
    public CSGPolygon(List <CSGVertex> vertices, CSGShared shared)
    {
        this.vertices = vertices;

        this.shared = shared;

        this.plane = CSGPlane.fromPoints(vertices[0].pos, vertices[1].pos, vertices[2].pos);
    }
    public CSGPolygon(CSGVertex[] verticesArray, CSGShared shared)
    {
        this.vertices = new List <CSGVertex>(verticesArray);

        this.shared = shared;

        this.plane = CSGPlane.fromPoints(vertices[0].pos, vertices[1].pos, vertices[2].pos);
    }
예제 #4
0
    // Construct a solid cylinder. Optional parameters are `start`, `end`,
    // `radius`, and `slices`, which default to `[0, -1, 0]`, `[0, 1, 0]`, `1`, and
    // `16`. The `slices` parameter controls the tessellation.
    //
    // Example usage:
    //
    //     var cylinder = CSG.cylinder({
    //       start: [0, -1, 0],
    //       end: [0, 1, 0],
    //       radius: 1,
    //       slices: 16
    //     });
    static public CSG cylinder(CSGVector3 s, CSGVector3 e, float radius, int slices, CSGShared shared)
    {
        CSGVector3 ray   = e.minus(s);
        CSGVector3 axisZ = ray.unit();
        bool       isY   = (Math.Abs(axisZ.y) > 0.5f);

        CSGVector3 axisX = new CSGVector3(isY ? 1 : 0, !isY ? 1 : 0, 0).cross(axisZ).unit();
        CSGVector3 axisY = axisX.cross(axisZ).unit();

        CSGVertex start = new CSGVertex(s, axisZ.negated());
        CSGVertex end   = new CSGVertex(e, axisZ.unit());

        List <CSGPolygon> polygons = new List <CSGPolygon>();

        for (float i = 0; i < slices; i++)
        {
            float t0 = i / slices;
            float t1 = (i + 1) / slices;

            //cylinderPoint(s, ray, radius, axisX, axisY, axisZ,

            polygons.Add(
                new CSGPolygon(
                    new CSGVertex[] {
                start,
                cylinderPoint(s, ray, radius, axisX, axisY, axisZ, 0, t0, -1),
                cylinderPoint(s, ray, radius, axisX, axisY, axisZ, 0, t1, -1)
            }, shared));

            polygons.Add(
                new CSGPolygon(
                    new CSGVertex[] {
                cylinderPoint(s, ray, radius, axisX, axisY, axisZ, 0, t1, 0),
                cylinderPoint(s, ray, radius, axisX, axisY, axisZ, 0, t0, 0),
                cylinderPoint(s, ray, radius, axisX, axisY, axisZ, 1, t0, 0),
                cylinderPoint(s, ray, radius, axisX, axisY, axisZ, 1, t1, 0)
            }, shared));

            polygons.Add(
                new CSGPolygon(
                    new CSGVertex[] {
                end,
                cylinderPoint(s, ray, radius, axisX, axisY, axisZ, 1, t1, 1),
                cylinderPoint(s, ray, radius, axisX, axisY, axisZ, 1, t0, 1)
            }, shared));
        }

        return(CSG.fromPolygons(polygons));
    }
예제 #5
0
    // Construct a solid sphere. Optional parameters are `center`, `radius`,
    // `slices`, and `stacks`, which default to `[0, 0, 0]`, `1`, `16`, and `8`.
    // The `slices` and `stacks` parameters control the tessellation along the
    // longitude and latitude directions.
    //
    // Example usage:
    //
    //     var sphere = CSG.sphere({
    //       center: [0, 0, 0],
    //       radius: 1,
    //       slices: 16,
    //       stacks: 8
    //     });
    static public CSG sphere(CSGVector3 center, float radius, int slices, int stacks, CSGShared shared)
    {
        List <CSGPolygon> polygons = new List <CSGPolygon>();

        for (float i = 0; i < slices; i++)
        {
            for (float j = 0; j < stacks; j++)
            {
                List <CSGVertex> vertices = new List <CSGVertex>();
                sphereVertex(center, radius, i / slices, j / stacks, vertices);

                if (j > 0)
                {
                    sphereVertex(center, radius, (i + 1) / slices, j / stacks, vertices);
                }

                if (j < stacks - 1)
                {
                    sphereVertex(center, radius, (i + 1) / slices, (j + 1) / stacks, vertices);
                }

                sphereVertex(center, radius, i / slices, (j + 1) / stacks, vertices);

                polygons.Add(new CSGPolygon(vertices, shared));
            }
        }

        return(CSG.fromPolygons(polygons));
    }