Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (() => {
- const canvas = document.getElementById('faceShader');
- if (!canvas){ console.warn('[faceShader] canvas not found'); return; }
- const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
- if (!gl){ console.warn('[faceShader] WebGL unavailable'); return; }
- canvas.addEventListener('webglcontextlost', (e) => {
- console.error('[faceShader] WebGL context lost');
- e.preventDefault();
- });
- const VS = `
- attribute vec2 a_position;
- attribute vec2 a_uv;
- varying vec2 v_uv;
- void main(){
- v_uv = a_uv;
- gl_Position = vec4(a_position, 0.0, 1.0);
- }
- `;
- const FS = `
- precision mediump float;
- uniform float u_time;
- uniform vec2 u_mouse;
- uniform vec2 u_resolution;
- varying vec2 v_uv;
- vec3 mod289(vec3 x){ return x - floor(x * (1.0/289.0)) * 289.0; }
- vec4 mod289(vec4 x){ return x - floor(x * (1.0/289.0)) * 289.0; }
- vec4 permute(vec4 x){ return mod289(((x*34.0)+1.0)*x); }
- vec4 taylorInvSqrt(vec4 r){ return 1.79284291400159 - 0.85373472095314 * r; }
- float snoise(vec3 v){
- const vec2 C = vec2(1.0/6.0, 1.0/3.0);
- const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
- vec3 i = floor(v + dot(v, C.yyy));
- vec3 x0 = v - i + dot(i, C.xxx);
- vec3 g = step(x0.yzx, x0.xyz);
- vec3 l = 1.0 - g;
- vec3 i1 = min(g.xyz, l.zxy);
- vec3 i2 = max(g.xyz, l.zxy);
- vec3 x1 = x0 - i1 + C.xxx;
- vec3 x2 = x0 - i2 + C.yyy;
- vec3 x3 = x0 - D.yyy;
- i = mod289(i);
- vec4 p = permute(permute(permute(
- i.z + vec4(0.0, i1.z, i2.z, 1.0))
- + i.y + vec4(0.0, i1.y, i2.y, 1.0))
- + i.x + vec4(0.0, i1.x, i2.x, 1.0));
- float n_ = 0.142857142857;
- vec3 ns = n_ * D.wyz - D.xzx;
- vec4 j = p - 49.0 * floor(p * ns.z * ns.z);
- vec4 x_ = floor(j * ns.z);
- vec4 y_ = floor(j - 7.0 * x_);
- vec4 x = x_ * ns.x + ns.yyyy;
- vec4 y = y_ * ns.x + ns.yyyy;
- vec4 h = 1.0 - abs(x) - abs(y);
- vec4 b0 = vec4(x.xy, y.xy);
- vec4 b1 = vec4(x.zw, y.zw);
- vec4 s0 = floor(b0)*2.0 + 1.0;
- vec4 s1 = floor(b1)*2.0 + 1.0;
- vec4 sh = -step(h, vec4(0.0));
- vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
- vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
- vec3 p0 = vec3(a0.xy, h.x);
- vec3 p1 = vec3(a0.zw, h.y);
- vec3 p2 = vec3(a1.xy, h.z);
- vec3 p3 = vec3(a1.zw, h.w);
- vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2,p2), dot(p3,p3)));
- p0 *= norm.x; p1 *= norm.y; p2 *= norm.z; p3 *= norm.w;
- vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
- m = m * m;
- return 42.0 * dot(m*m, vec4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3)));
- }
- float fbm(vec3 p){
- float value = 0.0;
- float amplitude = 0.5;
- float frequency = 1.0;
- for (int i = 0; i < 6; i++){
- value += amplitude * snoise(p * frequency);
- frequency *= 2.1;
- amplitude *= 0.48;
- }
- return value;
- }
- float marblePattern(vec3 p){
- float t = u_time * 0.15;
- float warp1 = fbm(p + vec3(1.7, 9.2, 3.4) + t * 0.3);
- float warp2 = fbm(p + vec3(8.3, 2.8, 5.1) - t * 0.2);
- vec3 warped = p + vec3(warp1, warp2, warp1 * 0.5) * 1.5;
- float pattern = fbm(warped);
- float veins = sin(p.x * 3.0 + p.y * 2.0 + pattern * 8.0 + t);
- veins = abs(veins);
- veins = pow(veins, 0.3);
- return mix(pattern, veins, 0.5);
- }
- void main(){
- vec2 uv = v_uv;
- float aspect = u_resolution.x / u_resolution.y;
- vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
- vec2 mouse = (u_mouse - 0.5) * vec2(aspect, 1.0);
- float mouseDist = length(p - mouse);
- float mouseInfluence = smoothstep(0.8, 0.0, mouseDist) * 0.15;
- float t = u_time * 0.1;
- vec3 pos1 = vec3(p * 2.0, t);
- vec3 pos2 = vec3(p * 3.5 + 1.5, t * 0.7);
- vec3 pos3 = vec3(p * 1.2 - 2.3, t * 1.3);
- pos1.xy += mouse * mouseInfluence * 2.0;
- pos2.xy += mouse * mouseInfluence * 1.5;
- float marble1 = marblePattern(pos1);
- float marble2 = marblePattern(pos2);
- float marble3 = marblePattern(pos3);
- float combined = marble1 * 0.5 + marble2 * 0.3 + marble3 * 0.2;
- combined = combined * 0.5 + 0.5;
- combined = pow(combined, 3.0);
- float veinStrength = snoise(vec3(p * 4.0 + marble1 * 2.0, t * 0.5));
- veinStrength = pow(abs(veinStrength), 2.5);
- float fineVeins = snoise(vec3(p * 8.0 + marble2 * 3.0, t * 0.3));
- fineVeins = pow(abs(fineVeins), 4.0);
- vec3 darkGold = vec3(0.78, 0.58, 0.20);
- vec3 deepGold = vec3(0.42, 0.30, 0.09);
- vec3 almostBlack = vec3(0.04, 0.03, 0.012);
- vec3 color = vec3(0.0);
- color = mix(color, almostBlack, combined * 0.9);
- float goldAmount = veinStrength * 0.30 + fineVeins * 0.14;
- goldAmount *= (1.0 + mouseInfluence * 8.0);
- float swirl = sin(p.x * 5.0 + marble1 * 10.0 + t * 2.0) * 0.5 + 0.5;
- swirl = pow(swirl, 8.0);
- goldAmount += swirl * 0.12;
- color = mix(color, darkGold, goldAmount * 0.85);
- color = mix(color, deepGold, combined * 0.18);
- float highlight = pow(max(marble1 * marble2, 0.0), 6.0);
- color += darkGold * highlight * 0.35 * (1.0 + mouseInfluence * 5.0);
- float mouseGlow = exp(-mouseDist * mouseDist * 8.0) * 0.09;
- color += darkGold * mouseGlow;
- float vignette = 1.0 - length(uv - 0.5) * 1.15;
- vignette = smoothstep(0.0, 1.0, vignette);
- color *= vignette;
- color = clamp(color, 0.0, 0.85);
- gl_FragColor = vec4(color, 1.0);
- }
- `;
- function compile(type, src){
- const s = gl.createShader(type);
- gl.shaderSource(s, src);
- gl.compileShader(s);
- if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)){
- console.error('[faceShader] ' + (type === gl.VERTEX_SHADER ? 'vertex' : 'fragment') + ' compile error:\n' + gl.getShaderInfoLog(s));
- gl.deleteShader(s);
- return null;
- }
- return s;
- }
- const vs = compile(gl.VERTEX_SHADER, VS);
- const fs = compile(gl.FRAGMENT_SHADER, FS);
- if (!vs || !fs) return;
- const program = gl.createProgram();
- gl.attachShader(program, vs);
- gl.attachShader(program, fs);
- gl.linkProgram(program);
- if (!gl.getProgramParameter(program, gl.LINK_STATUS)){
- console.error('[faceShader] link error:', gl.getProgramInfoLog(program));
- return;
- }
- gl.useProgram(program);
- const posBuf = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, posBuf);
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1]), gl.STATIC_DRAW);
- const aPos = gl.getAttribLocation(program, 'a_position');
- gl.enableVertexAttribArray(aPos);
- gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
- const uvBuf = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, uvBuf);
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0,0, 1,0, 0,1, 0,1, 1,0, 1,1]), gl.STATIC_DRAW);
- const aUv = gl.getAttribLocation(program, 'a_uv');
- gl.enableVertexAttribArray(aUv);
- gl.vertexAttribPointer(aUv, 2, gl.FLOAT, false, 0, 0);
- const uTime = gl.getUniformLocation(program, 'u_time');
- const uMouse = gl.getUniformLocation(program, 'u_mouse');
- const uRes = gl.getUniformLocation(program, 'u_resolution');
- let mx = 0.5, my = 0.5;
- const cardEl = canvas.closest('.card');
- function resize(){
- const dpr = Math.min(window.devicePixelRatio || 1, 2);
- const rect = canvas.getBoundingClientRect();
- const cssW = rect.width > 0 ? rect.width : (cardEl ? cardEl.getBoundingClientRect().width : 420);
- const cssH = rect.height > 0 ? rect.height : (cardEl ? cardEl.getBoundingClientRect().height : 600);
- const w = Math.max(2, Math.round(cssW * dpr));
- const h = Math.max(2, Math.round(cssH * dpr));
- if (canvas.width !== w || canvas.height !== h){
- canvas.width = w;
- canvas.height = h;
- gl.viewport(0, 0, w, h);
- }
- }
- resize();
- window.addEventListener('resize', resize);
- if (typeof ResizeObserver !== 'undefined') new ResizeObserver(resize).observe(canvas);
- window.addEventListener('pointermove', (e) => {
- if (!cardEl) return;
- const r = cardEl.getBoundingClientRect();
- mx = Math.max(0, Math.min(1, (e.clientX - r.left) / r.width));
- my = Math.max(0, Math.min(1, 1 - (e.clientY - r.top) / r.height));
- }, { passive: true });
- const start = performance.now();
- function frame(){
- resize();
- gl.uniform1f(uTime, (performance.now() - start) / 1000);
- gl.uniform2f(uMouse, mx, my);
- gl.uniform2f(uRes, canvas.width, canvas.height);
- gl.drawArrays(gl.TRIANGLES, 0, 6);
- requestAnimationFrame(frame);
- }
- requestAnimationFrame(frame);
- })();
Advertisement
Add Comment
Please, Sign In to add comment