Mouse Interaction
Uniforms are the shader's only input from the outside world. iTime is one; iMouse is another. It turns a shader into a toy you can poke, and it is the same mechanism a game uses to pass the player's position, a damage flash or a selected color to a material.
What iMouse contains
iMouse is a vec4, in pixels, with the origin at the bottom-left, like fragCoord:
| Component | While a button is held | After release | Before any click |
|---|---|---|---|
xy |
current pointer position | last position while held | (0, 0) |
z |
x where the press started (positive) | same value, negated | 0 |
w |
y where the press started (positive) | same value, negated | 0 |
So iMouse.z > 0.0 means "a button is down right now", and iMouse.xy == vec2(0.0) usually means "nobody has clicked yet". Good shaders handle that first-frame case, or the effect sits in the corner until the learner clicks.
Following the pointer
Convert the mouse position with the same formula as the pixel, so both live in the same space. Then distance to the mouse is just length(p - m). Drag in the frame below:
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
vec2 m = (iMouse.xy - 0.5 * iResolution.xy) / iResolution.y;
if (iMouse.xy == vec2(0.0)) m = 0.3 * vec2(cos(iTime), sin(iTime)); // no click yet: orbit
float d = length(p - m);
vec3 color = vec3(0.05, 0.06, 0.1);
color += vec3(1.0, 0.7, 0.3) * 0.02 / (d + 0.01); // glowing light
color += vec3(0.3, 0.5, 1.0) * (1.0 - smoothstep(0.0, 0.004, abs(d - 0.15))); // ring
fragColor = vec4(color, 1.0);
}Pressed or released
The sign of iMouse.z tells you whether the button is held. The press position (abs(iMouse.zw)) gives you a drag start point, so you can draw a line or a selection box from where the drag began to where the pointer is:
float sdSegment(vec2 p, vec2 a, vec2 b) {
vec2 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 toP = 1.0 / iResolution.yy;
vec2 p = fragCoord * toP;
vec2 cur = iMouse.xy * toP;
vec2 start = abs(iMouse.zw) * toP;
bool held = iMouse.z > 0.0;
vec3 color = held ? vec3(0.1, 0.15, 0.1) : vec3(0.12);
if (iMouse.xy != vec2(0.0)) {
float d = sdSegment(p, start, cur);
vec3 lineColor = held ? vec3(0.3, 1.0, 0.4) : vec3(0.6);
color = mix(color, lineColor, 1.0 - smoothstep(0.004, 0.008, d));
color = mix(color, vec3(1.0, 0.4, 0.3), 1.0 - smoothstep(0.012, 0.016, length(p - start)));
}
fragColor = vec4(color, 1.0);
}Press and drag: the line is green while the button is down and turns grey when you let go, because iMouse.z becomes negative.
A spotlight and a lens
Two classic interactive effects use the mouse as a center of influence. A spotlight darkens everything by distance; a lens distorts the coordinates near the pointer before drawing the pattern. Distorting p before it is used is the same "move the space, not the shape" idea as in Animating with iTime:
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
vec2 m = (iMouse.xy - 0.5 * iResolution.xy) / iResolution.y;
if (iMouse.xy == vec2(0.0)) m = vec2(0.35 * sin(iTime * 0.7), 0.0);
vec2 toM = p - m;
float r = length(toM);
float lens = 0.2;
if (r < lens) p = m + toM * mix(0.4, 1.0, smoothstep(0.0, lens, r)); // magnify inside
vec2 g = fract(p * 10.0) - 0.5;
float checker = step(0.0, g.x * g.y);
vec3 color = mix(vec3(0.2, 0.25, 0.35), vec3(0.85, 0.85, 0.75), checker);
float spot = 1.0 - smoothstep(0.0, 0.6, r); // spotlight falloff
color *= 0.25 + 0.75 * spot;
fragColor = vec4(color, 1.0);
}Mouse values in a real game
iMouse is a convenience for experiments. In a game, the CPU decides what the shader needs and sends it as a uniform in the space the shader works in:
| Shader needs | The game sends | Example |
|---|---|---|
| a world position (a force-field ripple where a bullet hit) | vec3 world position |
Unity material.SetVector("_HitPos", hit.point) |
| a screen position (a cursor glow in a UI shader) | normalized 0..1 screen coordinates | Three.js uniforms.uPointer.value.set(x, y) |
| a direction (a character's facing for a shield shader) | normalized vec3 |
Godot material.set_shader_parameter("facing", dir) |
Converting the pointer once on the CPU is cheaper than converting it in every pixel, and it keeps shaders independent of window size.
Tip: Sending time-varying values every frame is fine: setting a uniform is cheap. What costs time is changing which shader or material is bound, because that breaks batching.
Test yourself
What does iMouse.z > 0.0 tell you?
- The pointer is inside the frame
- A mouse button is currently held down
- The mouse moved this frame
- The frame is focused
Why convert iMouse.xy with the same formula as fragCoord?
- To make it faster
- So the pointer and the pixel are in the same coordinate space and distances make sense
- Because iMouse is in UV space
- It is required by WebGL
Before the learner has ever clicked, iMouse is...
- (-1, -1, -1, -1)
- (0, 0, 0, 0)
- the frame center
- undefined