Skip to content

Commit

Permalink
Format (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacksore authored Sep 16, 2023
1 parent b462f99 commit a930d0c
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 101 deletions.
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"printWidth": 120,
"singleQuote": false,
"quoteProps": "as-needed",
"arrowParens": "avoid",
"endOfLine": "auto"
}
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build:modules": "tsc -p .",
"build:bundles": "rollup -c --configPlugin=swc3",
"build": "npm run build:modules && npm run build:bundles",
"format": "prettier src --write",
"test:lint": "eslint --ext .ts src",
"test": "npm run test:lint",
"dev:watch:modules": "tsc -w --preserveWatchOutput --declaration --sourceMap --outDir libs -p .",
Expand Down Expand Up @@ -54,6 +55,7 @@
"eslint-plugin-tsdoc": "^0.2.17",
"local-web-server": "^5.2.1",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"rimraf": "^3.0.2",
"rollup": "^3.20.7",
"rollup-plugin-swc3": "^0.8.1",
Expand Down
20 changes: 7 additions & 13 deletions src/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { PlayerObject } from "./model.js";
* particular animations.
*/
export abstract class PlayerAnimation {

/**
* The speed of the animation.
*
Expand Down Expand Up @@ -64,7 +63,6 @@ export abstract class PlayerAnimation {
* ```
*/
export class FunctionAnimation extends PlayerAnimation {

fn: (player: PlayerObject, progress: number, delta: number) => void;

constructor(fn: (player: PlayerObject, progress: number, delta: number) => void) {
Expand All @@ -78,7 +76,6 @@ export class FunctionAnimation extends PlayerAnimation {
}

export class IdleAnimation extends PlayerAnimation {

protected animate(player: PlayerObject): void {
// Multiply by animation's natural speed
const t = this.progress * 2;
Expand All @@ -95,7 +92,6 @@ export class IdleAnimation extends PlayerAnimation {
}

export class WalkingAnimation extends PlayerAnimation {

/**
* Whether to shake head when walking.
*
Expand Down Expand Up @@ -134,7 +130,6 @@ export class WalkingAnimation extends PlayerAnimation {
}

export class RunningAnimation extends PlayerAnimation {

protected animate(player: PlayerObject): void {
// Multiply by animation's natural speed
const t = this.progress * 15 + Math.PI * 0.5;
Expand Down Expand Up @@ -173,26 +168,25 @@ function clamp(num: number, min: number, max: number): number {
}

export class FlyingAnimation extends PlayerAnimation {

protected animate(player: PlayerObject): void {
// Body rotation finishes in 0.5s
// Elytra expansion finishes in 3.3s

const t = this.progress > 0 ? this.progress * 20 : 0;
const startProgress = clamp((t * t) / 100, 0, 1);

player.rotation.x = startProgress * Math.PI / 2;
player.skin.head.rotation.x = startProgress > .5 ? Math.PI / 4 - player.rotation.x : 0;
player.rotation.x = (startProgress * Math.PI) / 2;
player.skin.head.rotation.x = startProgress > 0.5 ? Math.PI / 4 - player.rotation.x : 0;

const basicArmRotationZ = Math.PI * .25 * startProgress;
const basicArmRotationZ = Math.PI * 0.25 * startProgress;
player.skin.leftArm.rotation.z = basicArmRotationZ;
player.skin.rightArm.rotation.z = -basicArmRotationZ;

const elytraRotationX = .34906584;
const elytraRotationX = 0.34906584;
const elytraRotationZ = Math.PI / 2;
const interpolation = Math.pow(.9, t);
player.elytra.leftWing.rotation.x = elytraRotationX + interpolation * (.2617994 - elytraRotationX);
player.elytra.leftWing.rotation.z = elytraRotationZ + interpolation * (.2617994 - elytraRotationZ);
const interpolation = Math.pow(0.9, t);
player.elytra.leftWing.rotation.x = elytraRotationX + interpolation * (0.2617994 - elytraRotationX);
player.elytra.leftWing.rotation.z = elytraRotationZ + interpolation * (0.2617994 - elytraRotationZ);
player.elytra.updateRightWing();
}
}
93 changes: 63 additions & 30 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import type { ModelType } from "skinview-utils";
import { BoxGeometry, BufferAttribute, DoubleSide, FrontSide, Group, Mesh, MeshStandardMaterial, Object3D, Texture, Vector2 } from "three";

function setUVs(box: BoxGeometry, u: number, v: number, width: number, height: number, depth: number, textureWidth: number, textureHeight: number): void {
import {
BoxGeometry,
BufferAttribute,
DoubleSide,
FrontSide,
Group,
Mesh,
MeshStandardMaterial,
Object3D,
Texture,
Vector2,
} from "three";

function setUVs(
box: BoxGeometry,
u: number,
v: number,
width: number,
height: number,
depth: number,
textureWidth: number,
textureHeight: number
): void {
const toFaceVertices = (x1: number, y1: number, x2: number, y2: number) => [
new Vector2(x1 / textureWidth, 1.0 - y2 / textureHeight),
new Vector2(x2 / textureWidth, 1.0 - y2 / textureHeight),
new Vector2(x2 / textureWidth, 1.0 - y1 / textureHeight),
new Vector2(x1 / textureWidth, 1.0 - y1 / textureHeight)
new Vector2(x1 / textureWidth, 1.0 - y1 / textureHeight),
];

const top = toFaceVertices(u + depth, v, u + width + depth, v + depth);
Expand All @@ -18,12 +38,30 @@ function setUVs(box: BoxGeometry, u: number, v: number, width: number, height: n

const uvAttr = box.attributes.uv as BufferAttribute;
uvAttr.copyVector2sArray([
right[3], right[2], right[0], right[1],
left[3], left[2], left[0], left[1],
top[3], top[2], top[0], top[1],
bottom[0], bottom[1], bottom[3], bottom[2],
front[3], front[2], front[0], front[1],
back[3], back[2], back[0], back[1]
right[3],
right[2],
right[0],
right[1],
left[3],
left[2],
left[0],
left[1],
top[3],
top[2],
top[0],
top[1],
bottom[0],
bottom[1],
bottom[3],
bottom[2],
front[3],
front[2],
front[0],
front[1],
back[3],
back[2],
back[0],
back[1],
]);
uvAttr.needsUpdate = true;
}
Expand Down Expand Up @@ -51,7 +89,6 @@ export class BodyPart extends Group {
}

export class SkinObject extends Group {

// body parts
readonly head: BodyPart;
readonly body: BodyPart;
Expand All @@ -73,12 +110,12 @@ export class SkinObject extends Group {
super();

this.layer1Material = new MeshStandardMaterial({
side: FrontSide
side: FrontSide,
});
this.layer2Material = new MeshStandardMaterial({
side: DoubleSide,
transparent: true,
alphaTest: 1e-5
alphaTest: 1e-5,
});

this.layer1MaterialBiased = this.layer1Material.clone();
Expand Down Expand Up @@ -144,7 +181,7 @@ export class SkinObject extends Group {
const rightArmPivot = new Group();
rightArmPivot.add(rightArmMesh, rightArm2Mesh);
this.modelListeners.push(() => {
rightArmPivot.position.x = this.slim ? -.5 : -1;
rightArmPivot.position.x = this.slim ? -0.5 : -1;
});
rightArmPivot.position.y = -4;

Expand Down Expand Up @@ -206,7 +243,7 @@ export class SkinObject extends Group {
this.rightLeg.add(rightLegPivot);
this.rightLeg.position.x = -1.9;
this.rightLeg.position.y = -12;
this.rightLeg.position.z = -.1;
this.rightLeg.position.z = -0.1;
this.add(this.rightLeg);

// Left Leg
Expand All @@ -227,7 +264,7 @@ export class SkinObject extends Group {
this.leftLeg.add(leftLegPivot);
this.leftLeg.position.x = 1.9;
this.leftLeg.position.y = -12;
this.leftLeg.position.z = -.1;
this.leftLeg.position.z = -0.1;
this.add(this.leftLeg);

this.modelType = "default";
Expand Down Expand Up @@ -267,11 +304,11 @@ export class SkinObject extends Group {
}

setInnerLayerVisible(value: boolean): void {
this.getBodyParts().forEach(part => part.innerLayer.visible = value);
this.getBodyParts().forEach(part => (part.innerLayer.visible = value));
}

setOuterLayerVisible(value: boolean): void {
this.getBodyParts().forEach(part => part.outerLayer.visible = value);
this.getBodyParts().forEach(part => (part.outerLayer.visible = value));
}

resetJoints(): void {
Expand All @@ -284,7 +321,6 @@ export class SkinObject extends Group {
}

export class CapeObject extends Group {

readonly cape: Mesh;

private material: MeshStandardMaterial;
Expand All @@ -295,7 +331,7 @@ export class CapeObject extends Group {
this.material = new MeshStandardMaterial({
side: DoubleSide,
transparent: true,
alphaTest: 1e-5
alphaTest: 1e-5,
});

// +z (front) - inside of cape
Expand All @@ -304,7 +340,7 @@ export class CapeObject extends Group {
setCapeUVs(capeBox, 0, 0, 10, 16, 1);
this.cape = new Mesh(capeBox, this.material);
this.cape.position.y = -8;
this.cape.position.z = .5;
this.cape.position.z = 0.5;
this.add(this.cape);
}

Expand All @@ -319,7 +355,6 @@ export class CapeObject extends Group {
}

export class ElytraObject extends Group {

readonly leftWing: Group;
readonly rightWing: Group;

Expand All @@ -331,7 +366,7 @@ export class ElytraObject extends Group {
this.material = new MeshStandardMaterial({
side: DoubleSide,
transparent: true,
alphaTest: 1e-5
alphaTest: 1e-5,
});

const leftWingBox = new BoxGeometry(12, 22, 4);
Expand All @@ -356,13 +391,13 @@ export class ElytraObject extends Group {
this.add(this.rightWing);

this.leftWing.position.x = 5;
this.leftWing.rotation.x = .2617994;
this.leftWing.rotation.x = 0.2617994;
this.resetJoints();
}

resetJoints(): void {
this.leftWing.rotation.y = .01; // to avoid z-fighting
this.leftWing.rotation.z = .2617994;
this.leftWing.rotation.y = 0.01; // to avoid z-fighting
this.leftWing.rotation.z = 0.2617994;
this.updateRightWing();
}

Expand All @@ -389,7 +424,6 @@ export class ElytraObject extends Group {
}

export class EarsObject extends Group {

readonly rightEar: Mesh;
readonly leftEar: Mesh;

Expand All @@ -399,7 +433,7 @@ export class EarsObject extends Group {
super();

this.material = new MeshStandardMaterial({
side: FrontSide
side: FrontSide,
});
const earBox = new BoxGeometry(8, 8, 4 / 3);
setUVs(earBox, 0, 0, 6, 6, 1, 14, 7);
Expand Down Expand Up @@ -427,10 +461,9 @@ export class EarsObject extends Group {

export type BackEquipment = "cape" | "elytra";

const CapeDefaultAngle = 10.8 * Math.PI / 180;
const CapeDefaultAngle = (10.8 * Math.PI) / 180;

export class PlayerObject extends Group {

readonly skin: SkinObject;
readonly cape: CapeObject;
readonly elytra: ElytraObject;
Expand Down
Loading

0 comments on commit a930d0c

Please sign in to comment.