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

Add option to use flat lighting in pyrender.Viewer #211

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,16 @@ The available keyboard commands are as follows:
* `c`: Toggles backface culling.
* `f`: Toggles fullscreen mode.
* `h`: Toggles shadow rendering.
* `i`: Toggles axis display mode (no axes, world axis, mesh axes, all axes).
* `l`: Toggles lighting mode (scene lighting, Raymond lighting, or direct lighting).
* `i`: Cycles axis display mode (no axes, world axis, mesh axes, all axes).
* `l`: Cycles lighting mode (scene lighting, Raymond lighting, or direct lighting).
* `L`: Uses flat lighting mode.
* `m`: Toggles face normal visualization.
* `n`: Toggles vertex normal visualization.
* `o`: Toggles orthographic camera mode.
* `q`: Quits the viewer.
* `r`: Starts recording a GIF, and pressing again stops recording and opens a file dialog.
* `s`: Opens a file dialog to save the current view as an image.
* `w`: Toggles wireframe mode (scene default, flip wireframes, all wireframe, or all solid).
* `w`: Cycles wireframe mode (scene default, flip wireframes, all wireframe, or all solid).
* `z`: Resets the camera to the default view.

As a note, displaying shadows significantly slows down rendering, so if you're
Expand Down
32 changes: 27 additions & 5 deletions pyrender/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,19 @@ class Viewer(pyglet.window.Window):
- ``c``: Toggles backface culling.
- ``f``: Toggles fullscreen mode.
- ``h``: Toggles shadow rendering.
- ``i``: Toggles axis display mode
- ``i``: Cycles axis display mode
(no axes, world axis, mesh axes, all axes).
- ``l``: Toggles lighting mode
- ``l``: Cycles lighting mode
(scene lighting, Raymond lighting, or direct lighting).
- ``L``: Uses flat lighting mode.
- ``m``: Toggles face normal visualization.
- ``n``: Toggles vertex normal visualization.
- ``o``: Toggles orthographic mode.
- ``q``: Quits the viewer.
- ``r``: Starts recording a GIF, and pressing again stops recording
and opens a file dialog.
- ``s``: Opens a file dialog to save the current view as an image.
- ``w``: Toggles wireframe mode
- ``w``: Cycles wireframe mode
(scene default, flip wireframes, all wireframe, or all solid).
- ``z``: Resets the camera to the initial view.

Expand All @@ -126,6 +127,8 @@ class Viewer(pyglet.window.Window):
blue lines. Defaults to `False`.
- ``cull_faces``: `bool`, If `True`, backfaces will be culled.
Defaults to `True`.
- ``flat``: `bool`, If `True`, render the color buffer flat,
with no lighting computations. Defaults to `True`.
- ``point_size`` : float, The point size in pixels. Defaults to 1px.

Note
Expand Down Expand Up @@ -198,6 +201,7 @@ def __init__(self, scene, viewport_size=None,
'face_normals': False,
'cull_faces': True,
'point_size': 1.0,
'flat': False,
}
self._default_viewer_flags = {
'mouse_pressed': False,
Expand Down Expand Up @@ -746,9 +750,25 @@ def on_key_press(self, symbol, modifiers):
self._set_axes(True, False)
self._message_text = 'World Axis On'

# L toggles the lighting mode
# L cycles the lighting mode
elif symbol == pyglet.window.key.L:
if self.viewer_flags['use_raymond_lighting']:
# SHIFT+L sets flat lighting
if modifiers & pyglet.window.key.MOD_SHIFT:
self.render_flags['flat'] = True
self._message_text = 'Flat Lighting'

# L unsets flat lighting if set
elif self.render_flags['flat']:
self.render_flags['flat'] = False
if self.viewer_flags['use_raymond_lighting']:
self._message_text = 'Raymond Lighting'
elif self.viewer_flags['use_direct_lighting']:
self._message_text = 'Direct Lighting'
else:
self._message_text = 'Default Lighting'

# otherwise cycles lighting mode
elif self.viewer_flags['use_raymond_lighting']:
self.viewer_flags['use_raymond_lighting'] = False
self.viewer_flags['use_direct_lighting'] = True
self._message_text = 'Direct Lighting'
Expand Down Expand Up @@ -985,6 +1005,8 @@ def _render(self):
flags |= RenderFlags.FACE_NORMALS
if not self.render_flags['cull_faces']:
flags |= RenderFlags.SKIP_CULL_FACES
if self.render_flags['flat']:
flags |= RenderFlags.FLAT

self._renderer.render(self.scene, flags)

Expand Down