April_The_Sergal

LiquidGoldWebGLShader

Apr 20th, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (() => {
  2.   const canvas = document.getElementById('faceShader');
  3.   if (!canvas){ console.warn('[faceShader] canvas not found'); return; }
  4.   const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
  5.   if (!gl){ console.warn('[faceShader] WebGL unavailable'); return; }
  6.  
  7.   canvas.addEventListener('webglcontextlost', (e) => {
  8.     console.error('[faceShader] WebGL context lost');
  9.     e.preventDefault();
  10.   });
  11.  
  12.   const VS = `
  13.     attribute vec2 a_position;
  14.     attribute vec2 a_uv;
  15.     varying vec2 v_uv;
  16.     void main(){
  17.       v_uv = a_uv;
  18.       gl_Position = vec4(a_position, 0.0, 1.0);
  19.     }
  20.   `;
  21.  
  22.   const FS = `
  23.     precision mediump float;
  24.     uniform float u_time;
  25.     uniform vec2  u_mouse;
  26.     uniform vec2  u_resolution;
  27.     varying vec2  v_uv;
  28.  
  29.     vec3 mod289(vec3 x){ return x - floor(x * (1.0/289.0)) * 289.0; }
  30.     vec4 mod289(vec4 x){ return x - floor(x * (1.0/289.0)) * 289.0; }
  31.     vec4 permute(vec4 x){ return mod289(((x*34.0)+1.0)*x); }
  32.     vec4 taylorInvSqrt(vec4 r){ return 1.79284291400159 - 0.85373472095314 * r; }
  33.  
  34.     float snoise(vec3 v){
  35.       const vec2 C = vec2(1.0/6.0, 1.0/3.0);
  36.       const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
  37.       vec3 i = floor(v + dot(v, C.yyy));
  38.       vec3 x0 = v - i + dot(i, C.xxx);
  39.       vec3 g = step(x0.yzx, x0.xyz);
  40.       vec3 l = 1.0 - g;
  41.       vec3 i1 = min(g.xyz, l.zxy);
  42.       vec3 i2 = max(g.xyz, l.zxy);
  43.       vec3 x1 = x0 - i1 + C.xxx;
  44.       vec3 x2 = x0 - i2 + C.yyy;
  45.       vec3 x3 = x0 - D.yyy;
  46.       i = mod289(i);
  47.       vec4 p = permute(permute(permute(
  48.           i.z + vec4(0.0, i1.z, i2.z, 1.0))
  49.           + i.y + vec4(0.0, i1.y, i2.y, 1.0))
  50.           + i.x + vec4(0.0, i1.x, i2.x, 1.0));
  51.       float n_ = 0.142857142857;
  52.       vec3 ns = n_ * D.wyz - D.xzx;
  53.       vec4 j = p - 49.0 * floor(p * ns.z * ns.z);
  54.       vec4 x_ = floor(j * ns.z);
  55.       vec4 y_ = floor(j - 7.0 * x_);
  56.       vec4 x = x_ * ns.x + ns.yyyy;
  57.       vec4 y = y_ * ns.x + ns.yyyy;
  58.       vec4 h = 1.0 - abs(x) - abs(y);
  59.       vec4 b0 = vec4(x.xy, y.xy);
  60.       vec4 b1 = vec4(x.zw, y.zw);
  61.       vec4 s0 = floor(b0)*2.0 + 1.0;
  62.       vec4 s1 = floor(b1)*2.0 + 1.0;
  63.       vec4 sh = -step(h, vec4(0.0));
  64.       vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
  65.       vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
  66.       vec3 p0 = vec3(a0.xy, h.x);
  67.       vec3 p1 = vec3(a0.zw, h.y);
  68.       vec3 p2 = vec3(a1.xy, h.z);
  69.       vec3 p3 = vec3(a1.zw, h.w);
  70.       vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2,p2), dot(p3,p3)));
  71.       p0 *= norm.x; p1 *= norm.y; p2 *= norm.z; p3 *= norm.w;
  72.       vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
  73.       m = m * m;
  74.       return 42.0 * dot(m*m, vec4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3)));
  75.     }
  76.  
  77.     float fbm(vec3 p){
  78.       float value = 0.0;
  79.       float amplitude = 0.5;
  80.       float frequency = 1.0;
  81.       for (int i = 0; i < 6; i++){
  82.         value += amplitude * snoise(p * frequency);
  83.         frequency *= 2.1;
  84.         amplitude *= 0.48;
  85.       }
  86.       return value;
  87.     }
  88.  
  89.     float marblePattern(vec3 p){
  90.       float t = u_time * 0.15;
  91.       float warp1 = fbm(p + vec3(1.7, 9.2, 3.4) + t * 0.3);
  92.       float warp2 = fbm(p + vec3(8.3, 2.8, 5.1) - t * 0.2);
  93.       vec3 warped = p + vec3(warp1, warp2, warp1 * 0.5) * 1.5;
  94.       float pattern = fbm(warped);
  95.       float veins = sin(p.x * 3.0 + p.y * 2.0 + pattern * 8.0 + t);
  96.       veins = abs(veins);
  97.       veins = pow(veins, 0.3);
  98.       return mix(pattern, veins, 0.5);
  99.     }
  100.  
  101.     void main(){
  102.       vec2 uv = v_uv;
  103.       float aspect = u_resolution.x / u_resolution.y;
  104.       vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
  105.       vec2 mouse = (u_mouse - 0.5) * vec2(aspect, 1.0);
  106.       float mouseDist = length(p - mouse);
  107.       float mouseInfluence = smoothstep(0.8, 0.0, mouseDist) * 0.15;
  108.       float t = u_time * 0.1;
  109.  
  110.       vec3 pos1 = vec3(p * 2.0, t);
  111.       vec3 pos2 = vec3(p * 3.5 + 1.5, t * 0.7);
  112.       vec3 pos3 = vec3(p * 1.2 - 2.3, t * 1.3);
  113.       pos1.xy += mouse * mouseInfluence * 2.0;
  114.       pos2.xy += mouse * mouseInfluence * 1.5;
  115.  
  116.       float marble1 = marblePattern(pos1);
  117.       float marble2 = marblePattern(pos2);
  118.       float marble3 = marblePattern(pos3);
  119.  
  120.       float combined = marble1 * 0.5 + marble2 * 0.3 + marble3 * 0.2;
  121.       combined = combined * 0.5 + 0.5;
  122.       combined = pow(combined, 3.0);
  123.  
  124.       float veinStrength = snoise(vec3(p * 4.0 + marble1 * 2.0, t * 0.5));
  125.       veinStrength = pow(abs(veinStrength), 2.5);
  126.  
  127.       float fineVeins = snoise(vec3(p * 8.0 + marble2 * 3.0, t * 0.3));
  128.       fineVeins = pow(abs(fineVeins), 4.0);
  129.  
  130.       vec3 darkGold    = vec3(0.78, 0.58, 0.20);
  131.       vec3 deepGold    = vec3(0.42, 0.30, 0.09);
  132.       vec3 almostBlack = vec3(0.04, 0.03, 0.012);
  133.  
  134.       vec3 color = vec3(0.0);
  135.       color = mix(color, almostBlack, combined * 0.9);
  136.  
  137.       float goldAmount = veinStrength * 0.30 + fineVeins * 0.14;
  138.       goldAmount *= (1.0 + mouseInfluence * 8.0);
  139.  
  140.       float swirl = sin(p.x * 5.0 + marble1 * 10.0 + t * 2.0) * 0.5 + 0.5;
  141.       swirl = pow(swirl, 8.0);
  142.       goldAmount += swirl * 0.12;
  143.  
  144.       color = mix(color, darkGold, goldAmount * 0.85);
  145.       color = mix(color, deepGold, combined * 0.18);
  146.  
  147.       float highlight = pow(max(marble1 * marble2, 0.0), 6.0);
  148.       color += darkGold * highlight * 0.35 * (1.0 + mouseInfluence * 5.0);
  149.  
  150.       float mouseGlow = exp(-mouseDist * mouseDist * 8.0) * 0.09;
  151.       color += darkGold * mouseGlow;
  152.  
  153.       float vignette = 1.0 - length(uv - 0.5) * 1.15;
  154.       vignette = smoothstep(0.0, 1.0, vignette);
  155.       color *= vignette;
  156.  
  157.       color = clamp(color, 0.0, 0.85);
  158.  
  159.       gl_FragColor = vec4(color, 1.0);
  160.     }
  161.   `;
  162.  
  163.   function compile(type, src){
  164.     const s = gl.createShader(type);
  165.     gl.shaderSource(s, src);
  166.     gl.compileShader(s);
  167.     if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)){
  168.       console.error('[faceShader] ' + (type === gl.VERTEX_SHADER ? 'vertex' : 'fragment') + ' compile error:\n' + gl.getShaderInfoLog(s));
  169.       gl.deleteShader(s);
  170.       return null;
  171.     }
  172.     return s;
  173.   }
  174.   const vs = compile(gl.VERTEX_SHADER, VS);
  175.   const fs = compile(gl.FRAGMENT_SHADER, FS);
  176.   if (!vs || !fs) return;
  177.  
  178.   const program = gl.createProgram();
  179.   gl.attachShader(program, vs);
  180.   gl.attachShader(program, fs);
  181.   gl.linkProgram(program);
  182.   if (!gl.getProgramParameter(program, gl.LINK_STATUS)){
  183.     console.error('[faceShader] link error:', gl.getProgramInfoLog(program));
  184.     return;
  185.   }
  186.   gl.useProgram(program);
  187.  
  188.   const posBuf = gl.createBuffer();
  189.   gl.bindBuffer(gl.ARRAY_BUFFER, posBuf);
  190.   gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1]), gl.STATIC_DRAW);
  191.   const aPos = gl.getAttribLocation(program, 'a_position');
  192.   gl.enableVertexAttribArray(aPos);
  193.   gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
  194.  
  195.   const uvBuf = gl.createBuffer();
  196.   gl.bindBuffer(gl.ARRAY_BUFFER, uvBuf);
  197.   gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0,0, 1,0, 0,1, 0,1, 1,0, 1,1]), gl.STATIC_DRAW);
  198.   const aUv = gl.getAttribLocation(program, 'a_uv');
  199.   gl.enableVertexAttribArray(aUv);
  200.   gl.vertexAttribPointer(aUv, 2, gl.FLOAT, false, 0, 0);
  201.  
  202.   const uTime = gl.getUniformLocation(program, 'u_time');
  203.   const uMouse = gl.getUniformLocation(program, 'u_mouse');
  204.   const uRes = gl.getUniformLocation(program, 'u_resolution');
  205.  
  206.   let mx = 0.5, my = 0.5;
  207.   const cardEl = canvas.closest('.card');
  208.  
  209.   function resize(){
  210.     const dpr = Math.min(window.devicePixelRatio || 1, 2);
  211.     const rect = canvas.getBoundingClientRect();
  212.     const cssW = rect.width  > 0 ? rect.width  : (cardEl ? cardEl.getBoundingClientRect().width  : 420);
  213.     const cssH = rect.height > 0 ? rect.height : (cardEl ? cardEl.getBoundingClientRect().height : 600);
  214.     const w = Math.max(2, Math.round(cssW * dpr));
  215.     const h = Math.max(2, Math.round(cssH * dpr));
  216.     if (canvas.width !== w || canvas.height !== h){
  217.       canvas.width = w;
  218.       canvas.height = h;
  219.       gl.viewport(0, 0, w, h);
  220.     }
  221.   }
  222.   resize();
  223.   window.addEventListener('resize', resize);
  224.   if (typeof ResizeObserver !== 'undefined') new ResizeObserver(resize).observe(canvas);
  225.  
  226.   window.addEventListener('pointermove', (e) => {
  227.     if (!cardEl) return;
  228.     const r = cardEl.getBoundingClientRect();
  229.     mx = Math.max(0, Math.min(1, (e.clientX - r.left) / r.width));
  230.     my = Math.max(0, Math.min(1, 1 - (e.clientY - r.top) / r.height));
  231.   }, { passive: true });
  232.  
  233.   const start = performance.now();
  234.   function frame(){
  235.     resize();
  236.     gl.uniform1f(uTime, (performance.now() - start) / 1000);
  237.     gl.uniform2f(uMouse, mx, my);
  238.     gl.uniform2f(uRes, canvas.width, canvas.height);
  239.     gl.drawArrays(gl.TRIANGLES, 0, 6);
  240.     requestAnimationFrame(frame);
  241.   }
  242.   requestAnimationFrame(frame);
  243. })();
Advertisement
Add Comment
Please, Sign In to add comment