コード例 #1
0
        private void ClipTriangles(BaseMeshTriangle baseTri, MeshTriangle projTri, List <MeshPoint> outverts, List <int> inds)
        {
            //Sutherland–Hodgman algorithm
            //clip a triangle against another by iteratively clipping each edge of the second one

            //we want to clip against base tri
            var outputList = new MeshPolygon(projTri);
            var basePlane  = new Plane(baseTri.Vertices[0], baseTri.Vertices[1], baseTri.Vertices[2]);

            for (int i = 0; i < 3; i++)
            {
                if (outputList.Points.Count == 0)
                {
                    return;
                }
                var inputList = outputList;
                var edge      = new ClipEdge(baseTri.Vertices[i], baseTri.Vertices[(i + 1) % 3]);
                outputList = new MeshPolygon();
                var lastPoint = inputList.Points.Last();
                int j         = inputList.Points.Count - 1;
                foreach (var point in inputList.Points)
                {
                    if (!edge.ShouldClip(point.Position))
                    {
                        if (edge.ShouldClip(lastPoint.Position))
                        {
                            outputList.Points.Add(edge.IntersectLine(inputList, j));
                        }
                        //we still need to project the point onto the surface...
                        var ray        = new Ray(point.Position, new Vector3(0, -1, 0));
                        var intersect2 = ray.Intersects(basePlane);
                        if (intersect2 == null)
                        {
                            ray.Direction *= -1;
                            intersect2     = ray.Intersects(basePlane);
                            if (intersect2 == null)
                            {
                            }
                            intersect2 = -(intersect2 ?? 0f);
                        }
                        point.Position.Y -= intersect2.Value;
                        outputList.Points.Add(point);
                    }
                    else
                    {
                        if (!edge.ShouldClip(lastPoint.Position))
                        {
                            outputList.Points.Add(edge.IntersectLine(inputList, j));
                        }
                    }
                    j         = (j + 1) % inputList.Points.Count;
                    lastPoint = point;
                }
            }

            if (outputList.Points.Count < 3)
            {
                return;                              //?
            }
            outputList.Triangulate(outverts, inds);
        }
コード例 #2
0
 public bool RoughIntersects(BaseMeshTriangle other)
 {
     return(!(x1 > other.x2 || x2 < other.x1 || y1 > other.y2 || y2 < other.y1));
 }