Search Unity

Need help in combining mesh

Discussion in 'Scripting' started by Falihin, Mar 5, 2015.

  1. Falihin

    Falihin

    Joined:
    Aug 1, 2013
    Posts:
    11
    I have a hard time understanding how to combine meshes in unity by scripting. I am working with C# as javascript is a bit confusing for me.

    As an example, lets say i have two plane mesh, planeA and planeB. In update these two mesh will be combined, the combined mesh will then be assigned to planeA, and planeB will be destroyed. Next update, a new plane mesh, planeC is spawned and the previous process will be repeated. So planeA will have a growing mesh over the time.

    I have tried following the scripting API, but at the next update, the combined mesh is not assigned to planeA, it will become empty. Any tutorial or script that I found from the internet, from my point of understanding, combines meshes and assign it to a new gameObject, which is different to what I am trying to do.

    Please help me as this issue has been bugging me for quite a while and I cannot progress my project with this issue unsolved.
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I am not sure how anyone could help without seeing the actual script.
     
  3. Falihin

    Falihin

    Joined:
    Aug 1, 2013
    Posts:
    11
    I made a capsule follow a small sphere using navmesh. Every 1 second, a circle will spawn at the current position of the capsule.

    I want to combine these circle meshes into one gameobject, where the position of the gameobject is the capsule's starting position.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(MeshFilter))]
    6. [RequireComponent(typeof(MeshRenderer))]
    7. public class Traverser2 : MonoBehaviour {
    8.  
    9.     public float masa;
    10.     public Transform Region;
    11.     public GameObject traversedRegion, currentRegion;
    12.  
    13.     void Start () {
    14.         traversedRegion = new GameObject();
    15.         traversedRegion.AddComponent<MeshFilter>().mesh = Region.GetComponent<MeshFilter>().sharedMesh;
    16.         traversedRegion.AddComponent<MeshRenderer>();
    17.  
    18.         traversedRegion.transform.position = transform.position;
    19.         traversedRegion.transform.localScale = Region.transform.localScale;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.         masa += Time.deltaTime;
    25.  
    26.         if (masa >= 1f){
    27.             currentRegion = new GameObject();
    28.             currentRegion.AddComponent<MeshFilter>().mesh = Region.GetComponent<MeshFilter>().sharedMesh;
    29.             currentRegion.AddComponent<MeshRenderer>();
    30.          
    31.             currentRegion.transform.position = transform.position;
    32.             currentRegion.transform.localScale = Region.transform.localScale;
    33.  
    34.             CombineInstance[] combine = new CombineInstance[2];
    35.  
    36.             combine[0].mesh = traversedRegion.GetComponent<MeshFilter>().sharedMesh;
    37.             combine[0].transform = traversedRegion.GetComponent<MeshFilter>().transform.localToWorldMatrix;
    38.  
    39.             combine[1].mesh = currentRegion.GetComponent<MeshFilter>().sharedMesh;
    40.             combine[1].transform = currentRegion.GetComponent<MeshFilter>().transform.localToWorldMatrix;
    41.  
    42.             traversedRegion.GetComponent<MeshFilter>().mesh = new Mesh();
    43.             traversedRegion.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
    44.  
    45.             Destroy(currentRegion);
    46.  
    47.             masa = 0f;
    48.         }
    49.     }
    50. }
     
    Last edited: Mar 5, 2015
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You have quite some stuff going on in this script already, especially if you consider that it doesn't work appropriately. Did you try to minimize the functionality and to make sure it works step by step.

    You may e.g. start with combining two given meshes and as soon as this works, you go on. There are a few suspicious parts in your code. E.g. I don't understand why you are using transform.position at some points or why you create a new mesh at line 42 each time. Those may be totally valid though.
     
  5. Falihin

    Falihin

    Joined:
    Aug 1, 2013
    Posts:
    11
    I want to create something like a memory for the gameobject. What I mean by this is that I want the capsule to 'remember' where it has gone before. That is why every one second, the code will create a new mesh at its' current position. After several seconds, I expect the combined circle mesh to be like a pathway showing where the capsule had been.

    I don't really know how to check if the combined mesh is created. If I used debug.log, it will only print combineInstance. Can you provide me any tips regarding this.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I would start simpler. Just create a new scene with two meshes in it and then combine them on start/awake and verify it each time. If your code doesn't work now, you need to make one step back.
     
  7. Falihin

    Falihin

    Joined:
    Aug 1, 2013
    Posts:
    11
    Thanks. I will try that first.
     
  8. Falihin

    Falihin

    Joined:
    Aug 1, 2013
    Posts:
    11
    I tried to run my code a few times and after a while i noticed something. Before this, I view the scene tab to only focus on the part where the capsule is, so this is what I saw after zooming out, a lot.

    I'm assuming this is caused by the part 'transform.localtoworldmatrix'. That is the only part that I could not understand from the example code and the scripting help does not provide much help, and since my code is generating new mesh every second, calling that piece of code every time, it somehow caused the generated mesh to enlarge. Can someone help in explaining it (with some simple example if possible).

    myScene.jpg after5Sec.jpg