Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to add MeshCollider on every GameObject #137

Closed
ketourneau opened this issue Feb 16, 2021 · 8 comments
Closed

Best way to add MeshCollider on every GameObject #137

ketourneau opened this issue Feb 16, 2021 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@ketourneau
Copy link

ketourneau commented Feb 16, 2021

Hi,

We try to add mesh colliders on every GameObject in CustomInstantiator but we got some errors in unity :

public class CustomGameObjectInstantiator : GameObjectInstantiator {
  public CustomGameObjectInstantiator (Transform parent) : base (parent) { }
  
  public override void AddPrimitive (uint nodeIndex, string meshName, Mesh mesh, Material[] materials, int[] joints = null, bool first = true) {
      base.AddPrimitive (nodeIndex, meshName, mesh, materials, joints, first);
      nodes[nodeIndex].AddComponent<MeshCollider> ();
  }
}
[Physics.PhysX] cleaning the mesh failed
UnityEngine.MeshCollider:set_sharedMesh(Mesh)
CustomGameObjectInstantiator:AddPrimitive(UInt32, String, Mesh, Material[], Int32[], Boolean) (at Assets/Scripts/CustomGameObjectInstantiatorcs:12)
[Physics.PhysX] TriangleMesh::loadFromDesc: desc.isValid() failed!
UnityEngine.GameObject:AddComponent()
CustomGameObjectInstantiator:AddPrimitive(UInt32, String, Mesh, Material[], Int32[], Boolean) (at Assets/Scripts/CustomGameObjectInstantiator.cs:12)

How to avoid errors ?

@atteneder
Copy link
Owner

Hi @Blocovin ,

Thanks for reporting.

I'd have to try, but maybe it is related to the mesh flags (see this comment)

I'll try to test soon

@atteneder atteneder self-assigned this Feb 17, 2021
@atteneder atteneder added the bug Something isn't working label Feb 17, 2021
@ketourneau
Copy link
Author

ketourneau commented Feb 20, 2021

Thank you for your answer !
I forgot to specify that we use draco, so PrimitiveDracoCreateContext.csis called and there is no MeshUpdateFlags.

@atteneder
Copy link
Owner

Thank you for your answer !
I forgot to specify that we use draco, so PrimitiveDracoCreateContext.csis called and there is no MeshUpdateFlags.

Good point!
I've created a corresponding issue. The next DracoUnity version will support this.

@ketourneau
Copy link
Author

ketourneau commented Apr 8, 2021

I fork UnityDraco and I try to set defaultMeshUpdateFlags = MeshUpdateFlags.Default and
defaultMeshUpdateFlags = MeshUpdateFlags.DontNotifyMeshUsers | MeshUpdateFlags.DontResetBoneBounds | MeshUpdateFlags.DontValidateIndices; in DracoMeshLoader.cs

I always got unity errors when I add collider on draco model at runtime

[Physics.PhysX] TriangleMesh::loadFromDesc: desc.isValid() failed! UnityEngine.GameObject:AddComponent<UnityEngine.MeshCollider> ()

[Physics.PhysX] TriangleMesh::loadFromDesc: desc.isValid() failed!

Do you have another suggestion to fix it ?

@atteneder
Copy link
Owner

Erm....no, nothing obvious. I'll try to look into it next week.

@ketourneau
Copy link
Author

I found the solution, in Unity 2021.2.X the error is more explicit (https://issuetracker.unity3d.com/issues/physics-dot-raycast-throws-errors-when-used-on-a-navmesh).
So in Unity 2021.2.0a12 we got error : mesh must have at least three distinct vertices to be a valid collision mesh

It's a problem in our mesh, not in gltfast 😆
So I fix it with mesh.vertices.Distinct ().Count () >= 3 check.

using System.Linq;

public class CustomGameObjectInstantiator : GameObjectInstantiator {
  public CustomGameObjectInstantiator (Transform parent) : base (parent) { }
  
  public override void AddPrimitive (uint nodeIndex, string meshName, Mesh mesh, Material[] materials, int[] joints = null, bool first = true) {
      base.AddPrimitive (nodeIndex, meshName, mesh, materials, joints, first);
      if (mesh.vertices.Distinct ().Count () >= 3) {
        nodes[nodeIndex].AddComponent<MeshCollider> ();
      }
  }
}

@atteneder
Copy link
Owner

@Blocovin Great you found a solution!

My suspicion is that it isn't performant though (fetching Mesh.vertices and doing Distinct). Also LINQ increases build size drastically in my experience.

I'd try to avoid this checks in production code.

regards,
Andreas

@ketourneau
Copy link
Author

ketourneau commented Apr 9, 2021

@atteneder Indeed you are right it is not performant.
I use mesh.bounds.size to check minimal requirement, it is much more performant.

3D models are send by our customer, so unfortunately we must add this check in unity for the moment.

We will see if we can clean the mesh from our pipeline.

Thank you very much for your help and advice !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants