Search Unity

Mesh Filter workflow question

Discussion in 'Data Oriented Technology Stack' started by francois85, Sep 7, 2019.

  1. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    639
    I was wondering if someone could help me translate my monbehaviour workflow from my game into how it should be done in DOTS/ECS.

    Let me start my saying I have no idea how the new hybrid Renderer works or why we need it. I’m sure that will be the next bridge to cross.

    I’m my game I have a gameObject with an empty meshFilter and meshRender component. I would generate the mesh data at runtime and plug that into the empty mesh filter on my game object.

    Now I have all my mesh data being generated in a system and on the main thread I can replicate the same thing as in the old by finding my game object using tags and setting the mesh filter.

    This is obviously not using hybrid render or even converting my game object to an entity which is what I would like to do.

    Problem is I’m not sure how this is suppose to work since the game object would only be a way of authoring my entities. Entities don’t set gameObject or am I missing something.
     
  2. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    639
    After getting all my mesh data from my jobs im currently setting my mesh filter like this in the main thread:

    Code (CSharp):
    1.    
    2.  
    3. ......      
    4.             inputDeps.Complete();
    5.  
    6.  
    7.             hexMesh = GameObject.FindGameObjectWithTag("tagHexMeshGameObject").GetComponent<MeshFilter>().mesh;// GetComponent<MeshFilter>().sharedMesh;
    8.             if (hexMesh == null)
    9.                 throw new System.ArgumentNullException("GameObject.FindGameObjectWithTag(tagHexMeshGameObject).GetComponent<MeshFilter>()");
    10.  
    11.  
    12.             hexMesh.name = "HexShere";
    13.             hexMesh.vertices = vertcesBuffer.ToArray();
    14.             hexMesh.normals = normalsBuffer.ToArray();
    15.             hexMesh.uv = uvs01Buffer.ToArray();
    16.             hexMesh.tangents = tangentsBuffer.ToArray();
    17.             hexMesh.triangles = trianglesBuffer.ToArray();
    18.             //hexMesh.uv2 = uvs02Buffer.ToArray();
    19.             hexMesh.colors = colorsBuffer.ToArray();
    20.  
    21.             //
    22.             // Cleanup
    23.             vertcesBuffer.Dispose();
    24.             normalsBuffer.Dispose();
    25.             uvs01Buffer.Dispose();
    26.             tangentsBuffer.Dispose();
    27.             trianglesBuffer.Dispose();
    28.             uvs02Buffer.Dispose();
    29.             colorsBuffer.Dispose();
    30.  
    31.             //
    32.             //
    33.             return inputDeps;
    34.  
    GameObject.FindGameObjectWithTag feels very non ecs way. Should i be converting my gameObject ? and somehow set it ?
     
  3. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    96
    If you have the hybrid renderer installed, any GameObject marked for conversion (either by being in a subscene or via ConvertToEntity) with the MeshFilter and MeshRenderer components attached will automatically be converted to ECS representation.
     
  4. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    639
    Prefect thanks for the reply , I did not now this
     
  5. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    639
    How do I find a specific MeshFilter in my system after conversion. Also im not sure I want to loop over all my entities just to find the one mesh

    Code (CSharp):
    1.     protected override void OnUpdate()
    2.     {
    3.         var materials = new List<Material>(1);
    4.         Entities.ForEach((MeshRenderer meshRenderer, MeshFilter meshFilter) =>
    5.         {
    6.             if ( /*My wanted mesh */) ///////////////////////////// ???
    7.             {
    8.                 var entity = GetPrimaryEntity(meshRenderer);
    9.                 var hexMesh = meshFilter.sharedMesh;
    10.                 hexMesh.name = "HexShere";
    11.                 hexMesh.vertices = vertcesBuffer.ToArray();
    12.                 hexMesh.normals = normalsBuffer.ToArray();
    13.                 hexMesh.uv = uvs01Buffer.ToArray();
    14.                 hexMesh.tangents = tangentsBuffer.ToArray();
    15.                 hexMesh.triangles = trianglesBuffer.ToArray();
    16.                 //hexMesh.uv2 = uvs02Buffer.ToArray();
    17.                 hexMesh.colors = colorsBuffer.ToArray();
    18.             }
    19.         });
    20.     }
     
  6. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    96
    You can create a custom MonoBehaviour and include it in the ForEach iterator to single it out:
    Entities.ForEach((MeshFilter meshFilter, CustomMesh customMesh) =>
     
  7. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    639
    got it Thanks for the help. Actually one more question, is it best practice to create my MeshRendererInstance/Rebder component in a boost strap class or can I just do this in GameObjectConvert systerm?
     
    Last edited: Sep 10, 2019