Search Unity

Get UV coordinates

Discussion in 'Physics for ECS' started by SubdivisionCG, May 9, 2020.

  1. SubdivisionCG

    SubdivisionCG

    Joined:
    May 9, 2020
    Posts:
    15
    Hello,

    I am in the process of converting parts of our game to ECS. For this I need to sample the texture of an object, and I in regular MonoBehaviors I was using:
    Texture2D.GetPixelBilinear()

    The GetPixelBilinear() requires UV coordinates, and I get these from a raycast against the object's mesh collider.

    I've managed to convert the raycasting into an ECS version, however the ECS version of RaycastHit does not contain .textureCoord like the MonoBehavior version. I am quite stuck now, is there any workaround, what's my best option?
     
    PhilSA likes this.
  2. auteremiikka

    auteremiikka

    Joined:
    Jul 17, 2015
    Posts:
    4
    Shameless bump, did you or anyone else find a workaround for this?
     
    cultureulterior likes this.
  3. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Unfortunately, at the minute Unity Physics doesn't have an mapping to the graphical representation once the physical colliders are created.

    Not sure about your bigger use-case but it might be useful to look in the DOTSSample. I don't really know that much about the details but it does add a decal to a hit geometry when you fire.
    The ActiveUpdate.UpdateJob.EnterFiringPhase function saves the hit position from a Unity Physics cast query. This hit position is then passed to the Robot_Weapon_A_Impact_Generic VisualEffect in VFXSystem.OnUpdate.
    Of course this only helps if you can push your logic on to the graphics side and use the world position.
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I'm assuming textureCoord relies on mesh uv's. So the solution here is fairly straight forward it seems even if a bit of extra work.

    On conversion extract the vertices and uvs of the mesh. Map those to the collider so you can on hit iterate the vertices to find the closest one. Simple main thread only version a custom struct containing your float3/float2. Dictionary of collider hashcode key with value being a List of that struct. Job friendly version maybe NativeMultiHashMap.
     
  5. auteremiikka

    auteremiikka

    Joined:
    Jul 17, 2015
    Posts:
    4
    My bigger use-case would be to use the UIToolkit's RenderTexture to create a WorldSpace UI, which I would control using Raycasts. For that to work, I need the textureCoord from the RaycastHit and pass it to some code that simulates a mouse on that position on the UI.
     
  6. auteremiikka

    auteremiikka

    Joined:
    Jul 17, 2015
    Posts:
    4
    I'll take a look at this, thanks!