Search Unity

Resolved Fill with texture area define by its contour

Discussion in 'Scripting' started by helgrind21, Oct 27, 2020.

  1. helgrind21

    helgrind21

    Joined:
    Sep 13, 2018
    Posts:
    24
    Hello,

    I'm trying to fill the area of non convex objects. I have the points that define their contours but I don't know how to fill their area. Currently, I have a mesh collider and a line render. The line renderer is used just to drawn the contour using the points. The mesh uses the same set of points to create a collider contour to detect collisions.
    The objects have similar shapes to the examples below:


    This is how I'm creating the mesh:
    Code (CSharp):
    1. objRenderer.positionCount = conePoints.Length;
    2. objRenderer.SetPositions(conePoints);
    3. objRenderer.BakeMesh(mesh, true);
    4. Vector2[] points = new Vector2[mesh.uv.Length];
    5. for (int i = 0; i < mesh.vertices.Length; i++)
    6. {
    7.          points[i] = new Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
    8. }
    9. mesh.uv = points;
    10. objMeshCollider.sharedMesh = mesh;
    11. objMeshCollider.enabled = true;
    12. objMeshFilter.mesh = mesh;
    I've tried using MeshRenderer but it does not fill the area of the object. It fills the area of the Mesh which is the contour.

    Any help would be much appreciated :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    helgrind21 likes this.
  3. helgrind21

    helgrind21

    Joined:
    Sep 13, 2018
    Posts:
    24
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Yeah, it definitely only handles convex.

    Fortunately all polygons can be partitioned into a collection of convex polygons.

    Here's some guidance:

    https://en.wikipedia.org/wiki/Polygon_partition
     
    helgrind21 likes this.
  5. helgrind21

    helgrind21

    Joined:
    Sep 13, 2018
    Posts:
    24
    Kurt-Dekker likes this.