Published
Edited
Nov 8, 2020
Insert cell
Insert cell
Insert cell
Insert cell
// =====================================
// write your own fragment shader below
// =====================================
fragShaderSource = `
precision highp float; // 声明float的精度
#define PI 3.14159265359
// JS与shader单向通信的变量
uniform vec2 u_size; // 当前画布的尺寸
uniform float u_dpr; // 绘制环境的devicePixelRatio
float half_pi = PI / 2.0;

// 根据输入的value和目标value
// 返回位于目标点[-0.01, +0.01]范围内的平滑插值
float smmoth_filter(float value, float taget_value){
return smoothstep(taget_value - 0.1, taget_value, value) -
smoothstep(taget_value, taget_value + 0.1, value);
}

// 根据输入的value和目标value
// 返回位于目标点[0.0, +half_pi]范围内的突变值
float step_filter(float value, float taget_value){
return step(taget_value, value) -
step(taget_value + half_pi, value);
}

// 2D Random
float random (in vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))
* 43758.5453123);
}

// 2D Noise based on Morgan McGuire @morgan3d
// https://www.shadertoy.com/view/4dS3Wd
float noise (in vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);

// Four corners in 2D of a tile
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));

// Smooth Interpolation

// Cubic Hermine Curve. Same as SmoothStep()
vec2 u = f*f*(3.0-2.0*f);
// u = smoothstep(0.,1.,f);

// Mix 4 coorners percentages
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}

// color pattern
vec3 color_1 = vec3(45.0, 89, 198) / vec3(255.0);
vec3 color_2 = vec3(49, 142, 222) / vec3(255.0);
vec3 color_3 = vec3(38, 205, 213) / vec3(255.0);
vec3 color_4 = vec3(118, 224, 214) / vec3(255.0);

void main() {
float rate = 100.0 * u_dpr;
vec2 st = gl_FragCoord.xy / vec2(rate); // 减小画布坐标数值范围
vec3 color = vec3(0.0); // 黑色背景

float y = sin(st.x) + (u_size.y / rate) * 0.5; // 一个周期为2PI, 振幅为1的sin三角函数

float percent = smmoth_filter(st.y, y * (0.5 + noise(gl_FragCoord.xy * 5.0))); // 加入噪声因素

float segment_x_1 = st.x - mod(st.x, 2.0 * PI); // 每个周期中的第1个分割点的横坐标
float segment_x_2 = segment_x_1 + half_pi; // 每个周期中的第2个分割点的横坐标
float segment_x_3 = segment_x_2 + half_pi; // ...
float segment_x_4 = segment_x_3 + half_pi; // ...

color = mix(color, mix(color, color_1, step_filter(st.x, segment_x_1)), percent); // 第1个PI/2段
color = mix(color, mix(color, color_2, step_filter(st.x, segment_x_2)), percent); // 第2个PI/2段
color = mix(color, mix(color, color_3, step_filter(st.x, segment_x_3)), percent); // ...
color = mix(color, mix(color, color_4, step_filter(st.x, segment_x_4)), percent); // ...

gl_FragColor = vec4(color * 3.0, 1.0);
}
`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more