refac: optimize hueToRGB function & refac angle calc using TAU const

This commit is contained in:
Florian Sylvain
2025-04-06 01:53:46 +02:00
parent 1b90a6e3fe
commit 622667f855
+7 -12
View File
@@ -1,6 +1,8 @@
@group(0) @binding(0) var<uniform> grid: vec2f; @group(0) @binding(0) var<uniform> grid: vec2f;
@group(0) @binding(1) var<uniform> time: f32; @group(0) @binding(1) var<uniform> time: f32;
const TAU: f32 = 6.28318530718;
struct VertexInput { struct VertexInput {
@location(0) pos: vec2f, @location(0) pos: vec2f,
@builtin(instance_index) instance: u32, @builtin(instance_index) instance: u32,
@@ -25,21 +27,14 @@ fn vertexMain(input: VertexInput) -> VertexOutput {
} }
fn hueToRGB(hue: f32) -> vec3f { fn hueToRGB(hue: f32) -> vec3f {
let r = 0.5 + 0.5 * sin(6.28318 * (hue + 0.0)); return 0.5 + 0.5 * sin(TAU * (vec3f(hue) + vec3f(0.0, 0.33, 0.66)));
let g = 0.5 + 0.5 * sin(6.28318 * (hue + 0.33));
let b = 0.5 + 0.5 * sin(6.28318 * (hue + 0.66));
return vec3f(r, g, b);
} }
@fragment @fragment
fn fragmentMain(input: VertexOutput) -> @location(0) vec4f { fn fragmentMain(input: VertexOutput) -> @location(0) vec4f {
let center = grid * 0.5; let delta = input.cell - grid * 0.5;
let delta = input.cell - center;
let angle = atan2(delta.y, delta.x); let angle = atan2(delta.y, delta.x);
let normalizedAngle = (angle / (2.0 * 3.14159)) + 0.5; let normalizedAngle = (angle / TAU) + 0.5;
let hue = fract(normalizedAngle + time * 0.3);
let speed = 0.3; return vec4f(hueToRGB(hue), 1.0);
let hue = fract(normalizedAngle + time * speed);
let color = hueToRGB(hue);
return vec4f(color, 1.0);
} }