// animated textures play like minecraft's: the atlas holds one frame of each, and every tick the ones whose frame // changed copy the new frame's pixels over it. models never change, only the atlas does import type { SpriteRegion } from "$/common/constants.ts"; import type { Animation } from "$/common/texture_animation.ts"; // pixels to copy into a texture's place in the atlas: frame_width × frame_height of them, starting at offset bytes // into data, rows bytes_per_row apart export interface FrameUpload { data: Uint8ClampedArray; offset: number; bytes_per_row: number; } export class AnimatedTexture { // which of animation.frames shows, and how many ticks it has shown for #frame = 0; #ticks = 0; // the frame blended into the next one, for animations that interpolate readonly #blended: Uint8ClampedArray; // pixels is the whole png, rgba row by row, image_width pixels wide constructor( readonly region: SpriteRegion, readonly animation: Animation, readonly pixels: Uint8ClampedArray, readonly image_width: number, ) { this.#blended = new Uint8ClampedArray(animation.frame_width * animation.frame_height * 4); } // one tick on, and what to copy into the atlas if the texture looks different now tick(): FrameUpload | undefined { const frames = this.animation.frames; const current = frames[this.#frame]; this.#ticks += 1; if (this.#ticks >= current.time) { this.#ticks = 0; this.#frame = (this.#frame + 1) % frames.length; const next = frames[this.#frame]; if (next.x !== current.x || next.y !== current.y) return this.#frame_upload(next); } else if (this.animation.interpolate) { const next = frames[(this.#frame + 1) % frames.length]; if (next.x !== current.x || next.y !== current.y) { return this.#blend(current, next, this.#ticks / current.time); } } return undefined; } #frame_upload(frame: { x: number; y: number }): FrameUpload { return { data: this.pixels, offset: (frame.y * this.image_width + frame.x) * 4, bytes_per_row: this.image_width * 4, }; } // the colour moves from one frame to the next, the see-through-ness stays the current frame's, like minecraft #blend(from: { x: number; y: number }, to: { x: number; y: number }, amount: number): FrameUpload { const { frame_width, frame_height } = this.animation; for (let y = 0; y < frame_height; y++) { for (let x = 0; x < frame_width; x++) { const a = ((from.y + y) * this.image_width + from.x + x) * 4; const b = ((to.y + y) * this.image_width + to.x + x) * 4; const out = (y * frame_width + x) * 4; for (let c = 0; c < 3; c++) { this.#blended[out + c] = this.pixels[a + c] + (this.pixels[b + c] - this.pixels[a + c]) * amount; } this.#blended[out + 3] = this.pixels[a + 3]; } } return { data: this.#blended, offset: 0, bytes_per_row: frame_width * 4 }; } } let atlas: GPUTexture | undefined; let queue: GPUQueue | undefined; let animated: AnimatedTexture[] = []; // the atlas just made and its animated textures, replacing the last server's export function set_texture_animations(texture: GPUTexture, gpu_queue: GPUQueue, textures: AnimatedTexture[]) { atlas = texture; queue = gpu_queue; animated = textures; } // once a tick, like minecraft's TextureAtlas.cycleAnimationFrames export function tick_texture_animations() { if (!atlas || !queue) return; for (const texture of animated) { const upload = texture.tick(); if (!upload) continue; const { region, animation } = texture; queue.writeTexture( { texture: atlas, origin: [region.x, region.y] }, upload.data, { offset: upload.offset, bytesPerRow: upload.bytes_per_row }, [animation.frame_width, animation.frame_height], ); } }