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.

Question Custom Collector Filter (Raycast)

Discussion in 'Physics for ECS' started by OrientedPain, Jul 20, 2023.

  1. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    52
    Hello everyone,

    The Physics Queries (Raycast Sample) is working fine until I preferred to change custom physics tag to add more tags and try new filters. The TransparentCustomTag (const) does not change and its value is always 4.

    Code (CSharp):
    1. public struct CustomCollectorFilter : ICollector<RaycastHit>
    2.     {
    3.         public bool EarlyOutOnFirstHit => false;
    4.  
    5.         public float MaxFraction { get; private set; }
    6.  
    7.         public int NumHits { get; private set; }
    8.  
    9.         public RaycastHit ClosestHit;
    10.  
    11.         private CollisionWorld m_World;
    12.  
    13.         private const int TransparentCustomTag = (1 << 1);
    14.  
    15.         public CustomCollectorFilter(CollisionWorld world)
    16.         {
    17.             m_World = world;
    18.  
    19.             MaxFraction = 1.0f;
    20.             ClosestHit = default;
    21.             NumHits = 0;
    22.         }
    23.  
    24.         private static bool IsTransparent(BlobAssetReference<Collider> collider, ColliderKey key)
    25.         {
    26.             bool bIsTransparent = false;
    27.             unsafe
    28.             {
    29.                 // Only Convex Colliders have Materials associated with them. So base on CollisionType
    30.                 // we'll need to cast from the base Collider type, hence, we need the pointer.
    31.                 var c = collider.AsPtr();
    32.                 {
    33.                     var cc = ((ConvexCollider*)c);
    34.  
    35.                     // We also need to check if our Collider is Composite (i.e. has children).
    36.                     // If it is then we grab the actual leaf node hit by the ray.
    37.                     // Checking if our collider is composite
    38.                     if (c->CollisionType != CollisionType.Convex)
    39.                     {
    40.                         // If it is, get the leaf as a Convex Collider
    41.                         c->GetLeaf(key, out ChildCollider child);
    42.                         cc = (ConvexCollider*)child.Collider;
    43.                     }
    44.  
    45.                     // Now we've definitely got a ConvexCollider so can check the Material.
    46.                     bIsTransparent = (cc->Material.CustomTags & TransparentCustomTag ) != 0;
    47.                     Debug.Log($"[{cc->Material.CustomTags}]  [{TransparentCustomTag }]");
    48.                 }
    49.             }
    50.  
    51.             return bIsTransparent ;
    52.         }

    Do I have missunderstanding?
    private const int TransparentCustomTag = (1 << 0); The value should be 1 and Binary code 0000001
    private const int TransparentCustomTag = (1 << 1); The value should be 2 and Binary code 0000010
    private const int TransparentCustomTag = (1 << 2); The value should be 4 and Binary code 0000100
    etc...

    The Unity custom tag list should match binaries like that:

    Binary code: 0 0 0 0 0 0 1 0
    Unity List T : 7 6 5 4 3 2 1 0

    In this example should custom physics tag in Unity number 1 be active.

    cc->Material.CustomTags
    works fine, but
    TransparentCustomTag
    always = 4.

    Thanks.