public static ComplexShape ShapeFromUVTexture(Texture2D p_texture, Rect p_uvRect, float p_detail = 1, float p_alphaTolerance = 0f, bool p_holeDetection = true)
        {
            if (p_texture != null)
            {
                List <List <Vector2> > v_paths = Kyub.ExternLibs.ImgToPathLib.ImgToPath.PathFromTexture(p_texture, p_uvRect, p_alphaTolerance);

                var v_finalShape = new ComplexShape();
                foreach (var v_path in v_paths)
                {
                    var v_internalShape = new ComplexShape(new PolygonShape(v_path));
                    if (p_holeDetection || v_internalShape.IsOrientedClockWise())
                    {
                        v_internalShape.Optimize(true, Mathf.Max(v_internalShape.RectBounds.width, v_internalShape.RectBounds.height) * p_detail);
                        v_finalShape.AddShape(v_internalShape, false);
                    }
                }
                v_finalShape.Optimize(false);

                var v_width  = p_uvRect.size.x * p_texture.width;
                var v_height = p_uvRect.size.y * p_texture.height;
                v_finalShape.TransformPosition(new Rect(0, 0, v_width - 1, v_height - 1), new Rect(0, 0, 1, 1));

                return(v_finalShape);
            }
            return(null);
        }
Example #2
0
 public void AddShape(ComplexShape p_complexPolygon, bool p_optimize = true)
 {
     if (p_complexPolygon != null)
     {
         foreach (var v_shape in p_complexPolygon.Shapes)
         {
             AddShape(v_shape, false);
         }
     }
     MarkToRecalcBounds();
     if (p_optimize)
     {
         Optimize();
     }
 }
Example #3
0
        protected ComplexShape ExecuteGeometryClip(ComplexShape p_complexShape, ExternLibs.ClipperLib.ClipType p_clipType)
        {
            //PolygonShape v_rectShape = new PolygonShape(Rect.MinMaxRect(v_offset.x * i, v_offset.y * j, v_offset.x * (i + 1), v_offset.y * (j + 1)));
            Kyub.ExternLibs.ClipperLib.Clipper v_clipper = new ExternLibs.ClipperLib.Clipper();

            //Fix Scale to improve precision
            var v_resizeFactor = Mathf.Max(p_complexShape.GetScaleFixerValue(), GetScaleFixerValue());

            if (v_resizeFactor > 0 && v_resizeFactor < 1)
            {
                p_complexShape.Resize(Vector2.one / v_resizeFactor, 2);
                Resize(Vector2.one / v_resizeFactor, 2);
            }

            foreach (var v_shape in Shapes)
            {
                if (v_shape != null)
                {
                    v_clipper.AddPath(v_shape.Vertices, ExternLibs.ClipperLib.PolyType.ptSubject, true);
                }
            }
            if (p_complexShape != null)
            {
                foreach (var v_shape in p_complexShape.Shapes)
                {
                    if (v_shape != null)
                    {
                        v_clipper.AddPath(v_shape.Vertices, ExternLibs.ClipperLib.PolyType.ptClip, true);
                    }
                }
            }
            List <List <Vector2> > v_solutions = new List <List <Vector2> >();

            v_clipper.Execute(p_clipType, v_solutions, Kyub.ExternLibs.ClipperLib.PolyFillType.pftNonZero, Kyub.ExternLibs.ClipperLib.PolyFillType.pftNonZero);

            //v_solutions = Kyub.ExternLibs.ClipperLib.Clipper.CleanPolygons(v_solutions);
            //Revert to original size precision
            ComplexShape v_finalSolution = new ComplexShape(v_solutions);

            if (v_resizeFactor > 0 && v_resizeFactor < 1)
            {
                p_complexShape.Resize(Vector2.one * v_resizeFactor);
                Resize(Vector2.one * v_resizeFactor);
                v_finalSolution.Resize(Vector2.one * v_resizeFactor);
            }

            return(v_finalSolution);
        }
Example #4
0
 public ComplexShape(ComplexShape p_complexShape, bool p_optimize = false)
 {
     if (p_complexShape != null)
     {
         m_shapes = new List <PolygonShape>();
         //Clone Each Internal Polygon
         foreach (var v_shape in p_complexShape.Shapes)
         {
             m_shapes.Add(new PolygonShape(v_shape));
         }
         TryRecalcBounds(true);
         if (p_optimize)
         {
             Optimize();
             TryRecalcBounds();
         }
     }
 }
Example #5
0
 public ComplexShape Union(ComplexShape p_complexShape)
 {
     return(new ComplexShape(this).Union(p_complexShape));
 }
Example #6
0
 public ComplexShape Difference(ComplexShape p_complexShape)
 {
     return(new ComplexShape(this).Difference(p_complexShape));
 }
Example #7
0
 public ComplexShape Intersection(ComplexShape p_complexShape)
 {
     return(new ComplexShape(this).Intersection(p_complexShape));
 }
Example #8
0
 public ComplexShape Union(ComplexShape p_complexShape)
 {
     return(ExecuteGeometryClip(p_complexShape, ExternLibs.ClipperLib.ClipType.ctUnion));
 }
Example #9
0
 public ComplexShape Difference(ComplexShape p_complexShape)
 {
     return(ExecuteGeometryClip(p_complexShape, ExternLibs.ClipperLib.ClipType.ctDifference));
 }