Back to Drift City
SOURCE / PINNED RELEASE

Made of little things.

Drift City

Release
098731aa0f0f…
Author-recorded commit
2822ea0a4f5b…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-a4c0039c855

Archive hash verified: b3323e408235502b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

tools/make-sky-textures.mjs
// Makes the sky's four noise textures from the creator's Godot NoiseTexture2D resources
// (sky's/SourceCode/textures/clouds/*.tres), with FastNoiseLite: the same noise library
// Godot uses, the same settings. Writes 8-bit greyscale PNGs.
//
//   node tools/make-sky-textures.mjs <out-dir>
//
// Then add them with soyli: sky-clouds-1, sky-clouds-2, sky-stars, sky-night.
import fs from 'node:fs';
import path from 'node:path';
import zlib from 'node:zlib';
import FastNoiseLite from 'fastnoise-lite';

// The resources' settings (Godot's defaults where a resource doesn't give one: size 512,
// simplex smooth, seed 0, frequency 0.01, FBM, 5 octaves, skirt 0.1).
const SPECS = {
  'sky-clouds-1': { from: 'clouds_01', size: 1024, type: 0, seed: 4, octaves: 6, seamless: true, skirt: 0.2 },
  'sky-clouds-2': { from: 'clouds_02', size: 1024, type: 3, octaves: 8, seamless: true, skirt: 0.4 },
  'sky-stars': { from: 'clouds_04', size: 1024, type: 2, octaves: 3, seamless: true, invert: true, skirt: 0.2 },
  'sky-night': { from: 'clouds_03', type: 2, frequency: 0.008, seamless: true, skirt: 0.2 },
};
const TYPES = ['OpenSimplex2', 'OpenSimplex2S', 'Cellular', 'Perlin', 'ValueCubic', 'Value'];
const FRACTALS = ['None', 'FBm', 'Ridged', 'PingPong'];

function noiseImage(spec) {
  const W = spec.size ?? 512;
  const n = new FastNoiseLite(spec.seed ?? 0);
  n.SetNoiseType(TYPES[spec.type ?? 1]);
  n.SetFrequency(spec.frequency ?? 0.01);
  n.SetFractalType(FRACTALS[spec.fractal ?? 1]);
  n.SetFractalOctaves(spec.octaves ?? 5);
  n.SetFractalLacunarity(2);
  n.SetFractalGain(0.5);
  n.SetCellularDistanceFunction('Euclidean');
  n.SetCellularReturnType('Distance');
  n.SetCellularJitter(1);
  // Seamless: made skirt larger each way, the extra cross-faded back over the start.
  const S = spec.seamless ? Math.round(W * (spec.skirt ?? 0.1)) : 0;
  const G = W + S;
  const raw = new Float32Array(G * G);
  for (let y = 0; y < G; y++) for (let x = 0; x < G; x++) raw[y * G + x] = n.GetNoise(x, y);
  const ease = (k) => k * k * (3 - 2 * k);
  const rows = new Float32Array(W * G);
  for (let y = 0; y < G; y++)
    for (let x = 0; x < W; x++) {
      const v = raw[y * G + x];
      rows[y * W + x] = x < S ? raw[y * G + x + W] + (v - raw[y * G + x + W]) * ease(x / S) : v;
    }
  const img = new Float32Array(W * W);
  for (let y = 0; y < W; y++)
    for (let x = 0; x < W; x++) {
      const v = rows[y * W + x];
      img[y * W + x] = y < S ? rows[(y + W) * W + x] + (v - rows[(y + W) * W + x]) * ease(y / S) : v;
    }
  // Normalized to the full range (Godot's default), inverted if asked.
  let lo = Infinity;
  let hi = -Infinity;
  for (const v of img) {
    lo = Math.min(lo, v);
    hi = Math.max(hi, v);
  }
  const px = new Uint8Array(W * W);
  for (let i = 0; i < img.length; i++) {
    let v = (img[i] - lo) / (hi - lo || 1);
    if (spec.invert) v = 1 - v;
    px[i] = Math.round(v * 255);
  }
  return { W, px };
}

function crc32(buf) {
  let c = ~0;
  for (let i = 0; i < buf.length; i++) {
    c ^= buf[i];
    for (let k = 0; k < 8; k++) c = c & 1 ? (c >>> 1) ^ 0xedb88320 : c >>> 1;
  }
  return ~c >>> 0;
}
function chunk(type, data) {
  const len = Buffer.alloc(4);
  len.writeUInt32BE(data.length);
  const td = Buffer.concat([Buffer.from(type, 'ascii'), data]);
  const crc = Buffer.alloc(4);
  crc.writeUInt32BE(crc32(td));
  return Buffer.concat([len, td, crc]);
}
/** A greyscale PNG, each row filtered (Paeth, which suits smooth noise). */
function png(W, px) {
  const raw = Buffer.alloc((W + 1) * W);
  for (let y = 0; y < W; y++) {
    raw[y * (W + 1)] = 4;
    for (let x = 0; x < W; x++) {
      const a = x ? px[y * W + x - 1] : 0;
      const b = y ? px[(y - 1) * W + x] : 0;
      const c = x && y ? px[(y - 1) * W + x - 1] : 0;
      const p = a + b - c;
      const pa = Math.abs(p - a);
      const pb = Math.abs(p - b);
      const pc = Math.abs(p - c);
      const pred = pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
      raw[y * (W + 1) + 1 + x] = (px[y * W + x] - pred) & 255;
    }
  }
  const ihdr = Buffer.alloc(13);
  ihdr.writeUInt32BE(W, 0);
  ihdr.writeUInt32BE(W, 4);
  ihdr[8] = 8;
  ihdr[9] = 0;
  return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ihdr), chunk('IDAT', zlib.deflateSync(raw, { level: 9 })), chunk('IEND', Buffer.alloc(0))]);
}

const out = process.argv[2] ?? '.';
fs.mkdirSync(out, { recursive: true });
for (const [name, spec] of Object.entries(SPECS)) {
  const t0 = Date.now();
  const { W, px } = noiseImage(spec);
  const file = path.join(out, `${name}.png`);
  fs.writeFileSync(file, png(W, px));
  console.log(`${name} (${spec.from}): ${W}x${W}, ${fs.statSync(file).size} bytes, ${Date.now() - t0} ms`);
}