refactor: convert missing texture into a data url
This commit is contained in:
parent
2772577a7b
commit
bea25fc115
4 changed files with 11 additions and 14 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 82 B |
|
|
@ -4,6 +4,8 @@ import * as PIXI from "pixi.js";
|
||||||
import { useApp } from "@pixi/react";
|
import { useApp } from "@pixi/react";
|
||||||
import { CompositeTilemap, settings } from "@pixi/tilemap";
|
import { CompositeTilemap, settings } from "@pixi/tilemap";
|
||||||
|
|
||||||
|
import constants from "@/constants";
|
||||||
|
|
||||||
import _blockData from "@/data/blocks/programmer-art/data.json";
|
import _blockData from "@/data/blocks/programmer-art/data.json";
|
||||||
const blockData: BlockData = _blockData;
|
const blockData: BlockData = _blockData;
|
||||||
|
|
||||||
|
|
@ -24,8 +26,6 @@ settings.use32bitIndex = true;
|
||||||
|
|
||||||
function Blocks({ blocks, setBlocks, textures, solidTextures, image, imageDimensions, coords, scale, setLoading }: Props) {
|
function Blocks({ blocks, setBlocks, textures, solidTextures, image, imageDimensions, coords, scale, setLoading }: Props) {
|
||||||
const app = useApp();
|
const app = useApp();
|
||||||
const [missingTexture, setMissingTexture] = useState<PIXI.Texture>();
|
|
||||||
|
|
||||||
const tilemapRef = useRef<CompositeTilemap>();
|
const tilemapRef = useRef<CompositeTilemap>();
|
||||||
|
|
||||||
const tileBlocks = () => {
|
const tileBlocks = () => {
|
||||||
|
|
@ -36,24 +36,16 @@ function Blocks({ blocks, setBlocks, textures, solidTextures, image, imageDimens
|
||||||
// Tile solid colors at smaller scales
|
// Tile solid colors at smaller scales
|
||||||
if (scale >= 0.5) {
|
if (scale >= 0.5) {
|
||||||
blocks.forEach((block) => {
|
blocks.forEach((block) => {
|
||||||
tilemap.tile(textures[`${block.name}.png`] ?? missingTexture, block.x * 16, block.y * 16);
|
tilemap.tile(textures[`${block.name}.png`] ?? constants.MISSING_TEXTURE, block.x * 16, block.y * 16);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
blocks.forEach((block) => {
|
blocks.forEach((block) => {
|
||||||
tilemap.tile(solidTextures[`${block.name}`] ?? missingTexture, block.x * 16, block.y * 16);
|
tilemap.tile(solidTextures[`${block.name}`] ?? constants.MISSING_TEXTURE, block.x * 16, block.y * 16);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Load the missing texture
|
|
||||||
const loadMissingTexture = async () => {
|
|
||||||
const mTexture = await PIXI.Texture.from("/blocks/missing.png");
|
|
||||||
setMissingTexture(mTexture);
|
|
||||||
};
|
|
||||||
loadMissingTexture();
|
|
||||||
|
|
||||||
// Create tilemap
|
|
||||||
const tilemap = new CompositeTilemap();
|
const tilemap = new CompositeTilemap();
|
||||||
tilemapRef.current = tilemap;
|
tilemapRef.current = tilemap;
|
||||||
tilemap.cullable = true;
|
tilemap.cullable = true;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
import { useContext } from "react";
|
import { useContext } from "react";
|
||||||
|
|
||||||
|
import constants from "@/constants";
|
||||||
|
|
||||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
|
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
|
||||||
|
|
||||||
import { TexturesContext } from "@/context/Textures";
|
import { TexturesContext } from "@/context/Textures";
|
||||||
|
|
@ -11,9 +13,8 @@ function SelectedBlock() {
|
||||||
|
|
||||||
const convertToDataUrl = (textureName: string): string => {
|
const convertToDataUrl = (textureName: string): string => {
|
||||||
// Show missing texture if fail
|
// Show missing texture if fail
|
||||||
if (!textures["stone"])
|
|
||||||
return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAGUlEQVR42mPABX4w/MCKaKJhVMPgcOuoBgDZRfgBVl5QdQAAAABJRU5ErkJggg==";
|
|
||||||
const texture = textures[textureName];
|
const texture = textures[textureName];
|
||||||
|
if (!texture) return constants.MISSING_TEXTURE;
|
||||||
|
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
const context = canvas.getContext("2d");
|
const context = canvas.getContext("2d");
|
||||||
|
|
|
||||||
4
src/constants.ts
Normal file
4
src/constants.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
export default {
|
||||||
|
MISSING_TEXTURE:
|
||||||
|
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAGUlEQVR42mPABX4w/MCKaKJhVMPgcOuoBgDZRfgBVl5QdQAAAABJRU5ErkJggg==",
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue