Ejemplo n.º 1
0
        private static void AddToMesh(this Csg.Polygon p, ref Mesh mesh)
        {
            // Polygons coming back from Csg can have an arbitrary number
            // of vertices. We need to retessellate the returned polygon.
            var tess = new Tess();

            tess.NoEmptyPolygons = true;

            tess.AddContour(p.Vertices.ToContourVertices());

            tess.Tessellate(WindingRule.Positive, LibTessDotNet.Double.ElementType.Polygons, 3);
            for (var i = 0; i < tess.ElementCount; i++)
            {
                var a = tess.Vertices[tess.Elements[i * 3]].Position.ToVector3();
                var b = tess.Vertices[tess.Elements[i * 3 + 1]].Position.ToVector3();
                var c = tess.Vertices[tess.Elements[i * 3 + 2]].Position.ToVector3();

                var uva = (Csg.Vector2D)tess.Vertices[tess.Elements[i * 3]].Data;
                var uvb = (Csg.Vector2D)tess.Vertices[tess.Elements[i * 3 + 1]].Data;
                var uvc = (Csg.Vector2D)tess.Vertices[tess.Elements[i * 3 + 2]].Data;

                var v1 = mesh.AddVertex(a, uva.ToUV());
                var v2 = mesh.AddVertex(b, uvb.ToUV());
                var v3 = mesh.AddVertex(c, uvc.ToUV());
                mesh.AddTriangle(v1, v2, v3);
            }
        }
Ejemplo n.º 2
0
        internal static Csg.Solid ToCsg(this Mesh mesh)
        {
            var vertices = new List <Csg.Vertex>();

            foreach (var v in mesh.Vertices)
            {
                var vv = new Csg.Vertex(v.Position.ToCsgVector3(), v.UV.ToCsgVector2());
                vertices.Add(vv);
            }

            var polygons = new List <Csg.Polygon>();

            foreach (var t in mesh.Triangles)
            {
                var a       = vertices[t.Vertices[0].Index];
                var b       = vertices[t.Vertices[1].Index];
                var c       = vertices[t.Vertices[2].Index];
                var polygon = new Csg.Polygon(new List <Csg.Vertex>()
                {
                    a, b, c
                });
                polygons.Add(polygon);
            }

            return(Csg.Solid.FromPolygons(polygons));
        }
Ejemplo n.º 3
0
        internal static Csg.Solid ToCsg(this Solid solid)
        {
            var polygons = new List <Csg.Polygon>();

            foreach (var f in solid.Faces.Values)
            {
                var tess = new Tess();
                tess.NoEmptyPolygons = true;

                tess.AddContour(f.Outer.ToContourVertexArray(f));

                if (f.Inner != null)
                {
                    foreach (var loop in f.Inner)
                    {
                        tess.AddContour(loop.ToContourVertexArray(f));
                    }
                }

                tess.Tessellate(WindingRule.Positive, LibTessDotNet.Double.ElementType.Polygons, 3);

                var vertices = new List <Csg.Vertex>();
                for (var i = 0; i < tess.ElementCount; i++)
                {
                    var a = tess.Vertices[tess.Elements[i * 3]].ToCsgVector3();
                    var b = tess.Vertices[tess.Elements[i * 3 + 1]].ToCsgVector3();
                    var c = tess.Vertices[tess.Elements[i * 3 + 2]].ToCsgVector3();

                    Csg.Vertex av = null;
                    Csg.Vertex bv = null;
                    Csg.Vertex cv = null;

                    // Merge vertices.
                    foreach (var v in vertices)
                    {
                        if (v.Pos.IsAlmostEqualTo(a))
                        {
                            av = v;
                        }

                        if (v.Pos.IsAlmostEqualTo(b))
                        {
                            bv = v;
                        }

                        if (v.Pos.IsAlmostEqualTo(c))
                        {
                            cv = v;
                        }
                    }
                    if (av == null)
                    {
                        av = new Csg.Vertex(a, new Csg.Vector2D());
                        vertices.Add(av);
                    }
                    if (bv == null)
                    {
                        bv = new Csg.Vertex(b, new Csg.Vector2D());
                        vertices.Add(bv);
                    }
                    if (cv == null)
                    {
                        cv = new Csg.Vertex(c, new Csg.Vector2D());
                        vertices.Add(cv);
                    }

                    // TODO: Add texture coordinates.
                    var p = new Csg.Polygon(new List <Csg.Vertex>()
                    {
                        av, bv, cv
                    });
                    polygons.Add(p);
                }
            }
            return(Csg.Solid.FromPolygons(polygons));
        }
