Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Problem when using Reflect Prefab from Package Manager

Discussion in 'Unity Reflect' started by odehit, Aug 5, 2020.

  1. odehit

    odehit

    Joined:
    Aug 5, 2020
    Posts:
    6
    Hi, I have just started to look into customizing the Unity Reflect Viewer. I imported the package from the Package Manager and added the Reflect prefab to my project (Unity 2019.2.13f1)

    The viewer seems to launch fine but when I try to open a model exported from Revit, I get a lot of errors from a script. I tracked it down and it seems to be a problem with the List length for the triangles definition of the meshes.

    See the code in SyncMeshImporter.cs
    Code (CSharp):
    1.             for (var i = 0; i < subMeshCount; ++i)
    2.             {
    3.                 count = syncMesh.SubMeshes[i].Triangles.Count;
    4.                 // must expand here since triangle counts may vary
    5.                 #if USE_ARRAY_SLICES
    6.                 if (_intBuffer == null || _intBuffer.Length < count)
    7.                     _intBuffer = new int[count];
    8.                 #else
    9.                 if (_intBuffer == null || _intBuffer.Count < count)
    10.                     _intBuffer = new List<int>(count);
    11.                 #endif
    12.                
    13.                 // triangles
    14.                 #if USE_ARRAY_SLICES
    15.                 index = 0;
    16.                 foreach (var triangleIndex in syncMesh.SubMeshes[i].Triangles)
    17.                     _intBuffer[index++] = triangleIndex;
    18.                 mesh.SetTriangles(_intBuffer, 0, count, i);
    19.                 #else
    20.                 foreach (var triangleIndex in syncMesh.SubMeshes[i].Triangles)
    21.                     _intBuffer.Add(triangleIndex);
    22.                 mesh.SetTriangles(_intBuffer, i);
    23.                 #endif
    24.             }
    It looks like the USE_ARRAY_SLICES is not defined at the moment, and the code defaults to using Lists.
    Changing this:
    Code (CSharp):
    1.                 #else
    2.                 if (_intBuffer == null || _intBuffer.Count < count)
    3.                     _intBuffer = new List<int>(count);
    4.                 #endif
    into this:
    Code (CSharp):
    1.                 #else
    2.                 if (true)
    3.                     _intBuffer = new List<int>(count);
    4.                 #endif
    seems to do the trick for now.

    Can someone please tell me if this is indeed a bug, or am I doing something wrong?
     
  2. unity_Bionyx

    unity_Bionyx

    Unity Technologies

    Joined:
    Nov 1, 2019
    Posts:
    1
    This is indeed a bug!

    I would recommend upgrading to at least Unity 2019.3 to be able to use array slices in Mesh.SetVertices and Mesh.SetTriangles (see https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Mesh.SetVertices.html). This change was done to reduce garbage collection during the mesh import.

    If upgrading is not possible, then I would recommend using this code instead:

    Code (CSharp):
    1. if (_intBuffer == null)
    2.     _intBuffer = new List<int>(count);
    3. else
    4.     _intBuffer.Clear();
     
  3. odehit

    odehit

    Joined:
    Aug 5, 2020
    Posts:
    6
    Thank you for the response! I see the benefit of your solution as it avoids the GC.