Search Unity

Bug (Case 1325061) 2020.3.0f1 ExportPhysicsSystem throws Invalid Operation

Discussion in 'DOTS Physics' started by MNNoxMortem, Mar 21, 2021.

  1. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    685
    Code (CSharp):
    1. [Exception] InvalidOperationException: The BlobAssetReference is null.
    2. BlobAssetReferenceData.ValidateNotNull() at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/Blobs.cs:275
    3. 273:   {
    4. 274:       if(m_Ptr == null)
    5. -->275:           throw new InvalidOperationException("The BlobAssetReference is null.");
    6. 277:       ValidateNonBurst();
    7.  
    8. Unity.Entities.BlobAssetReference`1[T].get_Value() at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/Blobs.cs:365
    9. 363:   get
    10. 364:   {
    11. -->365:       m_data.ValidateNotNull();
    12. 366:       return ref UnsafeUtility.AsRef<T>(m_data.m_Ptr);
    13. 367:   }
    14.  
    15. ExportPhysicsWorld.CheckColliderFilterIntegrity() at Library/PackageCache/com.unity.physics@0.6.0-preview.3/Unity.Physics/ECS/Base/Systems/ExportPhysicsWorld.cs:307
    16. 305:   {
    17. 306:       var collider = colliders[i];
    18. -->307:       if (collider.Value.Value.Type == ColliderType.Compound)
    19. 308:       {
    20. 309:           unsafe
    21.  
    22. ExportPhysicsWorld+CheckColliderIntegrity.Execute() at Library/PackageCache/com.unity.physics@0.6.0-preview.3/Unity.Physics/ECS/Base/Systems/ExportPhysicsWorld.cs:271
    23. 270:           var colliders = batchInChunk.GetNativeArray(PhysicsColliderType);
    24. -->271:           CheckColliderFilterIntegrity(colliders);
    25. 272:       }
    26. 273:   }
    27.  
    28. Unity.Entities.JobEntityBatchExtensions+JobEntityBatchProducer`1[T].ExecuteInternal() at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/IJobEntityBatch.cs:571
    29. 569:           var batch = new ArchetypeChunk(chunk, chunks.EntityComponentStore);
    30. 570:           Assert.AreNotEqual(0, batch.Count);
    31. -->571:           jobWrapper.JobData.Execute(batch, batchIndex);
    32. 572:       }
    33. 573:   }
    34.  
    35. Unity.Entities.JobEntityBatchExtensions+JobEntityBatchProducer`1[T].Execute() at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/IJobEntityBatch.cs:513
    36. 511:       int jobIndex)
    37. 512:   {
    38. -->513:       ExecuteInternal(ref jobWrapper, bufferRangePatchData, ref ranges, jobIndex);
    39. 514:   }
    Updates some packages and Unity to Unity 2020.3.0f1
    upload_2021-3-21_21-1-33.png
    and now the I receive this Exception for Entities created from Archetype

    Code (CSharp):
    1.             archetype = em.CreateArchetype(
    2.                 // TRS
    3.                 typeof(LocalToWorld),
    4.                 typeof(Translation),
    5.                 typeof(Rotation),
    6.                 typeof(Scale),
    7.                 // Rendering
    8.                 typeof(RenderMesh),
    9.                 typeof(RenderBounds)
    10.                 // Physics
    11.                  ,typeof(PhysicsCollider)
    12. );
    13.  
    if no valid PhysicsCollider is set before ExportPhysicsSystem.

    Example:
    Code (CSharp):
    1. var Entities = new NativeArray<Entity>(count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
    2.  
    3. var archetype = Ecs.EntityManager.CreateArchetype(
    4.                 // TRS
    5.                 typeof(LocalToWorld),
    6.                 typeof(Translation),
    7.                 typeof(Rotation),
    8.                 typeof(Scale),
    9.                 // Rendering
    10.                 typeof(RenderMesh),
    11.                 typeof(RenderBounds),
    12.                 // Physics
    13.                 // BUG: If we create the archetype with this component it will cause the bug
    14.                 // typeof(PhysicsCollider));
    15.             Ecs.EntityManager.CreateEntity(archetype, Entities);
    I am not sure if this is working as intended or a bug, but my understanding was always, that it is valid to create entities with PhysicsCollider before setting them, as there is no guarantee that entity creation and entity component assignment happens atomically before ExportPhysicsSystem.
     
    Last edited: Mar 21, 2021
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Yes, it should be fine to create entities with PhysicsCollider and then assign colliders, but it needs to be done before BuildPhysicsWorld.OnUpdate() starts.

    As for package versions, I'd advise getting the ones listed in UnityPhysics package.json. For 0.6.0-preview.3 they are:
    "com.unity.burst": "1.4.1",
    "com.unity.collections": "0.15.0-preview.21",
    "com.unity.entities": "0.17.0-preview.41",
    "com.unity.jobs": "0.8.0-preview.23",
    "com.unity.mathematics": "1.2.1",
    "com.unity.test-framework": "1.1.11",
    "com.unity.test-framework.performance": "2.3.1-preview",
    "com.unity.modules.imgui": "1.0.0",
    "com.unity.modules.jsonserialize": "1.0.0"

    If you're still hitting some problem, it'd be good to try and reproduce it on one of the scenes in UnityPhysicsSamples or share the minimal project with repro.
     
    MNNoxMortem likes this.
  3. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    685
    @milos85miki, thanks. The idea to use the UnityPhysicsSample to reproduce it is actually a good one. I have reproduced it there and send a bug report!

    Edit: If I immediately after creating the entities, loop over them and set the PhysicsCollider to a BoxCollider, then the error goes away. Is there any guarantee that the BuildPhysicsWorld system runs after MonoBehaviour.Update?

    Code (CSharp):
    1.  
    2. archetype = World.DefaultGameObjectInjectionWorld.EntityManager.CreateArchetype(
    3. // TRS
    4. typeof(LocalToWorld),
    5. typeof(Translation),
    6. typeof(Rotation),
    7. typeof(Scale),
    8. // Rendering
    9. typeof(RenderMesh),
    10. typeof(RenderBounds)
    11. // Physics
    12. ,typeof(PhysicsCollider)
    13. );
    14.   em.CreateEntity(archetype, array);
    15.         var collider = BoxCollider.Create(new BoxGeometry {
    16.                                                               Center      = float3.zero,
    17.                                                               Orientation = quaternion.identity,
    18.                                                               Size        = new float3(1, 1, 1)
    19.                                                           });
    20. // Without this, the error is thrown, as soon as the PhysicsCollider component is initialized it goes away.
    21.         foreach (var entity in array)
    22.         {
    23.             em.SetComponentData(entity, new PhysicsCollider {
    24.                                                                 Value =collider
    25.                                                             });
    26.         }
     
    Last edited: Mar 28, 2021
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    BuildPhysicsWorld requires all colliders to be set properly, otherwise physics won't work.
    I don't think there are any guarantees for MonoBehaviour.Update and ECS systems execution order, but pinging @petarmHavok and @steveeHavok for confirmation.
     
  5. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    No, there is no guarantee.
     
    MNNoxMortem likes this.
  6. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    685
    @petarmHavok so far understood. How do I create an entity from an archetype that has a PhysicsCollider? If there is no way to create an entity from such an archetype and then later set the value, I would need to remove the PhysicsCollider from the archetype, which would mean that I have to later manually set the PhysicsCollider.Value and that would mean I need to move the Entity one by one, which completly defeats the purpose of pre-creation that archetype?
     
  7. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Of course you can create an Entity from an Archetype with PhysicsCollider, but as with many other components (like Translation), you need to set a proper value before BuildPhysicsWorld.OnUpdate() starts. BuildPhysicsWorld.AddInputDependencyToComplete(jobHandle) is very useful for ensuring that your job completes on time.
    For example, you can see how PeriodicallySpawnExplosionsSystem does it in UnityPhysicsSamples/Assets/Demos/5. Modify/5g. Change Collider Filter.unity sample scene.
     
    MNNoxMortem likes this.
unityunity