Ejemplo n.º 4
0
        internal static Csg.Solid ToCsg(this Solid solid)
        {
            var polygons = new List <Csg.Polygon>();

            foreach (var f in solid.Faces.Values)
            {
                var tess = new Tess();
                tess.NoEmptyPolygons = true;

                tess.AddContour(f.Outer.ToContourVertexArray(f));

                if (f.Inner != null)
                {
                    foreach (var loop in f.Inner)
                    {
                        tess.AddContour(loop.ToContourVertexArray(f));
                    }
                }

                tess.Tessellate(WindingRule.Positive, LibTessDotNet.Double.ElementType.Polygons, 3);

                Vector3 e1 = new Vector3();
                Vector3 e2 = new Vector3();

                var vertices = new List <Csg.Vertex>();
                for (var i = 0; i < tess.ElementCount; i++)
                {
                    var a = tess.Vertices[tess.Elements[i * 3]].ToCsgVector3();
                    var b = tess.Vertices[tess.Elements[i * 3 + 1]].ToCsgVector3();
                    var c = tess.Vertices[tess.Elements[i * 3 + 2]].ToCsgVector3();

                    Csg.Vertex av = null;
                    Csg.Vertex bv = null;
                    Csg.Vertex cv = null;

                    // Merge vertices.
                    foreach (var v in vertices)
                    {
                        if (v.Pos.IsAlmostEqualTo(a))
                        {
                            av = v;
                        }

                        if (v.Pos.IsAlmostEqualTo(b))
                        {
                            bv = v;
                        }

                        if (v.Pos.IsAlmostEqualTo(c))
                        {
                            cv = v;
                        }
                    }
                    if (i == 0)
                    {
                        var n = f.Plane().Normal;
                        e1 = n.Cross(n.IsParallelTo(Vector3.XAxis) ? Vector3.YAxis : Vector3.XAxis).Unitized();
                        e2 = n.Cross(e1).Unitized();
                    }
                    if (av == null)
                    {
                        var avv = new Vector3(a.X, a.Y, a.Z);
                        av = new Csg.Vertex(a, new Csg.Vector2D(e1.Dot(avv), e2.Dot(avv)));
                        vertices.Add(av);
                    }
                    if (bv == null)
                    {
                        var bvv = new Vector3(b.X, b.Y, b.Z);
                        bv = new Csg.Vertex(b, new Csg.Vector2D(e1.Dot(bvv), e2.Dot(bvv)));
                        vertices.Add(bv);
                    }
                    if (cv == null)
                    {
                        var cvv = new Vector3(c.X, c.Y, c.Z);
                        cv = new Csg.Vertex(c, new Csg.Vector2D(e1.Dot(cvv), e2.Dot(cvv)));
                        vertices.Add(cv);
                    }

                    var p = new Csg.Polygon(new List <Csg.Vertex>()
                    {
                        av, bv, cv
                    });
                    polygons.Add(p);
                }
            }
            return(Csg.Solid.FromPolygons(polygons));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Construct a CsgPolygonTessAdaptor.
 /// </summary>
 /// <param name="polygon"></param>
 public CsgPolygonTessAdapter(Csg.Polygon polygon)
 {
     this.polygon = polygon;
 }