Swirling Portal (2D, procedural, open / close)

A swirling magic portal for Godot 4: spiral arms, energy clouds, sparks pulled into the centre, a glowing rim and a dark core. Fully procedural, no textures. One parameter (appear) opens and closes it with a small flash.

Tested by hand in Godot 4.7 with the Compatibility renderer, so it also works in web exports.

How to use (2D)

  1. Add a ColorRect (or Sprite2D / TextureRect) and make it square.
  2. Give it a ShaderMaterial with this shader.
  3. Tweak color_outer, color_inner, core_color, arms, twist, flow_speed, energy, spark_amount.
  4. Animate appear from 0 to 1 to open the portal, 1 to 0 to close it (a Tween works great).

Tips

  • squash above 1.0 makes a tall oval door (lower radius a bit so the glow fits).
  • Negative twist spins the other way, negative flow_speed pushes things out instead of pulling them in.
  • core_color alpha 0 = no dark hole, just energy.
  • The glow fades out before the edge of the rect, so you never see a square cut.

Using it in 3D

Put the ColorRect inside a SubViewport (transparent_bg on, 512×512), then show the viewport texture on a QuadMesh. In the quad’s material use blend_premul_alpha and unshaded, for example:

shader_type spatial;
render_mode unshaded, cull_disabled, depth_draw_never, blend_premul_alpha;
uniform sampler2D tex : source_color;
void fragment() { vec4 c = texture(tex, UV); ALBEDO = c.rgb * 1.25; ALPHA = c.a; }

Code written with AI assistance, tested by hand in Godot 4.7.

If you want portals, spawn effects and 1,600+ more effects already set up for Godot 4: https://pixeldreamerstd.gumroad.com/l/vfxcol?utm_source=godotshaders

Shader code
// Swirling Portal (2D, procedural, open / close) — Pixel Dreamer Studio · MIT
// canvas_item shader: put it on a ColorRect / Sprite2D / TextureRect. No textures needed.
// Spiral arms + energy clouds + sparks pulled into the centre, glowing rim, dark core.
// `appear` 0 -> 1 opens the portal (with a flash), 1 -> 0 closes it.
// Works in the Compatibility renderer (web exports too).
shader_type canvas_item;

uniform vec4 color_outer : source_color = vec4(0.62, 0.28, 1.0, 1.0);
uniform vec4 color_inner : source_color = vec4(0.25, 0.9, 1.0, 1.0);
uniform vec4 core_color : source_color = vec4(0.01, 0.0, 0.035, 1.0);
uniform float radius : hint_range(0.05, 0.5) = 0.38;
uniform float squash : hint_range(0.5, 2.0) = 1.0; // > 1 = tall oval door
uniform float core_size : hint_range(0.0, 0.9) = 0.26;
uniform float twist : hint_range(-2.0, 2.0) = 0.5; // spiral turns
uniform int arms : hint_range(1, 8) = 3;
uniform float flow_speed : hint_range(-3.0, 3.0) = 0.6667; // + = pulled in
uniform float energy : hint_range(0.0, 3.0) = 1.3;
uniform float rim_width : hint_range(0.0, 0.3) = 0.07;
uniform float glow_size : hint_range(0.0, 1.0) = 0.22;
uniform float spark_amount : hint_range(0.0, 1.0) = 0.55;
uniform float appear : hint_range(0.0, 1.0) = 1.0;

const float NP = 6.0; // noise cells around the circle
const float NQ = 2.0; // noise period along the flow (keeps TIME small)

float hash(vec2 p) {
	p = fract(p * vec2(123.34, 456.21));
	p += dot(p, p + 45.32);
	return fract(p.x * p.y);
}

// value noise that tiles every `per` cells (so it wraps around the circle with no seam)
float pnoise(vec2 p, vec2 per) {
	vec2 i = floor(p);
	vec2 f = fract(p);
	vec2 u = f * f * (3.0 - 2.0 * f);
	float a = hash(mod(i, per));
	float b = hash(mod(i + vec2(1.0, 0.0), per));
	float c = hash(mod(i + vec2(0.0, 1.0), per));
	float d = hash(mod(i + vec2(1.0, 1.0), per));
	return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}

