From ce9418903de54a698e1e18c9c2937f718fb75783 Mon Sep 17 00:00:00 2001 From: Oran9eUtan Date: Thu, 22 Apr 2021 03:56:26 +0200 Subject: [PATCH] feat: Added 'isAnimated' and 'getAnimationPeriod' --- src/mesh.ts | 12 ++++++++++++ src/texture.ts | 8 ++++++++ src/utils.ts | 15 +++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/mesh.ts b/src/mesh.ts index b2454cf..41cda31 100644 --- a/src/mesh.ts +++ b/src/mesh.ts @@ -3,6 +3,7 @@ import { MinecraftModelGeometry } from './geometry' import { MinecraftModelMaterial } from './material' import { MinecraftModel } from './model' import { MISSING_TEXTURE, MinecraftTexture } from './texture' +import { lcm } from './utils' export class MinecraftModelMesh extends Mesh { @@ -31,9 +32,20 @@ export class MinecraftModelMesh extends Mesh { } } + public isAnimated() { + return Object.values(this.textureToMaterialMap).map(m => m.map).every(t=> (t as MinecraftTexture | undefined)?.isAnimated()) + } + public setAnimationFrame(index: number) { for(const texture of Object.values(this.textureToMaterialMap)) { (texture.map as MinecraftTexture).setAnimationFrame(index); } } + + public getAnimationPeriod() { + const x = Object.values(this.textureToMaterialMap) + .map(m => (m.map as MinecraftTexture)?.numFrames() ?? 1) + .reduce((prev, current) => lcm(prev, current), 1) + console.log(x); + } } diff --git a/src/texture.ts b/src/texture.ts index e1d5043..138bfb5 100644 --- a/src/texture.ts +++ b/src/texture.ts @@ -25,10 +25,18 @@ export class MinecraftTexture extends THREE.Texture { this.needsUpdate = true } + isAnimated() { + return this.numTiles > 1 + } + setAnimationFrame(index: number) { this.tileIdx = index % this.numTiles this.offset.y = this.tileIdx / this.numTiles } + + numFrames() { + return this.numTiles + } } export const MISSING_TEXTURE = new MinecraftTexture() diff --git a/src/utils.ts b/src/utils.ts index 0e725d0..6ff2422 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,18 @@ export function isObject(o: any): o is Object { return typeof o === 'object' && Array.isArray(o) === false && o !== null } + +export function gcd(a: number, b: number): number { + while(a != b) { + if(a > b) { + a -= b; + } else { + b -= a; + } + } + return b; +} + +export function lcm(a: number, b: number): number { + return (a*b) / gcd(a, b); +} \ No newline at end of file