Search Unity

Create & render Mesh using job System

Discussion in 'Graphics for ECS' started by franky_U, Aug 1, 2019.

  1. franky_U

    franky_U

    Joined:
    Mar 13, 2019
    Posts:
    12
    Hello,
    currently i am experimenting with the Mesh creation and rendering using Jobsystem.
    i have created Mesh Procedurally by passing the ]Native Array data to The Job system and getting it back on the main thread.
    I know that reference type data is not supported inside IJob Struct.
    Once i get the Native Array data for the Mesh.Is there any way i can attach this Mesh to the entity created using Jobsystem. or render it with Jobsystem?

    The whole point is i want to render the mesh using pure ecs /jobsystem for which i just have Nattive Array Data of mesh.
     
  2. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
  3. franky_U

    franky_U

    Joined:
    Mar 13, 2019
    Posts:
    12
    @francois85 Thanks for sharing the links. I went through the few of the links. and have been able to create mesh and Entity using Jobsystem. Passing Native Array Data to and fro ,from main thread and Worker Threads. now the only part pending is rendering the mesh in an efficient manner,perhaps on the main thread and Generating thousands of mesh instance at runtime.
     
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    OK started on this. Here is where im at. Did a super rough fist pass any critique would be appreciated. The code is ugly so please rip into any of it so I can refactor it as best as possible.

    The good news is the data is correct and a mesh is building with live sub scenes and a bunch of embedded data on the entities for pathfinding and shader so pretty happy.




    Code (CSharp):
    1.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    2.         {
    3.             EntityQuery qTriangleComponent = GetEntityQuery(ComponentType.ReadOnly<TriangleComponent>());
    4.             if (qTriangleComponent.CalculateChunkCount() == 0)
    5.             {
    6.                 //inputDeps.Complete();
    7.                 return inputDeps;
    8.             }
    9.              
    10.                 //
    11.                 // Get data
    12.                 var hexSphereBuildComponent = GetSingletonEntity<HexSphereBuildDataComponent>();
    13.             hexSphereBuildDataSingleton = EntityManager.GetComponentData<HexSphereBuildDataComponent>(hexSphereBuildComponent);
    14.             //int vertexBufferSize = hexSphereBuildDataSingleton.VertexCount * 3;
    15.             int triangleBufferSize = hexSphereBuildDataSingleton.TriangleCount * 3;
    16.  
    17.             //
    18.             // Schedule CreateMeshBuildBuffers
    19.             var vertcesBuffer = new NativeArray<Vector3>(triangleBufferSize, Allocator.Persistent);
    20.             var trianglesBuffer = new NativeArray<int>(triangleBufferSize, Allocator.Persistent);
    21.             var normalsBuffer = new NativeArray<Vector3>(triangleBufferSize, Allocator.Persistent);
    22.             var tangentsBuffer = new NativeArray<Vector4>(triangleBufferSize, Allocator.Persistent);
    23.             var uvs01Buffer = new NativeArray<Vector2>(triangleBufferSize, Allocator.Persistent);
    24.             var uvs02Buffer = new NativeArray<Vector2>(triangleBufferSize, Allocator.Persistent);
    25.             var colorsBuffer = new NativeArray<int>(triangleBufferSize, Allocator.Persistent);
    26.  
    27.             inputDeps = new CreateMeshBuildBuffers()
    28.             {
    29.                 VertcesBuffer = vertcesBuffer, //new NativeArray<Vector3>(triangleBufferSize, Allocator.TempJob),
    30.                 TrianglesBuffer = trianglesBuffer, // new NativeArray<int>(triangleBufferSize, Allocator.TempJob),
    31.                 NormalsBuffer = normalsBuffer, //new NativeArray<Vector3>(triangleBufferSize, Allocator.TempJob),
    32.                 TangentsBuffer = tangentsBuffer, //new NativeArray<Vector4>(triangleBufferSize, Allocator.TempJob),
    33.                 UVs01Buffer = uvs01Buffer, //new NativeArray<Vector2>(triangleBufferSize, Allocator.TempJob),
    34.                 UVs02Buffer = uvs02Buffer, //new NativeArray<Vector2>(triangleBufferSize, Allocator.TempJob),
    35.                 ColorsBuffer = colorsBuffer, //new NativeArray<int>(triangleBufferSize, Allocator.TempJob),
    36.                 DTileVertex = GetComponentDataFromEntity<TileVertexComponent>(true)
    37.             }.ScheduleSingle(this, inputDeps);
    38.  
    39.  
    40.  
    41.             //
    42.             // Complete
    43.             inputDeps.Complete();
    44.  
    45.             hexMesh = GameObject.FindGameObjectWithTag("tagHexMeshGameObject").GetComponent<MeshFilter>().mesh;// GetComponent<MeshFilter>().sharedMesh;
    46.             if (hexMesh == null)
    47.                 throw new System.ArgumentNullException("GameObject.FindGameObjectWithTag(tagHexMeshGameObject).GetComponent<MeshFilter>()");
    48.  
    49.             //TODO: If we go over 6 sub we need to use 32bin on mesh indexing
    50.             hexMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
    51.             hexMesh.name = "HexShere";
    52.             hexMesh.vertices = vertcesBuffer.ToArray();
    53.             hexMesh.normals = normalsBuffer.ToArray();
    54.             hexMesh.uv = uvs01Buffer.ToArray();
    55.             hexMesh.tangents = tangentsBuffer.ToArray();
    56.             hexMesh.triangles = trianglesBuffer.ToArray();
    57.             //hexMesh.uv2 = uvs02Buffer.ToArray();
    58.             //hexMesh.colors = colorsBuffer.ToArray();
    59.             //hexSphereMesh = hexMesh;
    60.          
    61.             vertcesBuffer.Dispose();
    62.             normalsBuffer.Dispose();
    63.             uvs01Buffer.Dispose();
    64.             tangentsBuffer.Dispose();
    65.             trianglesBuffer.Dispose();
    66.             uvs02Buffer.Dispose();
    67.             colorsBuffer.Dispose();
    68.  
    69.             return inputDeps;
    70.         }
    71.  
     
    bb8_1 likes this.
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Elecman, bb8_1, andywatts and 4 others like this.
  6. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    ahh good to know, I w
    Thank you i will check it out.