Skip to content

Commit

Permalink
add: workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Naibuu committed Apr 14, 2024
1 parent 90a0919 commit c566ed2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 71 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build & Publish

on: [push, pull_request]

permissions:
contents: write
pull-requests: write
packages: write

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v3
with:
node-version: 8
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://npm.pkg.github.com/
cache: 'pnpm'
- run: pnpm install
- run: pnpm test
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"rollup": "^2.79.1",
"typescript": "^4.9.5"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/@skinbook"
},
"files": [
"build"
],
Expand Down
93 changes: 22 additions & 71 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import type { TextureCanvas, TextureSource, ModelType } from "./types.js";

type CanvasContext =
| CanvasRenderingContext2D
| OffscreenCanvasRenderingContext2D;
type CanvasContext = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;

function hasTransparency(
context: CanvasImageData,
x0: number,
y0: number,
w: number,
h: number
): boolean {
function hasTransparency(context: CanvasImageData, x0: number, y0: number, w: number, h: number): boolean {
const imgData = context.getImageData(x0, y0, w, h);
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
Expand All @@ -27,11 +19,7 @@ function computeSkinScale(width: number): number {
return width / 64.0;
}

function fixOpaqueSkin(
context: CanvasImageData & CanvasRect,
width: number,
format1_8: boolean
): void {
function fixOpaqueSkin(context: CanvasImageData & CanvasRect, width: number, format1_8: boolean): void {
// see https://github.com/bs-community/skinview3d/issues/15
// see https://github.com/bs-community/skinview3d/issues/93

Expand Down Expand Up @@ -93,14 +81,7 @@ function convertSkinTo1_8(context: CanvasContext, width: number): void {
context.scale(-1, 1);

const scale = computeSkinScale(width);
const copySkin = (
sX: number,
sY: number,
w: number,
h: number,
dX: number,
dY: number
): void =>
const copySkin = (sX: number, sY: number, w: number, h: number, dX: number, dY: number): void =>
context.drawImage(
context.canvas,
sX * scale,
Expand Down Expand Up @@ -129,10 +110,7 @@ function convertSkinTo1_8(context: CanvasContext, width: number): void {
context.restore();
}

export function loadSkinToCanvas(
canvas: TextureCanvas,
image: TextureSource
): void {
export function loadSkinToCanvas(canvas: TextureCanvas, image: TextureSource): void {
let isOldFormat = false;
if (image.width !== image.height) {
if (image.width === 2 * image.height) {
Expand Down Expand Up @@ -172,16 +150,14 @@ function computeCapeScale(image: TextureSource): number {
} else if (image.width * 11 === image.height * 23) {
// 46x22
return image.width / 46;
} else if (image.height % (image.width / 2) == 0) {
return image.width / 64;
} else {
throw new Error(`Bad cape size: ${image.width}x${image.height}`);
}
}

export function loadCapeToCanvas(
canvas: TextureCanvas,
image: TextureSource,
frame?: number
): void {
export function loadCapeToCanvas(canvas: TextureCanvas, image: TextureSource, frame?: number): void {
const scale = computeCapeScale(image);
canvas.width = 64 * scale;
canvas.height = 32 * scale;
Expand All @@ -201,26 +177,10 @@ export function loadCapeToCanvas(
willReadFrequently: true,
}) as CanvasContext;
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(
image,
0,
frameOffset,
frameWidth,
frameHeight,
0,
0,
frameWidth,
frameHeight
);
context.drawImage(image, 0, frameOffset, frameWidth, frameHeight, 0, 0, frameWidth, frameHeight);
}

function isAreaBlack(
context: CanvasImageData,
x0: number,
y0: number,
w: number,
h: number
): boolean {
function isAreaBlack(context: CanvasImageData, x0: number, y0: number, w: number, h: number): boolean {
const imgData = context.getImageData(x0, y0, w, h);
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
Expand All @@ -240,13 +200,7 @@ function isAreaBlack(
return true;
}

function isAreaWhite(
context: CanvasImageData,
x0: number,
y0: number,
w: number,
h: number
): boolean {
function isAreaWhite(context: CanvasImageData, x0: number, y0: number, w: number, h: number): boolean {
const imgData = context.getImageData(x0, y0, w, h);
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
Expand Down Expand Up @@ -311,12 +265,7 @@ export function inferModelType(canvas: TextureCanvas): ModelType {
const context = canvas.getContext("2d", {
willReadFrequently: true,
}) as CanvasContext;
const checkTransparency = (
x: number,
y: number,
w: number,
h: number
): boolean =>
const checkTransparency = (x: number, y: number, w: number, h: number): boolean =>
hasTransparency(context, x * scale, y * scale, w * scale, h * scale);
const checkBlack = (x: number, y: number, w: number, h: number): boolean =>
isAreaBlack(context, x * scale, y * scale, w * scale, h * scale);
Expand Down Expand Up @@ -346,10 +295,7 @@ function computeEarsScale(image: TextureSource): number {
}
}

export function loadEarsToCanvas(
canvas: TextureCanvas,
image: TextureSource
): void {
export function loadEarsToCanvas(canvas: TextureCanvas, image: TextureSource): void {
const scale = computeEarsScale(image);
canvas.width = 14 * scale;
canvas.height = 7 * scale;
Expand All @@ -361,10 +307,7 @@ export function loadEarsToCanvas(
context.drawImage(image, 0, 0, image.width, image.height);
}

export function loadEarsToCanvasFromSkin(
canvas: TextureCanvas,
image: TextureSource
): void {
export function loadEarsToCanvasFromSkin(canvas: TextureCanvas, image: TextureSource): void {
if (image.width !== image.height && image.width !== 2 * image.height) {
throw new Error(`Bad skin size: ${image.width}x${image.height}`);
}
Expand All @@ -380,3 +323,11 @@ export function loadEarsToCanvasFromSkin(
context.clearRect(0, 0, w, h);
context.drawImage(image, 24 * scale, 0, w, h, 0, 0, w, h);
}

// export async function loadMolangToCanvas(canvas: HTMLCanvasElement, model: any, texture: string) {
// const viewer = new StandaloneModelViewer(canvas, model, texture, {
// antialias: true,
// });

// await viewer.loadedModel;
// }

0 comments on commit c566ed2

Please sign in to comment.