Animated textures !
This commit is contained in:
+36
-5
@@ -2,6 +2,8 @@
|
||||
// from the textures in the server's .bmod files
|
||||
import { TEXTURE_SIZE } from "$/common/constants.ts";
|
||||
import type { SpriteRegion } from "$/common/constants.ts";
|
||||
import { type Animation, texture_animation, type TextureJson } from "$/common/texture_animation.ts";
|
||||
import { AnimatedTexture } from "./texture_animations.ts";
|
||||
|
||||
export interface AtlasLayout {
|
||||
// in pixels, a power of two
|
||||
@@ -51,8 +53,10 @@ function pack_shelves(entries: [string, TextureSize][], size: number) {
|
||||
return y + shelf <= size ? regions : undefined;
|
||||
}
|
||||
|
||||
// textures are png bytes by id. ones that don't decode show as missing
|
||||
export async function build_atlas(textures: Map<string, Uint8Array>) {
|
||||
// textures are png bytes by id, texture_json the .json of the ones that have one. ones that don't decode show as
|
||||
// missing. an animated texture takes one frame's room, its first frame, and plays from animated (see
|
||||
// texture_animations.ts)
|
||||
export async function build_atlas(textures: Map<string, Uint8Array>, texture_json = new Map<string, TextureJson>()) {
|
||||
const images = new Map<string, ImageBitmap>();
|
||||
await Promise.all([...textures].map(async ([id, png]) => {
|
||||
try {
|
||||
@@ -64,7 +68,18 @@ export async function build_atlas(textures: Map<string, Uint8Array>) {
|
||||
images.get("engine:missing")?.close();
|
||||
images.delete("engine:missing");
|
||||
|
||||
const { size, regions } = atlas_layout(new Map([...images].map(([id, image]) => [id, image])));
|
||||
const animations = new Map<string, Animation>();
|
||||
for (const [id, image] of images) {
|
||||
const json = texture_json.get(id);
|
||||
const animation = json && texture_animation(json, image);
|
||||
if (animation) animations.set(id, animation);
|
||||
}
|
||||
const sizes = new Map([...images].map(([id, image]) => {
|
||||
const animation = animations.get(id);
|
||||
return [id, animation ? { width: animation.frame_width, height: animation.frame_height } : image];
|
||||
}));
|
||||
|
||||
const { size, regions } = atlas_layout(sizes);
|
||||
const canvas = new OffscreenCanvas(size, size);
|
||||
const ctx = canvas.getContext("2d")!;
|
||||
|
||||
@@ -78,8 +93,17 @@ export async function build_atlas(textures: Map<string, Uint8Array>) {
|
||||
ctx.fillRect(missing.x + half, missing.y, half, half);
|
||||
ctx.fillRect(missing.x, missing.y + half, half, half);
|
||||
|
||||
const animated: AnimatedTexture[] = [];
|
||||
for (const [id, image] of images) {
|
||||
ctx.drawImage(image, regions[id].x, regions[id].y);
|
||||
const animation = animations.get(id);
|
||||
const { x, y, width, height } = regions[id];
|
||||
if (animation) {
|
||||
const first = animation.frames[0];
|
||||
ctx.drawImage(image, first.x, first.y, width, height, x, y, width, height);
|
||||
animated.push(new AnimatedTexture(regions[id], animation, image_pixels(image), image.width));
|
||||
} else {
|
||||
ctx.drawImage(image, x, y);
|
||||
}
|
||||
image.close();
|
||||
}
|
||||
for (const id of textures.keys()) {
|
||||
@@ -100,7 +124,14 @@ export async function build_atlas(textures: Map<string, Uint8Array>) {
|
||||
opaque.set(id, { width: region.width, height: region.height, mask });
|
||||
}
|
||||
|
||||
return { image: canvas.transferToImageBitmap(), regions };
|
||||
return { image: canvas.transferToImageBitmap(), regions, animated };
|
||||
}
|
||||
|
||||
// every frame of an animated texture stays in memory, to copy into the atlas when it's that frame's turn
|
||||
function image_pixels(image: ImageBitmap) {
|
||||
const ctx = new OffscreenCanvas(image.width, image.height).getContext("2d")!;
|
||||
ctx.drawImage(image, 0, 0);
|
||||
return ctx.getImageData(0, 0, image.width, image.height).data;
|
||||
}
|
||||
|
||||
// a texture's pixels, 1 (can be seen) or 0, row by row from the top
|
||||
|
||||
Reference in New Issue
Block a user