/
gradient-shader.ts
112 lines (84 loc) 路 2.44 KB
/
gradient-shader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*!
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import sineInOut from './modules/easing/sine-in-out.glsl';
const vertex = /* glsl */ `
attribute vec3 position;
attribute vec2 uv;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragment = /* glsl */ `
precision highp float;
uniform sampler2D tMap;
uniform vec3 uGrayColor;
uniform float uProgress;
uniform float uAlpha;
uniform float uDebug;
uniform float uTime;
varying vec2 vUv;
${sineInOut}
void main() {
float time = fract(uTime / 6.0);
float t = abs(2.0 * time - 1.0);
// Rotation starts at 35.642掳, 3 sec 180掳 and 6 sec 360掳
float currentAngle = 35.642 + time * 360.0;
vec2 uv = vUv;
uv.x -= 0.1 * (1.0 - sineInOut(t));
vec2 origin = vec2(0.5, 0.5);
uv -= origin;
float angle = radians(currentAngle) + atan(uv.y, uv.x);
float len = length(uv);
uv = vec2(cos(angle) * len, sin(angle) * len) + origin;
gl_FragColor = texture2D(tMap, uv);
if (uDebug == 1.0) {
// Anti-aliased outer circle
float radius = 0.5;
float d = fwidth(len);
float circle = smoothstep(radius - d, radius + d, len);
gl_FragColor.a = (1.0 - circle) * uAlpha;
// Anti-aliased center point
radius = 0.005;
circle = smoothstep(radius - d, radius + d, len);
gl_FragColor.rgb = mix(vec3(1), gl_FragColor.rgb, circle);
} else {
gl_FragColor.a *= uAlpha;
}
if (uProgress > 0.0) {
// Anti-aliased gray unfilled angle
float theta = radians(20.0);
uv = vec2(cos(theta) * vUv.x - sin(theta) * vUv.y,
sin(theta) * vUv.x + cos(theta) * vUv.y);
float progress = 2.0 * uProgress - 1.0;
float d = 0.001;
float angle = smoothstep(uv.x - d, uv.x + d, progress);
gl_FragColor.rgb = mix(uGrayColor, gl_FragColor.rgb, angle);
}
}
`;
export default {
vertex100: vertex,
fragment100: /* glsl */ `#extension GL_OES_standard_derivatives : enable
precision highp float;
${fragment}`,
vertex300: /* glsl */ `#version 300 es
#define attribute in
#define varying out
${vertex}`,
fragment300: /* glsl */ `#version 300 es
precision highp float;
#define varying in
#define texture2D texture
#define gl_FragColor FragColor
out vec4 FragColor;
${fragment}`,
};