Search Unity

How to subdivide ProBuilder faces with code(Runtime)

Discussion in 'World Building' started by Aetrix, Dec 9, 2019.

  1. Aetrix

    Aetrix

    Joined:
    Nov 23, 2013
    Posts:
    12
    Hello to all, can anyone help me with probuilder face subdividing via script. I can extrude face via script but not subdivide. Am I missing something? I'm using the latest unity and latest ProBuilder imported in the project.
    Thank you in advance!
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Aetrix likes this.
  3. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    129
    I can confirm ConnectElements does work:
    Code (CSharp):
    1. // pb is your ProBuilderMesh object
    2. ConnectElements.Connect(pb, pb.faces);
     
    Aetrix and Olmi like this.
  4. Aetrix

    Aetrix

    Joined:
    Nov 23, 2013
    Posts:
    12
    Thank you so much guys, it does work and i have creared cube from script but when i deploy to android i have this error
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2.  
    3. UnityEngine.ProBuilder.BuiltinMaterials.Init () (at <643dd6e0a1b0436996d4f97add324e4f>:0)
    4. UnityEngine.ProBuilder.BuiltinMaterials.get_defaultMaterial () (at <643dd6e0a1b0436996d4f97add324e4f>:0)
    5. UnityEngine.ProBuilder.Face..ctor (System.Collections.Generic.IEnumerable`1[T] indices) (at <643dd6e0a1b0436996d4f97add324e4f>:0)
    6. ProBuilderPlane.Construct (UnityEngine.Vector3[] points, System.Single extrudeAmount) (at <7926f12d1fbd482bb6cb0f799a0b945b>:0)
    7. Assets.Map.Map.GenRegionTrigger (Assets.Map.Center SelectedCenter) (at <7926f12d1fbd482bb6cb0f799a0b945b>:0)
    8. Assets.Map.Map.<GenerateRegionTriggers>b__44_0 (Assets.Map.Center center) (at <7926f12d1fbd482bb6cb0f799a0b945b>:0)
    9. System.Collections.Generic.List`1[T].ForEach (System.Action`1[T] action) (at <319b5411003b47fbaceecace494b6f79>:0)
    10. Assets.Map.Map.GenerateRegionTriggers () (at <7926f12d1fbd482bb6cb0f799a0b945b>:0)
    11. Assets.Map.Map..ctor (System.Int32 numberOfPlayers, System.Collections.Generic.List`1[T] playerOccupationColors, UnityEngine.LineRenderer lineRenderer, UnityEngine.GameObject playerStartSphere, UnityEngine.Color32 colorMarkToFlood, System.Collections.Generic.List`1[T] ringsData, System.Byte interactableCellAlpha, System.Byte selectedPlayerSphereAlpha, UnityEngine.GameObject cellSelectionSphere, System.Int32 elevationOfSphereMultiplyer, System.Single triggerHeight, UnityEngine.GameObject actionPanel, UnityEngine.UI.Button actionButton, UnityEngine.GameObject cardsPanel, UnityEngine.Transform cardsLayoutPanel, UnityEngine.GameObject cardPrefab, UnityEngine.UI.Text statsGoldText, UnityEngine.UI.Text statsExperienceText) (at <7926f12d1fbd482bb6cb0f799a0b945b>:0)
    12. WorldMapManager.Awake () (at <7926f12d1fbd482bb6cb0f799a0b945b>:0)
    Can you help guys? All im doing is the same code from getting started and include this Connect and thats it. Im using latest Unity (lwrp template) and latest probuilder. Thank you all again!
     
  5. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    129
    Are you positively sure that it works everytime in editor? Can you post your code?
     
  6. Aetrix

    Aetrix

    Joined:
    Nov 23, 2013
    Posts:
    12
    I am positive, here is my script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.ProBuilder;
    5. using UnityEngine.ProBuilder.MeshOperations;
    6.  
    7.  
    8. public class ProBuilderPlane : MonoBehaviour
    9. {
    10.  
    11.     public Material material;
    12.     public Vector3[] mypoints;
    13.     public int[] myindexes;
    14.  
    15.     void Start()
    16.     {
    17.         Construct(mypoints, 1.0f);
    18.     }
    19.  
    20.     public GameObject Construct(Vector3[] points, float extrudeAmount)
    21.     {
    22.         mypoints = points;
    23.  
    24.         int[] indexes = new int[points.Length * 3];
    25.  
    26.         for (int i = 0; i < points.Length - 1; i++)
    27.         {
    28.             indexes[i * 3] = 0;
    29.             indexes[i * 3 + 1] = points.Length - i - 1;
    30.             indexes[i * 3 + 2] = points.Length - i - 2 == 0 ? points.Length - 1 : points.Length - i - 2;
    31.         }
    32.  
    33.         myindexes = indexes;
    34.  
    35.         ProBuilderMesh quad = ProBuilderMesh.Create(
    36.             points,
    37.             new Face[] { new Face(indexes) }
    38.         );
    39.         quad.Connect(quad.faces);
    40.         quad.SetMaterial(quad.faces, material);
    41.         quad.Extrude(quad.faces, ExtrudeMethod.FaceNormal, extrudeAmount);
    42.         quad.ToMesh();
    43.  
    44.         quad.Refresh(RefreshMask.All);
    45.         quad.gameObject.AddComponent<MeshCollider>();
    46.         //quad.gameObject.GetComponent<Renderer>().sharedMaterial = material;
    47.  
    48.         return quad.gameObject;
    49.     }
    50.  
    51.     void Update()
    52.     {
    53.  
    54.     }
    55. }
     
  7. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    129
    I don't undestand the Construct(mypoints, 1.0f) line. When in Construct method, this literaly executes mypoints = mypoints. Where do mypoints and material vars get initialized?
     
  8. Aetrix

    Aetrix

    Joined:
    Nov 23, 2013
    Posts:
    12
    You can set up points in the inspector, create 5 elements in mypoints, (0,0,0)(1,0,0)(1,0,1)(0,0,1)(0,0,0) and just crate one material with lightweight rendering pipeline mat. It is true that we generate our points with voronoi diagrams and feed them inside and construct a mesh. But even with this simple setup it trows error on android.
    Setup img: https://imgur.com/gallery/qOViHWv
     
  9. SgtLame

    SgtLame

    Joined:
    Nov 26, 2015
    Posts:
    129
    Do you mean that you define those points in the inspector at editor time, and then you build the project?
     
  10. Aetrix

    Aetrix

    Joined:
    Nov 23, 2013
    Posts:
    12
    Yes, sorry for delay, my team and i have reported this bug and ot has been updated in probuilder package 4.3.0 preview 1. Check it out :)