-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathstrands.js
More file actions
7 lines (7 loc) · 3.59 KB
/
Copy pathstrands.js
File metadata and controls
7 lines (7 loc) · 3.59 KB
1
2
3
4
5
6
7
/* strands.js — motion-anything recipe · ambient · faithful GPU shader (dependency-free WebGL via _fx/shaderbg.js). */
(function(g){ 'use strict';
var FRAG='#version 300 es\nprecision highp float;\n\nuniform float uTime;\nuniform vec2 uResolution;\nuniform vec3 uColors[8];\nuniform int uColorCount;\nuniform int uStrandCount;\nuniform float uSpeed;\nuniform float uAmplitude;\nuniform float uWaviness;\nuniform float uThickness;\nuniform float uGlow;\nuniform float uTaper;\nuniform float uSpread;\nuniform float uHueShift;\nuniform float uIntensity;\nuniform float uOpacity;\nuniform float uScale;\nuniform float uSaturation;\n\nout vec4 fragColor;\n\nconst float PI = 3.14159265;\n\nvec3 spectrum(float t) {\n return 0.5 + 0.5 * cos(2.0 * PI * (t + vec3(0.00, 0.33, 0.67)));\n}\n\nvec3 samplePalette(float t) {\n t = fract(t);\n float scaled = t * float(uColorCount);\n int idx = int(floor(scaled));\n float blend = fract(scaled);\n int nextIdx = idx + 1;\n if (nextIdx >= uColorCount) nextIdx = 0;\n return mix(uColors[idx], uColors[nextIdx], blend);\n}\n\nvec3 strandColor(float t) {\n if (uColorCount > 0) return samplePalette(t);\n return spectrum(t);\n}\n\nvoid main() {\n vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y;\n uv /= max(uScale, 0.0001);\n\n float e = 0.06 + uIntensity * 0.94;\n float env = pow(max(cos(uv.x * PI * 1.3), 0.0), uTaper);\n\n vec3 col = vec3(0.0);\n\n for (int i = 0; i < 12; i++) {\n if (i >= uStrandCount) break;\n\n float fi = float(i);\n float ph = fi * 1.7 * uSpread;\n float freq = (2.0 + fi * 0.35) * uWaviness;\n float spd = 1.4 + fi * 1.2;\n\n float tt = uTime * uSpeed;\n float w = sin(uv.x * freq + tt * spd + ph) * 0.60\n + sin(uv.x * freq * 1.1 - tt * spd * 0.7 + ph * 1.7) * 0.40;\n\n float amp = (0.1 + 0.02 * e) * env * uAmplitude;\n float y = w * amp;\n\n float d = abs(uv.y - y);\n float thick = (0.001 + 0.05 * e) * (0.35 + env) * uThickness;\n float g = thick / (d + thick * 0.45);\n g = g * g;\n\n float h = fi / float(uStrandCount) + uv.x * 0.30 + uTime * 0.04 + uHueShift;\n col += strandColor(h) * g * env;\n }\n\n col *= 0.45 + 0.7 * e;\n col = 1.0 - exp(-col * uGlow);\n\n float gray = dot(col, vec3(0.2126, 0.7152, 0.0722));\n col = max(mix(vec3(gray), col, uSaturation), 0.0);\n\n float lum = max(max(col.r, col.g), col.b);\n float alpha = clamp(lum, 0.0, 1.0) * uOpacity;\n\n fragColor = vec4(col * uOpacity, alpha);\n}\n';
function init(){ var els=document.querySelectorAll('.strands'); for(var i=0;i<els.length;i++){ if(els[i].__sbg) continue;
g.ShaderBG(els[i], FRAG, { uniforms:{"uColors":{"t":"3fv","v":[1,0.25882352941176473,0.25882352941176473,0.48627450980392156,0.22745098039215686,0.9294117647058824,0.023529411764705882,0.7137254901960784,0.8313725490196079,0.9176470588235294,0.7019607843137254,0.03137254901960784,0.9176470588235294,0.7019607843137254,0.03137254901960784,0.9176470588235294,0.7019607843137254,0.03137254901960784,0.9176470588235294,0.7019607843137254,0.03137254901960784,0.9176470588235294,0.7019607843137254,0.03137254901960784]},"uColorCount":{"t":"1i","v":4},"uStrandCount":{"t":"1i","v":3},"uSpeed":{"t":"1f","v":0.5},"uAmplitude":{"t":"1f","v":1},"uWaviness":{"t":"1f","v":1},"uThickness":{"t":"1f","v":0.7},"uGlow":{"t":"1f","v":2.6},"uTaper":{"t":"1f","v":3},"uSpread":{"t":"1f","v":1},"uHueShift":{"t":"1f","v":0},"uIntensity":{"t":"1f","v":0.6},"uOpacity":{"t":"1f","v":1},"uScale":{"t":"1f","v":1.5},"uSaturation":{"t":"1f","v":1.5}} }); } }
if(document.readyState!=='loading') init(); else document.addEventListener('DOMContentLoaded', init);
})(window);