diff options
| author | Matt Kosarek <matt.kosarek@canonical.com> | 2025-12-29 10:02:38 -0500 |
|---|---|---|
| committer | Matt Kosarek <matt.kosarek@canonical.com> | 2025-12-29 10:02:38 -0500 |
| commit | fbc5d7dd1a9192c29daccdf12a7a067037ff2c2a (patch) | |
| tree | 399ade27d2e76a40956aa45a789db98841e5ae62 | |
| parent | 301d0aa4d61b21d4daf703672aa4b1d438651296 (diff) | |
Improve the sun shader with the help of AI
| -rw-r--r-- | themes/dist/output.js | 7 | ||||
| -rwxr-xr-x | themes/dist/output.wasm | bin | 99246 -> 101730 bytes | |||
| -rw-r--r-- | themes/meson.build | 24 | ||||
| -rw-r--r-- | themes/src/_shaders/sun.frag | 40 | ||||
| -rw-r--r-- | themes/src/_shaders/sun.vert | 27 | ||||
| -rw-r--r-- | themes/src/shaders/sun_frag.cpp | 40 | ||||
| -rw-r--r-- | themes/src/shaders/sun_vert.cpp | 27 | ||||
| -rw-r--r-- | themes/src/summer/summer_theme.cpp | 8 | ||||
| -rw-r--r-- | themes/src/summer/summer_theme.h | 2 |
9 files changed, 153 insertions, 22 deletions
diff --git a/themes/dist/output.js b/themes/dist/output.js index 6f95b3e..011ac68 100644 --- a/themes/dist/output.js +++ b/themes/dist/output.js @@ -3170,6 +3170,11 @@ function dbg(...args) { } }; + var _glUniform1f = (location, v0) => { + GLctx.uniform1f(webglGetUniformLocation(location), v0); + }; + + var miniTempWebGLFloatBuffers = []; var _glUniformMatrix4fv = (location, count, transpose, value) => { @@ -3363,6 +3368,8 @@ var wasmImports = { /** @export */ glShaderSource: _glShaderSource, /** @export */ + glUniform1f: _glUniform1f, + /** @export */ glUniformMatrix4fv: _glUniformMatrix4fv, /** @export */ glUseProgram: _glUseProgram, diff --git a/themes/dist/output.wasm b/themes/dist/output.wasm Binary files differindex 24a3b4f..98fd466 100755 --- a/themes/dist/output.wasm +++ b/themes/dist/output.wasm diff --git a/themes/meson.build b/themes/meson.build index 9c9c76b..1e4c9f1 100644 --- a/themes/meson.build +++ b/themes/meson.build @@ -69,6 +69,30 @@ inc = include_directories( 'src/tools' ) +# Custom target to build shaders from GLSL files +node = find_program('node') +shader_script = meson.project_source_root() + '/src/tools/shader.js' + +# List all GLSL shader files as inputs +shader_inputs = files( + 'src/_shaders/renderer2d.frag', + 'src/_shaders/renderer2d.vert', + 'src/_shaders/renderer3d.frag', + 'src/_shaders/renderer3d.vert', + 'src/_shaders/sun.frag', + 'src/_shaders/sun.vert' +) + +# Custom target that runs whenever shader files change +sh = find_program('sh') +build_shaders = custom_target('build_shaders', + input: shader_inputs, + output: 'shaders.stamp', + command: [sh, '-c', node.full_path() + ' ' + shader_script + ' && touch @OUTPUT@'], + build_always_stale: true, + build_by_default: true +) + # Build the executable output_exe = executable('output', sources, diff --git a/themes/src/_shaders/sun.frag b/themes/src/_shaders/sun.frag index 8463e06..028c0c2 100644 --- a/themes/src/_shaders/sun.frag +++ b/themes/src/_shaders/sun.frag @@ -1,5 +1,43 @@ varying lowp vec4 VertexColor; +varying lowp vec2 TexCoord; +uniform lowp float time; + +// Simple noise function for edge distortion +lowp float noise(lowp vec2 p) { + return sin(p.x * 10.0 + time) * cos(p.y * 10.0 + time * 0.5) * 0.5 + 0.5; +} void main() { - gl_FragColor = VertexColor; + // TexCoord is now normalized: center is (0,0), edges are at distance ~1 + lowp float dist = length(TexCoord); + + // Add animated noise to the edge + lowp float angle = atan(TexCoord.y, TexCoord.x); + lowp float wave = sin(angle * 8.0 + time * 2.0) * 0.05 + sin(angle * 4.0 - time * 1.5) * 0.03; + lowp float noiseValue = noise(TexCoord + time * 0.1) * 0.02; + + // Create soft edge using smoothstep - ensure fade reaches zero at the actual edge + lowp float innerEdge = 0.2; + lowp float outerEdge = 1.0; + lowp float alpha = 1.0 - smoothstep(innerEdge, outerEdge, dist); + + // Apply wave distortion to the edge + alpha *= 1.0 - smoothstep(0.85 + wave + noiseValue * 2.0, 1.0, dist); + + // Make edges even more transparent with additional fade + alpha = alpha * alpha * alpha; + + // Add slight glow effect at the edge + lowp float glow = smoothstep(0.5, 0.8, dist) * (1.0 - smoothstep(0.8, 1.0, dist)); + + // Create orange gradient from center + lowp vec3 orangeColor = vec3(1.0, 0.5, 0.1); + lowp float centerGradient = smoothstep(0.6, 0.0, dist); + lowp vec3 baseColor = mix(VertexColor.rgb, orangeColor, centerGradient * 0.6); + + // Mix in the glow with a brighter color + lowp vec3 glowColor = baseColor * 1.3; + lowp vec3 finalColor = mix(baseColor, glowColor, glow * 0.5); + + gl_FragColor = vec4(finalColor, VertexColor.a * alpha); } diff --git a/themes/src/_shaders/sun.vert b/themes/src/_shaders/sun.vert index 76150f0..5ed77d7 100644 --- a/themes/src/_shaders/sun.vert +++ b/themes/src/_shaders/sun.vert @@ -1,13 +1,22 @@ -attribute vec2 position; -attribute vec4 color; -attribute mat4 vMatrix; -uniform mat4 projection; -uniform mat4 model; +attribute vec2 position; +attribute vec4 color; +attribute mat4 vMatrix; +uniform mat4 projection; +uniform mat4 model; varying lowp vec4 VertexColor; +varying lowp vec2 TexCoord; -void main() { - vec4 fragmentPosition = projection * model * vMatrix * vec4(position.x, position.y, 0, 1); - gl_Position = fragmentPosition; - VertexColor = color; +void main() { + vec4 fragmentPosition = projection * model * vMatrix * vec4(position.x, position.y, 0, 1); + gl_Position = fragmentPosition; + VertexColor = color; + // Normalize the position - the center is at (0,0) and edge vertices are at distance 'radius' + // We want TexCoord to be in the range roughly [-1, 1] at the edges + lowp float maxDist = length(position); + if (maxDist > 0.1) { + TexCoord = position / maxDist; + } else { + TexCoord = vec2(0.0, 0.0); + } } diff --git a/themes/src/shaders/sun_frag.cpp b/themes/src/shaders/sun_frag.cpp index d1ea160..30553e0 100644 --- a/themes/src/shaders/sun_frag.cpp +++ b/themes/src/shaders/sun_frag.cpp @@ -1,8 +1,46 @@ #include "sun_frag.h" const char* shader_sun_frag = "varying lowp vec4 VertexColor; \n" +"varying lowp vec2 TexCoord; \n" +"uniform lowp float time; \n" +" \n" +"// Simple noise function for edge distortion \n" +"lowp float noise(lowp vec2 p) { \n" +" return sin(p.x * 10.0 + time) * cos(p.y * 10.0 + time * 0.5) * 0.5 + 0.5; \n" +"} \n" " \n" "void main() { \n" -" gl_FragColor = VertexColor; \n" +" // TexCoord is now normalized: center is (0,0), edges are at distance ~1 \n" +" lowp float dist = length(TexCoord); \n" +" \n" +" // Add animated noise to the edge \n" +" lowp float angle = atan(TexCoord.y, TexCoord.x); \n" +" lowp float wave = sin(angle * 8.0 + time * 2.0) * 0.05 + sin(angle * 4.0 - time * 1.5) * 0.03; \n" +" lowp float noiseValue = noise(TexCoord + time * 0.1) * 0.02; \n" +" \n" +" // Create soft edge using smoothstep - ensure fade reaches zero at the actual edge \n" +" lowp float innerEdge = 0.2; \n" +" lowp float outerEdge = 1.0; \n" +" lowp float alpha = 1.0 - smoothstep(innerEdge, outerEdge, dist); \n" +" \n" +" // Apply wave distortion to the edge \n" +" alpha *= 1.0 - smoothstep(0.85 + wave + noiseValue * 2.0, 1.0, dist); \n" +" \n" +" // Make edges even more transparent with additional fade \n" +" alpha = alpha * alpha * alpha; \n" +" \n" +" // Add slight glow effect at the edge \n" +" lowp float glow = smoothstep(0.5, 0.8, dist) * (1.0 - smoothstep(0.8, 1.0, dist)); \n" +" \n" +" // Create orange gradient from center \n" +" lowp vec3 orangeColor = vec3(1.0, 0.5, 0.1); \n" +" lowp float centerGradient = smoothstep(0.6, 0.0, dist); \n" +" lowp vec3 baseColor = mix(VertexColor.rgb, orangeColor, centerGradient * 0.6); \n" +" \n" +" // Mix in the glow with a brighter color \n" +" lowp vec3 glowColor = baseColor * 1.3; \n" +" lowp vec3 finalColor = mix(baseColor, glowColor, glow * 0.5); \n" +" \n" +" gl_FragColor = vec4(finalColor, VertexColor.a * alpha); \n" "} \n" " \n"; diff --git a/themes/src/shaders/sun_vert.cpp b/themes/src/shaders/sun_vert.cpp index ca617c0..bacf3a6 100644 --- a/themes/src/shaders/sun_vert.cpp +++ b/themes/src/shaders/sun_vert.cpp @@ -1,16 +1,25 @@ #include "sun_vert.h" const char* shader_sun_vert = " \n" -"attribute vec2 position; \n" -"attribute vec4 color; \n" -"attribute mat4 vMatrix; \n" -"uniform mat4 projection; \n" -"uniform mat4 model; \n" +"attribute vec2 position; \n" +"attribute vec4 color; \n" +"attribute mat4 vMatrix; \n" +"uniform mat4 projection; \n" +"uniform mat4 model; \n" "varying lowp vec4 VertexColor; \n" +"varying lowp vec2 TexCoord; \n" " \n" -"void main() { \n" -" vec4 fragmentPosition = projection * model * vMatrix * vec4(position.x, position.y, 0, 1); \n" -" gl_Position = fragmentPosition; \n" -" VertexColor = color; \n" +"void main() { \n" +" vec4 fragmentPosition = projection * model * vMatrix * vec4(position.x, position.y, 0, 1); \n" +" gl_Position = fragmentPosition; \n" +" VertexColor = color; \n" +" // Normalize the position - the center is at (0,0) and edge vertices are at distance 'radius' \n" +" // We want TexCoord to be in the range roughly [-1, 1] at the edges \n" +" lowp float maxDist = length(position); \n" +" if (maxDist > 0.1) { \n" +" TexCoord = position / maxDist; \n" +" } else { \n" +" TexCoord = vec2(0.0, 0.0); \n" +" } \n" "} \n" " \n"; diff --git a/themes/src/summer/summer_theme.cpp b/themes/src/summer/summer_theme.cpp index 6935ad1..6d2cfec 100644 --- a/themes/src/summer/summer_theme.cpp +++ b/themes/src/summer/summer_theme.cpp @@ -59,19 +59,23 @@ void Sun::load(Renderer2d* renderer) { indices.add(second); indices.add(third); } - + mesh.load(&vertices.data[0], vertices.numElements, &indices.data[0], indices.numElements, renderer); mesh.model = Mat4x4().translateByVec2(Vector2(renderer->context->width / 2.f, renderer->context->height / 2.f)); + + timeUniform = getShaderUniform(renderer->shader, "time"); + vertices.deallocate(); indices.deallocate(); } void Sun::update(f32 dtSeconds) { - + elapsedTime += dtSeconds; } void Sun::render(Renderer2d* renderer) { setShaderMat4(renderer->uniforms.model, mesh.model); + glUniform1f(timeUniform, elapsedTime); glBindVertexArray(mesh.vao); glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0); glBindVertexArray(0); diff --git a/themes/src/summer/summer_theme.h b/themes/src/summer/summer_theme.h index cd25ff5..eb404fd 100644 --- a/themes/src/summer/summer_theme.h +++ b/themes/src/summer/summer_theme.h @@ -8,6 +8,8 @@ struct Sun { f32 radius = 20.f; i32 sectors = 180; Mesh2D mesh; + f32 elapsedTime = 0.f; + i32 timeUniform = -1; void load(Renderer2d* renderer); void update(f32 dtSeconds); |