float fbm(vec2 p, vec2 per) {
	float v = 0.0;
	float a = 0.5;
	for (int k = 0; k < 4; k++) {
		v += a * pnoise(p, per);
		p *= 2.0;
		per *= 2.0;
		a *= 0.5;
	}
	return v;
}

void fragment() {
	float open = smoothstep(0.0, 1.0, appear);
	float flash = 4.0 * open * (1.0 - open); // peaks while opening / closing
	vec2 p = UV - 0.5;
	p.y /= squash;
	float R = max(radius * mix(0.15, 1.0, open), 1e-4);
	float r = length(p) / R; // 1.0 = rim
	float ang = atan(p.y, p.x) / TAU; // -0.5 .. 0.5
	// log-polar flow: moving `sv` makes everything spiral into the centre
	float t = mod(TIME * flow_speed, NQ);
	float sv = log(max(r, 1e-3)) + t;
	float q = ang + twist * sv + (1.0 - open) * 0.6;

	// energy clouds + spiral arms
	float n = fbm(vec2(q * NP, sv * 2.0), vec2(NP, NQ * 2.0));
	float n2 = fbm(vec2(q * NP * 2.0 + 3.1, sv * 3.0), vec2(NP * 2.0, NQ * 3.0));
	float arm = 0.5 + 0.5 * sin(TAU * (q * float(arms) + 0.35 * n));
	arm = pow(arm, 2.5);
	float swirl = clamp(n * 1.25 * (0.35 + 0.9 * arm) + n2 * 0.25, 0.0, 1.5);

	// sparks: cells in log-polar space (conformal = round dots everywhere)
	vec2 sc = vec2(q * 24.0, sv * 4.0);
	vec2 cell = floor(sc);
	vec2 f = fract(sc);
	float h = hash(mod(cell, vec2(24.0, NQ * 4.0)) + 7.0);
	vec2 jit = vec2(hash(mod(cell, vec2(24.0, NQ * 4.0)) + 1.7), hash(mod(cell, vec2(24.0, NQ * 4.0)) + 4.3));
	jit = 0.25 + 0.5 * jit;
	vec2 dd = (f - jit) * vec2(TAU / 24.0, 0.25);
	dd.y /= 3.0; // streak along the flow
	float spark = step(h, spark_amount) * (1.0 - smoothstep(0.0, 0.045, length(dd)));
	spark *= (1.0 - smoothstep(1.2, 1.9, r)) * smoothstep(core_size, core_size + 0.25, r);

	// radial shape
	float wob = (n2 - 0.5) * 0.12;
	float inside = 1.0 - smoothstep(1.0 + wob - 0.02, 1.0 + wob + 0.02, r);
	float rim = exp(-pow((r - 1.0 - wob) / max(rim_width, 1e-3), 2.0));
	float glow = (1.0 - inside) * exp(-max(r - 1.0, 0.0) / max(glow_size, 1e-3)) * 0.55;
	float core = 1.0 - smoothstep(core_size * 0.55, core_size + 0.18, r);

	vec3 body = mix(color_inner.rgb, color_outer.rgb, smoothstep(0.15, 1.0, r));
	vec3 col = body * swirl * (0.3 + 0.9 * smoothstep(0.0, 1.0, r)) * inside;
	col = mix(col, core_color.rgb, core * core_color.a * inside);
	col += color_outer.rgb * glow;
	col += mix(color_outer.rgb, vec3(1.0), 0.25) * rim * (0.85 + flash * 1.5);
	col += mix(color_inner.rgb, vec3(1.0), 0.6) * spark * 1.4;
	col *= energy;

	// round fade before the edge of the rect so the glow never shows a square cut
	float edge_fade = 1.0 - smoothstep(0.36, 0.5, length(UV - 0.5));
	col *= edge_fade;
	float alpha = max(inside, max(rim, max(glow, spark))) * edge_fade;
	alpha *= smoothstep(0.0, 0.08, open);
	COLOR = vec4(col, clamp(alpha, 0.0, 1.0));
}
Live Preview
Tags
2d, Black hole, compatibility, Magic, portal, Procedural, spiral, Swirl, vortex, Wormhole
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from pixeldreamerstudio

Related shaders

Comments 0

No comments yet.