Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How to paint a polygon by brush?

Discussion in 'Scripting' started by YuGuJian, Feb 8, 2021.

  1. YuGuJian

    YuGuJian

    Joined:
    Oct 4, 2018
    Posts:
    6
    Hey guys:D
    Now I want to get a polygon defined by a sorted list of vertex coordinates. For example 4 coordinate ,(0,0) (1,0) (1,1) (0,1), define a rectangle.

    And I hope I can use a brush to paint this polygon.

    Maybe I can paint a texture like this upload_2021-2-8_21-31-15.png
    and convert it to a list of vertexes just like the little white rect points on the image below. (or piant vertexes immediately)
    upload_2021-2-8_21-29-57.png

    I had found a question (https://forum.unity.com/threads/how-to-make-field-painter-like-cities-skylines-districts.380061/) asking how to make field painter like cities skylines districts.

    There is a answer mentioned about Marching Squares algorithm. After having a quick look on the algorithm, I think it will work great when I just need to visualize the edge of the region.

    But now I wanna get a set of vertices sorted counterclockwise. I don't think the algorithm will work in this case.

    o_OI don't know how to make my idea come true.

    Someone know how to do this? thanks in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    This problem is a combination of computer vision and constructive geometry.

    If you start from a computer vision package such as OpenCV (there is a Unity3D package available on the asset store), that can give you a real leg up on a set of vectors defining the boundary of your area.

    From that you could perhaps construct a set of continuous lines forming a polygon.

    It's even possible that OpenCV has some kind of polygon-creation helper routines in it nowadays... I haven't used OpenCV since 2014, but I imagine it has had updates.
     
  3. YuGuJian

    YuGuJian

    Joined:
    Oct 4, 2018
    Posts:
    6
    Thank you for your help.:D I will try this method and see if it can be used on my project.
     
  4. YuGuJian

    YuGuJian

    Joined:
    Oct 4, 2018
    Posts:
    6
    Kurt-Dekker likes this.