Skip to content

Commit 6c5aa52

Browse files
authored
feat(shadertoy): add proper mouse/touch interaction support (#293)
* feat(shadertoy): add proper mouse/touch interaction support - Fix iMouse uniform to match official Shadertoy specification: - iMouse.xy only updates while button/touch is held down - iMouse.zw uses positive values when down, negative when released - Enables proper click detection via iMouse.z > 0 - Add touch event support for mobile devices: - touchstart/touchmove/touchend/touchcancel handlers - Same behavior as mouse events - Update tool description with mouse interaction documentation - Update README with: - Mouse & Touch Interaction feature - Improved Julia Set example with click detection - iMouse specification table and usage examples The iMouse uniform is a core Shadertoy feature used by popular shaders like Star Nest, raymarching examples, and particle systems. * style: fix Prettier formatting in shadertoy README
1 parent de8048c commit 6c5aa52

3 files changed

Lines changed: 115 additions & 26 deletions

File tree

examples/shadertoy-server/README.md

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A demo MCP App that renders [ShaderToy](https://www.shadertoy.com/)-compatible G
1515
- **Real-time Rendering**: Renders GLSL shaders using WebGL 2.0
1616
- **ShaderToy Compatibility**: Uses the standard `mainImage(out vec4 fragColor, in vec2 fragCoord)` entry point
1717
- **Multi-pass Rendering**: Supports buffers A-D for feedback effects, blur chains, and simulations
18+
- **Mouse & Touch Interaction**: Full iMouse support with click detection (works on mobile)
1819
- **Standard Uniforms**: iResolution, iTime, iTimeDelta, iFrame, iMouse, iDate, iChannel0-3
1920

2021
## Running
@@ -101,13 +102,20 @@ _Tool input:_
101102
}
102103
```
103104

104-
**Interactive Julia Set** (mouse controls the fractal's c parameter):
105+
**Interactive Julia Set** (click and drag to control the fractal's c parameter):
105106

106107
```glsl
107108
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
108109
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y * 2.5;
109-
vec2 mouse = (iMouse.xy / iResolution.xy - 0.5) * 2.0;
110-
vec2 c = mouse;
110+
// Use mouse position if clicked, otherwise use animated default
111+
vec2 c;
112+
if (iMouse.z > 0.0) {
113+
// Mouse is pressed - use mouse position
114+
c = (iMouse.xy / iResolution.xy - 0.5) * 2.0;
115+
} else {
116+
// Not pressed - animate around an interesting region
117+
c = vec2(-0.8 + 0.2 * sin(iTime * 0.5), 0.156 + 0.1 * cos(iTime * 0.7));
118+
}
111119
vec2 z = uv;
112120
float iter = 0.0;
113121
for (int i = 0; i < 100; i++) {
@@ -126,25 +134,33 @@ _Tool input:_
126134

127135
```json
128136
{
129-
"fragmentShader": "void mainImage(out vec4 fragColor, in vec2 fragCoord) {
130-
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y * 2.5;
131-
vec2 mouse = (iMouse.xy / iResolution.xy - 0.5) * 2.0;
132-
vec2 c = mouse;
133-
vec2 z = uv;
134-
float iter = 0.0;
135-
for (int i = 0; i < 100; i++) {
136-
z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;
137-
if (dot(z, z) > 4.0) break;
138-
iter++;
139-
}
140-
float t = iter / 100.0;
141-
vec3 col = 0.5 + 0.5 * cos(3.0 + t * 6.28 * 2.0 + vec3(0.0, 0.6, 1.0));
142-
if (iter == 100.0) col = vec3(0.0);
143-
fragColor = vec4(col, 1.0);
144-
}"
137+
"fragmentShader": "void mainImage(out vec4 fragColor, in vec2 fragCoord) {\n vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y * 2.5;\n vec2 c;\n if (iMouse.z > 0.0) {\n c = (iMouse.xy / iResolution.xy - 0.5) * 2.0;\n } else {\n c = vec2(-0.8 + 0.2 * sin(iTime * 0.5), 0.156 + 0.1 * cos(iTime * 0.7));\n }\n vec2 z = uv;\n float iter = 0.0;\n for (int i = 0; i < 100; i++) {\n z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;\n if (dot(z, z) > 4.0) break;\n iter++;\n }\n float t = iter / 100.0;\n vec3 col = 0.5 + 0.5 * cos(3.0 + t * 6.28 * 2.0 + vec3(0.0, 0.6, 1.0));\n if (iter == 100.0) col = vec3(0.0);\n fragColor = vec4(col, 1.0);\n}"
138+
}
139+
```
140+
141+
## Mouse & Touch Interaction
142+
143+
The `iMouse` uniform provides interactive input, compatible with the official Shadertoy specification:
144+
145+
| Component | When Button Down | After Release | Never Clicked |
146+
| ----------- | ---------------------- | ---------------- | ------------- |
147+
| `iMouse.xy` | Current position | Last position | `(0, 0)` |
148+
| `iMouse.zw` | Click start (positive) | Negated (-x, -y) | `(0, 0)` |
149+
150+
**Detecting button state:**
151+
152+
```glsl
153+
if (iMouse.z > 0.0) {
154+
// Button/touch is currently held down
155+
} else if (iMouse.z < 0.0) {
156+
// Button was released (can use abs(iMouse.zw) for last click position)
157+
} else {
158+
// Never clicked - show default state or animate
145159
}
146160
```
147161

162+
Touch events are automatically supported for mobile devices.
163+
148164
## Architecture
149165

150166
### Server (`server.ts`)

examples/shadertoy-server/server.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ AVAILABLE UNIFORMS:
3333
- iTime (float): elapsed time in seconds
3434
- iTimeDelta (float): time since last frame
3535
- iFrame (int): frame counter
36-
- iMouse (vec4): mouse position (xy=current, zw=click)
36+
- iMouse (vec4): mouse/touch position in pixels (see MOUSE INTERACTION below)
3737
- iDate (vec4): year, month, day, seconds
3838
- iChannel0-3 (sampler2D): buffer inputs for multi-pass shaders
3939
40+
MOUSE INTERACTION:
41+
The iMouse uniform provides interactive mouse/touch input (works on mobile):
42+
- iMouse.xy: Current position while button/touch is held down (frozen on release)
43+
- iMouse.zw: Click start position (positive when down, negative when released)
44+
- iMouse.z > 0: Button is currently pressed
45+
- iMouse.z < 0: Button was released
46+
- iMouse.z == 0: Never clicked
47+
48+
Example - camera control:
49+
vec2 uv = iMouse.xy / iResolution.xy; // normalized 0-1
50+
51+
Example - detect click:
52+
if (iMouse.z > 0.0) { /* button is down */ }
53+
4054
MULTI-PASS RENDERING:
4155
- Use bufferA-D parameters for feedback effects, blur chains, simulations
4256
- BufferA output -> iChannel0, BufferB -> iChannel1, etc.

examples/shadertoy-server/src/vendor/ShaderToyLite.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ function ShaderToyLite(canvasId) {
9090

9191
// uniforms
9292
var iFrame = 0;
93+
// iMouse state: xy = current position (only updates when button down)
94+
// zw = click start position (positive when down, negative when released)
9395
var iMouse = {x: 0, y: 0, clickX: 0, clickY: 0};
96+
var isMouseDown = false;
9497

9598
// shader common source
9699
var common = "";
@@ -145,19 +148,75 @@ function ShaderToyLite(canvasId) {
145148
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
146149
});
147150

151+
// Mouse event handlers - match official Shadertoy iMouse behavior:
152+
// iMouse.xy: current position (only updates when button is down)
153+
// iMouse.zw: click position (positive when down, negated when released)
148154
canvas.addEventListener("mousemove", (event) => {
149-
iMouse.x = event.offsetX;
150-
iMouse.y = canvas.height - event.offsetY;
155+
if (isMouseDown) {
156+
iMouse.x = event.offsetX;
157+
iMouse.y = canvas.height - event.offsetY;
158+
}
151159
});
152160

153161
canvas.addEventListener("mousedown", (event) => {
154-
iMouse.clickX = event.offsetX;
155-
iMouse.clickY = canvas.height - event.offsetY;
162+
isMouseDown = true;
163+
var x = event.offsetX;
164+
var y = canvas.height - event.offsetY;
165+
iMouse.x = x;
166+
iMouse.y = y;
167+
iMouse.clickX = x;
168+
iMouse.clickY = y;
156169
});
157170

158171
canvas.addEventListener("mouseup", () => {
159-
iMouse.clickX = 0;
160-
iMouse.clickY = 0;
172+
isMouseDown = false;
173+
// Negate click position to indicate button released (Shadertoy convention)
174+
iMouse.clickX = -Math.abs(iMouse.clickX);
175+
iMouse.clickY = -Math.abs(iMouse.clickY);
176+
});
177+
178+
// Prevent context menu on right-click to avoid interrupting interaction
179+
canvas.addEventListener("contextmenu", (event) => {
180+
event.preventDefault();
181+
});
182+
183+
// Touch support for mobile devices
184+
canvas.addEventListener("touchstart", (event) => {
185+
event.preventDefault();
186+
if (event.touches.length > 0) {
187+
var touch = event.touches[0];
188+
var rect = canvas.getBoundingClientRect();
189+
var x = touch.clientX - rect.left;
190+
var y = canvas.height - (touch.clientY - rect.top);
191+
isMouseDown = true;
192+
iMouse.x = x;
193+
iMouse.y = y;
194+
iMouse.clickX = x;
195+
iMouse.clickY = y;
196+
}
197+
}, { passive: false });
198+
199+
canvas.addEventListener("touchmove", (event) => {
200+
event.preventDefault();
201+
if (isMouseDown && event.touches.length > 0) {
202+
var touch = event.touches[0];
203+
var rect = canvas.getBoundingClientRect();
204+
iMouse.x = touch.clientX - rect.left;
205+
iMouse.y = canvas.height - (touch.clientY - rect.top);
206+
}
207+
}, { passive: false });
208+
209+
canvas.addEventListener("touchend", (event) => {
210+
event.preventDefault();
211+
isMouseDown = false;
212+
iMouse.clickX = -Math.abs(iMouse.clickX);
213+
iMouse.clickY = -Math.abs(iMouse.clickY);
214+
}, { passive: false });
215+
216+
canvas.addEventListener("touchcancel", () => {
217+
isMouseDown = false;
218+
iMouse.clickX = -Math.abs(iMouse.clickX);
219+
iMouse.clickY = -Math.abs(iMouse.clickY);
161220
});
162221
}
163222

0 commit comments

Comments
 (0)