Search Unity

Help with CombineMeshes.Combine Probuilder function

Discussion in 'World Building' started by demonixis, Mar 26, 2020.

  1. demonixis

    demonixis

    Joined:
    Aug 20, 2013
    Posts:
    185
    Hello,

    I try to merge some meshes at runtime but it doesn't work for me. I've an empty gameobject that have children. Each child has a MeshRenderer and one material. The idea is to merge all these objects into one.

    The first thing to do was to ProBuilderize the meshes, it's done. The second part is to use the Combine function.

    This is my code

    Code (CSharp):
    1.  
    2. var probuilderMeshes = GetComponentsInChildren<ProBuilderMesh>();
    3. var current = gameObject.AddComponent<ProBuilderMesh>();
    4. CombineMeshes.Combine(probuilderMeshes, current);
    There is no error in the console, but the MeshFilter has no mesh and the MeshRenderer has no materials.
    The combine function returns an array of merged meshes, but it also takes in argument the target ProbuilderMesh... So I'm a bit confused by how to use this function correctly.
     
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    If you're using ProBuilder at runtime make sure that in the ProBuilder preferences "Strip ProBuilder Scripts" is disabled.
     
  3. demonixis

    demonixis

    Joined:
    Aug 20, 2013
    Posts:
    185
    Hello,
    I don't have this option in the preference Windows/ProBuilder, is it elsewhere?
     
  4. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    Screen Shot 2020-03-30 at 4.33.35 PM.png

    Make sure this is toggled off.
     
    commadorecoder64 likes this.
  5. demonixis

    demonixis

    Joined:
    Aug 20, 2013
    Posts:
    185
    I've unchecked it, but it doesn't combine meshes. This is what I tried:

    - Empty GameObject
    - Mesh Renderer (child)
    - Mesh Renderer (child)
    - Mesh Renderer (child)

    I use the following script


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.ProBuilder;
    4. using UnityEngine.ProBuilder.MeshOperations;
    5.  
    6. namespace TES3Unity.Graphics
    7. {
    8.     public class ProBuilderize : MonoBehaviour
    9.     {
    10.         [SerializeField]
    11.         private bool m_Merge = false;
    12.  
    13.         private void Start()
    14.         {
    15.             var filters = GetComponentsInChildren<MeshFilter>();
    16.  
    17.             foreach (var filter in filters)
    18.             {
    19.                 var mesh = filter.gameObject.AddComponent<ProBuilderMesh>();
    20.  
    21.                 var importer = new MeshImporter(mesh);
    22.                 importer.Import(filter.sharedMesh);
    23.  
    24.                 filter.sharedMesh = new Mesh();
    25.  
    26.                 mesh.ToMesh();
    27.                 mesh.Refresh();
    28.             }
    29.  
    30.             if (m_Merge)
    31.             {
    32.                 Merge();
    33.             }
    34.         }
    35.  
    36. #if UNITY_EDITOR
    37.         [ContextMenu("Merge")]
    38. #endif
    39.         private void Merge()
    40.         {
    41.             var probuilderMeshes = GetComponentsInChildren<ProBuilderMesh>();
    42.             var current = gameObject.AddComponent<ProBuilderMesh>();
    43.             CombineMeshes.Combine(probuilderMeshes, current);
    44.         }
    45.     }
    46. }
    47.  
    I probably doing something wrong but can't figure out what.
     
    stychu and MrManiak like this.
  6. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
    I have a similar problem. The current (targetMesh) seems to be not working properly.
    What I did instead and it worked was to take the first element from the probuilderMeshes and use it as the targetMesh for the Combine Function and it should work. But I feel this is wrong.

    @demonixis Did you found the solution for this ?
     
  7. demonixis

    demonixis

    Joined:
    Aug 20, 2013
    Posts:
    185
    No solution.
     
  8. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
  9. commadorecoder64

    commadorecoder64

    Joined:
    Jan 2, 2020
    Posts:
    5
    This solved my issue, was getting a new value error on duplicate. Doing it on start instead of the editor though.

    Here is my little extrude script.
    Code (CSharp):
    1. public ProBuilderMesh meshtoextrude;
    2.     public float dis = 0.5f;
    3.    
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         ProBuilderMesh a = meshtoextrude;
    8.         Face[] front = new    Face[ a.faceCount];
    9.         Face[] back = new    Face[ a.faceCount];
    10.         int i = 0;
    11.         foreach (var f in a.faces)
    12.         {
    13.             //f.Reverse();
    14.             front[i]=f;
    15.             back[i]=f;
    16.             i++;
    17.         }
    18.        
    19.         meshtoextrude.DuplicateAndFlip(front);
    20.         meshtoextrude.Extrude(back,ExtrudeMethod.FaceNormal,dis);
    21.         meshtoextrude.ToMesh();
    22.         meshtoextrude.Refresh();
    23.     }