Below is one complete ShaderToy example. In this example the emoji “head” (a yellow circle) is drawn with two black circles as eyes and a smiling mouth (an arc made from a circle with a lower‐half mask). The whole emoji rotates over time. (We use signed distance functions for the head, eyes, and mouth.) You can paste the code into ShaderToy to run it.
/*
Title: Rotating Emoji with SDF
Description: A yellow emoji with a smiling face drawn with signed distance fields,
which rotates over time.
*/
#ifdef GL_ES
precision mediump float;
#endif
uniform float iTime;
uniform vec3 iResolution;
// Rotate a 2D vector by angle a.
mat2 rot(in float a)
{
float s = sin(a);
float c = cos(a);
return mat2(c, -s, s, c);
}
// SDF for a circle centered at the origin with radius r.
float sdCircle(in vec2 p, in float r)
{
return length(p) - r;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalize and center coordinates. Scale so that the emoji size stays constant.
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
// Rotate UV coordinates over time.
// This rotates the entire emoji.
float angle = iTime;
uv = rot(angle) * uv;
// ------------------------------------------------------------------
// Emoji Head
// ------------------------------------------------------------------
// Head is a circle of radius 0.5.
float dHead = sdCircle(uv, 0.5);
// ------------------------------------------------------------------
// Eyes
// ------------------------------------------------------------------
// We define the eyes relative to the head.
// Their positions will automatically rotate because we are working with uv.
vec2 eyeOffsetL = vec2(-0.2, 0.2);
vec2 eyeOffsetR = vec2( 0.2, 0.2);
float dEyeL = sdCircle(uv - eyeOffsetL, 0.07);
float dEyeR = sdCircle(uv - eyeOffsetR, 0.07);
// ------------------------------------------------------------------
// Smiling Mouth
// ------------------------------------------------------------------
// We create a circular arc by taking the difference from a circle
// and then “masking” the upper half so that only the lower half is drawn.
vec2 pm = uv - vec2(0.0, -0.1); // shift mouth center down a little
// Get distance from a circle of radius 0.3.
float dMouth = abs(length(pm) - 0.3) - 0.03;
// Only show the lower half of the mouth:
if(pm.y > 0.0){
dMouth = 0.1; // force the SDF to be positive (i.e. "outside")
}
// ------------------------------------------------------------------
// Compose the Emoji
// ------------------------------------------------------------------
// Background color
vec3 bgColor = vec3(1.0);
// Emoji yellow color
vec3 emojiColor = vec3(1.0, 0.8, 0.0);
// Black for facial features.
vec3 featureColor = vec3(0.0);
// Smooth anti-aliasing factor.
float aa = 0.005;
// Create a smooth alpha for the head.
float headAlpha = smoothstep( aa, -aa, dHead );
// Similarly, create a “feature mask” from the eyes and mouth.
float eyeAlpha = min(smoothstep(aa, -aa, dEyeL),
smoothstep(aa, -aa, dEyeR));
float mouthAlpha = smoothstep(aa, -aa, dMouth);
// Combine the features (if any feature SDF is inside, we darken it).
float features = max( eyeAlpha, mouthAlpha );
// Base color: if we are inside the head shape then use the yellow emoji color.
vec3 color = mix(bgColor, emojiColor, headAlpha);
// Then “carve out” the features by mixing in black.
color = mix(color, featureColor, features);
fragColor = vec4(color, 1.0);
}
void main(void)
{
mainImage(gl_FragColor, gl_FragCoord.xy);
}
Explanation
-
Coordinate Setup and Rotation:
We first normalize and center the coordinates (uv) and then rotate them by iTime using a simple 2D rotation matrix. This makes the entire emoji spin.
-
Head SDF:
The head is a circle of radius 0.5 drawn using the SDF function sdCircle.
-
Eyes:
Two smaller circles (radius 0.07) positioned symmetrically on the face serve as the eyes. Their positions are defined relative to the centered coordinate system, and they rotate along with the head.
-
Mouth:
The mouth is approximated by taking a circle of radius 0.3 (with a small thickness of 0.03) and then “masking” out the upper half by checking pm.y > 0.0. This leaves only the lower half visible, giving a smile.
-
Anti-Aliasing and Composition:
We use smoothstep for smooth anti-aliasing of the edges. The background is white, the head is yellow, and the facial features (eyes and mouth) are black.
Copy and paste the code into ShaderToy, and you should see a rotating yellow emoji with a smiley face rendered with signed distance fields. Enjoy experimenting!