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

// 根据输入的value和目标value
// 返回位于目标点[0.0, +half_pi]范围内的突变值
float step_filter(float value, float taget_value){
return step(taget_value, value) -
step(taget_value + 0.2, 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() {
vec2 st = gl_FragCoord.xy/u_size.xy;
vec3 color = vec3(0.0);

vec2 pos = vec2(0.5) - st; // 中心指向当前坐标点的向量
float distance = length(pos)*2.0; // 中心点到当前坐标点的距离

float n = noise(gl_FragCoord.xy); // 生成噪声值

float alpha = atan(pos.y,pos.x); // 当前位置对应的旋转角
float f = sin(alpha * 5.) * n; // 当前角度对应的函数值

color = mix(color, color_4, step_filter(distance, f)); // 比较实际距离 distance 和函数值 f 的大小
gl_FragColor = vec4(color, 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
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