Esempio n. 1
0
        public CSG Clone()
        {
            CSG csg = new CSG();

            csg.polygons = this.polygons.Clone <Polygon>(); // TODO should clone?
            return(csg);
        }
Esempio n. 2
0
        public static CSG FromPolygons(Polygon[] polygons)
        {
            CSG csg = new CSG();

            csg.polygons = polygons;
            return(csg);
        }
Esempio n. 3
0
            public CSG Generate(Vector3 start, Vector3 end, float radius, int slices)
            {
                this.start  = start;
                this.end    = end;
                this.radius = radius;

                ray   = end - start;
                axisZ = ray.normalized;
                bool isY = (Mathf.Abs(axisZ.y) > 0.5f);

                axisX = Vector3.Cross(new Vector3((isY ? 1 : 0), (isY ? 0 : 1), 0), axisZ).normalized;
                axisY = Vector3.Cross(axisX, axisZ).normalized;
                Vertex startVertex = new Vertex(start, axisZ * -1, Vector2.zero);
                Vertex endVertex   = new Vertex(end, axisZ.normalized, Vector2.zero);

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

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

                    polygons.Add(new Polygon(new Vertex[] { startVertex, Point(0, t0, -1), Point(0, t1, -1) }));
                    polygons.Add(new Polygon(new Vertex[] { Point(0, t1, 0), Point(0, t0, 0), Point(1, t0, 0), Point(1, t1, 0) }));
                    polygons.Add(new Polygon(new Vertex[] { endVertex, Point(1, t1, 1), Point(1, t0, 1) }));
                }
                return(CSG.FromPolygons(polygons.ToArray()));
            }
Esempio n. 4
0
            public CSG Generate(Vector3 center, float radius, int slices, int stacks)
            {
                this.center = center;
                this.radius = radius;
                for (float i = 0; i < slices; i++)
                {
                    for (float j = 0; j < stacks; j++)
                    {
                        vertices = new List <Vertex>();
                        Vertex(i / slices, j / stacks);
                        if (j > 0)
                        {
                            Vertex((i + 1) / slices, j / stacks);
                        }
                        if (j < stacks - 1)
                        {
                            Vertex((i + 1) / slices, (j + 1) / stacks);
                        }
                        Vertex(i / slices, (j + 1) / stacks);

                        for (int vi = 0; vi < vertices.Count; vi++)
                        {
                            //Debug.Log(vertices[vi].Position + " " + vertices[vi].Normal);
                        }

                        polygons.Add(new Polygon(vertices.ToArray()));
                    }
                }

                return(CSG.FromPolygons(polygons.ToArray()));
            }
