Skip to content

Commit

Permalink
Add iDate to glsl builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipitis committed Nov 18, 2023
1 parent 855b4fc commit 595d7fa
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions wgpu/utils/shadertoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
float i_time;
float i_time_delta;
int i_frame;
vec4 i_date;
// Shadertoy compatibility, see we can use the same code copied from shadertoy website
Expand All @@ -42,6 +43,7 @@
#define iTimeDelta i_time_delta
#define iMouse i_mouse
#define iFrame i_frame
#define iDate i_date
#define mainImage shader_main
"""
Expand All @@ -56,6 +58,7 @@
float time;
float time_delta;
int frame;
vec4 date;
};
layout(binding = 0) uniform ShadertoyInput input;
Expand All @@ -67,7 +70,7 @@
i_time_delta = input.time_delta;
i_mouse = input.mouse;
i_frame = input.frame;
i_date = input.date;
vec2 uv = vec2(uv.x, 1.0 - uv.y);
vec2 frag_coord = uv * i_resolution.xy;
Expand Down Expand Up @@ -239,8 +242,9 @@ class Shadertoy:
* ``i_frame``: the frame number
* ``i_resolution``: the resolution of the shadertoy
* ``i_mouse``: the mouse position in pixels
* ``i_date``: the current date and time as a vec4 (year, month, day, seconds)
For GLSL, you can also use the aliases ``iTime``, ``iTimeDelta``, ``iFrame``, ``iResolution``, and ``iMouse`` of these built-in variables,
For GLSL, you can also use the aliases ``iTime``, ``iTimeDelta``, ``iFrame``, ``iResolution``, ``iMouse`` and ``iDate`` of these built-in variables,
the entry point function also has an alias ``mainImage``, so you can use the shader code copied from shadertoy website without making any changes.
"""

Expand All @@ -255,6 +259,7 @@ def __init__(self, shader_code, resolution=(800, 450), offscreen=False) -> None:
("time", "f", 1),
("time_delta", "f", 1),
("frame", "I", 1),
("date", "f", 4),
)

self._shader_code = shader_code
Expand Down Expand Up @@ -428,10 +433,22 @@ def _update(self):

if not hasattr(self, "_frame"):
self._frame = 0

time_struct = time.localtime()
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,
)

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


def _draw_frame(self):
# Update uniform buffer
self._update()
Expand Down

0 comments on commit 595d7fa

Please sign in to comment.