-
Notifications
You must be signed in to change notification settings - Fork 5
[WIP] add Keyboard input #54
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
Draft
Vipitis
wants to merge
12
commits into
main
Choose a base branch
from
keyboard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d22db95
c&p to try
Vipitis c3e1d39
move events to channel
Vipitis 48e59e3
add api support
Vipitis 42b5162
fix texture orientation
Vipitis f709204
get 2nd row to work
Vipitis 4e1e544
fix missing events
Vipitis 76bdfb9
add keymap
Vipitis 0a516e1
simplify update maybe
Vipitis e881efd
fix upper keys
Vipitis c210559
bump rendercanvas
Vipitis b3dfb5f
use shared keyboard state
Vipitis 9a1be88
reuse event handlers and texture
Vipitis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| from wgpu_shadertoy import Shadertoy, ShadertoyChannelKeyboard | ||
|
|
||
| # shadertoy source: https://www.shadertoy.com/view/lsXGzf by iq CC-BY-NC-SA-3.0 | ||
|
|
||
| # TODO: maybe find a whole keyboard example too. | ||
|
|
||
| shader_code = """ | ||
| // Created by inigo quilez - iq/2013 | ||
|
|
||
|
|
||
| // An example showing how to use the keyboard input. | ||
| // | ||
| // Row 0: contain the current state of the 256 keys. | ||
| // Row 1: contains Keypress. | ||
| // Row 2: contains a toggle for every key. | ||
| // | ||
| // Texel positions correspond to ASCII codes. Press arrow keys to test. | ||
|
|
||
|
|
||
| // See also: | ||
| // | ||
| // Input - Keyboard : https://www.shadertoy.com/view/lsXGzf | ||
| // Input - Microphone : https://www.shadertoy.com/view/llSGDh | ||
| // Input - Mouse : https://www.shadertoy.com/view/Mss3zH | ||
| // Input - Sound : https://www.shadertoy.com/view/Xds3Rr | ||
| // Input - SoundCloud : https://www.shadertoy.com/view/MsdGzn | ||
| // Input - Time : https://www.shadertoy.com/view/lsXGz8 | ||
| // Input - TimeDelta : https://www.shadertoy.com/view/lsKGWV | ||
| // Inout - 3D Texture : https://www.shadertoy.com/view/4llcR4 | ||
|
|
||
|
|
||
| const int KEY_LEFT = 37; | ||
| const int KEY_UP = 38; | ||
| const int KEY_RIGHT = 39; | ||
| const int KEY_DOWN = 40; | ||
|
|
||
| void mainImage( out vec4 fragColor, in vec2 fragCoord ) | ||
| { | ||
| fragColor = vec4(texelFetch(iChannel0, ivec2((fragCoord.xy/iResolution.xy)*vec2(512.0, 3.0)), 0).xyz, 1.0); | ||
| return; // little shortcut for testing I guess. | ||
| vec2 uv = (-iResolution.xy + 2.0*fragCoord) / iResolution.y; | ||
| vec3 col = vec3(0.0); | ||
|
|
||
| // state | ||
| col = mix( col, vec3(1.0,0.0,0.0), | ||
| (1.0-smoothstep(0.3,0.31,length(uv-vec2(-0.75,0.0))))* | ||
| (0.3+0.7*texelFetch( iChannel0, ivec2(KEY_LEFT,0), 0 ).x) ); | ||
|
|
||
| col = mix( col, vec3(1.0,1.0,0.0), | ||
| (1.0-smoothstep(0.3,0.31,length(uv-vec2(0.0,0.5))))* | ||
| (0.3+0.7*texelFetch( iChannel0, ivec2(KEY_UP,0), 0 ).x)); | ||
|
|
||
| col = mix( col, vec3(0.0,1.0,0.0), | ||
| (1.0-smoothstep(0.3,0.31,length(uv-vec2(0.75,0.0))))* | ||
| (0.3+0.7*texelFetch( iChannel0, ivec2(KEY_RIGHT,0), 0 ).x)); | ||
|
|
||
| col = mix( col, vec3(0.0,0.0,1.0), | ||
| (1.0-smoothstep(0.3,0.31,length(uv-vec2(0.0,-0.5))))* | ||
| (0.3+0.7*texelFetch( iChannel0, ivec2(KEY_DOWN,0), 0 ).x)); | ||
|
|
||
|
|
||
| // keypress | ||
| col = mix( col, vec3(1.0,0.0,0.0), | ||
| (1.0-smoothstep(0.0,0.01,abs(length(uv-vec2(-0.75,0.0))-0.35)))* | ||
| texelFetch( iChannel0, ivec2(KEY_LEFT,1),0 ).x); | ||
|
|
||
| col = mix( col, vec3(1.0,1.0,0.0), | ||
| (1.0-smoothstep(0.0,0.01,abs(length(uv-vec2(0.0,0.5))-0.35)))* | ||
| texelFetch( iChannel0, ivec2(KEY_UP,1),0 ).x); | ||
|
|
||
| col = mix( col, vec3(0.0,1.0,0.0), | ||
| (1.0-smoothstep(0.0,0.01,abs(length(uv-vec2(0.75,0.0))-0.35)))* | ||
| texelFetch( iChannel0, ivec2(KEY_RIGHT,1),0 ).x); | ||
|
|
||
| col = mix( col, vec3(0.0,0.0,1.0), | ||
| (1.0-smoothstep(0.0,0.01,abs(length(uv-vec2(0.0,-0.5))-0.35)))* | ||
| texelFetch( iChannel0, ivec2(KEY_DOWN,1),0 ).x); | ||
|
|
||
|
|
||
| // toggle | ||
| col = mix( col, vec3(1.0,0.0,0.0), | ||
| (1.0-smoothstep(0.0,0.01,abs(length(uv-vec2(-0.75,0.0))-0.3)))* | ||
| texelFetch( iChannel0, ivec2(KEY_LEFT,2),0 ).x); | ||
|
|
||
| col = mix( col, vec3(1.0,1.0,0.0), | ||
| (1.0-smoothstep(0.0,0.01,abs(length(uv-vec2(0.0,0.5))-0.3)))* | ||
| texelFetch( iChannel0, ivec2(KEY_UP,2),0 ).x); | ||
|
|
||
| col = mix( col, vec3(0.0,1.0,0.0), | ||
| (1.0-smoothstep(0.0,0.01,abs(length(uv-vec2(0.75,0.0))-0.3)))* | ||
| texelFetch( iChannel0, ivec2(KEY_RIGHT,2),0 ).x); | ||
|
|
||
| col = mix( col, vec3(0.0,0.0,1.0), | ||
| (1.0-smoothstep(0.0,0.01,abs(length(uv-vec2(0.0,-0.5))-0.3)))* | ||
| texelFetch( iChannel0, ivec2(KEY_DOWN,2),0 ).x); | ||
|
|
||
| fragColor = vec4(col,1.0); | ||
| } | ||
| """ | ||
|
|
||
| keyboard_channel = ShadertoyChannelKeyboard(filter="nearest", wrap="clamp", vflip=False) | ||
|
|
||
| shader = Shadertoy(shader_code, resolution=(800, 450), inputs=[keyboard_channel]) | ||
|
|
||
| if __name__ == "__main__": | ||
| shader.show() | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| from .inputs import ShadertoyChannel, ShadertoyChannelBuffer, ShadertoyChannelTexture | ||
| from .inputs import ShadertoyChannel, ShadertoyChannelBuffer, ShadertoyChannelTexture, ShadertoyChannelKeyboard | ||
| from .passes import BufferRenderPass, ImageRenderPass | ||
| from .shadertoy import Shadertoy | ||
|
|
||
| __version__ = "0.2.0" | ||
| version_info = tuple(map(int, __version__.split("."))) # noqa | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will have to think about this a bit more since this means we don't actually need
self.updateat all - but not sure if this is the correct approach as this also means that static textures get written every single pass which I don't think is needed.Can also be problematic if the same channel/resource is used in multiple passes.