/*
 * Adapted from Dice So Nice! 6.4.5 by Simone Ricciardi, JDW and contributors.
 * Upstream commit b365efc7433eb2db2ccbd91da2029ac6c8b78ed5.
 * Modified for the standalone Beranth’ahl lab on 2026-11-06.
 * GNU AGPL version 3.0; see LICENSE.md and UPSTREAM.md. No warranty.
 */
// Verbatim upstream height conversion, preserved as a source reference.
	heightCanvasToNormalCanvas(srcCanvas, strength = this.normalMapStrength){
		const w = srcCanvas.width;
		const h = srcCanvas.height;
		const srcCtx = srcCanvas.getContext("2d");
		const srcData = srcCtx.getImageData(0, 0, w, h).data;

		//read the red channel as height - bump canvas is grayscale (white = high, dark = engraved)
		const raw = new Float32Array(w * h);
		for(let i = 0; i < w * h; i++) {
			raw[i] = srcData[i * 4] / 255.0;
		}

		//separable 1-2-1 Gaussian blur (3 passes ≈ 7×7 kernel) to smooth
		//high-frequency height noise before the Sobel pass
		const tmp = new Float32Array(w * h);
		let src = raw, dst = tmp;
		const blurPasses = 3;
		for(let pass = 0; pass < blurPasses; pass++) {
			const mid = (pass === blurPasses - 1) ? new Float32Array(w * h) : dst;
			for(let y = 0; y < h; y++) {
				for(let x = 0; x < w; x++) {
					const l = x > 0 ? src[y * w + x - 1] : src[y * w + x];
					const c = src[y * w + x];
					const r = x < w - 1 ? src[y * w + x + 1] : src[y * w + x];
					mid[y * w + x] = (l + 2 * c + r) * 0.25;
				}
			}
			for(let y = 0; y < h; y++) {
				for(let x = 0; x < w; x++) {
					const t = y > 0 ? mid[(y - 1) * w + x] : mid[y * w + x];
					const c = mid[y * w + x];
					const b = y < h - 1 ? mid[(y + 1) * w + x] : mid[y * w + x];
					dst[y * w + x] = (t + 2 * c + b) * 0.25;
				}
			}
			src = dst;
			dst = (src === tmp) ? raw : tmp;
		}
		const heights = src;

		const dstCanvas = document.createElement("canvas");
		dstCanvas.width = w;
		dstCanvas.height = h;
		const dstCtx = dstCanvas.getContext("2d");
		const dstImage = dstCtx.createImageData(w, h);
		const out = dstImage.data;

		const sample = (x, y) => {
			if(x < 0) x = 0; else if(x >= w) x = w - 1;
			if(y < 0) y = 0; else if(y >= h) y = h - 1;
			return heights[y * w + x];
		};

		for(let y = 0; y < h; y++) {
			for(let x = 0; x < w; x++) {
				const tl = sample(x - 1, y - 1);
				const t  = sample(x,     y - 1);
				const tr = sample(x + 1, y - 1);
				const l  = sample(x - 1, y);
				const r  = sample(x + 1, y);
				const bl = sample(x - 1, y + 1);
				const b  = sample(x,     y + 1);
				const br = sample(x + 1, y + 1);

				//Sobel kernels - gx = horizontal slope, gy = vertical slope (canvas-Y down)
				const gx = (tr + 2 * r + br) - (tl + 2 * l + bl);
				const gy = (bl + 2 * b + br) - (tl + 2 * t + tr);

				//build tangent-space normal. negate gradients so brighter (higher) regions
				//point outward; flipY=false on the texture aligns canvas-Y with V down.
				let nx = -gx * strength;
				let ny = -gy * strength;
				let nz = 1.0;
				const len = Math.sqrt(nx * nx + ny * ny + nz * nz);
				nx /= len; ny /= len; nz /= len;

				const idx = (y * w + x) * 4;
				out[idx]     = (nx * 0.5 + 0.5) * 255;
				out[idx + 1] = (ny * 0.5 + 0.5) * 255;
				out[idx + 2] = (nz * 0.5 + 0.5) * 255;
				out[idx + 3] = 255;
			}
		}

		dstCtx.putImageData(dstImage, 0, 0);
		return dstCanvas;
	}

