Skip to content

Commit

Permalink
feat: Added 'isAnimated' and 'getAnimationPeriod'
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeUtan committed Apr 22, 2021
1 parent e9f8d37 commit ce94189
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions src/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit ce94189

Please sign in to comment.