Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

RenderMeshRenderV2 not Working

Discussion in 'Graphics for ECS' started by RoughSpaghetti3211, Dec 26, 2019.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    Hello,

    Updated to 2019.3 and having some problems seeing my mesh render. I can see it in RenderMeshRenderV2 but is just not rendering, any idea. Even when I scale my mesh and set receive shadow I can see the components update

    Screen Shot 2019-12-25 at 10.07.49 PM.png
    Screen Shot 2019-12-25 at 1.54.52 PM.png
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,790
    What material / shader / render pipeline?
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    Im using :
    Unity 2019.3
    Hybrit Renderer 0.3.1 preview10
    Entities 0.4.0 preview10

    I have an empty gameObject with Material and Mesh Component
    Screen Shot 2019-12-28 at 2.21.29 PM.png

    My conversion

    GameObjectConversionSystem .

    Code (CSharp):
    1.  
    2. ...
    3.  
    4. Entities.ForEach((PlanetMeshProxy data) =>
    5. {
    6.  
    7.     Entity ePrimary = GetPrimaryEntity(data);
    8.  
    9.     DstEntityManager.AddComponentData(ePrimary, new TagHexMeshComponent { });
    10.  
    11.     var renderMesh = new RenderMesh
    12.     {
    13.         mesh = data.mesh,
    14.         material = data.material
    15.     };
    16.     DstEntityManager.AddSharedComponentData(ePrimary, renderMesh);
    17.  
    18. ...
    19.  
    And a system that procedurally create a mesh and does SetSharedComponentData

    Code (CSharp):
    1.  
    2.  
    3.      protected override JobHandle OnUpdate(JobHandle inputDeps)
    4.         {
    5.             // TODO : JobScheduling !! currently all single and loads of sync point
    6.             //
    7.             // Get Planet Data
    8.             var subdivisions = new NativeArray<int>(1, Allocator.TempJob);
    9.             var radius = new NativeArray<float>(1, Allocator.TempJob);
    10.             var vertexCount = new NativeArray<int>(1, Allocator.TempJob);
    11.             var triangleCount = new NativeArray<int>(1, Allocator.TempJob);
    12.  
    13.             inputDeps = new GetPlanetSizeData
    14.             {
    15.                 SubdivisionCount = subdivisions,
    16.                 Radius = radius,
    17.                 VertexCount = vertexCount,
    18.                 TriangleCount = triangleCount
    19.             }.ScheduleSingle(_qPlanetSize, inputDeps);
    20.  
    21.             inputDeps.Complete();
    22.  
    23.             //
    24.             // Get Mesh Positions
    25.             var meshTrianglePositions = new NativeArray<float3>(triangleCount[0] * 3, Allocator.TempJob);
    26.  
    27.             inputDeps = new GetMeshTrianglePositions
    28.             {
    29.                 MeshTrianglePositions = meshTrianglePositions,
    30.                 TileBuffer = GetBufferFromEntity<TilesBufferComponent>(true)
    31.             }.ScheduleSingle(_qTrianglesBuffer, inputDeps);
    32.  
    33.             //
    34.             // Get Mesh Normals
    35.             var meshTriangleNormals = new NativeArray<float3>(triangleCount[0] * 3, Allocator.TempJob);
    36.  
    37.             inputDeps = new GetMeshTriangleNormals
    38.             {
    39.                 MeshTriangleNormals = meshTriangleNormals,
    40.                 TileBuffer = GetBufferFromEntity<TilesBufferComponent>(true)
    41.             }.ScheduleSingle(_qTrianglesBuffer, inputDeps);
    42.  
    43.             //
    44.             // Get Mesh Tangents
    45.             var meshTriangleTangents = new NativeArray<float4>(triangleCount[0] * 3, Allocator.TempJob);
    46.  
    47.             inputDeps = new GetMeshTriangleTangent
    48.             {
    49.                 MeshTriangleTangents = meshTriangleTangents,
    50.                 TileBuffer = GetBufferFromEntity<TilesBufferComponent>(true)
    51.             }.ScheduleSingle(_qTrianglesBuffer, inputDeps);
    52.  
    53.             //
    54.             // Get Mesh UVs
    55.             var meshTriangleUVs = new NativeArray<float2>(triangleCount[0] * 3, Allocator.TempJob);
    56.  
    57.             inputDeps = new GetMeshTriangleUv
    58.             {
    59.                 MeshTriangleUVs = meshTriangleUVs,
    60.                 TileBuffer = GetBufferFromEntity<TilesBufferComponent>(true)
    61.             }.ScheduleSingle(_qTrianglesBuffer, inputDeps);
    62.  
    63.             //
    64.             // Get Mesh Indices
    65.             var meshTriangleIndices = new NativeArray<int>(triangleCount[0] * 3, Allocator.TempJob);
    66.  
    67.             inputDeps = new GetMeshTriangleIndices
    68.             {
    69.                 MeshTriangleIndex = meshTriangleIndices
    70.             }.ScheduleSingle(_qTrianglesBuffer, inputDeps);
    71.  
    72.  
    73.             //
    74.             // Set
    75.             inputDeps.Complete();
    76.            
    77.             // TODO : Hack
    78.             if (_qTrianglesBuffer.CalculateEntityCount() > 0)
    79.             {
    80.                 // Warning : If we go over 6 sub we need to use 32bin on mesh indexing
    81.                 _hexMesh.indexFormat = IndexFormat.UInt32;
    82.                 _hexMesh.name = "HexSphere";
    83.                 _hexMesh.SetVertices(meshTrianglePositions);
    84.                 _hexMesh.SetNormals(meshTriangleNormals);
    85.                 _hexMesh.SetTangents(meshTriangleTangents);
    86.                 _hexMesh.SetUVs(0, meshTriangleUVs );
    87.                
    88.                 var meshTriangleColors = new NativeArray<Color32>(triangleCount[0] * 3, Allocator.TempJob);
    89.                 _hexMesh.SetColors(meshTriangleColors);
    90.                 meshTriangleColors.Dispose();
    91.                
    92.                
    93.                 EntityQuery qHexMesh = GetEntityQuery
    94.                 (
    95.                     ComponentType.ReadOnly<TagHexMeshComponent>()
    96.                 );
    97.                 Entity eHexMesh = qHexMesh.GetSingletonEntity();
    98.                 var m = EntityManager.GetSharedComponentData<RenderMesh>(eHexMesh);
    99.                 EntityManager.SetSharedComponentData(eHexMesh, new RenderMesh
    100.                 {
    101.                     material = m.material,
    102.                     mesh = _hexMesh,
    103.                     castShadows = m.castShadows,
    104.                     receiveShadows = true  //m.receiveShadows
    105.                 });
    106.             }
    107.  
    108.             //
    109.             // Cleanup
    110.             meshTrianglePositions.Dispose();
    111.             meshTriangleNormals.Dispose();
    112.             meshTriangleTangents.Dispose();
    113.             meshTriangleUVs.Dispose();
    114.             meshTriangleIndices.Dispose();
    115.             subdivisions.Dispose();
    116.             radius.Dispose();
    117.             vertexCount.Dispose();
    118.             triangleCount.Dispose();
    119.  
    120.             return inputDeps;
    121.         }
    122.  
    123.  
    And looks like its in the RenderMeshRenderV2 but no render, any ideas ?
    Screen Shot 2019-12-25 at 1.54.52 PM.png
     
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    Wait what is VertexAttributeDescriptor and do I need it with the new mesh API ?
     
  5. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You need to set that if you use SetVertexBufferData and that does not seem to be the case by looking at your example.

    If you can post an example project of what you are trying to do I can have a look in the next few days.
     
  6. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    Ive made a mess trying to get this to work, do you have a simple example just creating a triangle. Maybe I can work my way back from that.
     
  7. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    Ok I think I found the problem, mesh.SetTriangels does not take a NativeAttay<int> ... why is this?
     
  8. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You have to use SetIndexBufferData instead when you setting data using native containers.
    Note that before setting index buffer data you must set params for it using
    SetIndexBufferParams.
    If you still unable to do generate the mesh using it I will post a sample code once I'm at the computer.
     
  9. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Here is an example of how you create a mesh using the new Mesh api.
    This example can be leverage to use more properties but right now it only sets vertex position and normals and has only a single submesh.

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Mathematics;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. public class GenerateMeshUtility {
    7.  
    8.   public struct VertexData {
    9.     public float3 Position;
    10.     public float3 Normal;
    11.   }
    12.  
    13.   public struct VertexIndexData {
    14.     public int Value;
    15.     public static implicit operator VertexIndexData(int v) { return new VertexIndexData { Value = v }; }
    16.     public static implicit operator int(VertexIndexData v) { return v.Value; }
    17.   }
    18.  
    19.   public static Mesh CreateMeshFrom(Mesh mesh) {
    20.     var vertexData = new NativeArray<VertexData>(mesh.vertexCount, Allocator.Temp);
    21.     for (var i = 0; i < mesh.vertexCount; ++i)
    22.       vertexData[i] = new VertexData {
    23.         Position = mesh.vertices[i],
    24.         Normal = mesh.normals[i]
    25.       };
    26.  
    27.     var triangles = mesh.triangles;
    28.     var vertexIndexData = new NativeArray<VertexIndexData>(mesh.triangles.Length, Allocator.Temp);
    29.     for (var i = 0; i < triangles.Length; ++i)
    30.       vertexIndexData[i] = triangles[i];
    31.  
    32.     return CreateMesh(vertexData, vertexIndexData);
    33.   }
    34.  
    35.   public static Mesh CreateMesh(NativeArray<VertexData> vertexArray, NativeArray<VertexIndexData> vertexIndexArray) {
    36.     var mesh = new Mesh();
    37.  
    38.     // This descriptor must match VertexData structure.
    39.     // Add more data if you need to (eg. colors, tangents, etc)
    40.     var attributeDescritpor = new VertexAttributeDescriptor[] {
    41.         new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3, 0),
    42.         new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3, 0)
    43.       };
    44.  
    45.     var vertexCount = vertexArray.Length;
    46.     // Tell the mesh what format the params will be sent
    47.     mesh.SetVertexBufferParams(vertexCount, attributeDescritpor);
    48.     mesh.SetVertexBufferData(vertexArray, 0, 0, vertexCount, 0);
    49.  
    50.     // The format must match VertexIndexData struct
    51.     // You could supply a native array of ints here.
    52.     // Using a struct instead will let you use it in dynamicBuffers
    53.     mesh.SetIndexBufferParams(vertexIndexArray.Length, IndexFormat.UInt32);
    54.     mesh.SetIndexBufferData(vertexIndexArray, 0, 0, vertexIndexArray.Length);
    55.  
    56.     // This is now static to a single submesh must you can leverage this part to your use case
    57.     mesh.subMeshCount = 1;
    58.     var descr = new SubMeshDescriptor() {
    59.       baseVertex = 0,
    60.       bounds = default,
    61.       indexCount = vertexIndexArray.Length,
    62.       indexStart = 0,
    63.       topology = MeshTopology.Triangles
    64.     };
    65.     mesh.SetSubMesh(0, descr);
    66.  
    67.     // If we want the correct bounds we must do this
    68.     mesh.RecalculateBounds();
    69.     return mesh;
    70.   }
    71. }
    This allows you to create a mesh using the new api from another mesh or create a mesh from vertex data and vertex index data supplies via native arrays.
     
  10. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    thanks heaps for the example. Digging into it now
     
  11. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    Your example was super helpful cant thank you enough. Got the mesh up and running with the exception of UVs, I cant see any obvious reason it not working but when I run Vector2[] t = _hexMesh.uv its all zero for some reason. Tracking the data up it appear that the data loss is at SetVertexBufferData or maybe UV need something special.


    Code (CSharp):
    1.  
    2. public struct VertexData
    3. {
    4.     public float3 Position;
    5.     public float3 Normal;
    6.     public float4 Tangent;
    7.     public float2 UVs;
    8.     public int4 Color;
    9. };
    10.  
    11. ...
    12.  
    13.  
    14. var vertexAttrDescriptor = new VertexAttributeDescriptor[]
    15. {
    16.     new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3, 0),
    17.     new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3,0),
    18.     new VertexAttributeDescriptor(VertexAttribute.Tangent, VertexAttributeFormat.Float32, 4,0),
    19.     new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2,0),
    20.     new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.UInt32, 4,0)
    21. };
    22.  
    23.  
    24. ...
    25.  
    26.  
    27. _hexMesh.SetVertexBufferParams(vertexArray.Length, vertexAttrDescriptor);
    28. _hexMesh.SetVertexBufferData(vertexArray, 0, 0, vertexArray.Length, 0);
    29.  
    30. _hexMesh.SetIndexBufferParams(vertexIndexData.Length, IndexFormat.UInt32);
    31. _hexMesh.SetIndexBufferData(vertexIndexData, 0, 0,vertexIndexData.Length);
    32.  
    33. _hexMesh.subMeshCount = 1;
    34. var descr = new SubMeshDescriptor() {
    35.     baseVertex = 0,
    36.     bounds = default,
    37.     indexCount = vertexArray.Length,
    38.     indexStart = 0,
    39.     topology = MeshTopology.Triangles
    40. };
    41. _hexMesh.SetSubMesh(0, descr);
    42. _hexMesh.RecalculateBounds();
    43.  
    44. // BUT
    45. Vector2[] t = _hexMesh.uv; // all zero for some reason
    46.  
     
  12. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    How are you creating your vertex array? I don't have any problem with UVs
     
  13. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Seems like you have to define Color before the UV. Actually the order is this:
    Quoted from Unity Docs
     
  14. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    Interesting, thanks again. I’ll give this another try tonight
     
  15. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    Yup, this was the problem , got it all working

     
    keeponshading and GilCat like this.
  16. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,720
    I dont understand the term sub-mesh, is it equivalent to maya mesh shells ?