How to use keyboard in ported Shadertoy demos

Started by Stefan, January 27, 2016, 05:08:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Stefan

Here is a nice keyboard controlled demo: Key Quest

I tried to port it to GeeXLab - download here

Keyboard action in the info quad works fine, but i have no idea how to send virtual key codes through the "iChannel" to the pixel shader.

Thats' what i tried:


...
  if (gh_input.keyboard_is_key_down(KC_B) == 1) then
    _keyboard_last_time = elapsed_time
    key_str = "B"
    virtual_key = 66
  end
...

-- this doesn't work...
gh_gpu_program.uniform1i(shadertoy_prog, "iChannel3", virtual_key)

-- this works
gfx.write_text(10, 160, 1, 1, 0, 1, "Press buttons ABCDEFGHK")
gfx.write_text(10, 180, 1, 0, 0, 1, "" .. key_str)



Stefan

Synthclipse has an "exact copy of the Shadertoy's keyboard input".

But i still didn't understand it...

Quote
Keyboard Uniform Control can be used for reading a keyboard state (if a key is pressed or toggled). It is exact copy of the Shadertoy's keyboard input. The keyboard state is kept in a texture with dimensions 256 x 2. Each row represents the same set of 256 characters. Index (from 0 to 255) of a texel in a row equals to a key code which the texel represents. Key codes are compatible with JavaScript and the list of most notable ones can be found here.
Each texel has only one component - R - which can have only one of the two values: 0.0 (off), 1.0 (on). The Lower row (the one sampled with y <= 0.5) of the texture keeps track of the pressed (1.0) / released (0.0) key state. The upper row (the one sampled with y > 0.5) keeps track of whether a key is toggled or not.
Keyboard input is gathered only when the Viewport View is focused.

JeGX

I'm working on a demo that will show how to retrieve GeeXLab keyboard codes in a shadertoy shader...


Stefan

Quote from: JeGX on January 28, 2016, 05:14:03 PM
Done!

http://www.geeks3d.com/hacklab/20160128/how-to-read-the-keyboard-in-a-glsl-shader/

Hope that helps!

Nice job,
i didn't expect that much code.


Here is a Digital clock - ported to GeeXLab

"Up key toggles AM/PM mode, Down key toggles red green. Left is microwavestyle, Right is off leds shown."





I'll search now for more keyboard controlled demos...

Stefan

Trace cone with CRT effect - >>>ported to GeeXLab<<<

Keys:
'A' - dither
'B' - CRT effect
'D', 'E' - number of colours if dither is on
'F' - pixelization
'G' - antialiasing
'H' - C64 palette
'I' - color weights for C64



stakov999

It seems to me there was a problem to use keyboard javascript code (i guess the translation wasnt done in the way i needed).
So i did another translation which works for me. It's  done with the string library from lua. It should be easier than the use than several variable "KC_SPACE  and KC_ESCAPE" because now one just need to do an iteration "for loop" on the "key" table to call whatever you want. Even if anyone want to reverse the table (make the index being the value and the value the index) it's possible from an for loop.  The complete key table is below.

hope it can be useful for someone
There are only a few key missing : the right CTRL key, and the right shift. Not a big deal, but if someone knows why it's missing, i'd be glad.


To use it : very easy : put it in a file .lua, like "key.lua" then put it in the libs/lua folder.
Then use "dofile".
Then I'm using the following code :



local lib_dir = gh_utils.get_scripting_libs_dir()
dofile(lib_dir .. "lua/key.lua")

local kbcode,state
gh_window.keyboard_update_buffer(0)


--KEYBOARD STATUS
for k,v in pairs(key) do
kbcode=k
for k1,v1 in pairs(key[k]) do
        if k1>1 then
        state=gh_input.keyboard_is_key_down(key[k][k1])
        end
end
if state==1 then text(kbcode,state,key[kbcode][1]) end
end


function text(keycode,status,str)
gh_imgui.text(string.format("%d_%d_%s",keycode,status,str))
end





