Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

ProBuilder API - using ProBuilder from script

Discussion in 'World Building' started by jariwake, Jul 8, 2018.

  1. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    90
    I am thinking of using ProBuilder for procedural level generation. But it seems there is no API documentation available anywhere. The documentation does not contain any information about the API and FAQ just says "The ProBuilder API is currently considered to be in beta. It will change before the final release."

    There is this ProBuilder API example repository, but it just contains a couple of examples which are not really documented/explained.

    So is it really true that there is no any kind of documentation for the ProBuilder API available yet? If so, is there a roadmap for the ProBuilder that would indicate when the API would be officially released and supported?
     
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    174
    It's true that currently there is not any official documentation for the API. However, preparing the code and documentation for public use is what I've been doing for the last few months. The 4.0.0 update introduces a public API along with a scripting reference.

    There is not a set release date yet, but I anticipate it within the next few weeks.
     
  3. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    90
    Thanks for the answer, @kaarrrllll. I have been toying with the API examples and it seems it is quite straight forward in terms of creating geometry. But will the API support all the geometry editing methods as well? In other words, will everything that is possible from the ProBuilder editor GUI be also possible via scripting? What I am especially interested are the boolean operations (which are now marked as experimental).
     
  4. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    174
    Yes, everything you can do from the editor is accessible in script (except generating lightmap uvs).

    If you're looking for just the CSG, here's the library that ProBuilder is using under the hood: https://github.com/karl-/pb_CSG
     
  5. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    107
    I've been playing with CSG and while it does work, it creates a real dogs-breakfast of the mesh after a couple of operations. The tris count skyrockets to the point where it would be unusable on mobile and eventually the mesh breaks catastrophically. It might be fine if we had a solid and efficient way of optimizing/simplifying the mesh between operations, but for my purposes right now it seems unworkable.

    Other issues include:
    • Not working with complicated or merged pb_objects, or pb_objects where extrude was used.
    • Losing nice quad face selection ability and can only select triangles.


    Code (CSharp):
    1.     public static pb_Object ExtractIntersection(GameObject source, GameObject stamp)
    2.     {
    3.         var sourcePb = source.GetComponent<pb_Object>();
    4.         var sourceMeshFilter = source.GetComponent<MeshFilter>();
    5.         var originalCenter = sourceMeshFilter.sharedMesh.bounds.center;
    6.  
    7.         // Create a mesh comprising only parts of the original mesh that overlap with the stamp.
    8.         var intersectionMesh = CSG.Intersect(source, stamp);
    9.  
    10.         // Create a game object and pb_object from mesh
    11.         var intersectionPb = ProBuilderize(intersectionMesh);
    12.  
    13.         // Cut out the intersection from the original, so that the original object remains the same visually but is now split into two objects.
    14.         var newMesh = CSG.Subtract(source, intersectionPb.gameObject);
    15.  
    16.         sourceMeshFilter.sharedMesh = newMesh;
    17.  
    18.         //// Update the pb_object to reflect changes to the underlying mesh.
    19.         ResetPbObjectWithMeshFilter(sourcePb, true);
    20.  
    21.         // The modified mesh coming out of CSG probably needs to be repositioned relative to previous pivot.
    22.         var newCenter = sourceMeshFilter.sharedMesh.bounds.center;
    23.         var offset = originalCenter - newCenter;
    24.         var indices = sourceMeshFilter.sharedMesh.GetIndices(0);
    25.  
    26.         sourcePb.TranslateVertices(indices, offset);
    27.         CenterPivot(sourcePb, indices);
    28.  
    29.         sourcePb.ToMesh();
    30.         sourcePb.Refresh();
    31.  
    32.         //pb_Smoothing.ApplySmoothingGroups(sourcePb, sourcePb.faces, 1, pb_Vertex.GetVertices(newMesh).Select(x => x.normal).ToArray());
    33.         //CollapseCoincidentVertices(sourcePb, sourcePb.faces);
    34.  
    35.         return intersectionPb;
    36.     }
    I think we need two things, a mesh simplification function and an easy way to clean a mesh, for example, if i create a rectangle box platform and a circular platform to place on the end of it, drag them over each other, then click a button and it strips all the overlapping parts.

    Currently unless i'm missing something, if i use 'merge objects' it keeps them intact and if i want to properly merge them i have to go in and edit out all the internal parts manually.
     
    Last edited: Jul 10, 2018
  6. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    107
    Another related and frustrating thing is that it doesn't sub-divide faces 'properly' when the faces next to them have already been sub-divided and extruded. This is pretty limiting for progressive modifications, we'll end up having to know in advance and sub-divide everything up front.



    In the video there, it knows already that it is a single big square face so it could re-do its layout to 2x2/2 tri
     
  7. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    90
    I see. I take that currently ProBuilder API does not expose CSG in any way, but it will be in the future. So right now my only option is to use CSG "in parallel" with ProBuilder?
     
  8. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    174
    Yes, that's correct.
     
  9. Aranda

    Aranda

    Joined:
    Jan 23, 2015
    Posts:
    16
    Hi @kaarrrllll, still looking forward to 4.0.0 and the docs!. In particular I'd like to clear smoothing groups and triangulate objects from the API, but I can't seem to find it. Is this already available in 3.X or do I need to wait for 4.0?
     
  10. ibyte

    ibyte

    Joined:
    Aug 14, 2009
    Posts:
    926
    TheGabelle likes this.
  11. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    90
    Well..it has been more than 5 months since you posted this, @kaarrrllll =)

    Could you maybe post an update on when the next version of ProBuilder with the API is out?
     
    HoldTheLine and Flavelius like this.
  12. gabrielw_unity

    gabrielw_unity

    Unity Technologies

    Joined:
    Feb 19, 2018
    Posts:
    653
    ibyte likes this.
  13. BBigG

    BBigG

    Joined:
    Aug 7, 2019
    Posts:
    1
    Hey hey, is there any news about this? The CSG tools is still experimental and therefore not accessible via the PB API?