[PR #5622] [MERGED] OptiFine entity models: read .jem and .jpm files into vanilla-frame geometry #6394

Closed
opened 2026-11-03 06:19:53 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/5622
Author: @some-du6e
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/1/2026
Merged by: @ludovic111

Base: main ← Head: optifine/emf-jem


📝 Commits (9)

  • b2a09a2 OptiFine CEM: read .jem and .jpm entity models into vanilla-frame geometry
  • d8c627e Merge main into optifine/emf-jem
  • 9469537 OptiFine CEM: read .jem files the way OptiFine does, and keep packs inside the root
  • af00d7a CLAUDE.md: list horizon-optifine among the crates
  • 5eeb2cd OptiFine reader: paths with a backslash are refused too
  • d037799 OptiFine CEM: inheriting shares instead of copying, every entry and bone counts
  • a52700e OptiFine CEM: third review round of the .jem reader
  • 3a16789 OptiFine CEM: fourth review round of the .jem reader
  • d454a1f OptiFine CEM: the work budget charges what is walked and copied

📊 Changes

7 files changed (+1589 additions, -4 deletions)

View changed files

📝 AGENTS.md (+1 -0)
📝 crates/horizon-optifine/Cargo.toml (+1 -1)
➕ crates/horizon-optifine/src/cem/json.rs (+340 -0)
➕ crates/horizon-optifine/src/cem/mod.rs (+7 -0)
➕ crates/horizon-optifine/src/cem/model.rs (+1234 -0)
📝 crates/horizon-optifine/src/lib.rs (+1 -0)
📝 crates/horizon-optifine/src/paths.rs (+5 -3)

📄 Description

OptiFine packs that change entity shapes (Fresh Animations and its add-ons, most "3D" mob packs) ship optifine/cem/<entity>.jem models. Horizon ignores them. This is the first PR of a stack that adds them, the way Entity Model Features does on Fabric. It only reads the files. Nothing in the game uses it yet, so players see no change.

horizon_optifine::cem::model::parse_jem reads a .jem and the .jpm part files it names, and bakes every box into the quads vanilla's ModelPart draws: vanilla's axes (y down), model pixels, UVs as a fraction of the texture. That's the same shape as horizon_client::mobs::Polygon, so the next PR can drop the result straight into Horizon's mob models.

What it reads:

  • the .jem's texture and textureSize (default 64×32)
  • each models entry's part, attach, scale, id, baseId and model:
    • baseId inherits the named entry's keys, under the entry's own and its own .jpm's
    • model loads a .jpm through the shared paths::resolve, so ~/, ./, bare names and ns: all work, and paths that leave the pack are refused
    • each .jpm is read once
  • part definitions: invertAxis, translate, rotate, mirrorTexture (u, v), textureSize (children inherit it), boxes, submodel and submodels
    • rotate is in degrees, mapped onto vanilla's axes: a flipped axis turns the other way, and an odd number of flips turns them all
  • boxes: coordinates, the textureOffset box UV (ModelPart.Cube's layout), per-face uvUp/uvDown/uvNorth/uvSouth/uvEast/uvWest with their uvFront/uvBack/uvLeft/uvRight aliases, sizeAdd, sizesAdd and EMF's sizeAddX/Y/Z
    • an empty or missing face is not drawn
    • positions grow, UVs don't
  • animations, kept as (target, expression) text in file order for the expression PR

Not read yet, until something draws them: shadowSize, per-bone texture, attachments, sprites.

Reading files the way OptiFine does:

  • A small JSON reader (cem/json.rs) keeps object keys in file order, because animations read what earlier ones wrote. serde_json's map sorts them.
  • It accepts what Gson's lenient mode does in hand-edited packs: a BOM, //, /* */ and # comments, trailing commas, single quotes, bare keys, =/=> for :, and ; between items. \u escapes need four hex digits; surrogate pairs combine, and lone halves become U+FFFD.
  • An entry's keys are layers: its own, then its .jpm's, then its baseId entry's. The layers are shared and indexed, so inheriting a big model copies nothing. The first entry with an id is the one baseId finds.
  • Missing or broken entries:
    • a missing or broken .jpm leaves out only that entry, with a warning in EntityModel::warnings; the rest of the model loads
    • a baseId naming nothing is warned about, and the entry is still built from its own keys
  • Numbers must be finite. attach may also be a string; animation values may be numbers or booleans.
  • Limits, so a hostile pack can't make loading run out of memory or time:
    • Bytes: the .jem and every .jpm it loads share MAX_BYTES (4 MiB). It's public so callers can refuse an oversized file before reading it. The .jem's own bytes count too. A cached .jpm counts once; one past the total is left out with a warning. A caller's read that refuses a too-large file shows up as missing.
    • Entries: at most 1024 per file.
    • Work: one budget of 65536 units per .jem, shared by all its entries. It charges what each entry walks and copies, not what the files hold, so sharing a .jpm or a base costs every entry that uses it.
      • a bone costs 1
      • a submodel costs 1 plus its keys
      • a part also costs its layer count
      • a box costs the most of its faces, its keys and 1
      • any other boxes/submodels item costs 1
      • an animation item costs the most of its pairs and 1
      • strings read or copied from the files (names, ids, targets, expressions, axes, numbers written as text) cost 1 per started 64 bytes, every time
    • Animations: a part's animations are checked all-or-nothing before its bone is baked, so either all of them fit or the entry is left out.
    • Partial boxes: a box that doesn't fit has its faces trimmed to what's left, or is dropped when its keys alone don't fit. An entry the budget cuts down to nothing is left out, so the vanilla part stays.
    • Bad geometry: boxes with non-finite corners or UVs are skipped, but still cost.
  • Per-part texture and sprites (anywhere in the tree) aren't drawn yet. A file that uses them gets one warning.

Placement follows the convention Blockbench's OptiFine export writes and OptiDocs' "Limitations: pivot points" page describes:

  • a part keeps its vanilla part's pivot, and its translate (minus the pivot) only shifts its content
  • a submodel's translate is its pivot in its parent's frame, and its boxes are relative to that pivot

The tests check that against hand-worked answers. A zombie head and right leg written the way an export writes them land on vanilla's boxes (head from (-4, -8, -4), leg -2..2 × 0..12 around (-1.9, 12, 0)) with the skin's face and leg UVs. Every face of a plain and a mirrored box winds outward on its own side.

Builds on #5574 (the shared horizon-optifine crate), now on main. New here: crates/horizon-optifine/src/cem/, a CLAUDE.md crate-table row, and one fix in the shared paths::resolve, which now also refuses backslashes (a separator on Windows).

Docs I wrote it from (no mod source read): OptiFine's cem_model.txt and cem_part.txt (github.com/sp614x/optifine, OptiFineDoc/doc), the OptiDocs CEM pages (optifine.readthedocs.io, CC0), and EMF's FEATURES.md and .github/emf_model.txt/emf_part.txt docs.

Next in the stack:

  1. the expression compiler
  2. OptiFine part names mapped to Horizon's model parts, and vanilla parts swapped at load
  3. animations driven by entity state
  4. random models (<entity>N.jem and .properties) and block entities

Checks:

  • cargo fmt -p horizon-optifine: clean
  • cargo test -p horizon-optifine cem:: (run locally, the one test run allowed on this machine): 24 passed. They cover:
    • animation order
    • each inverted axis and the rotation signs
    • face aliases and mirroring
    • sizeAdd precedence
    • textureSize reaching submodels
    • key precedence (own over .jpm over base, and a repeated key's last value)
    • the .jpm cache, including for a missing file
    • broken entries and path escapes
    • the shared byte budget
    • the work budget's edges: 1 unit left, exact use, faceless and junk-key boxes, animations that don't fit, non-object animation items
    • 1024 entries sharing a .jpm with a 1 MB expression or a 200k-key submodel stay bounded
    • non-finite boxes
    • the lenient JSON forms and surrogates
  • clippy and the full suite, via fork CI (this machine is too loaded to build): verify passed on earlier heads, and this head is running at https://github.com/some-du6e/horizon/actions/runs/41275085763
  • review loop: five rounds, each fixed (9469537b1; 5eeb2cdd7 and d0377992d; a52700eba; 3a1678920; d454a1fcb). Round 5's correctness reviewer found nothing. The finding that kept coming back was a new way for a hostile pack to make the reader walk or copy more than its budget charged, so d454a1fcb now charges everything walked or copied rather than patching each path. The live game test is skipped: there's no .local/ vanilla data here, and this PR has nothing in the game to run.

Note

Filed by Opus 5.5 using Claude Code.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/5622 **Author:** [@some-du6e](https://github.com/some-du6e) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `optifine/emf-jem` --- ### 📝 Commits (9) - [`b2a09a2`](https://github.com/horizon-rust/horizon/commit/b2a09a2118909ee17da22fb44c3006afbe650e11) OptiFine CEM: read .jem and .jpm entity models into vanilla-frame geometry - [`d8c627e`](https://github.com/horizon-rust/horizon/commit/d8c627e75ed61b3fef2dea5fdb877e642701a744) Merge main into optifine/emf-jem - [`9469537`](https://github.com/horizon-rust/horizon/commit/9469537b1409349518e732bfefb5f5b7e60b6dee) OptiFine CEM: read .jem files the way OptiFine does, and keep packs inside the root - [`af00d7a`](https://github.com/horizon-rust/horizon/commit/af00d7af2b79b3642073319d3632a2bb2c7f057c) CLAUDE.md: list horizon-optifine among the crates - [`5eeb2cd`](https://github.com/horizon-rust/horizon/commit/5eeb2cdd7e039ffae4f10f7e47597b783b354504) OptiFine reader: paths with a backslash are refused too - [`d037799`](https://github.com/horizon-rust/horizon/commit/d0377992dba118264623c58fffed4d0a0e6c9b66) OptiFine CEM: inheriting shares instead of copying, every entry and bone counts - [`a52700e`](https://github.com/horizon-rust/horizon/commit/a52700ebae645f5f7c4c74964c6e614a43a4e6ee) OptiFine CEM: third review round of the .jem reader - [`3a16789`](https://github.com/horizon-rust/horizon/commit/3a167892026ebb8f9915f48456099baaf7fa58f5) OptiFine CEM: fourth review round of the .jem reader - [`d454a1f`](https://github.com/horizon-rust/horizon/commit/d454a1fcbd7932de505ca76c7cbc782a05705dd9) OptiFine CEM: the work budget charges what is walked and copied ### 📊 Changes **7 files changed** (+1589 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `AGENTS.md` (+1 -0) 📝 `crates/horizon-optifine/Cargo.toml` (+1 -1) ➕ `crates/horizon-optifine/src/cem/json.rs` (+340 -0) ➕ `crates/horizon-optifine/src/cem/mod.rs` (+7 -0) ➕ `crates/horizon-optifine/src/cem/model.rs` (+1234 -0) 📝 `crates/horizon-optifine/src/lib.rs` (+1 -0) 📝 `crates/horizon-optifine/src/paths.rs` (+5 -3) </details> ### 📄 Description OptiFine packs that change entity shapes (Fresh Animations and its add-ons, most "3D" mob packs) ship `optifine/cem/<entity>.jem` models. Horizon ignores them. This is the first PR of a stack that adds them, the way Entity Model Features does on Fabric. It only reads the files. Nothing in the game uses it yet, so players see no change. `horizon_optifine::cem::model::parse_jem` reads a `.jem` and the `.jpm` part files it names, and bakes every box into the quads vanilla's `ModelPart` draws: vanilla's axes (y down), model pixels, UVs as a fraction of the texture. That's the same shape as `horizon_client::mobs::Polygon`, so the next PR can drop the result straight into Horizon's mob models. What it reads: - the `.jem`'s `texture` and `textureSize` (default 64×32) - each `models` entry's `part`, `attach`, `scale`, `id`, `baseId` and `model`: - `baseId` inherits the named entry's keys, under the entry's own and its own `.jpm`'s - `model` loads a `.jpm` through the shared `paths::resolve`, so `~/`, `./`, bare names and `ns:` all work, and paths that leave the pack are refused - each `.jpm` is read once - part definitions: `invertAxis`, `translate`, `rotate`, `mirrorTexture` (`u`, `v`), `textureSize` (children inherit it), `boxes`, `submodel` and `submodels` - `rotate` is in degrees, mapped onto vanilla's axes: a flipped axis turns the other way, and an odd number of flips turns them all - boxes: `coordinates`, the `textureOffset` box UV (`ModelPart.Cube`'s layout), per-face `uvUp`/`uvDown`/`uvNorth`/`uvSouth`/`uvEast`/`uvWest` with their `uvFront`/`uvBack`/`uvLeft`/`uvRight` aliases, `sizeAdd`, `sizesAdd` and EMF's `sizeAddX/Y/Z` - an empty or missing face is not drawn - positions grow, UVs don't - `animations`, kept as (target, expression) text in file order for the expression PR Not read yet, until something draws them: `shadowSize`, per-bone `texture`, `attachments`, `sprites`. **Reading files the way OptiFine does:** - A small JSON reader (`cem/json.rs`) keeps object keys in file order, because animations read what earlier ones wrote. `serde_json`'s map sorts them. - It accepts what Gson's lenient mode does in hand-edited packs: a BOM, `//`, `/* */` and `#` comments, trailing commas, single quotes, bare keys, `=`/`=>` for `:`, and `;` between items. `\u` escapes need four hex digits; surrogate pairs combine, and lone halves become U+FFFD. - An entry's keys are layers: its own, then its `.jpm`'s, then its `baseId` entry's. The layers are shared and indexed, so inheriting a big model copies nothing. The first entry with an id is the one `baseId` finds. - Missing or broken entries: - a missing or broken `.jpm` leaves out only that entry, with a warning in `EntityModel::warnings`; the rest of the model loads - a `baseId` naming nothing is warned about, and the entry is still built from its own keys - Numbers must be finite. `attach` may also be a string; animation values may be numbers or booleans. - Limits, so a hostile pack can't make loading run out of memory or time: - **Bytes:** the `.jem` and every `.jpm` it loads share `MAX_BYTES` (4 MiB). It's public so callers can refuse an oversized file before reading it. The `.jem`'s own bytes count too. A cached `.jpm` counts once; one past the total is left out with a warning. A caller's `read` that refuses a too-large file shows up as `missing`. - **Entries:** at most 1024 per file. - **Work:** one budget of 65536 units per `.jem`, shared by all its entries. It charges what each entry walks and copies, not what the files hold, so sharing a `.jpm` or a base costs every entry that uses it. - a bone costs 1 - a submodel costs 1 plus its keys - a part also costs its layer count - a box costs the most of its faces, its keys and 1 - any other `boxes`/`submodels` item costs 1 - an animation item costs the most of its pairs and 1 - strings read or copied from the files (names, ids, targets, expressions, axes, numbers written as text) cost 1 per started 64 bytes, every time - **Animations:** a part's animations are checked all-or-nothing before its bone is baked, so either all of them fit or the entry is left out. - **Partial boxes:** a box that doesn't fit has its faces trimmed to what's left, or is dropped when its keys alone don't fit. An entry the budget cuts down to nothing is left out, so the vanilla part stays. - **Bad geometry:** boxes with non-finite corners or UVs are skipped, but still cost. - Per-part `texture` and `sprites` (anywhere in the tree) aren't drawn yet. A file that uses them gets one warning. Placement follows the convention Blockbench's OptiFine export writes and OptiDocs' "Limitations: pivot points" page describes: - a part keeps its vanilla part's pivot, and its `translate` (minus the pivot) only shifts its content - a submodel's `translate` is its pivot in its parent's frame, and its boxes are relative to that pivot The tests check that against hand-worked answers. A zombie head and right leg written the way an export writes them land on vanilla's boxes (head from (-4, -8, -4), leg -2..2 × 0..12 around (-1.9, 12, 0)) with the skin's face and leg UVs. Every face of a plain and a mirrored box winds outward on its own side. Builds on #5574 (the shared `horizon-optifine` crate), now on main. New here: `crates/horizon-optifine/src/cem/`, a CLAUDE.md crate-table row, and one fix in the shared `paths::resolve`, which now also refuses backslashes (a separator on Windows). Docs I wrote it from (no mod source read): OptiFine's `cem_model.txt` and `cem_part.txt` (github.com/sp614x/optifine, OptiFineDoc/doc), the OptiDocs CEM pages (optifine.readthedocs.io, CC0), and EMF's `FEATURES.md` and `.github/emf_model.txt`/`emf_part.txt` docs. Next in the stack: 1. the expression compiler 2. OptiFine part names mapped to Horizon's model parts, and vanilla parts swapped at load 3. animations driven by entity state 4. random models (`<entity>N.jem` and `.properties`) and block entities Checks: - `cargo fmt -p horizon-optifine`: clean - `cargo test -p horizon-optifine cem::` (run locally, the one test run allowed on this machine): 24 passed. They cover: - animation order - each inverted axis and the rotation signs - face aliases and mirroring - `sizeAdd` precedence - `textureSize` reaching submodels - key precedence (own over `.jpm` over base, and a repeated key's last value) - the `.jpm` cache, including for a missing file - broken entries and path escapes - the shared byte budget - the work budget's edges: 1 unit left, exact use, faceless and junk-key boxes, animations that don't fit, non-object animation items - 1024 entries sharing a `.jpm` with a 1 MB expression or a 200k-key submodel stay bounded - non-finite boxes - the lenient JSON forms and surrogates - clippy and the full suite, via fork CI (this machine is too loaded to build): `verify` passed on earlier heads, and this head is running at https://github.com/some-du6e/horizon/actions/runs/41275085763 - review loop: five rounds, each fixed (9469537b1; 5eeb2cdd7 and d0377992d; a52700eba; 3a1678920; d454a1fcb). Round 5's correctness reviewer found nothing. The finding that kept coming back was a new way for a hostile pack to make the reader walk or copy more than its budget charged, so d454a1fcb now charges everything walked or copied rather than patching each path. The live game test is skipped: there's no `.local/` vanilla data here, and this PR has nothing in the game to run. > [!NOTE] > Filed by Opus 5.5 using Claude Code. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:19:53 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#6394
No description provided.