109 lines
3.1 KiB
JavaScript
109 lines
3.1 KiB
JavaScript
const dgram = require('dgram');
|
|
|
|
const {
|
|
clamp,
|
|
createFrame,
|
|
frameToPayload,
|
|
hexToRgb,
|
|
samplePalette,
|
|
toIndex,
|
|
} = require('./shared-frame-utils');
|
|
|
|
const DEFAULT_PORT = 4210;
|
|
const DEFAULT_WIDTH = 16;
|
|
const DEFAULT_HEIGHT = 16;
|
|
const DEFAULT_INTERVAL_MS = 60;
|
|
const RING_DENSITY = 8;
|
|
const RING_SPEED = 1.4;
|
|
const RING_SHARPNESS = 7.5;
|
|
const TWIST_INTENSITY = 2.2;
|
|
const TWIST_SPEED = 0.9;
|
|
const CORE_EXPONENT = 1.6;
|
|
|
|
const paletteStops = [
|
|
{ stop: 0.0, color: hexToRgb('010005') },
|
|
{ stop: 0.2, color: hexToRgb('07204f') },
|
|
{ stop: 0.45, color: hexToRgb('124aa0') },
|
|
{ stop: 0.7, color: hexToRgb('36a5ff') },
|
|
{ stop: 0.87, color: hexToRgb('99e6ff') },
|
|
{ stop: 1.0, color: hexToRgb('f1fbff') },
|
|
];
|
|
|
|
const host = process.argv[2];
|
|
const port = parseInt(process.argv[3] || String(DEFAULT_PORT), 10);
|
|
const width = parseInt(process.argv[4] || String(DEFAULT_WIDTH), 10);
|
|
const height = parseInt(process.argv[5] || String(DEFAULT_HEIGHT), 10);
|
|
const intervalMs = parseInt(process.argv[6] || String(DEFAULT_INTERVAL_MS), 10);
|
|
|
|
if (!host) {
|
|
console.error('Usage: node wormhole-tunnel.js <device-ip> [port] [width] [height] [interval-ms]');
|
|
process.exit(1);
|
|
}
|
|
|
|
if (Number.isNaN(port) || Number.isNaN(width) || Number.isNaN(height) || Number.isNaN(intervalMs)) {
|
|
console.error('Invalid numeric argument. Expected integers for port, width, height, and interval-ms.');
|
|
process.exit(1);
|
|
}
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
console.error('Matrix dimensions must be positive integers.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const socket = dgram.createSocket('udp4');
|
|
const isBroadcast = host === '255.255.255.255' || host.endsWith('.255');
|
|
const frame = createFrame(width, height);
|
|
let timeSeconds = 0;
|
|
const frameTimeSeconds = intervalMs / 1000;
|
|
|
|
if (isBroadcast) {
|
|
socket.bind(() => {
|
|
socket.setBroadcast(true);
|
|
});
|
|
}
|
|
|
|
socket.on('error', (error) => {
|
|
console.error('Socket error:', error.message);
|
|
});
|
|
|
|
function generateFrame() {
|
|
timeSeconds += frameTimeSeconds;
|
|
|
|
const cx = (width - 1) / 2;
|
|
const cy = (height - 1) / 2;
|
|
const radiusNorm = Math.hypot(cx, cy) || 1;
|
|
|
|
for (let row = 0; row < height; ++row) {
|
|
for (let col = 0; col < width; ++col) {
|
|
const dx = col - cx;
|
|
const dy = row - cy;
|
|
const radius = Math.hypot(dx, dy) / radiusNorm;
|
|
const angle = Math.atan2(dy, dx);
|
|
|
|
const radialPhase = radius * RING_DENSITY - timeSeconds * RING_SPEED;
|
|
const ring = Math.exp(-Math.pow(Math.sin(radialPhase * Math.PI), 2) * RING_SHARPNESS);
|
|
|
|
const twist = Math.sin(angle * TWIST_INTENSITY + timeSeconds * TWIST_SPEED) * 0.35 + 0.65;
|
|
const depth = Math.pow(clamp(1 - radius, 0, 1), CORE_EXPONENT);
|
|
|
|
const value = clamp(ring * 0.6 + depth * 0.3 + twist * 0.1, 0, 1);
|
|
frame[toIndex(col, row, width)] = samplePalette(paletteStops, value);
|
|
}
|
|
}
|
|
|
|
return frameToPayload(frame);
|
|
}
|
|
|
|
function sendFrame() {
|
|
const payload = generateFrame();
|
|
const message = Buffer.from(payload, 'utf8');
|
|
socket.send(message, port, host);
|
|
}
|
|
|
|
setInterval(sendFrame, intervalMs);
|
|
|
|
console.log(
|
|
`Streaming wormhole tunnel to ${host}:${port} (${width}x${height}, interval=${intervalMs}ms)`
|
|
);
|
|
|