Search Unity

Question Filtering Probuilder mesh vertices

Discussion in 'Scripting' started by stychu, Jan 27, 2021.

  1. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
    Hello there,
    I've stopped upon small problem when it hits me with big performance and Can't seem to find good solution.

    Given this image: I have two Probuilder meshes (red and white)
    Unity_d0QOTuADu3.png

    Legend:
    Red - path mesh
    White - terrain mesh
    Green points - vertices of the terrain mesh
    Yellow line - shows how many vertices are at the same position (4)
    Brown line - shows omitted vertices

    I'm trying to find out the best way to filter out terrain probuilder mesh vertices that don't share the vertices with the path probuilder mesh. Secondly, I want to somehow collapse shared vertices on the terrain mesh as there are 4 for each vertex position which is unnecessary calculations later on but can't find out a way to do it.

    I'm using Probuilder API via script to achieve this goal.

    This is the code that generated meshes on the image.

    Code (CSharp):
    1. void Start() {
    2.     quads.Add(ShapeGenerator.GeneratePlane(PivotLocation.FirstVertex, 1, 1, 1, 1, Axis.Up));
    3.     quads.Add(ShapeGenerator.GeneratePlane(PivotLocation.FirstVertex, 1, 1, 1, 1, Axis.Up));
    4.     quads.Add(ShapeGenerator.GeneratePlane(PivotLocation.FirstVertex, 1, 1, 1, 1, Axis.Up));
    5.    
    6.     quads[0].transform.position = new Vector3(0, 0, 2);
    7.     quads[1].transform.position = new Vector3(1, 0, 2);
    8.     quads[2].transform.position = new Vector3(2, 0, 2);
    9.  
    10.     ProBuilderMesh path = ShapeGenerator.GeneratePlane(PivotLocation.FirstVertex, 1, 1, widthCuts, heightCuts, Axis.Up);
    11.  
    12.     path.transform.position = new Vector3(2, 0, 3);
    13.    
    14.     ProBuilderMesh terrain = CombineMeshes.Combine(quads, quads.First()).First();
    15.  
    16.     List<Vector3> terrainVertices =terrain.VerticesInWorldSpace()
    17.      .Where(vector3 => !path.VerticesInWorldSpace().ToList().Contains(vector3))
    18.      .ToList()
    19.      .Select(
    20.         vector3 => {
    21.           Instantiate(gameObject, new Vector3(vector3.x, 0, vector3.z), Quaternion.identity);
    22.  
    23.           return vector3;
    24.         }
    25.       ).ToList();
    26.   }

    Is there a better way to find out only vertices that are not shared with path mesh vertices and how to reduce amout of vertices for mesh??