{"slug":"plasma","number":"01","title":"Plasma","era":"Demoscene · 1992","summary":"Color-field oscillator running as a fullscreen fragment shader.","description":"A demoscene plasma is a sum of sine fields in the plane, sampled through a cycling palette. This one is a single WebGL 1 fragment shader: two triangles, a time uniform, no textures. Drop the file on a page and it fills the window. Reduce motion and it holds a still frame.","apis":["WebGL 1","GLSL ES 1.0","requestAnimationFrame","matchMedia"],"tags":["shader","demoscene"],"hasGlsl":true,"interaction":null,"url":"https://3d-retro.com/experiments/plasma","example_url":"https://3d-retro.com/examples/plasma.html","api_url":"https://3d-retro.com/api/v1/experiments/plasma","html":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\" />\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n  <meta name=\"description\" content=\"Color-field oscillator running as a fullscreen fragment shader.\" />\n  <link rel=\"canonical\" href=\"https://3d-retro.com/experiments/plasma\" />\n  <title>Plasma · 3d retro graphics</title>\n  <style>\n    html, body {\n      margin: 0;\n      width: 100%;\n      height: 100%;\n      overflow: hidden;\n      background: #0b0c0e;\n      color: #e8e6e1;\n      position: absolute;\n      inset: 0;\n    }\n    canvas {\n      display: block;\n      position: absolute;\n      inset: 0;\n      width: 100%;\n      height: 100%;\n    }\n    .fallback {\n      position: absolute;\n      inset: 0;\n      display: grid;\n      place-items: center;\n      padding: 24px;\n      font: 14px/1.5 ui-monospace, \"IBM Plex Mono\", monospace;\n      text-align: center;\n      color: #e8e6e1;\n      background: #0b0c0e;\n    }\n    .fallback[hidden] { display: none; }\n  </style>\n</head>\n<body>\n<canvas id=\"c\"></canvas>\n<div id=\"fallback\" class=\"fallback\" hidden></div>\n<script>\n(function () {\n  const PARAMS = {\n    color: \"#6ee7b7\",\n    speed: 1,\n    scale: 1\n  };\n  function hexToRgb(hex) {\n    const n = parseInt(String(hex).replace(\"#\", \"\"), 16);\n    if (isNaN(n)) return [0.43, 0.91, 0.72];\n    return [(n >> 16 & 255) / 255, (n >> 8 & 255) / 255, (n & 255) / 255];\n  }\n\n  const canvas = document.getElementById(\"c\");\n  const fallback = document.getElementById(\"fallback\");\n\n  function fail(message) {\n    fallback.hidden = false;\n    fallback.textContent = message;\n  }\n\n  const gl = canvas.getContext(\"webgl\", { alpha: false, antialias: false, powerPreference: \"high-performance\" });\n  if (!gl) {\n    fail(\"WebGL is not available in this browser. Try a current Firefox, Chrome, Safari, or Edge.\");\n    return;\n  }\n\n  const VERT = `attribute vec2 a_pos;\nvoid main() {\n  gl_Position = vec4(a_pos, 0.0, 1.0);\n}`;\n\n  const FRAG = `precision mediump float;\nuniform float u_time;\nuniform vec2 u_resolution;\nuniform vec3 u_color;\nuniform float u_speed;\nuniform float u_scale;\n\nvoid main() {\n  vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution) / min(u_resolution.x, u_resolution.y);\n  uv *= u_scale;\n  float t = u_time * 0.55 * u_speed;\n\n  float v = 0.0;\n  v += sin(uv.x * 9.0 + t);\n  v += sin(uv.y * 8.0 + t * 1.15);\n  v += sin((uv.x + uv.y) * 6.0 + t * 0.7);\n  vec2 c = uv + vec2(sin(t * 0.31), cos(t * 0.27)) * 0.45;\n  v += sin(length(c) * 14.0 - t * 1.4);\n  v *= 0.25;\n\n  float r = 0.55 + 0.45 * sin(v * 6.28318 + t);\n  float g = 0.45 + 0.45 * sin(v * 6.28318 + t * 1.3 + 2.1);\n  float b = 0.55 + 0.45 * sin(v * 6.28318 + t * 0.7 + 4.2);\n  vec3 col = vec3(r, g, b);\n  col = mix(vec3(0.05, 0.18, 0.22), col, 0.92);\n  col = mix(col, vec3(0.43, 0.91, 0.72), 0.12 * (0.5 + 0.5 * sin(v * 12.0)));\n\n  vec2 q = gl_FragCoord.xy / u_resolution;\n  col *= 0.65 + 0.35 * pow(16.0 * q.x * q.y * (1.0 - q.x) * (1.0 - q.y), 0.32);\n  col = mix(col, u_color, 0.32);\n  gl_FragColor = vec4(col, 1.0);\n}`;\n\n  function compile(type, src) {\n    const sh = gl.createShader(type);\n    gl.shaderSource(sh, src);\n    gl.compileShader(sh);\n    if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) {\n      throw new Error(gl.getShaderInfoLog(sh) || \"Shader compile failed\");\n    }\n    return sh;\n  }\n\n  const prog = gl.createProgram();\n  gl.attachShader(prog, compile(gl.VERTEX_SHADER, VERT));\n  gl.attachShader(prog, compile(gl.FRAGMENT_SHADER, FRAG));\n  gl.linkProgram(prog);\n  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {\n    throw new Error(gl.getProgramInfoLog(prog) || \"Program link failed\");\n  }\n  gl.useProgram(prog);\n\n  const buf = gl.createBuffer();\n  gl.bindBuffer(gl.ARRAY_BUFFER, buf);\n  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]), gl.STATIC_DRAW);\n  const loc = gl.getAttribLocation(prog, \"a_pos\");\n  gl.enableVertexAttribArray(loc);\n  gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);\n\n  const uTime = gl.getUniformLocation(prog, \"u_time\");\n  const uRes = gl.getUniformLocation(prog, \"u_resolution\");\n  const uColor = gl.getUniformLocation(prog, \"u_color\");\n  const uSpeed = gl.getUniformLocation(prog, \"u_speed\");\n  const uScale = gl.getUniformLocation(prog, \"u_scale\");\n  const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  let raf = 0;\n\n  function resize() {\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n    const w = Math.max(1, Math.floor(canvas.clientWidth * dpr));\n    const h = Math.max(1, Math.floor(canvas.clientHeight * dpr));\n    if (canvas.width !== w || canvas.height !== h) {\n      canvas.width = w;\n      canvas.height = h;\n    }\n    gl.viewport(0, 0, canvas.width, canvas.height);\n  }\n\n  function frame(now) {\n    resize();\n    const t = reduced ? 2.4 : now * 0.001;\n    const rgb = hexToRgb(PARAMS.color);\n    gl.uniform1f(uTime, t);\n    gl.uniform2f(uRes, canvas.width, canvas.height);\n    gl.uniform3f(uColor, rgb[0], rgb[1], rgb[2]);\n    gl.uniform1f(uSpeed, PARAMS.speed);\n    gl.uniform1f(uScale, PARAMS.scale);\n    gl.drawArrays(gl.TRIANGLES, 0, 6);\n    if (!reduced) raf = requestAnimationFrame(frame);\n  }\n\n  canvas.addEventListener(\"webglcontextlost\", function (e) {\n    e.preventDefault();\n    cancelAnimationFrame(raf);\n    fail(\"The WebGL context was lost. Reload to restore the demo.\");\n  });\n\n  try {\n    raf = requestAnimationFrame(frame);\n  } catch (err) {\n    fail(err && err.message ? err.message : String(err));\n  }\n})();\n</script>\n</body>\n</html>\n"}