Skip to content

Commit

Permalink
feat: wings
Browse files Browse the repository at this point in the history
  • Loading branch information
Naibuu committed Apr 14, 2024
1 parent ec4536b commit a39b15e
Showing 1 changed file with 92 additions and 18 deletions.
110 changes: 92 additions & 18 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
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 @@ -19,7 +27,11 @@ 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 @@ -81,7 +93,14 @@ 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 @@ -110,7 +129,10 @@ 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 @@ -157,7 +179,11 @@ function computeCapeScale(image: TextureSource): number {
}
}

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 @@ -177,10 +203,26 @@ export function loadCapeToCanvas(canvas: TextureCanvas, image: TextureSource, fr
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 @@ -200,7 +242,13 @@ function isAreaBlack(context: CanvasImageData, x0: number, y0: number, w: number
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 @@ -265,7 +313,12 @@ 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 @@ -295,7 +348,10 @@ 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 @@ -307,7 +363,10 @@ export function loadEarsToCanvas(canvas: TextureCanvas, image: TextureSource): v
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 @@ -324,10 +383,25 @@ export function loadEarsToCanvasFromSkin(canvas: TextureCanvas, image: TextureSo
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,
// });
function computeWingsScale(image: TextureSource): number {
if (image.width === image.height) {
return image.height;
} else {
throw new Error(`Bad wings size: ${image.width}x${image.height}`);
}
}

export function loadWingsToCanvas(
canvas: TextureCanvas,
image: TextureSource
): void {
const scale = computeWingsScale(image);
canvas.width = 32 * scale;
canvas.height = 32 * scale;

// await viewer.loadedModel;
// }
const context = canvas.getContext("2d", {
willReadFrequently: true,
}) as CanvasContext;
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(image, 0, 11, image.width, image.height);
}

0 comments on commit a39b15e

Please sign in to comment.