Skip to content

Commit b5bda84

Browse files
authored
fixed gradient effects (#656)
Someone found out that bright colors caused some issues displaying the wrong color. In order to fix this, I changed the fragment shader parts for both effects, they should now properly render the right color.
2 parents 5a6e1f2 + bf37fe7 commit b5bda84

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,6 @@ export class LinearGradientEffect extends ShaderEffect {
121121
};
122122

123123
static override methods: Record<string, string> = {
124-
fromLinear: `
125-
vec4 function(vec4 linearRGB) {
126-
vec4 higher = vec4(1.055)*pow(linearRGB, vec4(1.0/2.4)) - vec4(0.055);
127-
vec4 lower = linearRGB * vec4(12.92);
128-
return mix(higher, lower, 1.0);
129-
}
130-
`,
131-
toLinear: `
132-
vec4 function(vec4 sRGB) {
133-
vec4 higher = pow((sRGB + vec4(0.055))/vec4(1.055), vec4(2.4));
134-
vec4 lower = sRGB/vec4(12.92);
135-
return mix(higher, lower, 1.0);
136-
}
137-
`,
138124
calcPoint: `
139125
vec2 function(float d, float angle) {
140126
return d * vec2(cos(angle), sin(angle)) + (u_dimensions * 0.5);
@@ -162,10 +148,29 @@ export class LinearGradientEffect extends ShaderEffect {
162148
vec2 gradVec = t - f;
163149
float dist = dot(v_textureCoordinate.xy * u_dimensions - f, gradVec) / dot(gradVec, gradVec);
164150
165-
float stopCalc = (dist - stops[0]) / (stops[1] - stops[0]);
166-
vec4 colorOut = $fromLinear(mix($toLinear(colors[0]), $toLinear(colors[1]), stopCalc));
167-
${this.ColorLoop(colors)}
168-
return mix(maskColor, colorOut, clamp(colorOut.a, 0.0, 1.0));
151+
//return early if dist is lower or equal to first stop
152+
if(dist <= stops[0]) {
153+
return mix(maskColor, colors[0], clamp(colors[0].a, 0.0, 1.0));
154+
}
155+
const int amount = ${colors};
156+
const int last = amount - 1;
157+
158+
if(dist >= stops[last]) {
159+
return mix(maskColor, colors[last], clamp(colors[last].a, 0.0, 1.0));
160+
}
161+
162+
for(int i = 0; i < last; i++) {
163+
float left = stops[i];
164+
float right = stops[i + 1];
165+
if(dist >= left && dist <= right) {
166+
float localDist = smoothstep(left, right, dist);
167+
vec4 colorOut = mix(colors[i], colors[i + 1], localDist);
168+
return mix(maskColor, colorOut, clamp(colorOut.a, 0.0, 1.0));
169+
}
170+
}
171+
172+
//final fallback
173+
return mix(maskColor, colors[last], clamp(colors[last].a, 0.0, 1.0));
169174
`;
170175
};
171176
}

src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ export class RadialGradientEffect extends ShaderEffect {
141141
},
142142
};
143143

144+
static override methods: Record<string, string> = {
145+
getGradientColor: `
146+
float function(float dist) {
147+
return clamp(-dist, 0.0, 1.0);
148+
}
149+
`,
150+
};
151+
144152
static ColorLoop = (amount: number): string => {
145153
let loop = '';
146154
for (let i = 2; i < amount; i++) {
@@ -159,10 +167,30 @@ export class RadialGradientEffect extends ShaderEffect {
159167
160168
float dist = length((point - projection) / vec2(width, height));
161169
162-
float stopCalc = (dist - stops[0]) / (stops[1] - stops[0]);
163-
vec4 colorOut = mix(colors[0], colors[1], stopCalc);
164-
${this.ColorLoop(colors)}
165-
return mix(maskColor, colorOut, clamp(colorOut.a, 0.0, 1.0));
170+
dist = clamp(dist, 0.0, 1.0);
171+
//return early if dist is lower or equal to first stop
172+
if(dist <= stops[0]) {
173+
return mix(maskColor, colors[0], clamp(colors[0].a, 0.0, 1.0));
174+
}
175+
const int amount = ${colors};
176+
const int last = amount - 1;
177+
178+
if(dist >= stops[last]) {
179+
return mix(maskColor, colors[last], clamp(colors[last].a, 0.0, 1.0));
180+
}
181+
182+
for(int i = 0; i < last; i++) {
183+
float left = stops[i];
184+
float right = stops[i + 1];
185+
if(dist >= left && dist <= right) {
186+
float localDist = smoothstep(left, right, dist);
187+
vec4 colorOut = mix(colors[i], colors[i + 1], localDist);
188+
return mix(maskColor, colorOut, clamp(colorOut.a, 0.0, 1.0));
189+
}
190+
}
191+
192+
//final fallback
193+
return mix(maskColor, colors[last], clamp(colors[last].a, 0.0, 1.0));
166194
`;
167195
};
168196
}
-6.33 KB
Loading
21.6 KB
Loading

0 commit comments

Comments
 (0)