diff --git a/common/block_models.ts b/common/block_models.ts index 5d73767..6a22e46 100644 --- a/common/block_models.ts +++ b/common/block_models.ts @@ -29,7 +29,7 @@ export interface ModelElementJson { rotation?: { origin: [number, number, number]; axis: "x" | "y" | "z"; - // -45, -22.5, 0, 22.5 or 45 + // degrees, any from -180 to 180. minecraft only takes -45, -22.5, 0, 22.5 or 45, blockbench's newer models any angle: number; // stretch the rotated faces back to the block's size, like the cross model does rescale?: boolean; diff --git a/common/mod_data.ts b/common/mod_data.ts index a30d2f2..0573526 100644 --- a/common/mod_data.ts +++ b/common/mod_data.ts @@ -808,10 +808,10 @@ export function validate_model(json: unknown): Problems { if (rotation !== undefined) { if ( !is_object(rotation) || !["x", "y", "z"].includes(rotation.axis as string) || - ![-45, -22.5, 0, 22.5, 45].includes(rotation.angle as number) || !Array.isArray(rotation.origin) + typeof rotation.angle !== "number" || Math.abs(rotation.angle) > 180 || !Array.isArray(rotation.origin) ) { problems.push( - `${where}.rotation must be { origin, axis: x, y or z, angle: -45, -22.5, 0, 22.5 or 45 }`, + `${where}.rotation must be { origin, axis: x, y or z, angle: degrees from -180 to 180 }`, ); } } diff --git a/docs/modding/resources/models.md b/docs/modding/resources/models.md index 1d89cae..cafd41e 100644 --- a/docs/modding/resources/models.md +++ b/docs/modding/resources/models.md @@ -68,7 +68,7 @@ on every face, since `top` and `bottom` default to `#side`. | `parent` | Another model this one starts from, see [Parents](#parents). | | `textures` | Defaults for texture variables. Values are texture ids or other variables, like `"#side"`. | | `elements[].from`, `to` | Opposite corners of the box, `[x, y, z]` in pixels. | -| `elements[].rotation` | `{ origin, axis, angle, rescale }`: turns the box around `origin` on `x`, `y` or `z` by -45, -22.5, 0, 22.5 or 45 degrees. `rescale` stretches it back to the block's width. | +| `elements[].rotation` | `{ origin, axis, angle, rescale }`: turns the box around `origin` on `x`, `y` or `z` by `angle` degrees, from -180 to 180. `rescale` stretches it back to the block's width. | | `elements[].shade` | Darker faces on the sides and bottom. `true` by default, plants turn it off. | | `elements[].faces` | Any of `top`, `bottom`, `north`, `south`, `west` and `east`. | | `faces.*.texture` | A variable like `"#side"`, filled in by the block's `textures`, or a texture id. | diff --git a/mods/bworld/blocks/worm_bin.json b/mods/bworld/blocks/worm_bin.json index ae1cd05..d15dab2 100644 --- a/mods/bworld/blocks/worm_bin.json +++ b/mods/bworld/blocks/worm_bin.json @@ -3,11 +3,10 @@ "block": { "id": "bworld:worm_bin", "name": "Worm Bin", - "textures": { - "front": "bworld:worm_bin_front", - "side": "bworld:worm_bin_side", - "top": "bworld:worm_bin_top" - }, + "model": "bworld:worm_bin", + "textures": {}, + "item_model": "bworld:worm_bin", + "render_layer": "cutout", "states": [ { "name": "facing", diff --git a/mods/bworld/models/worm_bin.json b/mods/bworld/models/worm_bin.json new file mode 100644 index 0000000..276e6cd --- /dev/null +++ b/mods/bworld/models/worm_bin.json @@ -0,0 +1,330 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:worm_bin", + "textures": { "0": "bworld:worm_bin", "particle": "bworld:worm_bin" }, + "elements": [ + { + "from": [8, 13, 8], + "to": [9, 14, 12], + "rotation": { "origin": [7, 13, 9], "axis": "y", "angle": 90 }, + "faces": { + "north": { "texture": "#0", "uv": [9.25, 11.5, 9.5, 11.75] }, + "east": { "texture": "#0", "uv": [8.5, 11.25, 9.5, 11.5] }, + "south": { "texture": "#0", "uv": [9.5, 11.5, 9.75, 11.75] }, + "west": { "texture": "#0", "uv": [9.5, 11.25, 10.5, 11.5] }, + "top": { "texture": "#0", "uv": [4.5, 5.5, 4.75, 6.5] }, + "bottom": { "texture": "#0", "uv": [4.75, 5.5, 5, 6.5] } + } + }, + { + "from": [9, 12, 7], + "to": [10, 13, 8], + "faces": { + "north": { "texture": "#0", "uv": [9.75, 11.5, 10, 11.75] }, + "east": { "texture": "#0", "uv": [10, 11.5, 10.25, 11.75] }, + "south": { "texture": "#0", "uv": [10.25, 11.5, 10.5, 11.75] }, + "west": { "texture": "#0", "uv": [11, 11.5, 11.25, 11.75] }, + "top": { "texture": "#0", "uv": [11.25, 11.5, 11.5, 11.75] }, + "bottom": { "texture": "#0", "uv": [11.5, 11.5, 11.75, 11.75] } + } + }, + { + "from": [6, 12, 7], + "to": [7, 13, 8], + "faces": { + "north": { "texture": "#0", "uv": [2, 11.75, 2.25, 12] }, + "east": { "texture": "#0", "uv": [2.25, 11.75, 2.5, 12] }, + "south": { "texture": "#0", "uv": [2.5, 11.75, 2.75, 12] }, + "west": { "texture": "#0", "uv": [2.75, 11.75, 3, 12] }, + "top": { "texture": "#0", "uv": [3, 11.75, 3.25, 12] }, + "bottom": { "texture": "#0", "uv": [3.25, 11.75, 3.5, 12] } + } + }, + { + "from": [3, 11, 3], + "to": [13, 12, 13], + "faces": { + "north": { "texture": "#0", "uv": [11, 3.5, 13.5, 3.75] }, + "east": { "texture": "#0", "uv": [11, 3.75, 13.5, 4] }, + "south": { "texture": "#0", "uv": [11, 4, 13.5, 4.25] }, + "west": { "texture": "#0", "uv": [11, 4.25, 13.5, 4.5] }, + "top": { "texture": "#0", "uv": [5, 5, 7.5, 7.5] }, + "bottom": { "texture": "#0", "uv": [0, 5.5, 2.5, 8] } + } + }, + { + "from": [13, 11, 7], + "to": [21, 12, 8], + "rotation": { "origin": [15, 11, 6], "axis": "y", "angle": -90 }, + "faces": { + "north": { "texture": "#0", "uv": [11, 6.25, 13, 6.5] }, + "east": { "texture": "#0", "uv": [11.75, 3.25, 12, 3.5] }, + "south": { "texture": "#0", "uv": [6.5, 11, 8.5, 11.25] }, + "west": { "texture": "#0", "uv": [3.5, 11.75, 3.75, 12] }, + "top": { "texture": "#0", "uv": [11, 6.5, 13, 6.75] }, + "bottom": { "texture": "#0", "uv": [11, 6.75, 13, 7] } + } + }, + { + "from": [2, 11, 7], + "to": [10, 12, 8], + "rotation": { "origin": [4, 11, 6], "axis": "y", "angle": -90 }, + "faces": { + "north": { "texture": "#0", "uv": [11, 7, 13, 7.25] }, + "east": { "texture": "#0", "uv": [3.75, 11.75, 4, 12] }, + "south": { "texture": "#0", "uv": [11, 7.25, 13, 7.5] }, + "west": { "texture": "#0", "uv": [7.5, 11.75, 7.75, 12] }, + "top": { "texture": "#0", "uv": [11, 7.5, 13, 7.75] }, + "bottom": { "texture": "#0", "uv": [11, 7.75, 13, 8] } + } + }, + { + "from": [4, 11, 13], + "to": [12, 12, 14], + "faces": { + "north": { "texture": "#0", "uv": [11, 8, 13, 8.25] }, + "east": { "texture": "#0", "uv": [7.75, 11.75, 8, 12] }, + "south": { "texture": "#0", "uv": [11, 8.25, 13, 8.5] }, + "west": { "texture": "#0", "uv": [8, 11.75, 8.25, 12] }, + "top": { "texture": "#0", "uv": [8.5, 11, 10.5, 11.25] }, + "bottom": { "texture": "#0", "uv": [11, 8.5, 13, 8.75] } + } + }, + { + "from": [4, 11, 2], + "to": [12, 12, 3], + "faces": { + "north": { "texture": "#0", "uv": [11, 8.75, 13, 9] }, + "east": { "texture": "#0", "uv": [8.25, 11.75, 8.5, 12] }, + "south": { "texture": "#0", "uv": [11, 9, 13, 9.25] }, + "west": { "texture": "#0", "uv": [8.5, 11.75, 8.75, 12] }, + "top": { "texture": "#0", "uv": [11, 9.25, 13, 9.5] }, + "bottom": { "texture": "#0", "uv": [11, 9.5, 13, 9.75] } + } + }, + { + "from": [16, 11, 7], + "to": [20, 12, 8], + "rotation": { "origin": [16, 11, 6], "axis": "y", "angle": -90 }, + "faces": { + "north": { "texture": "#0", "uv": [11, 11.25, 12, 11.5] }, + "east": { "texture": "#0", "uv": [8.75, 11.75, 9, 12] }, + "south": { "texture": "#0", "uv": [11.5, 0, 12.5, 0.25] }, + "west": { "texture": "#0", "uv": [9, 11.75, 9.25, 12] }, + "top": { "texture": "#0", "uv": [11.5, 0.25, 12.5, 0.5] }, + "bottom": { "texture": "#0", "uv": [11.5, 0.5, 12.5, 0.75] } + } + }, + { + "from": [3, 11, 7], + "to": [7, 12, 8], + "rotation": { "origin": [3, 11, 6], "axis": "y", "angle": -90 }, + "faces": { + "north": { "texture": "#0", "uv": [11.5, 0.75, 12.5, 1] }, + "east": { "texture": "#0", "uv": [9.25, 11.75, 9.5, 12] }, + "south": { "texture": "#0", "uv": [11.5, 1, 12.5, 1.25] }, + "west": { "texture": "#0", "uv": [9.5, 11.75, 9.75, 12] }, + "top": { "texture": "#0", "uv": [11.5, 1.25, 12.5, 1.5] }, + "bottom": { "texture": "#0", "uv": [11.5, 1.5, 12.5, 1.75] } + } + }, + { + "from": [6, 11, 14], + "to": [10, 12, 15], + "faces": { + "north": { "texture": "#0", "uv": [11.5, 1.75, 12.5, 2] }, + "east": { "texture": "#0", "uv": [9.75, 11.75, 10, 12] }, + "south": { "texture": "#0", "uv": [2, 11.5, 3, 11.75] }, + "west": { "texture": "#0", "uv": [10, 11.75, 10.25, 12] }, + "top": { "texture": "#0", "uv": [11.5, 2, 12.5, 2.25] }, + "bottom": { "texture": "#0", "uv": [11.5, 2.25, 12.5, 2.5] } + } + }, + { + "from": [6, 11, 1], + "to": [10, 12, 2], + "faces": { + "north": { "texture": "#0", "uv": [11.5, 2.5, 12.5, 2.75] }, + "east": { "texture": "#0", "uv": [10.25, 11.75, 10.5, 12] }, + "south": { "texture": "#0", "uv": [11.5, 2.75, 12.5, 3] }, + "west": { "texture": "#0", "uv": [10.5, 11.75, 10.75, 12] }, + "top": { "texture": "#0", "uv": [3, 11.5, 4, 11.75] }, + "bottom": { "texture": "#0", "uv": [11.5, 3, 12.5, 3.25] } + } + }, + { + "from": [3, 0, 3], + "to": [13, 11, 13], + "faces": { + "north": { "texture": "#0", "uv": [0, 0, 2.5, 2.75] }, + "east": { "texture": "#0", "uv": [2.5, 0, 5, 2.75] }, + "south": { "texture": "#0", "uv": [0, 2.75, 2.5, 5.5] }, + "west": { "texture": "#0", "uv": [2.5, 2.75, 5, 5.5] }, + "top": { "texture": "#0", "uv": [5, 0, 7.5, 2.5] }, + "bottom": { "texture": "#0", "uv": [5, 2.5, 7.5, 5] } + } + }, + { + "from": [4, 0, 2], + "to": [12, 11, 3], + "faces": { + "north": { "texture": "#0", "uv": [2.5, 5.5, 4.5, 8.25] }, + "east": { "texture": "#0", "uv": [6, 10.25, 6.25, 13] }, + "south": { "texture": "#0", "uv": [7.5, 0, 9.5, 2.75] }, + "west": { "texture": "#0", "uv": [6.25, 10.25, 6.5, 13] }, + "top": { "texture": "#0", "uv": [2, 11, 4, 11.25] }, + "bottom": { "texture": "#0", "uv": [11, 4.5, 13, 4.75] } + } + }, + { + "from": [13, 0, 7], + "to": [21, 11, 8], + "rotation": { "origin": [15, 0, 6], "axis": "y", "angle": -90 }, + "faces": { + "north": { "texture": "#0", "uv": [7.5, 2.75, 9.5, 5.5] }, + "east": { "texture": "#0", "uv": [10.5, 3.5, 10.75, 6.25] }, + "south": { "texture": "#0", "uv": [4.5, 7.5, 6.5, 10.25] }, + "west": { "texture": "#0", "uv": [10.5, 6.25, 10.75, 9] }, + "top": { "texture": "#0", "uv": [11, 4.75, 13, 5] }, + "bottom": { "texture": "#0", "uv": [11, 5, 13, 5.25] } + } + }, + { + "from": [16, 0, 7], + "to": [20, 11, 8], + "rotation": { "origin": [16, 0, 6], "axis": "y", "angle": -90 }, + "faces": { + "north": { "texture": "#0", "uv": [8.5, 8.25, 9.5, 11] }, + "east": { "texture": "#0", "uv": [10.5, 9, 10.75, 11.75] }, + "south": { "texture": "#0", "uv": [9.5, 0, 10.5, 2.75] }, + "west": { "texture": "#0", "uv": [0, 10.75, 0.25, 13.5] }, + "top": { "texture": "#0", "uv": [11, 10.25, 12, 10.5] }, + "bottom": { "texture": "#0", "uv": [11, 10.5, 12, 10.75] } + } + }, + { + "from": [2, 0, 7], + "to": [10, 11, 8], + "rotation": { "origin": [4, 0, 6], "axis": "y", "angle": -90 }, + "faces": { + "north": { "texture": "#0", "uv": [7.5, 5.5, 9.5, 8.25] }, + "east": { "texture": "#0", "uv": [0.25, 10.75, 0.5, 13.5] }, + "south": { "texture": "#0", "uv": [0, 8, 2, 10.75] }, + "west": { "texture": "#0", "uv": [0.5, 10.75, 0.75, 13.5] }, + "top": { "texture": "#0", "uv": [11, 5.25, 13, 5.5] }, + "bottom": { "texture": "#0", "uv": [11, 5.5, 13, 5.75] } + } + }, + { + "from": [3, 0, 7], + "to": [7, 11, 8], + "rotation": { "origin": [3, 0, 6], "axis": "y", "angle": -90 }, + "faces": { + "north": { "texture": "#0", "uv": [9.5, 2.75, 10.5, 5.5] }, + "east": { "texture": "#0", "uv": [0.75, 10.75, 1, 13.5] }, + "south": { "texture": "#0", "uv": [9.5, 5.5, 10.5, 8.25] }, + "west": { "texture": "#0", "uv": [1, 10.75, 1.25, 13.5] }, + "top": { "texture": "#0", "uv": [11, 10.75, 12, 11] }, + "bottom": { "texture": "#0", "uv": [11, 11, 12, 11.25] } + } + }, + { + "from": [6, 0, 1], + "to": [10, 11, 2], + "faces": { + "north": { "texture": "#0", "uv": [9.5, 8.25, 10.5, 11] }, + "east": { "texture": "#0", "uv": [1.25, 10.75, 1.5, 13.5] }, + "south": { "texture": "#0", "uv": [4, 10.25, 5, 13] }, + "west": { "texture": "#0", "uv": [1.5, 10.75, 1.75, 13.5] }, + "top": { "texture": "#0", "uv": [2, 11.25, 3, 11.5] }, + "bottom": { "texture": "#0", "uv": [3, 11.25, 4, 11.5] } + } + }, + { + "from": [6, 0, 14], + "to": [10, 11, 15], + "faces": { + "north": { "texture": "#0", "uv": [5, 10.25, 6, 13] }, + "east": { "texture": "#0", "uv": [1.75, 10.75, 2, 13.5] }, + "south": { "texture": "#0", "uv": [10.5, 0, 11.5, 2.75] }, + "west": { "texture": "#0", "uv": [10.75, 3.5, 11, 6.25] }, + "top": { "texture": "#0", "uv": [6.5, 11.25, 7.5, 11.5] }, + "bottom": { "texture": "#0", "uv": [7.5, 11.25, 8.5, 11.5] } + } + }, + { + "from": [4, 0, 13], + "to": [12, 11, 14], + "faces": { + "north": { "texture": "#0", "uv": [2, 8.25, 4, 11] }, + "east": { "texture": "#0", "uv": [10.75, 6.25, 11, 9] }, + "south": { "texture": "#0", "uv": [6.5, 8.25, 8.5, 11] }, + "west": { "texture": "#0", "uv": [10.75, 9, 11, 11.75] }, + "top": { "texture": "#0", "uv": [11, 5.75, 13, 6] }, + "bottom": { "texture": "#0", "uv": [11, 6, 13, 6.25] } + } + }, + { + "from": [5, 9, 14], + "to": [6, 12, 15], + "faces": { + "north": { "texture": "#0", "uv": [4, 8.25, 4.25, 9] }, + "east": { "texture": "#0", "uv": [4.25, 8.25, 4.5, 9] }, + "south": { "texture": "#0", "uv": [4, 9, 4.25, 9.75] }, + "west": { "texture": "#0", "uv": [4.25, 9, 4.5, 9.75] }, + "top": { "texture": "#0", "uv": [8.25, 11.5, 8.5, 11.75] }, + "bottom": { "texture": "#0", "uv": [8.5, 11.5, 8.75, 11.75] } + } + }, + { + "from": [10, 9, 14], + "to": [11, 12, 15], + "faces": { + "north": { "texture": "#0", "uv": [6.5, 11.5, 6.75, 12.25] }, + "east": { "texture": "#0", "uv": [6.75, 11.5, 7, 12.25] }, + "south": { "texture": "#0", "uv": [7, 11.5, 7.25, 12.25] }, + "west": { "texture": "#0", "uv": [7.25, 11.5, 7.5, 12.25] }, + "top": { "texture": "#0", "uv": [8.75, 11.5, 9, 11.75] }, + "bottom": { "texture": "#0", "uv": [9, 11.5, 9.25, 11.75] } + } + }, + { + "from": [6, 4, 0], + "to": [10, 7, 1], + "faces": { + "north": { "texture": "#0", "uv": [6.5, 7.5, 7.5, 8.25] }, + "east": { "texture": "#0", "uv": [4.5, 6.5, 4.75, 7.25] }, + "south": { "texture": "#0", "uv": [10.5, 2.75, 11.5, 3.5] }, + "west": { "texture": "#0", "uv": [4.75, 6.5, 5, 7.25] }, + "top": { "texture": "#0", "uv": [11, 9.75, 12, 10] }, + "bottom": { "texture": "#0", "uv": [11, 10, 12, 10.25] } + } + }, + { + "from": [9, 7, 0], + "to": [10, 8, 1], + "faces": { + "north": { "texture": "#0", "uv": [4.5, 7.25, 4.75, 7.5] }, + "east": { "texture": "#0", "uv": [4.75, 7.25, 5, 7.5] }, + "south": { "texture": "#0", "uv": [2, 8, 2.25, 8.25] }, + "west": { "texture": "#0", "uv": [2.25, 8, 2.5, 8.25] }, + "top": { "texture": "#0", "uv": [4, 9.75, 4.25, 10] }, + "bottom": { "texture": "#0", "uv": [4.25, 9.75, 4.5, 10] } + } + }, + { + "from": [6, 7, 0], + "to": [7, 8, 1], + "faces": { + "north": { "texture": "#0", "uv": [4, 10, 4.25, 10.25] }, + "east": { "texture": "#0", "uv": [4.25, 10, 4.5, 10.25] }, + "south": { "texture": "#0", "uv": [11.5, 3.25, 11.75, 3.5] }, + "west": { "texture": "#0", "uv": [7.5, 11.5, 7.75, 11.75] }, + "top": { "texture": "#0", "uv": [7.75, 11.5, 8, 11.75] }, + "bottom": { "texture": "#0", "uv": [8, 11.5, 8.25, 11.75] } + } + } + ] + } +} diff --git a/mods/bworld/textures/worm_bin.png b/mods/bworld/textures/worm_bin.png new file mode 100644 index 0000000..bf532f8 Binary files /dev/null and b/mods/bworld/textures/worm_bin.png differ diff --git a/mods/bworld/textures/worm_bin_front.png b/mods/bworld/textures/worm_bin_front.png deleted file mode 100644 index ab5a1c7..0000000 Binary files a/mods/bworld/textures/worm_bin_front.png and /dev/null differ diff --git a/mods/bworld/textures/worm_bin_side.png b/mods/bworld/textures/worm_bin_side.png deleted file mode 100644 index bd80816..0000000 Binary files a/mods/bworld/textures/worm_bin_side.png and /dev/null differ diff --git a/mods/bworld/textures/worm_bin_top.png b/mods/bworld/textures/worm_bin_top.png deleted file mode 100644 index 0451566..0000000 Binary files a/mods/bworld/textures/worm_bin_top.png and /dev/null differ diff --git a/tests/importer_test.ts b/tests/importer_test.ts index feea5dc..33c5f3e 100644 --- a/tests/importer_test.ts +++ b/tests/importer_test.ts @@ -111,7 +111,7 @@ Deno.test("models that can't be converted are reported, not written", () => { "item/shiny_ingot": { parent: "gears:item/ingot" }, "block/tilted": { ...GEAR, - elements: [{ ...GEAR.elements[0], rotation: { angle: 30, axis: "x", origin: [8, 8, 8] } }], + elements: [{ ...GEAR.elements[0], rotation: { angle: 200, axis: "x", origin: [8, 8, 8] } }], }, // these two would both be gears:block_a_b "block/a/b": GEAR, @@ -125,7 +125,7 @@ Deno.test("models that can't be converted are reported, not written", () => { "gears:item/shiny_ingot", ]); const errors = Object.fromEntries(report.errors.map(({ location, problems }) => [location, problems.join(" ")])); - assertStringIncludes(errors["gears:block/tilted"], "30"); + assertStringIncludes(errors["gears:block/tilted"], "200"); assertStringIncludes(errors["gears:block/a_b"] ?? errors["gears:block/a/b"], "--remap"); assertEquals(report.written.length, 1); }); @@ -361,3 +361,45 @@ Deno.test("locations the remaps skip are left out of every kind, and blocks draw // it would be drawn with a model that isn't there assertEquals(JSON.parse(Deno.readTextFileSync(`${output}/gear_stand.json`)).block.model, "bworld:cube"); }); + +// a blockbench export outside any mod's resources, next to a folder that's a bworld mod +function loose_model(json: unknown) { + const dir = Deno.makeTempDirSync(); + Deno.writeTextFileSync(`${dir}/thing.json`, JSON.stringify(json)); + const mod = Deno.makeTempDirSync(); + Deno.writeTextFileSync(`${mod}/manifest.json`, JSON.stringify({ format_version: 1, id: "tinker" })); + return { file: `${dir}/thing.json`, output: `${mod}/models/thing.json` }; +} + +Deno.test("a loose model is the mod's its output goes in, and so are its bare texture names", () => { + const { file, output } = loose_model({ textures: { "0": "thing" }, elements: GEAR.elements }); + assertThrows(() => import_models(file), Error, "--namespace"); + const report = import_models(file, { output }); + assertEquals(report.errors, []); + const model = JSON.parse(Deno.readTextFileSync(output)).model; + assertEquals(model.id, "tinker:thing"); + assertEquals(model.textures, { "0": "tinker:thing" }); +}); + +Deno.test("blockbench's newer rotations become bworld's, at any angle around one axis", () => { + const element = (rotation: unknown) => ({ ...GEAR.elements[0], rotation }); + const { file, output } = loose_model({ + textures: { "0": "thing" }, + elements: [ + element({ x: 0, y: 90, z: 0, origin: [7, 13, 9] }), + element({ x: 0, y: 0, z: 0, origin: [8, 8, 8] }), + element({ angle: 0, axis: "y", origin: [8, 8, 8] }), + ], + }); + import_models(file, { output }); + const model = JSON.parse(Deno.readTextFileSync(output)).model; + assertEquals(validate_model(model), []); + assertEquals(model.elements[0].rotation, { origin: [7, 13, 9], axis: "y", angle: 90 }); + // turning by nothing isn't a rotation + assertEquals(model.elements[1].rotation, undefined); + assertEquals(model.elements[2].rotation, undefined); + + const { file: tilted } = loose_model({ elements: [element({ x: 45, y: 90, z: 0, origin: [8, 8, 8] })] }); + const report = import_models(tilted, { namespace: "tinker" }); + assertStringIncludes(report.errors[0].problems[0], "x and y"); +}); diff --git a/tools/importer.ts b/tools/importer.ts index a7e7a82..18ffd53 100644 --- a/tools/importer.ts +++ b/tools/importer.ts @@ -30,7 +30,8 @@ kinds: options: --output PATH the file (for one input file) or the folder to write. one model without it is printed --namespace NS the namespace, when the input isn't inside assets//, or the only one to take from a - folder with several + folder with several. a file outside assets/ is otherwise the mod's that --output is in, and its + bare names (a texture "gear") are that mod's too, the way blockbench exports them --parents FOLDER another folder to follow parent models through, like another mod's resources. can be repeated --remap FILE json that renames locations before they become ids, longest match first, and leaves out locations that match a "skip" pattern: diff --git a/tools/importer/blocks.ts b/tools/importer/blocks.ts index e184622..515f0e1 100644 --- a/tools/importer/blocks.ts +++ b/tools/importer/blocks.ts @@ -76,7 +76,7 @@ const CUBES = [ export function import_blocks(input: string, options: ImportOptions = {}): ImportReport { if (options.output === undefined) throw new Error("--output is needed for blocks"); const remaps = options.remaps ?? DEFAULT_REMAPS; - const files = input_files(input, "blockstates", ".json", options.namespace); + const files = input_files(input, "blockstates", ".json", options.namespace, options.output); // models say how a block is drawn (a full cube or not, cutout or translucent) and whether it has an item const read = model_source(models_near(input, files.map(({ root }) => root), options)); const report = empty_report(); diff --git a/tools/importer/items.ts b/tools/importer/items.ts index f6e037a..6e1d946 100644 --- a/tools/importer/items.ts +++ b/tools/importer/items.ts @@ -31,7 +31,7 @@ export function import_items(input: string, options: ImportOptions = {}): Import if (options.output === undefined) throw new Error("--output is needed for items"); const remaps = options.remaps ?? DEFAULT_REMAPS; // an item is a model directly in models/item, the folders under it are parts of models - const files = input_files(input, "models", ".json", options.namespace) + const files = input_files(input, "models", ".json", options.namespace, options.output) .filter(({ root, file }) => /^item\/[^/]+$/.test(location_of(root, file, ".json").path)); const roots = [...new Set(files.map(({ root }) => root))]; const read = model_source(models_near(input, roots, options)); diff --git a/tools/importer/models.ts b/tools/importer/models.ts index 6c96617..89d6b79 100644 --- a/tools/importer/models.ts +++ b/tools/importer/models.ts @@ -44,7 +44,6 @@ const FACE_NAMES: Record = { west: "west", east: "east", }; -const ANGLES = [-45, -22.5, 0, 22.5, 45]; // item models that are a flat sprite or drawn by code, not boxes, and so is anything that has one as a parent const NOT_BOXES: Record = { @@ -74,7 +73,7 @@ export function import_models(input: string, options: ImportOptions = {}): Impor const remaps = options.remaps ?? DEFAULT_REMAPS; const single = is_file(input); if (!single && options.output === undefined) throw new Error("--output is needed to convert a folder"); - const files = input_files(input, "models", ".json", options.namespace); + const files = input_files(input, "models", ".json", options.namespace, options.output); const read = model_source([ ...new Set(files.map(({ root }) => root)), ...(options.parents ?? []).flatMap((dir) => find_roots(dir, "models")), @@ -87,7 +86,8 @@ export function import_models(input: string, options: ImportOptions = {}): Impor const where = location_of(root, file, ".json"); const location = location_string(where); if (skips(location, remaps, report)) continue; - const result = convert_model(location, read, remaps); + // a loose file's bare texture names are its own, like blockbench writes them + const result = convert_model(location, read, remaps, root.loose ? root.namespace : "minecraft"); if (result.kind === "skipped") report.skipped.push({ location, reason: result.reason }); else if (result.kind === "error") report.errors.push({ location, problems: result.problems }); else if (claim(result.id, location)) { @@ -124,7 +124,8 @@ export function model_source(roots: ResourceRoot[]): ModelSource { }; } -export function convert_model(location: string, read: ModelSource, remaps: Remaps): ModelResult { +// bare is the namespace of textures named without one +export function convert_model(location: string, read: ModelSource, remaps: Remaps, bare = "minecraft"): ModelResult { const json = read(location_string(parse_location(location))); if (!json) return { kind: "error", problems: [`${location} wasn't found`] }; const made = made_by(location, read); @@ -137,7 +138,7 @@ export function convert_model(location: string, read: ModelSource, remaps: Remap } const texture = (value: string, where: string) => { if (value.startsWith("#")) return value; - const id = to_id(value, remaps.textures); + const id = to_id(value, remaps.textures, bare); if (!is_valid_id(id)) problems.push(`${where}: "${value}" doesn't make a valid texture id (${id})`); return id; }; @@ -208,16 +209,28 @@ function convert_element( }; const out = { from: point("from"), to: point("to") } as ModelElementJson; + // { axis, angle }, or blockbench's newer { x, y, z } with a turn around each const rotation = element.rotation as Record | undefined; - if (rotation && rotation.angle !== 0) { - if (!ANGLES.includes(rotation.angle as number)) { - problems.push(`${where}.rotation: an angle of ${rotation.angle} isn't one of ${ANGLES.join(", ")}`); + const turns = rotation === undefined + ? [] + : typeof rotation.axis === "string" + ? [[rotation.axis, rotation.angle]] + : ["x", "y", "z"].map((axis) => [axis, rotation[axis] ?? 0]); + const turned = turns.filter(([, angle]) => angle !== 0); + if (turned.length > 1) { + problems.push( + `${where}.rotation turns around ${turned.map(([axis]) => axis).join(" and ")} at once, bworld only one`, + ); + } else if (turned.length === 1) { + const [[axis, angle]] = turned; + if (typeof angle !== "number" || Math.abs(angle) > 180) { + problems.push(`${where}.rotation: ${angle} isn't an angle from -180 to 180`); } out.rotation = { - origin: rotation.origin as [number, number, number], - axis: rotation.axis as "x" | "y" | "z", - angle: rotation.angle as number, - ...(rotation.rescale === true ? { rescale: true } : {}), + origin: rotation!.origin as [number, number, number], + axis: axis as "x" | "y" | "z", + angle: angle as number, + ...(rotation!.rescale === true ? { rescale: true } : {}), }; } if (element.shade === false) out.shade = false; diff --git a/tools/importer/resources.ts b/tools/importer/resources.ts index 195fdde..ccdbc87 100644 --- a/tools/importer/resources.ts +++ b/tools/importer/resources.ts @@ -12,10 +12,11 @@ export interface Location { path: string; } -export function parse_location(value: string): Location { +// bare is the namespace of a location written without one: minecraft's, in a mod's resources +export function parse_location(value: string, bare = "minecraft"): Location { const colon = value.indexOf(":"); return colon < 0 - ? { namespace: "minecraft", path: value } + ? { namespace: bare, path: value } : { namespace: value.slice(0, colon), path: value.slice(colon + 1) }; } @@ -53,8 +54,8 @@ function template_remaps(): Remap { ); } -export function remap_location(location: string, remap: Remap = {}): Location { - const full = location_string(parse_location(location)); +export function remap_location(location: string, remap: Remap = {}, bare = "minecraft"): Location { + const full = location_string(parse_location(location, bare)); for (const from of Object.keys(remap).sort((a, b) => b.length - a.length)) { const prefix = from.endsWith("/") || from.endsWith(":"); if (prefix ? full.startsWith(from) : full === from) { @@ -64,8 +65,8 @@ export function remap_location(location: string, remap: Remap = {}): Location { return parse_location(full); } -export function to_id(location: string, remap: Remap = {}): string { - const { namespace, path } = remap_location(location, remap); +export function to_id(location: string, remap: Remap = {}, bare = "minecraft"): string { + const { namespace, path } = remap_location(location, remap, bare); return `${namespace}:${path.replaceAll("/", "_")}`; } @@ -77,6 +78,9 @@ export function is_valid_id(id: string): boolean { export interface ResourceRoot { namespace: string; dir: string; + // not in a mod's assets/ but a loose folder, like blockbench's exports. what it names without a namespace is its + // own, not minecraft's: a texture "gear" next to a model is that model's texture + loose?: boolean; } // the roots of a kind inside a folder: every assets// under it (only namespace's, when it's given), or @@ -93,7 +97,7 @@ export function find_roots(input: string, kind: string, namespace?: string): Res } } } - if (roots.length === 0) return namespace ? [{ namespace, dir }] : []; + if (roots.length === 0) return namespace ? [{ namespace, dir, loose: true }] : []; return namespace ? roots.filter((root) => root.namespace === namespace) : roots; } @@ -103,12 +107,21 @@ export interface InputFile { file: string; } -// what to convert: one file, or every file with the extension in a folder's roots of a kind -export function input_files(input: string, kind: string, extension: string, namespace?: string): InputFile[] { +// what to convert: one file, or every file with the extension in a folder's roots of a kind. a file outside any +// assets/ folder is the namespace's, or the mod's that output is in +export function input_files( + input: string, + kind: string, + extension: string, + namespace?: string, + output?: string, +): InputFile[] { if (is_file(input)) { - const root = enclosing_root(input, kind) ?? (namespace && { namespace, dir: dirname(resolve(input)) }); - if (!root) throw new Error(`can't tell ${input}'s namespace: pass --namespace`); - return [{ root, file: resolve(input) }]; + const enclosing = enclosing_root(input, kind); + if (enclosing) return [{ root: enclosing, file: resolve(input) }]; + const loose = namespace ?? (output && mod_namespace(output)); + if (!loose) throw new Error(`can't tell ${input}'s namespace: pass --namespace, or an --output in a mod`); + return [{ root: { namespace: loose, dir: dirname(resolve(input)), loose: true }, file: resolve(input) }]; } const roots = find_roots(input, kind, namespace); if (roots.length === 0) { @@ -169,6 +182,16 @@ function read_dir(dir: string): Deno.DirEntry[] { } } +// the id of the mod a path is in, from the manifest.json of the nearest folder up that has one +export function mod_namespace(path: string): string | undefined { + for (let dir = dirname(resolve(path)); dir !== dirname(dir); dir = dirname(dir)) { + if (!is_file(`${dir}/manifest.json`)) continue; + const id = JSON.parse(Deno.readTextFileSync(`${dir}/manifest.json`)).id; + return typeof id === "string" ? id : undefined; + } + return undefined; +} + export function is_file(path: string): boolean { try { return Deno.statSync(path).isFile; diff --git a/tools/importer/textures.ts b/tools/importer/textures.ts index 636e15f..c88f198 100644 --- a/tools/importer/textures.ts +++ b/tools/importer/textures.ts @@ -60,7 +60,7 @@ export function import_textures(input: string, options: ImportOptions = {}): Imp const single = is_file(input); const report = empty_report(); const claim = id_claims(report); - const files = input_files(input, "textures", ".png", options.namespace); + const files = input_files(input, "textures", ".png", options.namespace, options.output); // a texture gets its namespace from the mod it's in, so two namespaces' textures can't share one folder const namespaces = [...new Set(files.map(({ root }) => root.namespace))].filter((ns) => ns !== "minecraft"); if (namespaces.length > 1) {