Beispiel #1
0
        /// <summary>
        /// Extrudes a polygon.
        /// </summary>
        /// <param name="polygon">The polygon to extrude</param>
        /// <param name="distance">Extrusion distance</param>
        /// <param name="normal">Optional normal to extrude along, default is polygon normal</param>
        /// <returns></returns>
        public static List<Polygon> extrudePolygon(Polygon polygon, float distance, Vector3? normal = null)
        {
            normal = normal != null ? normal : polygon.plane.normal;

            Vector3 du = normal.GetValueOrDefault();
            IVertex[] vertices = polygon.vertices;
            List<IVertex> top = new List<IVertex>();
            List<IVertex> bot = new List<IVertex>();
            List<Polygon> polygons = new List<Polygon>();
            Vector3 invNormal = normal.GetValueOrDefault();

            du *= distance;
            invNormal *= -1f;

            for (int i = 0; i < vertices.Length; i++)
            {
                int j = (i + 1) % vertices.Length;
                Vector3 p1 = vertices[i].pos;
                Vector3 p2 = vertices[j].pos;
                Vector3 p3 = p2 + du;
                Vector3 p4 = p1 + du;
                Plane plane = Plane.fromPoints(p1, p2, p3);
                Vertex v1 = new Vertex(p1, plane.normal);
                Vertex v2 = new Vertex(p2, plane.normal);
                Vertex v3 = new Vertex(p3, plane.normal);
                Vertex v4 = new Vertex(p4, plane.normal);
                Polygon poly = new Polygon(new List<IVertex>(new IVertex[] { v1, v2, v3, v4 }), polygon.shared);
                polygons.Add(poly);
                top.Add(new Vertex(p4, normal.GetValueOrDefault()));
                bot.Insert(0, new Vertex(p1, invNormal));
            }

            polygons.Add(new Polygon(top, polygon.shared));
            polygons.Add(new Polygon(bot, polygon.shared));

            return polygons;
        }
Beispiel #2
0
        /// <summary>
        /// Cube function, Untested but compiles
        /// </summary>
        /// <param name="center">world space center of the cube</param>
        /// <param name="radius">size of the cube created at center</param>
        /// <returns></returns>
        public static CSG cube(Vector3? center, Vector3? radius)
        {
            Vector3 c = center.GetValueOrDefault(Vector3.zero);
            Vector3 r = radius.GetValueOrDefault(Vector3.one);

            //TODO: Test if this works
            Polygon[] polygons = new Polygon[6];
            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}}
            };
            for (int x = 0; x < 6; x++)
            {
                int[][] v = data[x];
                Vector3 normal = new Vector3((float)v[1][0], (float)v[1][1], (float)v[1][2]);

                IVertex[] verts = new IVertex[4];
                for (int i = 0; i < 4; i++)
                {
                    verts[i] = new Vertex(
                        new Vector3(
                            c.x + (r.x * (2 * (((v[0][i] & 1) > 0) ? 1 : 0) - 1)),
                            c.y + (r.y * (2 * (((v[0][i] & 2) > 0) ? 1 : 0) - 1)),
                            c.z + (r.z * (2 * (((v[0][i] & 4) > 0) ? 1 : 0) - 1))),
                            normal
                        );
                }
                polygons[x] = new Polygon(verts);
            }
            return CSG.fromPolygons(polygons);
        }