Files
spore-ledlab/presets/nebula-drift-preset.js
2025-10-12 10:35:56 +02:00

95 lines
3.6 KiB
JavaScript

// Nebula Drift preset for LEDLab
const BasePreset = require('./base-preset');
const { createFrame, frameToPayload, hexToRgb, samplePalette, toIndex, clamp } = require('./frame-utils');
class NebulaDriftPreset extends BasePreset {
constructor(width = 16, height = 16) {
super(width, height);
this.timeSeconds = 0;
this.defaultParameters = {
primarySpeed: 0.15,
secondarySpeed: 0.32,
waveScale: 0.75,
brightness: 1.0,
color1: '100406',
color2: '2e0f1f',
color3: '6a1731',
color4: 'b63b32',
color5: 'f48b2a',
color6: 'ffe9b0',
};
}
layeredWave(u, v, speed, offset) {
return Math.sin((u * 3 + v * 2) * Math.PI * this.getParameter('waveScale') + this.timeSeconds * speed + offset);
}
renderFrame() {
this.timeSeconds += 0.016; // Assume 60 FPS
const frame = createFrame(this.width, this.height);
const brightness = this.getParameter('brightness') || 1.0;
const paletteStops = [
{ stop: 0.0, color: hexToRgb(this.getParameter('color1') || '100406') },
{ stop: 0.25, color: hexToRgb(this.getParameter('color2') || '2e0f1f') },
{ stop: 0.5, color: hexToRgb(this.getParameter('color3') || '6a1731') },
{ stop: 0.7, color: hexToRgb(this.getParameter('color4') || 'b63b32') },
{ stop: 0.85, color: hexToRgb(this.getParameter('color5') || 'f48b2a') },
{ stop: 1.0, color: hexToRgb(this.getParameter('color6') || 'ffe9b0') },
];
for (let row = 0; row < this.height; ++row) {
const v = row / Math.max(1, this.height - 1);
for (let col = 0; col < this.width; ++col) {
const u = col / Math.max(1, this.width - 1);
const primary = this.layeredWave(u, v, this.getParameter('primarySpeed') || 0.15, 0);
const secondary = this.layeredWave(v, u, this.getParameter('secondarySpeed') || 0.32, Math.PI / 4);
const tertiary = Math.sin((u + v) * Math.PI * 1.5 + this.timeSeconds * 0.18);
const combined = 0.45 * primary + 0.35 * secondary + 0.2 * tertiary;
const envelope = Math.sin((u * v) * Math.PI * 2 + this.timeSeconds * 0.1) * 0.25 + 0.75;
const value = clamp((combined * 0.5 + 0.5) * envelope, 0, 1);
let color = samplePalette(paletteStops, value);
// Apply brightness
if (brightness < 1.0) {
const rgb = hexToRgb(color);
color = Math.round(rgb.r * brightness).toString(16).padStart(2, '0') +
Math.round(rgb.g * brightness).toString(16).padStart(2, '0') +
Math.round(rgb.b * brightness).toString(16).padStart(2, '0');
}
frame[toIndex(col, row, this.width)] = color;
}
}
return frame;
}
getMetadata() {
return {
name: 'Nebula Drift',
description: 'Organic drifting nebula with layered wave patterns',
parameters: {
primarySpeed: { type: 'range', min: 0.05, max: 0.3, step: 0.05, default: 0.15 },
secondarySpeed: { type: 'range', min: 0.2, max: 0.5, step: 0.02, default: 0.32 },
waveScale: { type: 'range', min: 0.5, max: 1.0, step: 0.05, default: 0.75 },
brightness: { type: 'range', min: 0.3, max: 1.0, step: 0.1, default: 1.0 },
color1: { type: 'color', default: '100406' },
color2: { type: 'color', default: '2e0f1f' },
color3: { type: 'color', default: '6a1731' },
color4: { type: 'color', default: 'b63b32' },
color5: { type: 'color', default: 'f48b2a' },
color6: { type: 'color', default: 'ffe9b0' },
},
width: this.width,
height: this.height,
};
}
}
module.exports = NebulaDriftPreset;