fix: show missing texture on error
This commit is contained in:
parent
5def8fb126
commit
c38307a127
1 changed files with 12 additions and 9 deletions
|
|
@ -1,18 +1,20 @@
|
||||||
import { useEffect } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Sprite } from "@pixi/react";
|
import { Sprite } from "@pixi/react";
|
||||||
|
|
||||||
import blocksData from "@/data/blocks/programmer-art/average_colors.json";
|
import blocksData from "@/data/blocks/programmer-art/average_colors.json";
|
||||||
import { Texture } from "pixi.js";
|
import * as PIXI from "pixi.js";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
blocks: Block[];
|
blocks: Block[];
|
||||||
setBlocks: React.Dispatch<React.SetStateAction<Block[]>>;
|
setBlocks: React.Dispatch<React.SetStateAction<Block[]>>;
|
||||||
textures: Record<string, Texture>;
|
textures: Record<string, PIXI.Texture>;
|
||||||
image: HTMLImageElement | undefined;
|
image: HTMLImageElement | undefined;
|
||||||
imageDimensions: Dimension;
|
imageDimensions: Dimension;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Blocks({ blocks, setBlocks, textures, image, imageDimensions }: Props) {
|
function Blocks({ blocks, setBlocks, textures, image, imageDimensions }: Props) {
|
||||||
|
const [missingTexture, setMissingTexture] = useState<PIXI.Texture>();
|
||||||
|
|
||||||
const findClosestBlock = (r: number, g: number, b: number, a: number) => {
|
const findClosestBlock = (r: number, g: number, b: number, a: number) => {
|
||||||
let closestBlock = "";
|
let closestBlock = "";
|
||||||
let closestDistance = Infinity;
|
let closestDistance = Infinity;
|
||||||
|
|
@ -29,6 +31,12 @@ function Blocks({ blocks, setBlocks, textures, image, imageDimensions }: Props)
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const loadMissingTexture = async () => {
|
||||||
|
const mTexture = await PIXI.Texture.from("/blocks/missing.png");
|
||||||
|
setMissingTexture(mTexture);
|
||||||
|
};
|
||||||
|
loadMissingTexture();
|
||||||
|
|
||||||
if (image) {
|
if (image) {
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
const ctx = canvas.getContext("2d");
|
const ctx = canvas.getContext("2d");
|
||||||
|
|
@ -59,17 +67,12 @@ function Blocks({ blocks, setBlocks, textures, image, imageDimensions }: Props)
|
||||||
}
|
}
|
||||||
}, [image, imageDimensions, setBlocks]);
|
}, [image, imageDimensions, setBlocks]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
console.log(blocks);
|
|
||||||
}, [blocks]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{blocks.map((block, index) => {
|
{blocks.map((block, index) => {
|
||||||
const texture = textures[block.name];
|
const texture = textures[block.name] ?? missingTexture;
|
||||||
if (!texture) {
|
if (!texture) {
|
||||||
console.warn(`Texture not found for block: ${block.name}`);
|
console.warn(`Texture not found for block: ${block.name}`);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return <Sprite key={index} texture={texture} x={block.x * 16} y={block.y * 16} />;
|
return <Sprite key={index} texture={texture} x={block.x * 16} y={block.y * 16} />;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue