Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I render a mesh to an image without textures? #333

Closed
WMCh opened this issue Aug 21, 2020 · 3 comments
Closed

How can I render a mesh to an image without textures? #333

WMCh opened this issue Aug 21, 2020 · 3 comments
Assignees
Labels
how to How to use PyTorch3D in my project

Comments

@WMCh
Copy link

WMCh commented Aug 21, 2020

Hi, I tried to render meshes without textures using the codes in the tutorials, but failed because the shader samples the textures of meshes by default.

Traceback (most recent call last):
  File "e:/code/python/Test/render_textured_meshes.py", line 81, in <module>
    images = renderer(mesh)
  File "D:\programs\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "D:\programs\Anaconda3\lib\site-packages\pytorch3d-0.2.0-py3.8-win-amd64.egg\pytorch3d\renderer\mesh\renderer.py", line 49, in forward
    images = self.shader(fragments, meshes_world, **kwargs)
  File "D:\programs\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "D:\programs\Anaconda3\lib\site-packages\pytorch3d-0.2.0-py3.8-win-amd64.egg\pytorch3d\renderer\mesh\shader.py", line 108, in forward
    texels = meshes.sample_textures(fragments)
  File "D:\programs\Anaconda3\lib\site-packages\pytorch3d-0.2.0-py3.8-win-amd64.egg\pytorch3d\structures\meshes.py", line 1500, in sample_textures
    raise ValueError("Meshes does not have textures")
ValueError: Meshes does not have textures

In short, how can I simply render a mesh without textures? Or if I modify the source code, will there be any bad effects? Looking forward to you reply, thank you!

@WMCh
Copy link
Author

WMCh commented Aug 21, 2020

I made some modification to the source code to get the results I wanted.

  1. Line 108 in pytorch3d/renderer/mesh/shader.py
texels = None
if meshes.textures is not None:
    texels = meshes.sample_textures(fragments)
  1. Line 83 in pytorch3d/renderer/mesh/shading.py
if texels is not None:
    colors = (ambient + diffuse) * texels + specular
    return colors
else:
    colors = ambient + diffuse + specular
    return colors

and the rendered image turned out to be like below:
test

@gkioxari gkioxari self-assigned this Aug 21, 2020
@gkioxari gkioxari added the how to How to use PyTorch3D in my project label Aug 21, 2020
@gkioxari
Copy link
Contributor

Shaders require textures. Otherwise what are you shading?! This is also shown in the table at the bottom of the notes here

https://github.com/facebookresearch/pytorch3d/blob/master/docs/notes/renderer_getting_started.md

The only shader that doesn't require texture is the SoftSilhouetteShader which returns the silhouette of the object.
If you want to shade your object but it doesn't have textures then you can assign it arbitrary RGB textures, e.g. white (1.0, 1.0, 1.0)

@qiuyu96
Copy link

qiuyu96 commented Apr 17, 2021

Hi, I tried to render meshes without textures using the codes in the tutorials, but failed because the shader samples the textures of meshes by default.

Traceback (most recent call last):
  File "e:/code/python/Test/render_textured_meshes.py", line 81, in <module>
    images = renderer(mesh)
  File "D:\programs\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "D:\programs\Anaconda3\lib\site-packages\pytorch3d-0.2.0-py3.8-win-amd64.egg\pytorch3d\renderer\mesh\renderer.py", line 49, in forward
    images = self.shader(fragments, meshes_world, **kwargs)
  File "D:\programs\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "D:\programs\Anaconda3\lib\site-packages\pytorch3d-0.2.0-py3.8-win-amd64.egg\pytorch3d\renderer\mesh\shader.py", line 108, in forward
    texels = meshes.sample_textures(fragments)
  File "D:\programs\Anaconda3\lib\site-packages\pytorch3d-0.2.0-py3.8-win-amd64.egg\pytorch3d\structures\meshes.py", line 1500, in sample_textures
    raise ValueError("Meshes does not have textures")
ValueError: Meshes does not have textures

In short, how can I simply render a mesh without textures? Or if I modify the source code, will there be any bad effects? Looking forward to you reply, thank you!

I think this url #51 will give you the answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
how to How to use PyTorch3D in my project
Projects
None yet
Development

No branches or pull requests

3 participants