Skip to content

Instantly share code, notes, and snippets.

@tejainece
Created July 14, 2025 13:13
Show Gist options
  • Save tejainece/d670fca5a29af7aed2b80126f3b14512 to your computer and use it in GitHub Desktop.
Save tejainece/d670fca5a29af7aed2b80126f3b14512 to your computer and use it in GitHub Desktop.
Rectangle electric
vec3 random3(vec3 c) {
float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));
vec3 r;
r.z = fract(512.0*j);
j *= .125;
r.x = fract(512.0*j);
j *= .125;
r.y = fract(512.0*j);
return r-0.5;
}
/* skew constants for 3d simplex functions */
const float F3 = 0.3333333;
const float G3 = 0.1666667;
/* 3d simplex noise */
float simplex3d(vec3 p) {
/* 1. find current tetrahedron T and it's four vertices */
/* s, s+i1, s+i2, s+1.0 - absolute skewed (integer) coordinates of T vertices */
/* x, x1, x2, x3 - unskewed coordinates of p relative to each of T vertices*/
/* calculate s and x */
vec3 s = floor(p + dot(p, vec3(F3)));
vec3 x = p - s + dot(s, vec3(G3));
/* calculate i1 and i2 */
vec3 e = step(vec3(0.0), x - x.yzx);
vec3 i1 = e*(1.0 - e.zxy);
vec3 i2 = 1.0 - e.zxy*(1.0 - e);
/* x1, x2, x3 */
vec3 x1 = x - i1 + G3;
vec3 x2 = x - i2 + 2.0*G3;
vec3 x3 = x - 1.0 + 3.0*G3;
/* 2. find four surflets and store them in d */
vec4 w, d;
/* calculate surflet weights */
w.x = dot(x, x);
w.y = dot(x1, x1);
w.z = dot(x2, x2);
w.w = dot(x3, x3);
/* w fades from 0.6 at the center of the surflet to 0.0 at the margin */
w = max(0.6 - w, 0.0);
/* calculate surflet components */
d.x = dot(random3(s), x);
d.y = dot(random3(s + i1), x1);
d.z = dot(random3(s + i2), x2);
d.w = dot(random3(s + 1.0), x3);
/* multiply d by w^4 */
w *= w;
w *= w;
d *= w;
/* 3. return the sum of the four surflets */
return dot(d, vec4(52.0));
}
float noise(vec3 m) {
return 0.5333333*simplex3d(m)
+0.2666667*simplex3d(2.0*m)
+0.1333333*simplex3d(4.0*m)
+0.0666667*simplex3d(8.0*m);
}
/**
* @notice Returns the signed distance from point p to a rounded box.
* @param p The sample coordinate.
* @param b The half-dimensions of the box.
* @param r The corner radius.
* @return The signed distance.
*/
float sdRoundedBox(in vec2 p, in vec2 b, in float r) {
vec2 q = abs(p) - b;
return length(max(q, vec2(0))) + min(max(q.x, q.y), 0.0) - r;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates, ranging from -1 to 1
vec2 uv = fragCoord.xy / iResolution.xy;
uv = uv * 2. - 1.;
// Uncomment the next line to correct for aspect ratio, preventing stretching
// uv.x *= iResolution.x / iResolution.y;
// Set up 3D coordinates for noise, including time for animation
vec2 p = fragCoord.xy/iResolution.x;
vec3 p3 = vec3(p, iTime*0.4);
// Calculate a fractal noise value
float intensity = noise(vec3(p3*12.0+12.0));
// --- MODIFICATION START ---
// Define the properties of the rounded rectangle
vec2 boxHalfSize = vec2(0.8, 0.25);
float cornerRadius = 0.15;
// Calculate the distance 'y' from the pixel to the noisy rectangle boundary.
// 'sdRoundedBox' gives the distance to the clean shape.
// Subtracting 'intensity' perturbs the shape, making its border wavy.
// 'abs()' creates a glow on both the inside and outside of the boundary.
float y = abs(sdRoundedBox(uv, boxHalfSize, cornerRadius) - intensity * 0.15);
// --- MODIFICATION END ---
// Use the distance 'y' to calculate a gradient value 'g'
float g = pow(y, 0.2);
// Calculate the final color based on the gradient
vec3 col = vec3(1.70, 1.48, 1.78);
col = col * -g + col; // Equivalent to col * (1.0 - g)
col = col * col;
col = col * col;
// Discard pixels that are too dark to create a sharp cutoff
if(col.x < 0.1 && col.y < 0.1 && col.z < 0.1) {
fragColor = vec4(0.);
} else {
fragColor = vec4(col, 1.);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment