Skip to content

Commit

Permalink
Improve rendering even more
Browse files Browse the repository at this point in the history
  • Loading branch information
blaumeise20 committed May 12, 2023
1 parent 7c2ec2d commit a3abb2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
23 changes: 13 additions & 10 deletions src/core/textures/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ export class Texture {
return this.url;
}

static async fromBlob(blob: Blob, mipmapLevels = 3) {
static async fromBlob(blob: Blob, doMipmap = true) {
const url = URL.createObjectURL(blob);

const original = await createImageBitmap(blob);
const mipmaps = [original];
const bitmaps = [original];
let width = original.width;
let height = original.height;
for (let i = 0; i < mipmapLevels; i++) {
width = Math.max(1, width >> 1);
height = Math.max(1, height >> 1);
resizeCanvas.width = width;
resizeCanvas.height = height;
resizeContext.drawImage(original, 0, 0, width, height);
mipmaps.push(await createImageBitmap(resizeCanvas));
if (doMipmap) {
// max 8 textures (1 original + 7 downscaled)
for (let i = 0; i < 7 && width > 1 && height > 1; i++) {
width = Math.max(1, width >> 1);
height = Math.max(1, height >> 1);
resizeCanvas.width = width;
resizeCanvas.height = height;
resizeContext.drawImage(original, 0, 0, width, height);
bitmaps.push(await createImageBitmap(resizeCanvas));
}
}

return new Texture(url, mipmaps);
return new Texture(url, bitmaps);
}
}
27 changes: 10 additions & 17 deletions src/ui/editor/render.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CellGrid } from "@core/grid/cellGrid";
import { Direction } from "@core/cells/direction";
import type { Position } from "@core/coord/positions";
import type { Size } from "@core/coord/size";
import { Tile } from "@core/tiles";
Expand Down Expand Up @@ -91,28 +90,22 @@ export function renderGrid(
if (pasteboard && pasteboardPos) {
const pasteboardCell = pasteboard.cells.getXY(x - pasteboardPos.x, y - pasteboardPos.y);
if (pasteboardCell) {
const cellX = hWidth + (x - cx) * cellSize;
const cellY = hHeight - (y - cy + 1) * cellSize;
const centerX = cellX + halfSize;
const centerY = cellY + halfSize;
ctx.restore();
ctx.save();
if (pasteboardCell.direction != Direction.Right) {
ctx.translate(centerX, centerY);
ctx.rotate(pasteboardCell.direction * halfPi);
ctx.translate(-centerX, -centerY);
}
const cellX = hWidth + (x - cx) * cellSize + halfSize;
const cellY = hHeight - (y - cy + 1) * cellSize + halfSize;
const angle = pasteboardCell.direction * halfPi % R;
ctx.rotate((angle - prevAngle + R) % R);
prevAngle = angle;
const newX = cellX * Math.cos(R - angle) - cellY * Math.sin(R - angle) - halfSize;
const newY = cellX * Math.sin(R - angle) + cellY * Math.cos(R - angle) - halfSize;
ctx.globalAlpha = 0.5;
ctx.drawImage(
tex.cells[pasteboardCell.type.getTex(pasteboardCell)].bitmap(cellSize),
cellX,
cellY,
newX,
newY,
cellSize,
cellSize,
);
ctx.restore();
ctx.save();
prevAngle = 0;
ctx.globalAlpha = 1;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/texturePacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class Textures {
[new Uint8Array(imageContent[0]).buffer],
{ type: "image/png" }
);
ui[k] = await Texture.fromBlob(blob);
ui[k] = await Texture.fromBlob(blob, false);
} catch {}
}

Expand Down

0 comments on commit a3abb2a

Please sign in to comment.