Here the keyboard table mapping


key={}
key[8]={"backspace", 14 }
key[9]={"tab", 15 }
key[13]={"enter", 156,28 }
key[16]={"shift", 54,42 }
key[17]={"ctrl", 157,29 }
key[18]={"alt", 184,56 }
key[19]={"pause", 223 }
key[20]={"capital", 58 }
key[27]={"escape", 1 }
key[32]={"space", 57 }
key[33]={"pageup", 201 }
key[34]={"pagedown", 209 }
key[35]={"_end", 207 }
key[36]={"home", 199 }
key[37]={"left", 75 ,203}
key[38]={"up", 72,200 }
key[39]={"right", 77 ,205}
key[40]={"down", 80 ,208}
key[45]={"insert", 210 }
key[46]={"delete", 211 }
key[48]={"_0", 11 }
key[49]={"_1", 2 }
key[50]={"_2", 3 }
key[51]={"_3", 4 }
key[52]={"_4", 5 }
key[53]={"_5", 6 }
key[54]={"_6", 7 }
key[55]={"_7", 8 }
key[56]={"_8", 9 }
key[57]={"_9", 10 }
key[65]={"a", 30 }
key[66]={"b", 48 }
key[67]={"c", 46 }
key[68]={"d", 32 }
key[69]={"e", 18 }
key[70]={"f", 33 }
key[71]={"g", 34 }
key[72]={"h", 35 }
key[73]={"i", 23 }
key[74]={"j", 36 }
key[75]={"k", 37 }
key[76]={"l", 38 }
key[77]={"m", 50 }
key[78]={"n", 49 }
key[79]={"o", 24 }
key[80]={"p", 25 }
key[81]={"q", 16 }
key[82]={"r", 19 }
key[83]={"s", 31 }
key[84]={"t", 20 }
key[85]={"u", 22 }
key[86]={"v", 47 }
key[87]={"w", 17 }
key[88]={"x", 45 }
key[89]={"y", 21 }
key[90]={"z", 44 }
key[91]={"lwin", 219 }
key[92]={"rwin", 220 }
key[96]={"numpad0", 82 }
key[97]={"numpad1", 79 }
key[98]={"numpad2", 80 }
key[99]={"numpad3", 81 }
key[100]={"numpad4", 75 }
key[101]={"numpad5", 76 }
key[102]={"numpad6", 77 }
key[103]={"numpad7", 71 }
key[104]={"numpad8", 72 }
key[105]={"numpad9", 73 }
key[106]={"multiply", 55 }
key[107]={"add", 78 }
key[109]={"subtract", 74 }
key[110]={"decimal", 83 }
key[111]={"divide", 181 }
key[112]={"f1", 59 }
key[113]={"f2", 60 }
key[114]={"f3", 61 }
key[115]={"f4", 62 }
key[116]={"f5", 63 }
key[117]={"f6", 64 }
key[118]={"f7", 65 }
key[119]={"f8", 66 }
key[120]={"f9", 67 }
key[121]={"f10", 68 }
key[122]={"f11", 87 }
key[123]={"f12", 88 }
key[144]={"numlock", 69 }
key[145]={"scroll", 70 }


JeGX

Thanks for your work!
I added a new key.lua file in the libs/lua/ folder.  Will be shipped in the next update  ;)

stakov999

Great,
let me know if there is a problem.

Any idea about the right CTRL and right shift ?


JeGX

The GeeXLab codes for shift and ctrl:

KC_LEFT_SHIFT   = 42
KC_RIGHT_SHIFT  = 54

KC_LEFT_CTRL    = 29
KC_RIGHT_CTRL   = 157

Other codes are available here: libs/lua/keyboard_codes.lua

stakov999

Then the right shift/right ctrl / right alt should be working, but it's not.
Right shift and right ctrl aren't responding. And right alt is responding to 29 geexlab code (associated to left control if i'm right).

hope it works for anyone else, probably it's my keyboard/OS responding weardly.