webgl+canvas实现GLSL 着色器振荡梯度动画效果代码
代码语言:html
所属分类:动画
代码描述:webgl+canvas实现GLSL 着色器振荡梯度动画效果代码
代码标签: webgl canvas GLSL 着色器 振荡 梯度 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/reset.min.css"> <style> html, body { width: 100%; height: 100%; overflow: hidden; } canvas { display: block; width: 100vw; height: 100vh; background: #000; } </style> </head> <body translate="no"> <canvas id="c"></canvas> <!-- Vertex Shader --> <script id="vertex-shader" type="x-shader/x-vertex"> attribute vec2 a_position; void main() { gl_Position = vec4(a_position, 0, 1); } </script> <!-- Fragment Shader --> <script id="fragment-shader" type="x-shader/x-fragment"> precision mediump float; uniform vec3 iResolution; uniform float iTime; uniform int iFrame; uniform sampler2D iChannel0; float adjustValue(float value, float progress) { float offset = 0.7; return tan(value * (1.0 - progress) + offset * progress); } float whiteNoise(vec2 uv) { float x = (uv.x + 0.1) * (uv.y + 0.1); return fract(sin(x) * 43758.5453123); } float fBm(vec2 uv) { float sum = 0.0; float scale = 1.0; float amplitude = 1.0; for (int i = 0; i < 4; ++i) { sum += whiteNoise(uv * scale) * amplitude; scale *= 2.0; amplitude *= 0.5; } return sum; } void main() { float grainTileSize = 5.0; // the bigger the number, the finer the grain float grainTileOpacity = 0.15; // the opacity of the grain tile float frequency = 0.5; // The higher the value, the faster the oscillation float amplitude = 0.4; // The range within which to oscillate float widthFactor = 3.0; // the width of the color float centerValue = 0.15; // center position of the middle color bool animateGrain = true; float angle = 1.03; // Angle in radians vec4 bgColor = vec4(0.0, 0.0, 0.0, 0.0); vec4 colors[7]; colors[0] = vec4(0.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0, 1.0); // #000000 colors[1] = vec4(44.0 / 255.0, 56.0 / 255.0, 195.0 / 255.0, 1.0); // #2C38C3 colors[2] = vec4(190.0 / 255.0, 191.0 / 255.0, 212.0 / 255.0, 0.78); // rgba(190, 191, 212, 0.78) colors[3] = vec4(190.0 / 255.0, 191.0 / 255.0, 212.0 / 255.0, 0.94); // rgba(190, 191, 212, 0.94) colors[4] = vec4(206.0 / 255.0, 195.0 / 255.0, 155.0 / 255.0, 0.94); // rgba(206, 195, 155, 0.94) colors[5] = vec4(197.0 / 255.0, 158.0 / 255.0, 37.0 / 255.0, 1.0); // #C59E25 colors[6] = vec4(113.0 / 255.0, 0.0 / 255.0, 64.0 / 255.0, 1.0); // #710040 // Gradient stop values for each color float originalValues[7]; originalValues[0] = 0.026; originalValues[1] = 0.1027; originalValues[2] = 0.1656; originalValues[3] = 0.2207; originalValues[4] = 0.2579; originalValues[5] = 0.319; originalValues[6] = 0.4154; float gradientStops[7]; float adjustedValues[7]; const int MAX_COLORS = 7; // Number of colors // Normalized coordinates ([0,1]) vec2 uv = gl_FragCoord.xy/iResolution.xy; // Center the coordinates vec2 centeredCoord = uv - vec2(0.5); float pulse = amplitude * sin(frequency * iTime) + amplitude; float progress = ((1.0 - pulse) / 2.0) + (uv.x * pulse); // Rotation matrix float cosA = cos(angle); float sinA = sin(angle); mat2 rotation = mat2(cosA, -sinA, sinA, cosA); // Apply rotation to the centered coordinates vec2 rotatedCoord = rotation * centeredCoord; // Calculate gradient based on rotated x-coordinate float gradient = rotatedCoord.x + 0.5; // Define colors float bgGradientEnd = adjustValue(0.0, progress); for(int i = 0; i < MAX_COLORS; i++) { // Reverse the order and adjust the original values adjustedValues[i] = 0.5 - originalValues[6 - i]; // Center around centerValue and apply the width factor gradientStops[i] = adjustValue(centerValue + (adjustedValues[i] - centerValue) * widthFactor, progress); } // Interpolate colors based on gradient vec4 color = bgColor; // Loop through all color stops for (int i = 0; i < MAX_COLORS; i++) { if (gradient >= gradientStops[i] && gradient < gradientStops[i + 1]) { float t = (gradient - gradientStops[i]) / (gradientStops[i + 1] - gradientStops[i]); color = mix(colors[i], colors[i + 1], t); //color = mix(colors[i], colors[i + 1], smoothstep(gradientStops[i], gradientStops[i + 1], gradient)); break; // Exit loop once color is determined } } // Handle edge case for the first color stop if (gradient >= gradientStops[MAX_COLORS - 1]) { //color = mix(bgColor, colors[0], (gradient - bgGradientEnd) / (gradientStops[0] - bgGradien.........完整代码请登录后点击上方下载按钮下载查看
网友评论0