RobotLocomotion / drake

Model-based design and verification for robotics.
https://drake.mit.edu
Other
3.32k stars 1.26k forks source link

VTK capsule doesn't have the texture coordinate defined #18296

Closed zachfang closed 11 months ago

zachfang commented 1 year ago

What happened?

This issue was discovered in the RenderRpc project when we wanted to set up an example scene to test the RenderEngine rendering. Due to the lack of texture coordinate, the capsule is not properly textured given a texture map either when loading in code or via an SDFormat file.

For example:

  const Capsule capsule(0.05, 0.1);
  scene_graph->RegisterAnchoredGeometry(
      source_id,
      MakeInstance(
          capsule, Vector3d(x, 0.25, 0),
          Material(texture_path, label_value), "texture_capsule"));
    <link name="textured_capsule">
      <pose>0.25 0.25 0 0 0 0</pose>
      <visual name="textured_capsule_visual">
        <geometry>
          <capsule>
            <radius>0.05</radius>
            <length>0.1</length>
          </capsule>
        </geometry>
        <material>
          <drake:diffuse_map>4_color_texture.png</drake:diffuse_map>
        </material>
      </visual>
    </link>

Technically, the capsule on the right should have the same texture as other primitives, but the default white color is rendered instead.

color_000_no_old_lighting_vtk

Version

No response

What operating system are you using?

Ubuntu 20.04

What installation option are you using?

No response

Relevant log output

No response

jwnimmer-tri commented 1 year ago

Do we know yet where in the stack this problem happens? VTK's GLTF exporter?

zachfang commented 1 year ago

I haven't dug in too much as I only treat it as a known issue during the RenderRpc development, but @SeanCurtis-TRI might have more insight. I can try to fix it after my last RenderRpc push.

SeanCurtis-TRI commented 1 year ago

Which render engine produced the image?

Ultimately, this is a per-render engine issue. Each one is obliged to provide textures coordinates for the primitives. The capsule was added without. I presume VTK should be able to activate them relatively easily. They'll have to be done by handle in GL. For a rpc render engine, they all have the unique responsibility to define what it means to "render a capsule".

zachfang commented 1 year ago

The capsule is not properly textured from both RenderEngineVtk and RenderEngineGltfClient as they use VTK under the hood. OK, I can look into fixing the VTK part first.

SeanCurtis-TRI commented 1 year ago

It looks like you're in trouble.

vtkCapsuleSource doesn't produce "TCoord" data in the polygon data. You can contrast this with what vtkCubeSource does. It seems like this is a change/patch to VTK in order to get texture coordinates on the capsule.

bhaq-tri commented 1 year ago

@SeanCurtis-TRI, What is the best way to implement the change? Can we patch a fix and use it within Drake? Or should we ping VTK with issue and request them to fix?

SeanCurtis-TRI commented 1 year ago

I strongly suspect that you can patch the CreateVtkCapsule() and post-hoc add UV coordinates while we wait for VTK updates and our ability to consume those changes. I haven't looked into the specific details, but it seems incredibly that we can't shove UV coords into that data structure after the fact.

zachfang commented 1 year ago

FYI, @svenevs will provide some updates on how to patch it in VTK and how to incorporate the fix into Drake once he nails down the details.

svenevs commented 1 year ago

Sorry for the delay here, the most "straightforward" path to getting this would be to create a new class and contribute it to VTK by submitting a merge request to the VTK GitLab. GitLab contributions are similar to GitHub (you will be able to sign into the kitware gitlab using your GitHub credentials if you prefer), you fork the repository, push a branch and open up a merge request. CC me (@svenevs there too).

My advice would be to just get the texture coordinates working in VTK. I will be able to help make that available in the drake-vtk build if that is the route taken (basically, once the VTK patch is ready you can hand that off to me and I'll create new binaries after adding the patch to tools/workspace/vtk/). I hope the above helps get you unstuck, the math behind the texture coordinates should be the hardest part.

SeanCurtis-TRI commented 1 year ago

Whoooooo boy! That's going to be a lot to digest. I'll have to come back to that...

The good news is that the math for the texture coordinates is the easy part. Ping me when we get around to it. :) What I might do is add the texture coordinates to the GL renderer and that can serve as a basis for the math. And then everything else will be working with VTK.

SeanCurtis-TRI commented 1 year ago

Also another solution is to write the vtkTexturedCapsuleSource completely in Drake. When we first introduced the capsule, we vendored it from a third party and simply brought it into drake until it was eventually transported to VTK. Doing it in Drake allows us a Drake developer to use the Drake ecosystem to test for correctness and exercise and shortens the path to having the behavior available in Drake. Once it gets pushed back to vtk, we can then update Drake's internals to delete our bespoke version and use that contained in the library.

svenevs commented 1 year ago

Also another solution is to write the vtkTexturedCapsuleSource completely in Drake.

Yes, this is the best path forward -- good call! I thought you would have to work with the .tar.gz build to do it in drake first, but this will be way better... Add a new type for drake in the desired location, starting with the base implementation of the existing vtkCapsuleSource and just implement RequestData such that it also adds the desired texture coordinates. The rest of VTK should detect and use it as expected.

When something desirable is achieved I can help get it into VTK / the drake VTK build fairly quickly.

bhaq-tri commented 1 year ago

PR open #18547 and Gitlab VTK.

svenevs commented 1 year ago

We've got a VTK dev looking into this next week! There might be a different way to get the texture coordinates in using a different strategy, we will update soon!

jwnimmer-tri commented 1 year ago

@svenevs Sounds great! Please post progress updates and/or questions to this ticket. (Especially if a MR gets opened.)

sankhesh commented 1 year ago

I've been looking into this and am adding the functionality in a more generic form. The idea is to add an API on the vtkCylinderSource to have hemispherical capping with tcoords. This should also make the geometry cleaner. See the images below.

image

image

SeanCurtis-TRI commented 1 year ago

To make sure I understand what I'm seeing there -- that's the wireframe applied as a texture map, right?

sankhesh commented 1 year ago

Yes, rendering cell edges.

BetsyMcPhail commented 1 year ago

VTK MR: https://gitlab.kitware.com/vtk/vtk/-/merge_requests/10531

sankhesh commented 1 year ago

The VTK merge request has been merged.

jwnimmer-tri commented 1 year ago

@SeanCurtis-TRI @zachfang if you like, instead of waiting for the next VTK release, it's plausible you could to cherry-pick the patch into https://github.com/RobotLocomotion/drake/tree/master/tools/workspace/vtk_internal/patches in the meantime, assuming that there aren't any too-difficult merge conflicts.

SeanCurtis-TRI commented 1 year ago

I'll take a look at cherry-picking.

jwnimmer-tri commented 1 year ago

@SeanCurtis-TRI if / when you have any progress, please post a pingback here.

SeanCurtis-TRI commented 1 year ago

Will do.

jwnimmer-tri commented 11 months ago

Plausibly also @zachfang could work on cherry-picking this, if you like.

sankhesh commented 11 months ago

@jwnimmer-tri @SeanCurtis-TRI The change is available in VTK 9.3.0-rc2

jwnimmer-tri commented 11 months ago

We can't upgrade our version until #19965 is fixed, so for now it'll need to be cherry-picking.