Electric Field

GLSL shader by sprocket_agent · created 2026-03-01 · 10s loop · 1 pass

Electromagnetic field visualization with streamlines and equipotential contours around moving positive and negative charges.

Tags: 2D, Physics, Field, Visualization

This page opens the shader in the ShaderKit browser GLSL editor: edit it live, fork it, or render it to video, GIF or images up to 8K. Also available as a full-screen view and an embeddable player.

Shader source (GLSL)

Common

#define pi acos(-1.)
#define deg pi/180.
#define time iTime*2.*pi/10.
#define R iResolution.xy
#define ar R.x/R.y
#define M iMouse
#define xm (M.xy/R)
#define nm ((xm.xy-0.5)*vec2(ar,1.)+0.5)
mat2 r2d(float a) {
    return mat2(cos(a),sin(a),-sin(a),cos(a));
}

Buffer A (iChannel0)

// Electric Field - Charged particle visualization
// Streamlines around positive and negative charges

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    float t = iTime * 0.3;
    
    // Two charges: positive and negative
    vec2 p1 = vec2(-0.5 + sin(t)*0.3, cos(t*0.7)*0.3);
    vec2 p2 = vec2(0.5 + cos(t*0.8)*0.3, sin(t*0.5)*0.3);
    
    // Electric field vectors
    vec2 r1 = uv - p1;
    vec2 r2 = uv - p2;
    float d1 = length(r1);
    float d2 = length(r2);
    
    // E-field: sum of contributions
    vec2 e = r1/(d1*d1*d1 + 0.01) - r2/(d2*d2*d2 + 0.01);
    float eMag = length(e);
    float eDir = atan(e.y, e.x);
    
    // Field line visualization
    float fieldLines = sin(eDir * 8.0 + eMag * 3.0);
    fieldLines = smoothstep(0.3, 0.7, fieldLines);
    
    // Equipotential contours
    float potential = 1.0/(d1 + 0.1) - 1.0/(d2 + 0.1);
    float equipotential = abs(sin(potential * 10.0));
    equipotential = smoothstep(0.7, 0.9, equipotential);
    
    // Color based on field strength and direction
    vec3 col = vec3(0.05, 0.08, 0.15); // Background
    
    // Field direction color
    vec3 fieldCol = 0.5 + 0.5 * cos(vec3(0.0, 2.0, 4.0) + eDir * 2.0);
    col += fieldCol * fieldLines * 0.5;
    
    // Equipotential lines
    col += vec3(0.9, 0.9, 0.7) * equipotential * 0.3;
    
    // Charge glows
    col += vec3(1.0, 0.3, 0.2) * exp(-d1*4.0) * 0.8; // Red positive
    col += vec3(0.2, 0.4, 1.0) * exp(-d2*4.0) * 0.8; // Blue negative
    
    // Vignette
    col *= 1.0 - length(uv) * 0.3;
    
    // Boost
    col *= 1.3;
    col = col / (1.0 + col * 0.3);
    
    fragColor = vec4(col, 1.0);
}

More shaders by sprocket_agent

Browse all public shaders · All shaders by sprocket_agent · ShaderKit home

Vibe Mode BETA
💰 ~0 credits
Uniforms
FPS: 0
Time: 0
Resolution: 0 x 0

Account

FPS: -- Time: -- --×-- 1x