Search Unity

How to properly move ProBuilderMesh vertices

Discussion in 'World Building' started by JPonzo, Aug 25, 2021.

  1. JPonzo

    JPonzo

    Joined:
    Sep 29, 2013
    Posts:
    5
    Hello !

    I try to make a simple stand-alone mesh editor with Unity. As long as ProBuilder exposes a public runtime API I'm currently prototyping a solution built on top of it.

    I already have some encouraging results but so far I could not manage to conveniently modify the verticies positions of a ProBuilderMesh.

    I looked around a bit in the source code of ProBuilder and actualy the most part is sealed/internal (for good reasons I assume) so that the only way I found to modify vertices is :

    Code (CSharp):
    1. namespace UnityEngine.ProBuilder
    2. {
    3.    public static class VertexPositioning
    4.    {
    5.  
    6.        ///The rest of the VertexPositioning class ...
    7.  
    8.        public static void TranslateVertices(this ProBuilderMesh mesh, IEnumerable<int> indexes, Vector3 offset)
    9.            {
    10.                if (mesh == null)
    11.                    throw new ArgumentNullException("mesh");
    12.                mesh.GetCoincidentVertices(indexes, s_CoincidentVertices);
    13.                TranslateVerticesInternal(mesh, s_CoincidentVertices, offset);
    14.            }
    15.  
    16.        ///The rest of the VertexPositioning class ...
    17.    }
    18. }
    It fits perfectly for my "move selection" operation but unfortunatly not for the "rotate/scale selection" one.

    I tried an ugly workaround to achieve a "rotate selection" operation using VertexPositioning.TranslateVertices(...) individualy on each vertices :

    Code (CSharp):
    1. public static class ProbuilderMeshExtention
    2. {
    3.     public static void RotateVertices(this ProBuilderMesh proBuilderMesh, IEnumerable<int> indices, Vector3 pivot, Quaternion offset)
    4.     {
    5.         List<int> sharedVertices = proBuilderMesh.GetCoincidentVertices(indices);
    6.  
    7.         foreach (int idx in sharedVertices)
    8.         {
    9.             Vector3 initPos = proBuilderMesh.positions[idx];
    10.             Vector3 rotatedPos = offset * (pivot - initPos) + pivot;
    11.             Vector3 translation = rotatedPos - initPos;
    12.  
    13.             proBuilderMesh.TranslateVertices(new int[] { idx }, translation);
    14.  
    15.             //proBuilderMesh.SetSharedVertexPosition(idx, rotatedPos);
    16.         }
    17.     }
    18. }
    But the result seems ok for some frames and randomely wrong for others (vertices seems swapped) :

    oknok.jpg

    Anyway even if it worked I strongly doubt this is a good solution.

    Can you show me the appropriate way to modify vertices positions of a ProBuilderMesh please ?

    Thanks in advance