|
| 1 | +import { ItemRenderer, ItemStack, NbtString } from 'deepslate' |
| 2 | +import { Identifier, ItemModel } from 'deepslate/render' |
| 3 | +import { useVersion } from '../../contexts/index.js' |
| 4 | +import { useAsync } from '../../hooks/useAsync.js' |
| 5 | +import { AsyncCancel } from '../../hooks/useAsyncFn.js' |
| 6 | +import { getResources, ResourceWrapper } from '../../services/Resources.js' |
| 7 | +import { isObject, safeJsonParse } from '../../Utils.js' |
| 8 | +import { ErrorPanel } from '../ErrorPanel.jsx' |
| 9 | +import type { PreviewProps } from './index.js' |
| 10 | + |
| 11 | +const PREVIEW_ID = Identifier.parse('misode:preview') |
| 12 | +const RENDER_SIZE = 512 |
| 13 | + |
| 14 | +export const ItemModelPreview = ({ docAndNode, shown }: PreviewProps) => { |
| 15 | + const { version } = useVersion() |
| 16 | + |
| 17 | + const text = docAndNode.doc.getText() |
| 18 | + |
| 19 | + const { value: render, error } = useAsync(async () => { |
| 20 | + if (!shown) return AsyncCancel |
| 21 | + const resources = await getResources(version, new Map()) |
| 22 | + const data = safeJsonParse(text) ?? {} |
| 23 | + if (!isObject(data) || !isObject(data.model)) { |
| 24 | + return undefined |
| 25 | + } |
| 26 | + const itemModel = ItemModel.fromJson(data.model) |
| 27 | + const wrapper = new ResourceWrapper(resources, { |
| 28 | + getItemModel(id) { |
| 29 | + if (id.equals(PREVIEW_ID)) return itemModel |
| 30 | + return null |
| 31 | + }, |
| 32 | + }) |
| 33 | + const canvas = document.createElement('canvas') |
| 34 | + canvas.width = RENDER_SIZE |
| 35 | + canvas.height = RENDER_SIZE |
| 36 | + const gl = canvas.getContext('webgl2', { preserveDrawingBuffer: true }) |
| 37 | + if (!gl) { |
| 38 | + throw new Error('Cannot get WebGL2 context') |
| 39 | + } |
| 40 | + const item = new ItemStack(PREVIEW_ID, 1, new Map(Object.entries({ |
| 41 | + 'minecraft:item_model': new NbtString(PREVIEW_ID.toString()), |
| 42 | + }))) |
| 43 | + const renderer = new ItemRenderer(gl, item, wrapper, { display_context: 'gui' }) |
| 44 | + renderer.drawItem() |
| 45 | + const url = canvas.toDataURL() |
| 46 | + console.log('DRAW', url) |
| 47 | + return url |
| 48 | + }, [shown, version, text]) |
| 49 | + |
| 50 | + if (error) { |
| 51 | + return <ErrorPanel error={error} prefix="Failed to initialize preview: " /> |
| 52 | + } |
| 53 | + |
| 54 | + return <> |
| 55 | + <div class="preview-overlay"> |
| 56 | + <img src="/images/single_item.png" alt="Container background" class="pixelated" draggable={false} /> |
| 57 | + {render && <div class="flex items-center justify-center" style={slotStyle()}> |
| 58 | + <img src={render} class="w-[88.888%]" /> |
| 59 | + </div>} |
| 60 | + </div> |
| 61 | + </> |
| 62 | +} |
| 63 | + |
| 64 | +const GUI_WIDTH = 176 |
| 65 | +const GUI_HEIGHT = 81 |
| 66 | +const SLOT_SIZE = 72 |
| 67 | + |
| 68 | +function slotStyle() { |
| 69 | + const x = 52 |
| 70 | + const y = 4 |
| 71 | + return { |
| 72 | + left: `${x*100/GUI_WIDTH}%`, |
| 73 | + top: `${y*100/GUI_HEIGHT}%`, |
| 74 | + width: `${SLOT_SIZE*100/GUI_WIDTH}%`, |
| 75 | + height: `${SLOT_SIZE*100/GUI_HEIGHT}%`, |
| 76 | + } |
| 77 | +} |
0 commit comments