Search Unity

Bug BuiltinMaterialPropertyUnity_MatrixPreviousM cannot be accessed

Discussion in 'Graphics for ECS' started by argibaltzi, Jan 7, 2023.

  1. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Hello

    I have a problem with 2 meshes (1 has physics, other has not) and i enable and disable each one them as i need them.

    The problem is when i switch, i need to copy all matrix and state from one to another but i get a VISUAL GLITCH for 1 frame and i think its caused from motion vectors or some shader stuff.

    The only thing i am not able to copy now is the BuiltinMaterialPropertyUnity_MatrixPreviousM data....
    that struct is internal and i can't access it

    What am i supposed to do for such a case? Can that problem be fixed in another way?
    Thanks!
     
  2. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    351
    One way to fix this problem is to access the LocalToWorld component of these kinds of Entities using a read/write handle. This marks the LocalToWorld as "updated", which will then cause the Entities Graphics package to automatically update the previous matrix.
     
  3. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Can you elaborate how exactly i would do that?
    I am using straight get/set component at the moment

    Code (CSharp):
    1. frameCmd.SetComponent<LocalToWorld>(pairs[i].Ragdoll, EntityManager.GetComponentData<LocalToWorld>(pairs[i].NonRagdoll));
    2. frameCmd.SetComponent<WorldTransform>(pairs[i].Ragdoll, EntityManager.GetComponentData<WorldTransform>(pairs[i].NonRagdoll));
    3.  
     
  4. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    351
    Setting the LocalToWorld transform like that should be OK. When is the 'frameCmd' executed? It should be executed before EntitiesGraphicsSystem for things to work properly.
     
  5. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Its done immediately, after this piece of code runs.
    I am not sure what else could be wrong really

    I copy the matrix from object 2 to 1,
    enable object 1,
    disable object 2

    There is still 1 frame glitch
    the disabled object 2 is in another location previously because its not getting updated.

    So copying matrix information from object 1 should put it where it should be. I wonder if the physics system causes the glitch
     
  6. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    I have a lot of other problems with frame 1, when i load objects the first frame they are blurred. It seems to be blending 2 matrix states (one where its now, and another somewhere else, maybe identity)

    How are these problems fixed???
     
  7. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    You would need to add EntityQueryOptions.IncludeDisabledEntities to your EntityQuery in MatrixPreviousSystem for that to work correctly. Otherwise you have one frame where the entity was just enabled and the current LocalToWorld is updated but the previous matrix is still whatever it was when the entity was disabled, and won't be updated until right after the matrix was uploaded to the GPU.
     
    JussiKnuuttila likes this.
  8. azmi_unity

    azmi_unity

    Joined:
    Dec 13, 2020
    Posts:
    62
    I'm having the same problem. It looks like BuiltinMaterialPropertyUnity_MatrixPreviousM is only updated after it gets uploaded to GPU.

    My workaround is to set ForceNoMotion for the instantiated object for that first frame, and resetting it back afterwards.