True 16-Bit Big-Endian Grayscale PNG Depth Maps & Watertight Binary STL Reliefs in Browser TypeScript
If you've ever tried engraving a 3D brass challenge coin on a MOPA fiber laser (LightBurn 3D Slice / EZCAD / xTool F1 Ultra) or carving a walnut bas-relief plaque on a 3-axis CNC router (VCarve / Aspire / Carveco / Fusio
If you've ever tried engraving a 3D brass challenge coin on a MOPA fiber laser (LightBurn 3D Slice / EZCAD / xTool F1 Ultra) or carving a walnut bas-relief plaque on a 3-axis CNC router (VCarve / Aspire / Carveco / Fusion 360), you've almost certainly hit two frustrating technical ceilings in web tools:
-
8-bit grayscale terracing: Standard browser
<canvas>PNG exports are locked to 8 bits per channel (256discrete Z levels). On a4.0 mmdeep fiber-laser coin carve across 128–256 passes, 8-bit quantization creates visible contour stair-stepping across smooth cheeks, crests, and beveled rims. - Non-manifold / paper-thin STL meshes: Naive heightfield-to-STL exporters emit only the top displaced surface with zero thickness, inverted triangle normals, or open perimeter edges — which CAM roughing/finishing toolpaths and 3D slicers reject as non-watertight.
In building CutPath CAD's WB-02 2.5D Bas-Relief STL & 16-Bit Depth Map Workbench (https://www.cutpathcad.com/relief-stl), we wrote both a zero-dependency true 16-bit big-endian grayscale PNG encoder and a watertight little-endian binary STL manifold generator in pure client-side TypeScript, paired with an interactive WebGL raking-light previewer.
Why HTML5 Canvas Fails Fiber Laser Depth Maps (and How We Bypass It)
canvas.toDataURL('image/png') always outputs 8-bit RGBA. To give LightBurn 3D Slice and EZCAD true 65,536 discrete Z-depth levels (16-bit unsigned grayscale, PNG Color Type 0, Bit Depth 16), we bypass Canvas encoding completely and assemble the raw PNG binary structure directly from our floating-point Float32Array heightfield:
export function encode16BitHeightmapPng(relief: ReliefResult): Uint8Array {
const cols = relief.gridCols;
const rows = relief.gridRows;
const heights = relief.heights;
const ihdr = new Uint8Array(13);
const ihdrDv = new DataView(ihdr.buffer);
ihdrDv.setUint32(0, cols, false);
ihdrDv.setUint32(4, rows, false);
ihdr[8] = 16; // 16-bit depth (0..65535 Z levels)
ihdr[9] = 0; // Color Type 0 = True Grayscale
ihdr[10] = 0; // Deflate/zlib compression
ihdr[11] = 0; // Adaptive filter method
ihdr[12] = 0; // Non-interlaced
const rowStride = 1 + cols * 2;
const rawScanlines = new Uint8Array(rows * rowStride);
for (let r = 0; r < rows; r++) {
const rowBase = r * rowStride;
rawScanlines[rowBase] = 0; // PNG Filter Type 0 (None)
const hRow = r * cols;
for (let c = 0; c < cols; c++) {
const h = heights[hRow + c];
const u16 = h <= 0 ? 0 : h >= 1 ? 65535 : Math.round(h * 65535);
const pxOff = rowBase + 1 + c * 2;
rawScanlines[pxOff] = (u16 >>> 8) & 0xff; // Big-endian MSB
rawScanlines[pxOff + 1] = u16 & 0xff; // Big-endian LSB
}
}
const compressedIdat = zlibSync(rawScanlines, { level: 6 });
// Wrap IHDR + IDAT + IEND chunks with IEEE CRC-32 checksums
}
The critical detail in the PNG specification is network byte order (big-endian) for 16-bit samples (MSB first, LSB second). Getting byte order backwards turns a smooth relief into high-frequency static noise inside LightBurn. Writing raw big-endian scanlines directly with zlibSync produces a crisp, spec-compliant 16-bit grayscale PNG in milliseconds.
Building a Guaranteed Watertight Binary .STL Manifold
For CNC routers (VCarve, Aspire, Fusion 360) and resin/FDM 3D printers, a relief mesh must be a closed 2-manifold solid — every edge shared by exactly two consistently wound triangles, with outward-facing unit normals.
Our encodeBinaryStl() constructs the mesh on a cols × rows heightfield grid (192 × 192 default = 75,254 triangles; optional higher-density grid for fine coin detail):
-
Top Relief Surface:
2 × (cols - 1) × (rows - 1)counter-clockwise triangles displaced toz = baseThicknessMm + height[r, c] * reliefDepthMm, shaped by gamma curve, multi-pass Laplacian surface smoothing, optional raised challenge-coin rim or chamfered plaque bevel, and optional lithophane Z-inversion (Backlit Corian / 3D Lithophane). -
Four Perimeter Side Skirts: Connects every boundary edge
(x, y, z_top)down to the solid base plane(x, y, 0)with matching vertex coordinates — zero T-junctions, zero cracked seams. -
Closed Solid Floor: Triangulates the bottom face at
z = 0with downward normals(0, 0, -1)sharing the exact perimeter vertices of the skirts. -
Little-Endian Binary Packing: Writes the 80-byte ASCII header, 4-byte
uint32triangle count, and 50 bytes per facet (3 × float32unit normal +9 × float32vertices +2-byteattribute count) directly into a single pre-allocatedArrayBuffer— avoiding multi-megabyte ASCII STL string bloat.
Interactive WebGL Raking-Light Inspection Before You Touch Metal
A flat 2D heightmap hides subtle flaws like sharp noise spikes, overly steep overhangs that snap a 0.5 mm tapered ball-nose bit, or weak mid-tone contrast.
We render the live heightfield in a dedicated WebGL orbit viewport (reliefWebgl.ts) with a draggable 360° raking light angle and material shaders (Brass, Walnut, Steel, Litho). Rotating a low-angle raking light across the brass shader immediately reveals surface roughness and bevel transitions before you ever run a 36-minute 3D finishing pass or burn 120 passes into a $6 brass coin blank.
Preview Free on Your Own Photo, Logo, or Crest
Drop any portrait, crest, family photo, or heightmap PNG/JPG/WEBP straight into your browser to inspect the 3D manifold mesh and 16-bit depth map live — zero signup required:
- WB-02 2.5D Bas-Relief STL & 16-Bit Depth Map Workbench: https://www.cutpathcad.com/relief-stl
- Free 16-Bit Fiber Laser Coin Blank & Depth Map Test Card: https://www.cutpathcad.com/depth-map-test
- Free Online 3D STL Viewer & Watertight Checker: https://www.cutpathcad.com/stl-viewer
When your relief looks right under raking light, download the full production pack (Watertight Binary .STL + True 16-Bit Grayscale .PNG + Shop Traveler PDF) at our Launch Special of $4.99 (reg. $9) via 1-click guest checkout — including commercial rights, 7-day unlimited tweaks, and a 14-day 1-click CAM money-back guarantee (or $9 3-File Pack, $39 14-Day Fabrication Pass, $69 1-Year Maker Pass, zero forced subscriptions).
What fiber laser or 3-axis CNC finishing parameters are you running for brass coins or hardwood reliefs? Drop your bit/lens setup below.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.