This commit is contained in:
2025-07-14 12:56:16 +02:00
parent d64b3839e8
commit 2cf306ee8c
2 changed files with 72 additions and 9 deletions

View File

@ -185,7 +185,22 @@ class ShaderWorker {
'midLevel',
'trebleLevel',
'bpm',
'_t',
'bx',
'by',
'sx',
'sy',
'qx',
'qy',
`
// Shader-specific helper functions
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
const lerp = (a, b, t) => a + (b - a) * t;
const smooth = (edge, x) => { const t = Math.min(Math.max((x - edge) / (1 - edge), 0), 1); return t * t * (3 - 2 * t); };
const step = (edge, x) => x < edge ? 0 : 1;
const fract = (x) => x - Math.floor(x);
const mix = (a, b, t) => a + (b - a) * t;
// Timeout protection
const startTime = performance.now();
let iterations = 0;
@ -223,7 +238,7 @@ class ShaderWorker {
private isStaticExpression(code: string): boolean {
// Check if code contains any variables using regex for better accuracy
const variablePattern = /\b(x|y|t|i|r|a|u|v|c|f|d|n|b|bn|bs|be|bw|m|l|k|s|e|w|h|p|z|j|o|g|bpm|mouse[XY]|mousePressed|mouseV[XY]|mouseClickTime|touchCount|touch[01][XY]|pinchScale|pinchRotation|accel[XYZ]|gyro[XYZ]|audioLevel|bassLevel|midLevel|trebleLevel)\b/;
const variablePattern = /\b(x|y|t|i|r|a|u|v|c|f|d|n|b|bn|bs|be|bw|m|l|k|s|e|w|h|p|z|j|o|g|bpm|bx|by|sx|sy|qx|qy|mouse[XY]|mousePressed|mouseV[XY]|mouseClickTime|touchCount|touch[01][XY]|pinchScale|pinchRotation|accel[XYZ]|gyro[XYZ]|audioLevel|bassLevel|midLevel|trebleLevel)\b/;
return !variablePattern.test(code);
}
@ -482,6 +497,14 @@ class ShaderWorker {
const oscillation = Math.sin(time * 2 * Math.PI + radius * 0.1); // wave oscillation
const goldenRatio = 1.618033988749; // golden ratio constant
// Calculate new spatial variables
const bx = x >> 4; // Block x coordinate (16-pixel blocks)
const by = actualY >> 4; // Block y coordinate (16-pixel blocks)
const sx = x - (fullWidth >> 1); // Signed x coordinate (centered at origin)
const sy = actualY - (fullHeight >> 1); // Signed y coordinate (centered at origin)
const qx = x >> 3; // Quarter block x coordinate (8-pixel blocks)
const qy = actualY >> 3; // Quarter block y coordinate (8-pixel blocks)
const value = this.compiledFunction!(
x,
actualY,
@ -535,7 +558,14 @@ class ShaderWorker {
message.bassLevel || 0,
message.midLevel || 0,
message.trebleLevel || 0,
message.bpm || 120
message.bpm || 120,
(mod: number) => time % mod,
bx,
by,
sx,
sy,
qx,
qy
);
const safeValue = isFinite(value) ? value : 0;
const [r, g, b] = this.calculateColor(
@ -1113,6 +1143,9 @@ class ShaderWorker {
processedCode = processedCode.replace(/\bSQRT1_2\b/g, 'Math.SQRT1_2');
processedCode = processedCode.replace(/\bSQRT2\b/g, 'Math.SQRT2');
// Add custom time function t() with modulo wrapping
processedCode = processedCode.replace(/\bt\s*\(/g, '_t(');
return processedCode;
}

View File

@ -72,7 +72,7 @@ export function HelpPopup() {
<strong>x, y</strong> - Pixel coordinates
</p>
<p>
<strong>t</strong> - Time (enables animation)
<strong>t</strong> - Time (enables animation) - also available as t(n) for modulo wrapping
</p>
<p>
<strong>bpm</strong> - Current BPM from tap tempo (default: 120)
@ -105,6 +105,15 @@ export function HelpPopup() {
<div className="help-section">
<h4>Core Variables - Advanced</h4>
<p>
<strong>bx, by</strong> - Block coordinates (16-pixel chunks, great for pixelated effects)
</p>
<p>
<strong>sx, sy</strong> - Signed coordinates (centered at origin, negative to positive)
</p>
<p>
<strong>qx, qy</strong> - Quarter-block coordinates (8-pixel chunks, finer than bx/by)
</p>
<p>
<strong>n</strong> - Noise value (0.0 to 1.0)
</p>
@ -239,20 +248,41 @@ export function HelpPopup() {
<strong>sin, cos, tan</strong> - Trigonometric functions
</p>
<p>
<strong>abs, sqrt, pow</strong> - Absolute, square root, power
<strong>asin, acos, atan, atan2</strong> - Inverse trigonometric functions
</p>
<p>
<strong>floor, ceil, round</strong> - Rounding functions
<strong>abs, sqrt, cbrt, pow</strong> - Absolute, square root, cube root, power
</p>
<p>
<strong>min, max</strong> - Minimum and maximum
<strong>floor, ceil, round, trunc</strong> - Rounding functions
</p>
<p>
<strong>min, max, sign</strong> - Minimum, maximum, sign (-1/0/1)
</p>
<p>
<strong>log, log10, log2, exp</strong> - Logarithmic and exponential
</p>
<p>
<strong>clamp(val, min, max)</strong> - Constrain value between min and max
</p>
<p>
<strong>lerp(a, b, t)</strong> - Linear interpolation between a and b
</p>
<p>
<strong>smooth(edge, x)</strong> - Smooth step function for gradients
</p>
<p>
<strong>step(edge, x)</strong> - Step function (0 if x&lt;edge, 1 otherwise)
</p>
<p>
<strong>fract(x)</strong> - Fractional part (x - floor(x))
</p>
<p>
<strong>mix(a, b, t)</strong> - Alias for lerp
</p>
<p>
<strong>random</strong> - Random number 0-1
</p>
<p>
<strong>log, exp</strong> - Natural logarithm, exponential
</p>
<p>
<strong>PI, E</strong> - Math constants
</p>