mirror of
https://github.com/emilk/egui.git
synced 2026-09-03 15:20:05 -04:00
Make egui_glow painter to work on web (#868)
Add WebGL1 and WebGL2 support to glow painter. Add "glow" feature to egui_web to use the glow painter there. Make winit an optional part of egui_glow
This commit is contained in:
@@ -3,71 +3,75 @@ precision mediump float;
|
||||
#endif
|
||||
|
||||
uniform sampler2D u_sampler;
|
||||
#if defined(GL_ES) || __VERSION__ < 140
|
||||
varying vec4 v_rgba;
|
||||
varying vec2 v_tc;
|
||||
#else
|
||||
|
||||
#ifdef NEW_SHADER_INTERFACE
|
||||
in vec4 v_rgba;
|
||||
in vec2 v_tc;
|
||||
out vec4 f_color;
|
||||
// a dirty hack applied to support webGL2
|
||||
#define gl_FragColor f_color
|
||||
#define texture2D texture
|
||||
#else
|
||||
varying vec4 v_rgba;
|
||||
varying vec2 v_tc;
|
||||
#endif
|
||||
|
||||
#ifdef GL_ES
|
||||
#ifndef SRGB_SUPPORTED
|
||||
// 0-255 sRGB from 0-1 linear
|
||||
vec3 srgb_from_linear(vec3 rgb) {
|
||||
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
|
||||
vec3 lower = rgb * vec3(3294.6);
|
||||
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
|
||||
vec3 lower = rgb * vec3(3294.6);
|
||||
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
}
|
||||
|
||||
vec4 srgba_from_linear(vec4 rgba) {
|
||||
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
|
||||
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
|
||||
}
|
||||
|
||||
#if __VERSION__ < 300
|
||||
// 0-1 linear from 0-255 sRGB
|
||||
vec3 linear_from_srgb(vec3 srgb) {
|
||||
bvec3 cutoff = lessThan(srgb, vec3(10.31475));
|
||||
vec3 lower = srgb / vec3(3294.6);
|
||||
vec3 higher = pow((srgb + vec3(14.025)) / vec3(269.025), vec3(2.4));
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
bvec3 cutoff = lessThan(srgb, vec3(10.31475));
|
||||
vec3 lower = srgb / vec3(3294.6);
|
||||
vec3 higher = pow((srgb + vec3(14.025)) / vec3(269.025), vec3(2.4));
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
}
|
||||
|
||||
vec4 linear_from_srgba(vec4 srgba) {
|
||||
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
|
||||
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef GL_ES
|
||||
void main() {
|
||||
#if __VERSION__ < 300
|
||||
// We must decode the colors, since WebGL doesn't come with sRGBA textures:
|
||||
vec4 texture_rgba = linear_from_srgba(texture2D(u_sampler, v_tc) * 255.0);
|
||||
#else
|
||||
// The texture is set up with `SRGB8_ALPHA8`, so no need to decode here!
|
||||
vec4 texture_rgba = texture2D(u_sampler, v_tc);
|
||||
#endif
|
||||
// We must decode the colors, since WebGL doesn't come with sRGBA textures:
|
||||
vec4 texture_rgba = linear_from_srgba(texture2D(u_sampler, v_tc) * 255.0);
|
||||
/// Multiply vertex color with texture color (in linear space).
|
||||
gl_FragColor = v_rgba * texture_rgba;
|
||||
|
||||
/// Multiply vertex color with texture color (in linear space).
|
||||
gl_FragColor = v_rgba * texture_rgba;
|
||||
// WebGL doesn't support linear blending in the framebuffer,
|
||||
// so we do a hack here where we change the premultiplied alpha
|
||||
// to do the multiplication in gamma space instead:
|
||||
|
||||
// We must gamma-encode again since WebGL doesn't support linear blending in the framebuffer.
|
||||
gl_FragColor = srgba_from_linear(v_rgba * texture_rgba) / 255.0;
|
||||
// Unmultiply alpha:
|
||||
if (gl_FragColor.a > 0.0) {
|
||||
gl_FragColor.rgb /= gl_FragColor.a;
|
||||
}
|
||||
|
||||
// WebGL doesn't support linear blending in the framebuffer,
|
||||
// so we apply this hack to at least get a bit closer to the desired blending:
|
||||
gl_FragColor.a = pow(gl_FragColor.a, 1.6); // Empiric nonsense
|
||||
// Empiric tweak to make e.g. shadows look more like they should:
|
||||
gl_FragColor.a *= sqrt(gl_FragColor.a);
|
||||
|
||||
// To gamma:
|
||||
gl_FragColor = srgba_from_linear(gl_FragColor) / 255.0;
|
||||
|
||||
// Premultiply alpha, this time in gamma space:
|
||||
if (gl_FragColor.a > 0.0) {
|
||||
gl_FragColor.rgb *= gl_FragColor.a;
|
||||
}
|
||||
}
|
||||
#else
|
||||
#else
|
||||
void main() {
|
||||
// The texture sampler is sRGB aware, and OpenGL already expects linear rgba output
|
||||
// so no need for any sRGB conversions here:
|
||||
#if __VERSION__ < 140
|
||||
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
|
||||
#else
|
||||
f_color = v_rgba * texture(u_sampler, v_tc);
|
||||
#endif
|
||||
// The texture sampler is sRGB aware, and OpenGL already expects linear rgba output
|
||||
// so no need for any sRGB conversions here:
|
||||
|
||||
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
|
||||
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
22
egui_glow/src/shader/post_fragment_100es.glsl
Normal file
22
egui_glow/src/shader/post_fragment_100es.glsl
Normal file
@@ -0,0 +1,22 @@
|
||||
precision mediump float;
|
||||
uniform sampler2D u_sampler;
|
||||
varying vec2 v_tc;
|
||||
|
||||
// 0-255 sRGB from 0-1 linear
|
||||
vec3 srgb_from_linear(vec3 rgb) {
|
||||
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
|
||||
vec3 lower = rgb * vec3(3294.6);
|
||||
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
}
|
||||
|
||||
// 0-255 sRGBA from 0-1 linear
|
||||
vec4 srgba_from_linear(vec4 rgba) {
|
||||
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
|
||||
}
|
||||
|
||||
void main() {
|
||||
gl_FragColor = texture2D(u_sampler, v_tc);
|
||||
|
||||
gl_FragColor = srgba_from_linear(gl_FragColor) / 255.;
|
||||
}
|
||||
8
egui_glow/src/shader/post_vertex_100es.glsl
Normal file
8
egui_glow/src/shader/post_vertex_100es.glsl
Normal file
@@ -0,0 +1,8 @@
|
||||
precision mediump float;
|
||||
attribute vec2 a_pos;
|
||||
varying vec2 v_tc;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0);
|
||||
v_tc = a_pos;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#if !defined(GL_ES) && __VERSION__ >= 140
|
||||
#ifdef NEW_SHADER_INTERFACE
|
||||
#define I in
|
||||
#define O out
|
||||
#define V(x) x
|
||||
|
||||
Reference in New Issue
Block a user