Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ melonJS is supporting the below tools and frameworks natively or through our off
[![Spine](https://github.com/melonjs/melonJS/assets/4033090/7a8d81b8-bc80-47bd-80dc-d9a054c78c96 "Spine")](http://esotericsoftware.com)
[![aseprite](https://github.com/melonjs/melonJS/assets/4033090/65d19fef-2eba-461a-b925-cc2ad3bb270c "aseprite")](https://www.aseprite.org)

Any other tool that exports the TexturePacker ("JSON (Array)" or "JSON (Hash)") or Aseprite JSON layout works as well, such as [spritesheet.dev](https://spritesheet.dev/).

3D scenes come from [Blender](https://www.blender.org) or any other tool that exports glTF (`.glb` or `.gltf`), such as Maya, 3ds Max or Cinema 4D.

Tools integration and usage with melonJS is documented in our [Wiki](https://github.com/melonjs/melonJS/wiki#third-party-tools-usage).

Getting Started
Expand Down
3 changes: 3 additions & 0 deletions packages/melonjs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- Levels: `registerObjectClass` and `registerObjectFactory` register the classes a level builds, whatever editor it comes from
- Levels: `level.load` takes `objects`, to choose which objects of a level are built
- Levels: an object built from a level carries the level's `id`, so `getChildByProp("id", id)` finds it
- `TextureAtlas`: an atlas from a tool it does not know by name (such as [spritesheet.dev](https://spritesheet.dev/)) loads when it has the TexturePacker or Aseprite JSON layout, instead of throwing `Unknown texture atlas format`
- `TextureAtlas`: TexturePacker's "JSON (Hash)" export is supported, as "JSON (Array)" is

### Deprecated
- Levels: `registerTiledObjectClass` and `registerTiledObjectFactory`, for `registerObjectClass` and `registerObjectFactory` (the same functions; the old names keep working)
Expand All @@ -22,6 +24,7 @@
- Loader: `loader.getTMX`, for `loader.getTileMap` (the same function)

### Fixed
- `TextureAtlas`: a ShoeBox atlas made with the melonJS exporter was refused with `Unknown texture atlas format: ShoeBox`
- TypeScript: an asset's `data` accepts a parsed map object
- Tile maps: a floating tile layer disappeared once the camera moved
- Tile maps: two tilesets cut from one image at different tile sizes drew the same tiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ many it kept.

| source | how |
|---|---|
| TexturePacker / Free Texture Packer JSON | `new TextureAtlas(loader.getJSON(name), loader.getImage(name))` |
| TexturePacker / Free Texture Packer JSON, "JSON (Array)" or "JSON (Hash)" | `new TextureAtlas(loader.getJSON(name), loader.getImage(name))` |
| Aseprite **JSON export** | same as TexturePacker — the format is detected from `meta.app` |
| another tool writing either layout (spritesheet.dev, your own exporter) | same call. Since 20.18 an unknown `meta.app` is recognised from the file's structure: `meta.frameTags` reads as Aseprite, frames with a `frame` rectangle as TexturePacker. Before, it threw `Unknown texture atlas format` |
| Aseprite **`.aseprite` / `.ase` binary** | loader type `"aseprite"`, which stores the composited image *and* a JSON sidecar under the one asset name |
| ShoeBox | JSON export using the melonJS exporter settings file; same call as TexturePacker |
| fixed-cell spritesheet | `new TextureAtlas({ framewidth, frameheight, anchorPoint }, loader.getImage(name))` |
Expand Down Expand Up @@ -237,6 +238,9 @@ Two consequences:
| sprites do not batch | more distinct textures in one frame than `renderer.maxTextures` |
| nine-slice corners stretch anyway | `insetX`/`insetY` instead of `insetx`/`insety` |
| an atlas animation blinks: the sprite vanishes on some of its frames (before 20.17) | the frames the packer stored rotated were drawn off the screen unless the sprite was at (0, 0); a nine-slice sprite from a rotated frame was stretched along the wrong axis. Update; on an older version, turn rotation off in the packer |
| `Unknown texture atlas format: <name or URL>` from `new TextureAtlas` | before 20.18, an atlas whose `meta.app` named a tool the engine did not know, even in the TexturePacker or Aseprite layout (and `ShoeBox`, matched in lowercase only); since 20.18, a file that is in neither layout: no `meta.size`, a frame with no `frame` rectangle, or a list of frames with no `filename` |
| `frames.forEach is not a function` from `new TextureAtlas` | before 20.18, a TexturePacker "JSON (Hash)" export; re-export as "JSON (Array)" or update |
| a frame of an Aseprite JSON export is not found by its name | exported as "Array": the frames are then named by their position (`"0"`, `"1"`…); export as "Hash" to keep the names |
| a tint will not turn the sprite the colour asked for, only a darker one | a tint multiplies the art's own colours: recolour with a `GradientMapEffect`, flash with a `FlashEffect` |

## Related skills
Expand Down
5 changes: 3 additions & 2 deletions packages/melonjs/skills/melonjs/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ what it is called here:
| [melonjs-20-migration](../melonjs-20-migration/SKILL.md) | Code based on melonJS 19.x or earlier, or an API that "should exist" but doesn't — `video.init`, `Entity`, `renderable.shader =`. |
| [melonjs-scenes-and-state](../melonjs-scenes-and-state/SKILL.md) | `Stage` subclasses, the state manager, transitions, pausing, the update loop, timers, tweens, saved data. |
| [melonjs-renderables](../melonjs-renderables/SKILL.md) | Subclassing `Renderable`, custom `draw()`, draw order, anchors, `floating`, post effects on an object. |
| [melonjs-sprites-and-animation](../melonjs-sprites-and-animation/SKILL.md) | `Sprite`, frame animation, texture atlases (TexturePacker / Aseprite / ShoeBox), tint, flip, pooling. |
| [melonjs-sprites-and-animation](../melonjs-sprites-and-animation/SKILL.md) | `Sprite`, frame animation, texture atlases (TexturePacker / Aseprite / ShoeBox, or any tool exporting their JSON layout), tint, flip, pooling. |
| [melonjs-input](../melonjs-input/SKILL.md) | Keyboard actions, pointer and touch handling, drag, gamepad, virtual controls (fixed or floating sticks and buttons, tap to act). |
| [melonjs-physics](../melonjs-physics/SKILL.md) | Collision, bodies, movement, spatial queries, and the planck / matter / Rapier / Rapier 3D adapters. |
| [melonjs-math](../melonjs-math/SKILL.md) | Vectors and matrices: what mutates, what allocates, and interpolating with lerp, slerp, damp or moveTowards. |
Expand Down Expand Up @@ -272,7 +272,8 @@ check for:
`UICheckbox` (a radio button with a `group`), `UISlider`, `ProgressBar`,
`UISpriteElement`, keyboard and gamepad navigation between widgets once one
has `focus()`, `NineSliceSprite`, `Draggable` / `DropTarget`, `BitmapText`.
- **Art:** `TextureAtlas` (TexturePacker, ShoeBox, Aseprite),
- **Art:** `TextureAtlas` (TexturePacker, ShoeBox, Aseprite, and any tool that
exports the TexturePacker or Aseprite JSON layout),
`CanvasRenderTarget` for bake-once drawing, `NoiseTexture2d`.
- **Game flow:** `state.transition()` (`"fade"`, `"mask"` or `"pixelate"`)
between screens, with a mask shaped like an image or the player's own sprite
Expand Down
2 changes: 1 addition & 1 deletion packages/melonjs/src/loader/parsers/aseprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ function composite(parsed) {

/**
* Build the JSON sidecar in the shape expected by parseAseprite()
* in src/video/texture/parser/aseprite.js — meta.app must include
* in src/video/texture/parser/aseprite.js — meta.app includes
* "aseprite" so identifyFormat() picks the right route.
* @ignore
* @internal
Expand Down
153 changes: 82 additions & 71 deletions packages/melonjs/src/video/texture/atlas.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,72 @@ export function createAtlas(
}

/**
* return a string that identifies the texture atlas type
* return a string that identifies the texture atlas type: from the name of
* the tool in `meta.app` when it is a known one, else from the structure of
* the file, so that any exporter writing the TexturePacker or Aseprite JSON
* layout is accepted under its own name
* @param {object} atlas - the atlas description (with a `meta` object)
* @returns {string} "texturepacker", "shoebox", "aseprite" or "melonJS"
* @ignore
* @internal
*/
export function identifyFormat(app) {
export function identifyFormat(atlas) {
const meta = atlas.meta;
const app = typeof meta.app === "string" ? meta.app : "";
if (app.includes("texturepacker") || app.includes("free-tex-packer")) {
return "texturepacker";
} else if (app.includes("shoebox")) {
} else if (app.toLowerCase().includes("shoebox")) {
// the melonJS exporter for ShoeBox writes "ShoeBox"
return "shoebox";
} else if (app.includes("aseprite")) {
return "aseprite";
} else if (app.includes("melonJS")) {
return "melonJS";
} else {
throw new Error("Unknown texture atlas format: " + app);
}

// an unknown tool: the file has to be one a parser reads in full, that
// is the sheet size and, for every frame, its rectangle
const frames = atlas.frames;
if (
typeof frames === "object" &&
frames !== null &&
typeof meta.size?.w === "number" &&
typeof meta.size?.h === "number"
) {
const entries = Object.values(frames);
const hasFrames =
entries.length > 0 &&
entries.every((entry) => {
return typeof entry?.frame === "object" && entry.frame !== null;
});
if (hasFrames && Array.isArray(meta.frameTags)) {
// animation tags are Aseprite's
return "aseprite";
}
// frames keyed by name ("JSON (Hash)"), or a list of them
// ("JSON (Array)") that each carry their name
const isNamed =
!Array.isArray(frames) ||
frames.every((entry) => {
return typeof entry.filename === "string";
});
if (hasFrames && isNamed) {
return "texturepacker";
}
}

throw new Error("Unknown texture atlas format: " + meta.app);
}

/**
* A Texture atlas class, currently supports : <br>
* - [TexturePacker]{@link http://www.codeandweb.com/texturepacker/} : through JSON export (standard and multipack texture atlas) <br>
* - [TexturePacker]{@link http://www.codeandweb.com/texturepacker/} : through JSON export, "JSON (Array)" or "JSON (Hash)" (standard and multipack texture atlas) <br>
* - [Free Texture Packer]{@link http://free-tex-packer.com/app/} : through JSON export (standard and multipack texture atlas) <br>
* - [aseprite]{@link https://www.aseprite.org/} : through JSON export (standard and multipack texture atlas) <br>
* - [ShoeBox]{@link http://renderhjs.net/shoebox/} : through JSON export using the
* melonJS setting [file]{@link https://github.com/melonjs/melonJS/raw/master/media/shoebox_JSON_export.sbx} <br>
* - any other tool that writes the TexturePacker or Aseprite JSON layout, such as [spritesheet.dev]{@link https://spritesheet.dev/} : the format is
* recognised from the structure of the file when `meta.app` is not one of the names above <br>
* - Standard (fixed cell size) spritesheet : through a {framewidth:xx, frameheight:xx, anchorPoint:me.Vector2d} object
* );
* @category Game Objects
Expand All @@ -125,7 +166,9 @@ export class TextureAtlas extends Texture2d {
* @param {HTMLImageElement|HTMLCanvasElement|OffscreenCanvas|CompressedImage|string|OffscreenCanvas[]|HTMLImageElement[]|HTMLCanvasElement[]|string[]} [src=atlas.meta.image] - Image source
* @param {boolean|object} [options] - either a boolean (legacy `cache` flag — `false` disables `TextureCache` registration; default behavior is to cache) or an options object `{ cache?: boolean, normalMap?: HTMLImageElement|HTMLCanvasElement|OffscreenCanvas|ImageBitmap|string }`. When `normalMap` is provided, the atlas exposes a paired normal-map texture sharing the same UVs as the color texture, used for per-pixel lighting by the GPU renderers, WebGL and WebGPU (the Canvas renderer draws its sprites unlit; see {@link Sprite#normalMap}). An `HTMLVideoElement` is refused with a `TypeError`: a normal map is uploaded to the GPU once, so a video would stay on its first frame.
* @example
* // create a texture atlas from a JSON Object
* // create a texture atlas from a JSON Object: a TexturePacker
* // ("JSON (Array)" or "JSON (Hash)") or Aseprite export, or the same
* // layout written by any other tool
* game.texture = new me.TextureAtlas(
* me.loader.getJSON("texture")
* );
Expand Down Expand Up @@ -244,74 +287,42 @@ export class TextureAtlas extends Texture2d {
const atlas = atlases[i];

if (typeof atlas.meta !== "undefined") {
this.format = identifyFormat(atlas.meta.app);
this.format = identifyFormat(atlas);
this.repeat = atlas.meta.repeat || "no-repeat";
switch (this.format) {
case "texturepacker":
case "aseprite":
// set the texture
if (typeof src === "undefined") {
// get the texture name from the atlas meta data
const image = getImage(atlas.meta.image);
if (!image) {
throw new Error("Atlas texture '" + image + "' not found");
}
this.sources.set(atlas.meta.image, image);
} else {
this.sources.set(
atlas.meta.image || "default",
typeof src === "string" ? getImage(src) : src,
);
}
// initialize the atlas
if (this.format === "texturepacker") {
this.atlases.set(
atlas.meta.image || "default",
parseTexturePacker(atlas, this),
);
} else {
this.atlases.set(
atlas.meta.image || "default",
parseAseprite(atlas, this),
);
}
break;
case "shoebox":
if (
!atlas.meta.exporter ||
!atlas.meta.exporter.includes("melonJS")
) {
throw new Error(
"ShoeBox requires the JSON exporter : " +
"https://github.com/melonjs/melonJS/tree/master/media/shoebox_JSON_export.sbx",
);
}
this.sources.set(
"default",
typeof src === "string" ? getImage(src) : src,
);
// initialize the atlas
this.atlases.set(
atlas.meta.image || "default",
parseTexturePacker(atlas, this),
);
break;
case "melonJS":
this.sources.set(
"default",
typeof src === "string" ? getImage(src) : src,
);
// initialize the atlas
this.atlases.set(
atlas.meta.image || "default",
parseTexturePacker(atlas, this),
);
break;
default:
if (
this.format === "shoebox" &&
(!atlas.meta.exporter || !atlas.meta.exporter.includes("melonJS"))
) {
throw new Error(
"ShoeBox requires the JSON exporter : " +
"https://github.com/melonjs/melonJS/tree/master/media/shoebox_JSON_export.sbx",
);
}
// set the texture
if (this.format === "shoebox" || this.format === "melonJS") {
this.sources.set(
"default",
typeof src === "string" ? getImage(src) : src,
);
} else if (typeof src === "undefined") {
// get the texture name from the atlas meta data
const image = getImage(atlas.meta.image);
if (!image) {
throw new Error(
"Unknown texture atlas format: " + atlas.meta.app,
"Atlas texture '" + atlas.meta.image + "' not found",
);
}
this.sources.set(atlas.meta.image, image);
} else {
this.sources.set(
atlas.meta.image || "default",
typeof src === "string" ? getImage(src) : src,
);
}
// initialize the atlas
const parse =
this.format === "aseprite" ? parseAseprite : parseTexturePacker;
this.atlases.set(atlas.meta.image || "default", parse(atlas, this));
} else {
// a regular spritesheet
if (
Expand Down
44 changes: 3 additions & 41 deletions packages/melonjs/src/video/texture/parser/aseprite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ETA } from "../../../math/math.ts";
import { Vector2d } from "../../../math/vector2d.ts";
import { addFrame } from "./frame.js";

/**
* parse the given data and return a corresponding atlas
Expand All @@ -21,47 +20,10 @@ export function parseAseprite(data, textureAtlas) {
// in aseprite's JSON export (hash and array forms alike), `trimmed`,
// `spriteSourceSize`, `sourceSize`, `rotated`, `pivot` and `duration`
// are SIBLINGS of the `frame` rect on each entry — same layout as
// TexturePacker's (see texturepacker.js)
// TexturePacker's (see frame.js)
const entry = frames[name];
const frame = entry.frame;
const trimmed = !!entry.trimmed;

let trim;

if (trimmed) {
trim = {
x: entry.spriteSourceSize.x,
y: entry.spriteSourceSize.y,
w: entry.spriteSourceSize.w,
h: entry.spriteSourceSize.h,
};
}

let originX;
let originY;
// Pixel-based offset origin from the top-left of the source frame
const hasTextureAnchorPoint = entry.sourceSize && entry.pivot;
if (hasTextureAnchorPoint) {
originX = entry.sourceSize.w * entry.pivot.x - (trimmed ? trim.x : 0);
originY = entry.sourceSize.h * entry.pivot.y - (trimmed ? trim.y : 0);
}

atlas[name] = {
name: name, // frame name
texture: data.meta.image || "default", // the source texture
offset: new Vector2d(frame.x, frame.y),
anchorPoint: hasTextureAnchorPoint
? new Vector2d(originX / frame.w, originY / frame.h)
: null,
trimmed: trimmed,
trim: trim,
width: frame.w,
height: frame.h,
sourceSize: entry.sourceSize || { w: frame.w, h: frame.h },
angle: entry.rotated === true ? -ETA : 0,
};
addFrame(atlas, name, entry, data, textureAtlas);
frameDurations.push(entry.duration);
textureAtlas.addUVs(atlas, name, data.meta.size.w, data.meta.size.h);
}

const anims = {};
Expand Down
56 changes: 56 additions & 0 deletions packages/melonjs/src/video/texture/parser/frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ETA } from "../../../math/math.ts";
import { Vector2d } from "../../../math/vector2d.ts";

/**
* Add one frame of a TexturePacker or Aseprite JSON file to an atlas page.
* Both tools describe a frame the same way: a `frame` rectangle, with
* `trimmed`, `spriteSourceSize`, `sourceSize`, `rotated` and `pivot` beside it.
* @param {Object} atlas - the atlas page being built
* @param {string} name - the name the frame is stored under
* @param {Object} entry - the frame, as the file describes it
* @param {Object} data - atlas data information. See {@link loader.getJSON}
* @param {TextureAtlas} textureAtlas - the texture atlas class calling the parser
* @ignore
* @internal
*/
export function addFrame(atlas, name, entry, data, textureAtlas) {
// Source coordinates
const s = entry.frame;
const trimmed = !!entry.trimmed;

let trim;

if (trimmed) {
trim = {
x: entry.spriteSourceSize.x,
y: entry.spriteSourceSize.y,
w: entry.spriteSourceSize.w,
h: entry.spriteSourceSize.h,
};
}

let originX;
let originY;
// Pixel-based offset origin from the top-left of the source frame
const hasTextureAnchorPoint = entry.sourceSize && entry.pivot;
if (hasTextureAnchorPoint) {
originX = entry.sourceSize.w * entry.pivot.x - (trimmed ? trim.x : 0);
originY = entry.sourceSize.h * entry.pivot.y - (trimmed ? trim.y : 0);
}

atlas[name] = {
name: name, // frame name
texture: data.meta.image || "default", // the source texture
offset: new Vector2d(s.x, s.y),
anchorPoint: hasTextureAnchorPoint
? new Vector2d(originX / s.w, originY / s.h)
: null,
trimmed: trimmed,
trim: trim,
width: s.w,
height: s.h,
sourceSize: entry.sourceSize || { w: s.w, h: s.h },
angle: entry.rotated === true ? -ETA : 0,
};
textureAtlas.addUVs(atlas, name, data.meta.size.w, data.meta.size.h);
}
Loading
Loading