Below is a complete Shadertoy shader written in GLSL that renders a yellow emoji with a simple smiley face using Signed Distance Fields (SDFs). The emoji rotates over time based on the shader's uniform iTime.
Explanation:
- SDF Basics: We use SDF functions to define shapes (e.g., circles for the face, eyes, and mouth). The SDF returns the distance from a point to the shape's boundary.
- Shapes:
- The face is a large yellow circle.
- The eyes are two smaller black circles inside the face.
- The mouth is approximated as the lower half of a circle (to simulate a smile) by conditionally rendering only the bottom part.
- Rotation: The entire emoji is rotated by applying a rotation matrix to the coordinates before computing SDFs.
- Rendering: We use smoothstep for antialiasing to create smooth edges. The face is rendered first (yellow), and then the eyes and mouth are overlaid (black).
- Shadertoy Setup: This code assumes you're pasting it into Shadertoy, where
iTime provides the time in seconds, and iResolution gives the screen resolution.
Shader Code:
// Signed Distance Field for a circle
float sdCircle(vec2 p, float r) {
return length(p) - r;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// Normalize coordinates to [-aspect/2, aspect/2] in x and [-0.5, 0.5] in y
vec2 uv = (fragCoord.xy / iResolution.xy) * 2.0 - 1.0;
uv.x *= iResolution.x / iResolution.y; // Correct for aspect ratio
// Apply rotation based on time
float angle = iTime * 0.5; // Rotation speed: 0.5 radians per second
mat2 rotate = mat2(cos(angle), -sin(angle), sin(angle), cos(angle));
vec2 p = rotate * uv; // Rotate the point
// Define SDFs for the shapes
float dFace = sdCircle(p, 0.5); // Face: circle with radius 0.5
float dLeftEye = sdCircle(p - vec2(-0.2, 0.1), 0.1); // Left eye: small circle
float dRightEye = sdCircle(p - vec2(0.2, 0.1), 0.1); // Right eye: small circle
float dMouth = sdCircle(p - vec2(0.0, -0.2), 0.2); // Mouth: circle for lower half
// Antialiasing width (approximate based on pixel size)
float aa = length(vec2(dFdx(p.x), dFdy(p.x))) * 0.5; // Use derivative for accuracy, or use 0.01 for simplicity
// Start with background color (black)
vec4 color = vec4(0.0);
// Render the face: mix background with yellow where inside the face SDF
color = mix(color, vec4(1.0, 1.0, 0.0, 1.0), smoothstep(0.0, aa, -dFace));
// Render the eyes: overlay black where inside the eye SDFs
color = mix(color, vec4(0.0, 0.0, 0.0, 1.0), smoothstep(0.0, aa, -dLeftEye));
color = mix(color, vec4(0.0, 0.0, 0.0, 1.0), smoothstep(0.0, aa, -dRightEye));
// Render the mouth: only the lower half (simulate smile)
float mouthFactor = (dMouth < 0.0 && p.y < -0.2) ? smoothstep(0.0, aa, -dMouth) : 0.0;
color = mix(color, vec4(0.0, 0.0, 0.0, 1.0), mouthFactor);
// Output the final color
fragColor = color;
}
How to Use in Shadertoy:
- Go to shadertoy.com and create a new shader.
- Paste the code into the code editor.
- Run the shader. You'll see a yellow smiley face emoji rotating in the center of the screen.
- The emoji will rotate continuously, and the SDFs ensure smooth, antialiased edges.
Notes:
- Appearance: The face is yellow, and the eyes and mouth are black. The mouth is a simple approximation (lower half of a circle), which gives a basic smile effect.
- Customization:
- Change the rotation speed by modifying
float angle = iTime * 0.5; (e.g., use 1.0 for faster rotation).
- Adjust positions and sizes in the SDF calls (e.g., change
vec2(-0.2, 0.1) for the left eye).
- For a more advanced smile, you could replace the mouth with a true arc SDF, but that would require additional functions (e.g., SDF for an ellipse or torus slice).
- Performance: This is a simple shader and should run efficiently on most devices.
- Limitations: The mouth isn't a perfect smile (it's just the bottom half of a circle), but it works as a quick SDF-based approximation.
If you'd like to enhance this (e.g., add more details, improve the mouth shape, or add animations), let me know!