Search Unity

How to create a 3D enviroment from a lot of meshes

Discussion in 'Scripting' started by mtm47, Feb 9, 2021.

  1. mtm47

    mtm47

    Joined:
    Oct 21, 2020
    Posts:
    6
    I am doing a robotics project in which I intend to simulate the environment that the robot is visualizing. To do this, I read the point cloud of the robot's camera and send it to unity, converting it into a mesh. The position of the vertices of the mesh is referred to the robot's camera, so if I modify the position of the gameobject transform using this offset, I can show what the robot is seeing at all times. This is the result at the moment:

    Captura de pantalla de 2021-02-09 18-55-33.png Captura de pantalla de 2021-02-09 18-55-44.png Captura de pantalla de 2021-02-09 18-55-59.png


    Now I want to create the whole environment so I am trying to combine all those meshes and complete it. I have tried various things but none of them work:

    - Show all meshes separately at once using multiple mesh filters. Is it possible to show all meshes at once without creating a gameobject for each one?

    - Combine the meshes using combinemeshes but it gives me the error (Failed getting triangles. Submesh topology is lines or points.) Since meshes cannot be combined without mesh.triangles and in this case I only have the 3d positions of the vertices

    - This is the one that has given me the best result. Add the vertices of the next mesh to the previous one concatenating vector3[] (taking into account duplicates using a dictionary so as not to introduce them).
    To add them, I must convert the coordinates from one reference axis to the other, so I calculate the transformation matrix for each robot movement and convert each point. This generates good results but, as you can see, I have to perform numerous calculations for each point at every moment which causes my program to freeze.

    I think the first option is the optimal. Anyone have any idea how I can do it?
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    This sounds like a Hard Problem(tm) inasmuch as there will always be more than one way to make what the robot is seeing.

    Once you get past that Hard Problem by whatever heuristic you choose, making geometry in Unity is super easy. You're welcome to look at my random bits of procedural geometry code in my MakeGeo project.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
  3. mtm47

    mtm47

    Joined:
    Oct 21, 2020
    Posts:
    6
    Thanks for your examples. I am trying to create a gameobject with a different mesh for each point cloud at every moment from a script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. public class enviroment2 : MonoBehaviour
    7. {
    8.     public PointCloudRenderer pointCloudRenderer;
    9.  
    10.     public Transform offset;
    11.  
    12.     public float pointSize = 1f;
    13.  
    14.     private Mesh input_mesh;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         input_mesh = new Mesh{indexFormat = UnityEngine.Rendering.IndexFormat.UInt32};
    20.     }
    21.      
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         Vector3[] positions = pointCloudRenderer.getVertices ();
    26.         Debug.Log (positions.Length);
    27.         Color[] colours = pointCloudRenderer.getColours ();
    28.         if (positions == null)
    29.         {
    30.             return;
    31.         }
    32.         input_mesh.Clear();
    33.         input_mesh.vertices = positions;
    34.         input_mesh.colors = colours;
    35.         int[] indices = new int[positions.Length];
    36.         for (int i = 0; i < positions.Length; i++)
    37.         {
    38.             indices[i] = i;
    39.         }
    40.         input_mesh.SetIndices(indices, MeshTopology.Points, 0);
    41.  
    42.  
    43.         GameObject mesh = new GameObject();
    44.         mesh.AddComponent<MeshFilter>();
    45.         mesh.AddComponent<MeshRenderer>();
    46.         mesh.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Custom/PointCloudShader"));
    47.         mesh.GetComponent<MeshRenderer>().material.SetFloat("_PointSize", pointSize);
    48.         mesh.GetComponent<MeshFilter> ().mesh = input_mesh;
    49.         mesh.transform.position = offset.position;
    50.         mesh.transform.rotation = offset.rotation;
    51.     }
    52. }
    53.  
    The problem is that although I introduce a different mesh to each gameobject, in execution all the meshes are updated to the last one so, although I manage to generate the environment, it is made up of the same repeated mesh.

    Captura de pantalla de 2021-02-11 13-10-41.png

    How can I make the gameobjects remain unchanged and not update to the latest mesh?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Of course it is: you make the mesh in Start() on line 19.

    That's ALWAYS the same mesh.

    Remake a new one if you want a new one!