Esempio n. 5
0
            public CSG Generate(Vector3 center, Vector3 radius, Quaternion rotation)
            {
                Polygon[] polygons = new Polygon[6];

                polygons[0] = new Polygon(new Vertex[] {
                    new Vertex(new Vector3(-1, -1, -1).Multiply(radius), new Vector3(-1, 0, 0), new Vector2(radius.z * 2, 0)),
                    new Vertex(new Vector3(-1, -1, 1).Multiply(radius), new Vector3(-1, 0, 0), new Vector2(0, 0)),
                    new Vertex(new Vector3(-1, 1, 1).Multiply(radius), new Vector3(-1, 0, 0), new Vector2(0, radius.y * 2)),
                    new Vertex(new Vector3(-1, 1, -1).Multiply(radius), new Vector3(-1, 0, 0), new Vector2(radius.z * 2, radius.y * 2)),
                });

                polygons[1] = new Polygon(new Vertex[] {
                    new Vertex(new Vector3(1, -1, -1).Multiply(radius), new Vector3(1, 0, 0), new Vector2(radius.y * 2, 0)),
                    new Vertex(new Vector3(1, 1, -1).Multiply(radius), new Vector3(1, 0, 0), new Vector2(0, 0)),
                    new Vertex(new Vector3(1, 1, 1).Multiply(radius), new Vector3(1, 0, 0), new Vector2(0, radius.z * 2)),
                    new Vertex(new Vector3(1, -1, 1).Multiply(radius), new Vector3(1, 0, 0), new Vector2(radius.y * 2, radius.z * 2)),
                });

                polygons[2] = new Polygon(new Vertex[] {
                    new Vertex(new Vector3(-1, -1, -1).Multiply(radius), new Vector3(0, -1, 0), new Vector2(radius.x * 2, 0)),
                    new Vertex(new Vector3(1, -1, -1).Multiply(radius), new Vector3(0, -1, 0), new Vector2(0, 0)),
                    new Vertex(new Vector3(1, -1, 1).Multiply(radius), new Vector3(0, -1, 0), new Vector2(0, radius.z * 2)),
                    new Vertex(new Vector3(-1, -1, 1).Multiply(radius), new Vector3(0, -1, 0), new Vector2(radius.x * 2, radius.z * 2)),
                });

                polygons[3] = new Polygon(new Vertex[] {
                    new Vertex(new Vector3(-1, 1, -1).Multiply(radius), new Vector3(0, 1, 0), new Vector2(radius.z * 2, 0)),
                    new Vertex(new Vector3(-1, 1, 1).Multiply(radius), new Vector3(0, 1, 0), new Vector2(0, 0)),
                    new Vertex(new Vector3(1, 1, 1).Multiply(radius), new Vector3(0, 1, 0), new Vector2(0, radius.x * 2)),
                    new Vertex(new Vector3(1, 1, -1).Multiply(radius), new Vector3(0, 1, 0), new Vector2(radius.z * 2, radius.x * 2)),
                });

                polygons[4] = new Polygon(new Vertex[] {
                    new Vertex(new Vector3(-1, -1, -1).Multiply(radius), new Vector3(0, 0, -1), new Vector2(radius.y * 2, 0)),
                    new Vertex(new Vector3(-1, 1, -1).Multiply(radius), new Vector3(0, 0, -1), new Vector2(0, 0)),
                    new Vertex(new Vector3(1, 1, -1).Multiply(radius), new Vector3(0, 0, -1), new Vector2(0, radius.x * 2)),
                    new Vertex(new Vector3(1, -1, -1).Multiply(radius), new Vector3(0, 0, -1), new Vector2(radius.y * 2, radius.x * 2)),
                });

                polygons[5] = new Polygon(new Vertex[] {
                    new Vertex(new Vector3(-1, -1, 1).Multiply(radius), new Vector3(0, 0, 1), new Vector2(radius.x * 2, 0)),
                    new Vertex(new Vector3(1, -1, 1).Multiply(radius), new Vector3(0, 0, 1), new Vector2(0, 0)),
                    new Vertex(new Vector3(1, 1, 1).Multiply(radius), new Vector3(0, 0, 1), new Vector2(0, radius.y * 2)),
                    new Vertex(new Vector3(-1, 1, 1).Multiply(radius), new Vector3(0, 0, 1), new Vector2(radius.x * 2, radius.y * 2)),
                });

                for (int i = 0; i < polygons.Length; i++)
                {
                    for (int j = 0; j < polygons[i].Vertices.Length; j++)
                    {
                        polygons[i].Vertices[j].Position = rotation * polygons[i].Vertices[j].Position + center;
                        polygons[i].Vertices[j].Normal   = rotation * polygons[i].Vertices[j].Normal;
                    }
                }

                return(CSG.FromPolygons(polygons));
            }
Esempio n. 6
0
        public CSG Inverse()
        {
            CSG csg = this.Clone();

            for (int i = 0; i < csg.polygons.Length; i++)
            {
                csg.polygons[i].Flip();
            }
            return(csg);
        }
Esempio n. 7
0
        public CSG Intersect(CSG csg)
        {
            Node a = new Node(this.Clone().polygons);
            Node b = new Node(csg.Clone().polygons);

            a.Invert();
            b.ClipTo(a);
            b.Invert();
            a.ClipTo(b);
            b.ClipTo(a);
            a.Build(b.AllPolygons.ToList());
            a.Invert();
            return CSG.FromPolygons(a.AllPolygons);
        }
Esempio n. 8
0
        public CSG Intersect(CSG csg)
        {
            Node a = new Node(this.Clone().polygons);
            Node b = new Node(csg.Clone().polygons);

            a.Invert();
            b.ClipTo(a);
            b.Invert();
            a.ClipTo(b);
            b.ClipTo(a);
            a.Build(b.AllPolygons.ToList());
            a.Invert();
            return(CSG.FromPolygons(a.AllPolygons));
        }
Esempio n. 9
0
 public CSG Clone()
 {
     CSG csg = new CSG();
     csg.polygons = this.polygons.Clone<Polygon>(); // TODO should clone?
     return csg;
 }
Esempio n. 10
0
 public static CSG FromPolygons(Polygon[] polygons)
 {
     CSG csg = new CSG();
     csg.polygons = polygons;
     return csg;
 }