@@ -21,9 +21,8 @@ import {
2121 RadialGradientTemplate ,
2222 type RadialGradientProps ,
2323} from '../templates/RadialGradientTemplate.js' ;
24- import { genGradientColors } from '../../renderers/webgl/internal/ShaderUtils.js' ;
25- import type { WebGlRenderer } from '../../renderers/webgl/WebGlRenderer.js' ;
2624import type { WebGlShaderType } from '../../renderers/webgl/WebGlShaderNode.js' ;
25+ import type { WebGlRenderer } from '../../renderers/webgl/WebGlRenderer.js' ;
2726
2827export const RadialGradient : WebGlShaderType < RadialGradientProps > = {
2928 props : RadialGradientTemplate . props ,
@@ -48,38 +47,61 @@ export const RadialGradient: WebGlShaderType<RadialGradientProps> = {
4847 } ,
4948 fragment ( renderer : WebGlRenderer , props : RadialGradientProps ) {
5049 return `
51- # ifdef GL_FRAGMENT_PRECISION_HIGH
52- precision highp float;
53- # else
54- precision mediump float;
55- # endif
50+ # ifdef GL_FRAGMENT_PRECISION_HIGH
51+ precision highp float;
52+ # else
53+ precision mediump float;
54+ # endif
5655
57- #define PI 3.14159265359
56+ #define MAX_STOPS ${ props . colors . length }
57+ #define LAST_STOP ${ props . colors . length - 1 }
5858
59- uniform float u_alpha;
60- uniform vec2 u_dimensions;
59+ uniform float u_alpha;
60+ uniform vec2 u_dimensions;
6161
62- uniform sampler2D u_texture;
62+ uniform sampler2D u_texture;
6363
64- uniform vec2 u_projection;
65- uniform vec2 u_size;
66- uniform float u_stops[${ props . stops . length } ];
67- uniform vec4 u_colors[${ props . colors . length } ];
64+ uniform vec2 u_projection;
65+ uniform vec2 u_size;
66+ uniform float u_stops[MAX_STOPS ];
67+ uniform vec4 u_colors[MAX_STOPS ];
6868
69- varying vec4 v_color;
70- varying vec2 v_textureCoords;
69+ varying vec4 v_color;
70+ varying vec2 v_textureCoords;
7171
72- vec2 calcPoint(float d, float angle) {
73- return d * vec2(cos(angle), sin(angle)) + (u_dimensions * 0.5);
74- }
72+ vec2 calcPoint(float d, float angle) {
73+ return d * vec2(cos(angle), sin(angle)) + (u_dimensions * 0.5);
74+ }
7575
76- void main() {
77- vec4 color = texture2D(u_texture, v_textureCoords) * v_color;
78- vec2 point = v_textureCoords.xy * u_dimensions;
79- float dist = length((point - u_projection) / u_size);
80- ${ genGradientColors ( props . stops . length ) }
81- gl_FragColor = mix(color, colorOut, clamp(colorOut.a, 0.0, 1.0));
82- }
83- ` ;
76+ vec4 getGradientColor(float dist) {
77+ dist = clamp(dist, 0.0, 1.0);
78+
79+ if(dist <= u_stops[0]) {
80+ return u_colors[0];
81+ }
82+
83+ if(dist >= u_stops[LAST_STOP]) {
84+ return u_colors[LAST_STOP];
85+ }
86+
87+ for(int i = 0; i < LAST_STOP; i++) {
88+ float left = u_stops[i];
89+ float right = u_stops[i + 1];
90+ if(dist >= left && dist <= right) {
91+ float lDist = smoothstep(left, right, dist);
92+ return mix(u_colors[i], u_colors[i + 1], lDist);
93+ }
94+ }
95+ }
96+
97+ void main() {
98+ vec4 color = texture2D(u_texture, v_textureCoords) * v_color;
99+ vec2 point = v_textureCoords.xy * u_dimensions;
100+ float dist = length((point - u_projection) / u_size);
101+
102+ vec4 colorOut = getGradientColor(dist);
103+ gl_FragColor = mix(color, colorOut, clamp(colorOut.a, 0.0, 1.0));
104+ }
105+ ` ;
84106 } ,
85107} ;
0 commit comments