Skip to content

Commit

Permalink
add quantized-mesh to README, implement oct16 decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
applecuckoo committed Nov 3, 2024
1 parent 0625b7c commit 84c0039
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Everything will immediately show up in ImHex's Content Store and gets bundled wi
| PYC | | [`patterns/pyc.hexpat`](patterns/pyc.hexpat) | Python bytecode files |
| QBCL | | [`patterns/qbcl.hexpat`](patterns/qbcl.hexpat) | Qubicle voxel scene project file |
| QOI | `image/qoi` | [`patterns/qoi.hexpat`](patterns/qoi.hexpat) | QOI image files |
| quantized-mesh | | [`patterns/quantized-mesh.hexpat`](patterns/quantized-mesh.hexpat) | Cesium quantized-mesh terrain |
| RAS | `image/x-sun-raster` | [`patterns/ras.hexpat`](patterns/ras.hexpat) | RAS image files |
| ReFS | | [`patterns/refs.hexpat`](patterns/refs.hexpat) | Microsoft Resilient File System |
| RGBDS | | [`patterns/rgbds.hexpat`](patterns/rgbds.hexpat) | [RGBDS](https://rgbds.gbdev.io) object file format |
Expand Down
47 changes: 44 additions & 3 deletions patterns/quantized-mesh.hexpat
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#pragma endian little

// based on https://github.com/CesiumGS/quantized-mesh
// potential improvements: figure out how to decode values, i.e. high water mark encoding for indices and oct16 for normals
// potential improvements: figure out high water mark encoding for indices

import std.math;
import std.io;

u8 extensionCount;
extensionCount = 3; // NOTE: set this to the amount of extensions in your terrain.
extensionCount = 0; // NOTE: set this to the amount of extensions in your terrain.

// ZigZag decoder based on protobuf.hexpat by WerWolv

Expand Down Expand Up @@ -87,8 +90,46 @@ enum ExtensionTypes : u8 {
Metadata = 0x4,
};

// Oct16 decode based on https://github.com/loicgasser/quantized-mesh-tile/blob/master/quantized_mesh_tile/utils.py

fn signNotZero(float v) {
if (v < 0.0)
return -1.0;
else
return 1.0;
};

fn fromSnorm(u8 value) {
return float(std::math::clamp(value, 0.0, 255.0) / 255.0 * 2.0 - 1.0);
};

struct Oct16 {
u8 x;
u8 y;
}[[sealed, format("format_oct16")]];

fn format_oct16(Oct16 oct) {
float xOut;
float yOut;
float zOut;

xOut = fromSnorm(oct.x);
yOut = fromSnorm(oct.y);
zOut = 1.0 - (std::math::abs(xOut) + std::math::abs(yOut));

if (zOut < 0.0) {
float oldX;

oldX = xOut;
xOut = (1.0 - std::math::abs(yOut)) * signNotZero(oldX);
yOut = (1.0 - std::math::abs(oldX)) * signNotZero(yOut);
}

return std::format("{}, {}, {}", xOut, yOut, zOut);
};

struct OctEncodedVertexNormals {
u8 xy[parent.parent.vertdata.vertexCount * 2]; // TODO:
Oct16 xy[parent.parent.vertdata.vertexCount];
};

struct WaterMask {
Expand Down

0 comments on commit 84c0039

Please sign in to comment.