mc-assets 0.2.38 → 0.2.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.MD CHANGED
@@ -28,7 +28,7 @@ For contributing & building instructions see [building](#building) section.
28
28
  > Tested on Node.js 18 and above.
29
29
 
30
30
 
31
- All blockstates + models + all atlas textures for all versions bundled with rsbuild (uncompressed): 5.15 MB.
31
+ All blockstates + models + all atlas textures for all versions bundled with rsbuild (uncompressed): 5.12 MB.
32
32
 
33
33
  This packages includes versions for: 1.7.10, 1.8, 1.8.1, 1.8.2, 1.8.3, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.8.8, 1.8.9, 1.9, 1.9.1, 1.9.2, 1.9.3, 1.9.4, 1.10, 1.10.1, 1.10.2, 1.11, 1.11.1, 1.11.2, 1.12, 1.12.1, 1.12.2, 1.13, 1.13.1, 1.13.2, 1.14, 1.14.1, 1.14.2, 1.14.3, 1.14.4, 1.15, 1.15.1, 1.15.2, 1.16, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5, 1.17, 1.17.1, 1.18, 1.18.1, 1.18.2, 1.19, 1.19.1, 1.19.2, 1.19.3, 1.19.4, 1.20, 1.20.1, 1.20.2, 1.20.3, 1.20.4, 1.20.5, 1.20.6, 1.21, 1.21.1, 1.21.2, 1.21.3, 1.21.4.
34
34
 
@@ -22,6 +22,7 @@ export declare class AssetsParser {
22
22
  matchedModels: string[];
23
23
  matchedConditions: string[];
24
24
  private getModelsByBlock;
25
+ private getModelData;
25
26
  getResolvedModelsByModel(model: string, debugQueryName?: string, clearModel?: boolean): {
26
27
  resolvedModel: Pick<BlockModel, "textures" | "ao" | "elements"> & {
27
28
  x?: number;
@@ -171,11 +171,15 @@ export class AssetsParser {
171
171
  }
172
172
  return modelsResolved; // todo figure out the type error
173
173
  }
174
+ getModelData(model) {
175
+ const modelData = this.blockModelsStore.get(this.version, model);
176
+ return modelData && typeof modelData === 'object' ? structuredClone(modelData) : modelData;
177
+ }
174
178
  getResolvedModelsByModel(model, debugQueryName, clearModel = true) {
175
179
  if (clearModel) {
176
180
  this.resolvedModel = {};
177
181
  }
178
- const modelData = this.blockModelsStore.get(this.version, model);
182
+ const modelData = this.getModelData(model);
179
183
  if (!modelData) {
180
184
  this.issues.push(`Model ${model} not found. Ensure it is present in assets/${getNamespace(model)}/models/${model}.json`);
181
185
  return;
@@ -191,7 +195,7 @@ export class AssetsParser {
191
195
  const collectModels = (model) => {
192
196
  collectedParentModels.push(model);
193
197
  if (model.parent) {
194
- const parent = this.blockModelsStore.get(this.version, model.parent);
198
+ const parent = this.getModelData(model.parent);
195
199
  if (!parent) {
196
200
  this.issues.push(`Parent model ${model.parent} not found for ${debugQueryName}`);
197
201
  return;
@@ -218,11 +222,16 @@ export class AssetsParser {
218
222
  }
219
223
  }
220
224
  if (model.elements) {
221
- this.resolvedModel.elements = structuredClone(model.elements);
225
+ this.resolvedModel.elements = model.elements;
222
226
  }
223
227
  for (const [key, value] of Object.entries(model)) {
224
228
  if (key !== 'elements' && key !== 'textures' && key !== 'parent') {
225
- this.resolvedModel[key] = value;
229
+ if (this.resolvedModel[key] && typeof this.resolvedModel[key] === 'object' && value && typeof value === 'object') {
230
+ Object.assign(this.resolvedModel[key], value);
231
+ }
232
+ else {
233
+ this.resolvedModel[key] = value;
234
+ }
226
235
  }
227
236
  }
228
237
  }
@@ -1,4 +1,4 @@
1
- import { createAtlas } from 'apl-image-packer';
1
+ import { MaxRectsPacker, PACKING_LOGIC, Rectangle } from 'maxrects-packer';
2
2
  export const MAX_CANVAS_SIZE = 16_384;
3
3
  function nextPowerOfTwo(n) {
4
4
  if (n === 0)
@@ -20,7 +20,7 @@ export const getAtlasSize = (numberOfTiles, tileSize) => {
20
20
  };
21
21
  export const makeTextureAtlas = ({ input, getLoadedImage, tileSize = 16, getCanvas = (imgSize) => typeof document !== 'undefined' && document.createElement ? document.createElement('canvas') : new globalThis.Canvas(imgSize, imgSize, 'png'), }) => {
22
22
  // Pre-calculate all texture dimensions and prepare images
23
- const texturesWithDimensions = input.map(keyValue => {
23
+ const texturesWithDimensions = [...new Set(input)].map(keyValue => {
24
24
  const inputData = getLoadedImage(keyValue);
25
25
  let img;
26
26
  if (inputData.image) {
@@ -53,12 +53,45 @@ export const makeTextureAtlas = ({ input, getLoadedImage, tileSize = 16, getCanv
53
53
  renderSourceStartY: inputData.renderSourceStartY ?? 0,
54
54
  };
55
55
  });
56
- // Use apl-image-packer to calculate optimal positions
57
- const atlas = createAtlas(texturesWithDimensions.map(tex => ({
58
- width: tex.renderWidth,
59
- height: tex.renderHeight,
60
- data: tex // Store all texture data for later use
61
- })));
56
+ // Use MaxRectsPacker to calculate optimal positions
57
+ const packer = new MaxRectsPacker(undefined, undefined, 0, {
58
+ square: true,
59
+ smart: true,
60
+ pot: true,
61
+ logic: PACKING_LOGIC.MAX_AREA,
62
+ });
63
+ // Add all textures as rectangles
64
+ const rectangles = texturesWithDimensions.map(tex => {
65
+ const rect = new Rectangle(tex.renderWidth, tex.renderHeight);
66
+ rect.data = tex; // Store texture data for later use
67
+ return rect;
68
+ });
69
+ packer.addArray(rectangles);
70
+ const firstBin = packer.bins[0];
71
+ if (!firstBin || !firstBin.rects || !firstBin.rects.length) {
72
+ throw new Error('Failed to pack textures');
73
+ }
74
+ const atlas = {
75
+ width: firstBin.width,
76
+ height: firstBin.height,
77
+ coords: firstBin.rects.map(rect => ({
78
+ x: rect.x,
79
+ y: rect.y,
80
+ img: { data: rect.data }
81
+ }))
82
+ };
83
+ // Check for duplicate keyValues in atlas
84
+ const seenKeyValues = new Set();
85
+ for (const coord of atlas.coords) {
86
+ const tex = coord.img.data;
87
+ if (seenKeyValues.has(tex.keyValue)) {
88
+ throw new Error(`Duplicate texture keyValue found in atlas: ${tex.keyValue}`);
89
+ }
90
+ seenKeyValues.add(tex.keyValue);
91
+ }
92
+ if (seenKeyValues.size !== input.length) {
93
+ throw new Error(`Lost some textures in packing: ${input.length - seenKeyValues.size}`);
94
+ }
62
95
  // Round up atlas size to power of 2
63
96
  const imgSize = Math.max(nextPowerOfTwo(atlas.width), nextPowerOfTwo(atlas.height));
64
97
  if (imgSize > MAX_CANVAS_SIZE) {
Binary file
Binary file