yandex-music-rs/shaders/vibe.wgsl
2026-11-27 13:17:37 +03:00

346 lines
11 KiB
WebGPU Shading Language

// Verbatim 1-to-1 WGSL port of Yandex Music Desktop 5.129.0's official WebGL My Vibe shader
// Extracted from `original_yandex_music/decompiled/vibe_wheel_foryou_spec.md` (§1.2):
// - Exact `desktopScale = 0.35`, `vRotation[3] = [(-0.3, 0.3, 0.2), (-0.3, -0.3, -0.2), (-0.3, -0.3, 0.2)]`
// - Exact `CIRCLE_RADIUS_BASE = 0.95`, `CIRCLE_RADIUS_STEP = 0.15`, `CIRCLE_WIDTH_BASE = 0.8`, `CIRCLE_WIDTH_STEP = 0.2`
// - Exact 6-color dual gradient `vColor[0..6]` (`mix(color1, color2, localUv.y * 2.0)`)
// - Exact `makeNoiseBlob2` + `makeBlob` + `light(..., 10.0, d0)` + `triNoise3D` sunburst rays
// - Lossless CPU-hoisted per-frame invariants (112-byte Vulkan push constants) & exact conservative radial reject
// - Exact `.VibeWidgetAnimation_root_visible { opacity: 0.8 }` premultiplied alpha output (`color.rgb * 0.80, color.a * 0.80`)
struct VibeGpuPush {
// [uv_scale.x, uv_scale.y, v_time, max_outer_radius]
uv_scale_time_maxr: vec4<f32>,
// [rot0.x, rot0.y, rot1.x, rot1.y]
rot01: vec4<f32>,
// [rot2.x, rot2.y, inner_add0, inner_add1]
rot2_inner01: vec4<f32>,
// [inner_add2, glow_int0, glow_int1, glow_int2]
inner2_glow012: vec4<f32>,
// [a0, a1, a2, 0.0]
audio012_pad: vec4<f32>,
// Packed RGB8: [c0_start, c1_start, c2_start, c0_end]
colors_a: vec4<u32>,
// Packed RGB8: [c1_end, c2_end, 0, 0]
colors_b: vec4<u32>,
}
var<push_constant> u: VibeGpuPush;
struct VertOut {
@builtin(position) pos: vec4<f32>,
@location(0) uv: vec2<f32>,
}
@vertex
fn vs_main(@builtin(vertex_index) idx: u32) -> VertOut {
var positions = array<vec2<f32>, 3>(
vec2<f32>(-1.0, -1.0),
vec2<f32>( 3.0, -1.0),
vec2<f32>(-1.0, 3.0),
);
var out: VertOut;
let p = positions[idx];
out.pos = vec4<f32>(p, 0.0, 1.0);
out.uv = p * 0.5 + vec2<f32>(0.5, 0.5);
return out;
}
const EFFECT_PI: f32 = 3.1415;
const MATH_PI: f32 = 3.14159265359;
const TWO_MATH_PI: f32 = 6.28318530718;
const INV_TWO_EFFECT_PI: f32 = 0.15915963712;
const CIRCLE_WIDTH_BASE: f32 = 0.8;
const CIRCLE_WIDTH_STEP: f32 = 0.2;
const SPARK_STRENGTH_BASE: f32 = 1.0;
const SPARK_STRENGTH_STEP: f32 = 0.3;
const CIRCLE_RADIUS_BASE: f32 = 0.95;
const CIRCLE_RADIUS_STEP: f32 = 0.15;
const CIRCLE_OFFSET_BASE: f32 = 0.0;
const CIRCLE_OFFSET_STEP: f32 = 1.57;
fn unpack_rgb(packed: u32) -> vec3<f32> {
let r = f32(packed & 255u) * (1.0 / 255.0);
let g = f32((packed >> 8u) & 255u) * (1.0 / 255.0);
let b = f32((packed >> 16u) & 255u) * (1.0 / 255.0);
return vec3<f32>(r, g, b);
}
fn mod289_4(x: vec4<f32>) -> vec4<f32> {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
fn mod289_3(x: vec3<f32>) -> vec3<f32> {
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
fn permute(x: vec4<f32>) -> vec4<f32> {
return mod289_4(((x * 34.0) + vec4<f32>(1.0)) * x);
}
fn taylorInvSqrt(r: vec4<f32>) -> vec4<f32> {
return vec4<f32>(1.79284291400159) - 0.85373472095314 * r;
}
fn snoise3(v: vec3<f32>) -> f32 {
let C = vec2<f32>(0.1666667, 0.3333333);
let D = vec4<f32>(0.0, 0.5, 1.0, 2.0);
let i0 = floor(v + vec3<f32>(dot(v, C.yyy)));
let x0 = v - i0 + vec3<f32>(dot(i0, C.xxx));
let g = step(x0.yzx, x0.xyz);
let l = vec3<f32>(1.0) - g;
let i1 = min(g.xyz, l.zxy);
let i2 = max(g.xyz, l.zxy);
let x1 = x0 - i1 + C.xxx;
let x2 = x0 - i2 + 2.0 * C.xxx;
let x3 = x0 - vec3<f32>(1.0) + 3.0 * C.xxx;
let i = mod289_3(i0);
let p = permute(permute(permute(
vec4<f32>(i.z) + vec4<f32>(0.0, i1.z, i2.z, 1.0))
+ vec4<f32>(i.y) + vec4<f32>(0.0, i1.y, i2.y, 1.0))
+ vec4<f32>(i.x) + vec4<f32>(0.0, i1.x, i2.x, 1.0));
let n_ = 0.142857142857;
let ns = n_ * D.wyz - D.xzx;
let j = p - 49.0 * floor(p * ns.z * ns.z);
let x_ = floor(j * ns.z);
let y_ = floor(j - 7.0 * x_);
let x = x_ * ns.x + ns.yyyy;
let y = y_ * ns.x + ns.yyyy;
let h = vec4<f32>(1.0) - abs(x) - abs(y);
let b0 = vec4<f32>(x.xy, y.xy);
let b1 = vec4<f32>(x.zw, y.zw);
let s0 = floor(b0) * 2.0 + vec4<f32>(1.0);
let s1 = floor(b1) * 2.0 + vec4<f32>(1.0);
let sh = -step(h, vec4<f32>(0.0));
let a0 = b0.xzyw + s0.xzyw * sh.xxyy;
let a1 = b1.xzyw + s1.xzyw * sh.zzww;
var p0 = vec3<f32>(a0.xy, h.x);
var p1 = vec3<f32>(a0.zw, h.y);
var p2 = vec3<f32>(a1.xy, h.z);
var p3 = vec3<f32>(a1.zw, h.w);
let norm = taylorInvSqrt(vec4<f32>(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
p0 = p0 * norm.x;
p1 = p1 * norm.y;
p2 = p2 * norm.z;
p3 = p3 * norm.w;
var m = max(vec4<f32>(0.6) - vec4<f32>(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), vec4<f32>(0.0));
m = m * m;
return 42.0 * dot(m * m, vec4<f32>(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));
}
fn tri(x: f32) -> f32 {
return abs(fract(x) - 0.5);
}
fn tri3(p: vec3<f32>) -> vec3<f32> {
let tx = tri(p.x);
return vec3<f32>(
tri(p.z + tri(p.y * 20.0)),
tri(p.z + tx),
tri(p.y + tx),
);
}
fn triNoise3D(p_in: vec3<f32>, timeShift: f32) -> f32 {
var p = p_in;
var z = 0.4;
var rz = 0.1;
var bp = p;
let ts = vec3<f32>(timeShift);
for (var i = 0; i < 5; i = i + 1) {
let dg = tri3(bp * 0.01);
p = p + (dg + ts);
bp = bp * 4.0;
z = z * 0.9;
p = p * 1.6;
rz = rz + tri(p.z + tri(0.6 * p.x + 0.1 * tri(p.y))) / z;
}
return smoothstep(0.0, 8.0, rz + sin(rz + sin(z) * 2.8) * 2.2);
}
fn shapeSpark(rawSpark: f32) -> f32 {
let s2 = rawSpark * rawSpark;
let s4 = s2 * s2;
let s8 = s4 * s4;
let spark = rawSpark * 0.2 + s8 * s2;
let gateInput = clamp(0.3 / max(spark, 0.00001), 0.0, 1.0);
let gate = gateInput * gateInput * (3.0 - 2.0 * gateInput);
return spark * gate;
}
fn light(intensity: f32, attenuation: f32, dist: f32) -> f32 {
return intensity / (1.0 + dist + dist * attenuation);
}
// Verbatim port of official GLSL `makeNoiseBlob2`
fn makeNoiseBlob2(
localUv: vec2<f32>,
color1: vec3<f32>,
color2: vec3<f32>,
offset: f32,
innerAdd: f32,
glowIntensity: f32,
vTimeHalf: f32,
) -> vec4<f32> {
let len = length(localUv);
let n0 = snoise3(vec3<f32>(localUv * 1.2 + vec2<f32>(offset), vTimeHalf + offset)) * 0.5 + 0.5;
let r0 = clamp(n0, 0.0, 1.0);
let d0 = abs(len - r0);
// Identical to GLSL `smoothstep(r0 + 0.1 + (sin(vTime + offset) + 1.0), r0, len)` (innerAdd = 1.1 + sin(vTime + offset) >= 0.1)
let v0 = 1.0 - smoothstep(r0, max(r0 + innerAdd, r0 + 0.001), len);
let v1 = light(glowIntensity, 10.0, d0);
let col = clamp(mix(color1, color2, localUv.y * 2.0) + vec3<f32>(v1), vec3<f32>(0.0), vec3<f32>(1.0));
return vec4<f32>(col, v0);
}
// Verbatim port of official GLSL `makeBlob`
fn makeBlob(
uv: vec2<f32>,
baseLen: f32,
blob: f32,
color1: vec3<f32>,
color2: vec3<f32>,
width: f32,
baseReaction: f32,
audioStrength: f32,
offset: f32,
noiseOffset: vec2<f32>,
innerAdd: f32,
glowIntensity: f32,
vTimeHalf: f32,
) -> vec4<f32> {
let outerRadius = blob + width * 0.5
+ baseReaction * (1.0 + (audioStrength * 0.6) * 50.0 * baseReaction);
if (baseLen >= outerRadius) {
return vec4<f32>(0.0);
}
var noise = makeNoiseBlob2(
uv + noiseOffset,
color1,
color2,
offset,
innerAdd,
glowIntensity,
vTimeHalf,
);
// Identical to GLSL `mix(0.0, noise.a, smoothstep(outerRadius, 0.5, len))`
let outerMask = 1.0 - smoothstep(0.5, max(outerRadius, 0.501), baseLen);
noise.a = noise.a * outerMask;
return noise;
}
@fragment
fn fs_main(in: VertOut) -> @location(0) vec4<f32> {
let uv = vec2<f32>(in.uv.x * 2.0 - 1.0, 1.0 - in.uv.y * 2.0) * u.uv_scale_time_maxr.xy;
let baseLen = length(uv);
// Exact lossless early radial reject (skips ~61% of transparent outer pixels with zero visual delta)
if (baseLen >= u.uv_scale_time_maxr.w) {
return vec4<f32>(0.0);
}
let vTime = u.uv_scale_time_maxr.z;
let vTimeHalf = vTime * 0.5;
let timeShift = vTime * 0.01;
// Exact official `getRawSpark` + `shapeSpark` radial sunburst rays (`vibe_worker_0.js`)
let angle = atan2(uv.y, uv.x);
let angleIndex = angle * INV_TWO_EFFECT_PI;
var shiftedAngle = angle + EFFECT_PI;
if (shiftedAngle > MATH_PI) {
shiftedAngle = shiftedAngle - TWO_MATH_PI;
}
let shiftedIndex = shiftedAngle * INV_TWO_EFFECT_PI;
let seamAngle = (shiftedAngle + EFFECT_PI) * 0.5;
let seamWeight = smoothstep(0.9, 1.0, sin(seamAngle));
var rawSpark = triNoise3D(vec3<f32>(angleIndex, 0.0, 0.0), timeShift);
if (seamWeight > 0.0) {
let seamSpark = triNoise3D(vec3<f32>(shiftedIndex, 0.0, shiftedIndex), timeShift);
rawSpark = mix(rawSpark, seamSpark, seamWeight);
}
let spark = shapeSpark(rawSpark);
let c0_start = unpack_rgb(u.colors_a.x);
let c1_start = unpack_rgb(u.colors_a.y);
let c2_start = unpack_rgb(u.colors_a.z);
let c0_end = unpack_rgb(u.colors_a.w);
let c1_end = unpack_rgb(u.colors_b.x);
let c2_end = unpack_rgb(u.colors_b.y);
var color = vec4<f32>(0.0, 0.0, 0.0, 0.0);
let n0 = clamp(snoise3(vec3<f32>(uv * 1.2, vTimeHalf)), -1.0, 1.0);
let sharedOffset = 0.3 * n0;
// Layer 0 (bottom)
let b0 = makeBlob(
uv, baseLen,
CIRCLE_RADIUS_BASE + sharedOffset,
c0_start, c0_end,
CIRCLE_WIDTH_BASE,
SPARK_STRENGTH_BASE * spark,
u.audio012_pad.x,
CIRCLE_OFFSET_BASE,
u.rot01.xy,
u.rot2_inner01.z,
u.inner2_glow012.y,
vTimeHalf,
);
color = vec4<f32>(mix(color.rgb, b0.rgb, b0.a), max(b0.a, color.a));
// Layer 1 (middle)
let b1 = makeBlob(
uv, baseLen,
(CIRCLE_RADIUS_BASE - CIRCLE_RADIUS_STEP) + sharedOffset,
c1_start, c1_end,
CIRCLE_WIDTH_BASE - CIRCLE_WIDTH_STEP,
(SPARK_STRENGTH_BASE - SPARK_STRENGTH_STEP) * spark,
u.audio012_pad.y,
CIRCLE_OFFSET_BASE + CIRCLE_OFFSET_STEP,
u.rot01.zw,
u.rot2_inner01.w,
u.inner2_glow012.z,
vTimeHalf,
);
color = vec4<f32>(mix(color.rgb, b1.rgb, b1.a), max(b1.a, color.a));
// Layer 2 (top)
let b2 = makeBlob(
uv, baseLen,
(CIRCLE_RADIUS_BASE - CIRCLE_RADIUS_STEP * 2.0) + sharedOffset,
c2_start, c2_end,
CIRCLE_WIDTH_BASE - CIRCLE_WIDTH_STEP * 2.0,
(SPARK_STRENGTH_BASE - SPARK_STRENGTH_STEP * 2.0) * spark,
u.audio012_pad.z,
CIRCLE_OFFSET_BASE + CIRCLE_OFFSET_STEP * 2.0,
u.rot2_inner01.xy,
u.inner2_glow012.x,
u.inner2_glow012.w,
vTimeHalf,
);
color = vec4<f32>(mix(color.rgb, b2.rgb, b2.a), max(b2.a, color.a));
// Exact `.VibeWidgetAnimation_root_visible { opacity: 0.8 }` premultiplied alpha output
// (Layer 0 mix from vec4(0.0) already premultiplies color.rgb by color.a — never multiply by out_a!)
let out_a = clamp(color.a * 0.80, 0.0, 1.0);
let out_rgb = clamp(color.rgb, vec3<f32>(0.0), vec3<f32>(1.0)) * 0.80;
return vec4<f32>(out_rgb, out_a);
}