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 more uniforms to .snapshot() #37

Merged
merged 12 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
- name: Install llvmpipe and lavapipe for offscreen canvas
run: |
sudo apt-get update -y -qq
sudo apt install -y libegl1-mesa libgl1-mesa-dri libxcb-xfixes0-dev mesa-vulkan-drivers
sudo apt install -y libegl1-mesa-dev libgl1-mesa-dri libxcb-xfixes0-dev mesa-vulkan-drivers
- name: Install dev dependencies
run: |
python -m pip install --upgrade pip
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update -y -qq
sudo apt install -y libegl1-mesa libgl1-mesa-dri libxcb-xfixes0-dev mesa-vulkan-drivers
sudo apt install -y libegl1-mesa-dev libgl1-mesa-dri libxcb-xfixes0-dev mesa-vulkan-drivers
- name: Install dev dependencies
run: |
python -m pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/screenshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
sudo apt-get update -y -qq
sudo add-apt-repository ppa:oibaf/graphics-drivers -y
sudo apt-get update -y -qq
sudo apt install -y libegl1-mesa libgl1-mesa-dri libxcb-xfixes0-dev mesa-vulkan-drivers
sudo apt install -y libegl1-mesa-dev libgl1-mesa-dri libxcb-xfixes0-dev mesa-vulkan-drivers
- name: Install dev dependencies
run: |
python -m pip install --upgrade pip
Expand Down
6 changes: 3 additions & 3 deletions examples/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def unload_module():
imageio.imwrite(screenshot_path, img)

# if a reference screenshot exists, assert it is equal
assert (
screenshot_path.exists()
), "found # test_example = true but no reference screenshot available"
assert screenshot_path.exists(), (
"found # test_example = true but no reference screenshot available"
)
stored_img = imageio.imread(screenshot_path)
# assert similarity
is_similar = np.allclose(img, stored_img, atol=1)
Expand Down
35 changes: 27 additions & 8 deletions wgpu_shadertoy/shadertoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,23 +613,27 @@ def _update(self):
if not hasattr(self, "_frame"):
self._frame = 0

time_struct = time.localtime()
current_time = time.time()
time_struct = time.localtime(current_time)
fractional_seconds = current_time % 1

self._uniform_data["date"] = (
float(time_struct.tm_year),
float(time_struct.tm_mon - 1),
float(time_struct.tm_mday),
time_struct.tm_hour * 3600
+ time_struct.tm_min * 60
+ time_struct.tm_sec
+ now % 1,
+ fractional_seconds,
)

self._uniform_data["frame"] = self._frame
self._frame += 1

def _draw_frame(self):
# Update uniform buffer
self._update()
if not self._offscreen:
self._update()
self._device.queue.write_buffer(
self._uniform_buffer,
0,
Expand Down Expand Up @@ -669,26 +673,41 @@ def show(self):
else:
run()

def snapshot(self, time_float: float = 0.0, mouse_pos: tuple = (0, 0, 0, 0)):
def snapshot(
self,
time_float: float = 0.0,
time_delta: float = 0.167,
frame: int = 0,
framerate: int = 60.0,
mouse_pos: tuple = (0.0, 0.0, 0.0, 0.0),
date: tuple = (0.0, 0.0, 0.0, 0.0),
) -> memoryview:
"""
Returns an image of the specified time. (Only available when ``offscreen=True``)
Returns an image of the specified time. (Only available when ``offscreen=True``), you can set the uniforms manually via the parameters.

Parameters:
time_float (float): The time to snapshot. It essentially sets ``i_time`` to a specific number. (Default is 0.0)
mouse_pos (tuple): The mouse position in pixels in the snapshot. It essentially sets ``i_mouse`` to a 4-tuple. (Default is (0,0,0,0))
time_delta (float): Value for ``i_time_delta`` uniform. (Default is 0.167)
frame (int): The frame number for ``i_frame`` uniform. (Default is 0)
framerate (float): The framerate number for ``i_framerate``, only changes the value passed to the uniform. (Default is 60.0)
mouse_pos (tuple(float)): The mouse position in pixels in the snapshot. It essentially sets ``i_mouse`` to a 4-tuple. (Default is (0.0,0.0,0.0,0.0))
date (tuple(float)): The 4-tuple for ``i_date`` in year, months, day, seconds. (Default is (0.0,0.0,0.0,0.0))
Returns:
frame (memoryview): snapshot with transparency. This object can be converted to a numpy array (without copying data)
using ``np.asarray(arr)``
"""
if not self._offscreen:
raise NotImplementedError("Snapshot is only available in offscreen mode.")

if hasattr(self, "_last_time"):
self.__delattr__("_last_time")
self._uniform_data["time"] = time_float
self._uniform_data["time_delta"] = time_delta
self._uniform_data["frame"] = frame
self._uniform_data["framerate"] = framerate
self._uniform_data["mouse"] = mouse_pos
self._uniform_data["date"] = date
self._canvas.request_draw(self._draw_frame)
frame = self._canvas.draw()

return frame


Expand Down
Loading