Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(rendering): add noise texture support to gradient shader

Richie McIlroy2cd07cd0ed77
diff --git a/crates/rendering/src/shaders/gradient-or-color.wgsl b/crates/rendering/src/shaders/gradient-or-color.wgsl
index 000a8748e..cdd77ea1a 100644
--- a/crates/rendering/src/shaders/gradient-or-color.wgsl
+++ b/crates/rendering/src/shaders/gradient-or-color.wgsl
@@ -1,14 +1,71 @@
struct Uniforms {
start: vec4<f32>,
end: vec4<f32>,
- angle: f32
+ angle: f32,
+ noise_intensity: f32,
+ noise_scale: f32,
+ _padding: f32,
};
@group(0) @binding(0) var<uniform> u: Uniforms;
+fn hash22(p: vec2<f32>) -> f32 {
+ var p3 = fract(vec3<f32>(p.x, p.y, p.x) * 0.1031);
+ p3 += dot(p3, p3.yzx + 33.33);
+ return fract((p3.x + p3.y) * p3.z);
+}
+
+fn value_noise(p: vec2<f32>) -> f32 {
+ let i = floor(p);
+ let f = fract(p);
+ let u_interp = f * f * (3.0 - 2.0 * f);
+
+ return mix(
+ mix(hash22(i + vec2(0.0, 0.0)), hash22(i + vec2(1.0, 0.0)), u_interp.x),
+ mix(hash22(i + vec2(0.0, 1.0)), hash22(i + vec2(1.0, 1.0)), u_interp.x),
+ u_interp.y
+ );
+}
+
+fn fbm(p: vec2<f32>) -> f32 {
+ var value = 0.0;
+ var amplitude = 0.5;
+ var coord = p;
+ for (var i = 0; i < 4; i++) {
+ value += amplitude * value_noise(coord);
+ coord *= 2.0;
+ amplitude *= 0.5;
+ }
+ return value;
+}
+
+fn overlay_channel(base: f32, blend: f32) -> f32 {
+ if base < 0.5 {
+ return 2.0 * base * blend;
+ } else {
+ return 1.0 - 2.0 * (1.0 - base) * (1.0 - blend);
+ }
+}
+
@fragment
fn fs_main(@location(0) tex_coords: vec2<f32>) -> @location(0) vec4<f32> {
- return gradient(tex_coords);
+ var color = gradient(tex_coords);
+
+ if u.noise_intensity > 0.0 {
+ let freq = 0.3 + ((100.0 - u.noise_scale) / 100.0) * 1.2;
+ let n = fbm(tex_coords * freq * 600.0);
+
+ let blended = vec3<f32>(
+ overlay_channel(color.r, n),
+ overlay_channel(color.g, n),
+ overlay_channel(color.b, n),
+ );
+
+ let intensity = u.noise_intensity / 100.0;
+ color = vec4<f32>(mix(color.rgb, blended, intensity), color.a);
+ }
+
+ return color;
}
fn gradient(uv: vec2<f32>) -> vec4<f32> {