|
| 1 | +/* eslint-disable no-await-in-loop */ |
| 2 | +import { useSnapshot } from 'valtio' |
| 3 | +import { useEffect, useState } from 'react' |
| 4 | +import { getLoadedImage } from 'mc-assets/dist/utils' |
| 5 | +import { createCanvas } from 'renderer/viewer/lib/utils' |
| 6 | + |
| 7 | +const TEXTURE_UPDATE_INTERVAL = 100 // 5 times per second |
| 8 | + |
| 9 | +export default () => { |
| 10 | + const { onFire, perspective } = useSnapshot(appViewer.playerState.reactive) |
| 11 | + const [fireTextures, setFireTextures] = useState<string[]>([]) |
| 12 | + const [currentTextureIndex, setCurrentTextureIndex] = useState(0) |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + let animationFrameId: number |
| 16 | + let lastTextureUpdate = 0 |
| 17 | + |
| 18 | + const updateTexture = (timestamp: number) => { |
| 19 | + if (onFire && fireTextures.length > 0) { |
| 20 | + if (timestamp - lastTextureUpdate >= TEXTURE_UPDATE_INTERVAL) { |
| 21 | + setCurrentTextureIndex(prev => (prev + 1) % fireTextures.length) |
| 22 | + lastTextureUpdate = timestamp |
| 23 | + } |
| 24 | + } |
| 25 | + animationFrameId = requestAnimationFrame(updateTexture) |
| 26 | + } |
| 27 | + |
| 28 | + animationFrameId = requestAnimationFrame(updateTexture) |
| 29 | + return () => cancelAnimationFrame(animationFrameId) |
| 30 | + }, [onFire, fireTextures]) |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const loadTextures = async () => { |
| 34 | + const fireImageUrls: string[] = [] |
| 35 | + |
| 36 | + const { resourcesManager } = appViewer |
| 37 | + const { blocksAtlasParser } = resourcesManager |
| 38 | + if (!blocksAtlasParser?.atlas?.latest) { |
| 39 | + console.warn('FireRenderer: Blocks atlas parser not available') |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + const keys = Object.keys(blocksAtlasParser.atlas.latest.textures).filter(key => /^fire_\d+$/.exec(key)) |
| 44 | + for (const key of keys) { |
| 45 | + const textureInfo = blocksAtlasParser.getTextureInfo(key) as { u: number, v: number, width?: number, height?: number } |
| 46 | + if (textureInfo) { |
| 47 | + const defaultSize = blocksAtlasParser.atlas.latest.tileSize |
| 48 | + const imageWidth = blocksAtlasParser.atlas.latest.width |
| 49 | + const imageHeight = blocksAtlasParser.atlas.latest.height |
| 50 | + const textureWidth = textureInfo.width ?? defaultSize |
| 51 | + const textureHeight = textureInfo.height ?? defaultSize |
| 52 | + |
| 53 | + // Create a temporary canvas for the full texture |
| 54 | + const tempCanvas = createCanvas(textureWidth, textureHeight) |
| 55 | + const tempCtx = tempCanvas.getContext('2d') |
| 56 | + if (tempCtx && blocksAtlasParser.latestImage) { |
| 57 | + const image = await getLoadedImage(blocksAtlasParser.latestImage) |
| 58 | + tempCtx.drawImage( |
| 59 | + image, |
| 60 | + textureInfo.u * imageWidth, |
| 61 | + textureInfo.v * imageHeight, |
| 62 | + textureWidth, |
| 63 | + textureHeight, |
| 64 | + 0, |
| 65 | + 0, |
| 66 | + textureWidth, |
| 67 | + textureHeight |
| 68 | + ) |
| 69 | + |
| 70 | + // Create final canvas with only top 20% of the texture |
| 71 | + const finalHeight = Math.ceil(textureHeight * 0.4) |
| 72 | + const canvas = createCanvas(textureWidth, finalHeight) |
| 73 | + const ctx = canvas.getContext('2d') |
| 74 | + if (ctx) { |
| 75 | + // Draw only the top portion |
| 76 | + ctx.drawImage( |
| 77 | + tempCanvas, |
| 78 | + 0, |
| 79 | + 0, // Start from top |
| 80 | + textureWidth, |
| 81 | + finalHeight, |
| 82 | + 0, |
| 83 | + 0, |
| 84 | + textureWidth, |
| 85 | + finalHeight |
| 86 | + ) |
| 87 | + |
| 88 | + const blob = await canvas.convertToBlob() |
| 89 | + const url = URL.createObjectURL(blob) |
| 90 | + fireImageUrls.push(url) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + setFireTextures(fireImageUrls) |
| 97 | + } |
| 98 | + |
| 99 | + // Load textures initially |
| 100 | + if (appViewer.resourcesManager.currentResources) { |
| 101 | + void loadTextures() |
| 102 | + } |
| 103 | + |
| 104 | + // Set up listener for texture updates |
| 105 | + const onAssetsUpdated = () => { |
| 106 | + void loadTextures() |
| 107 | + } |
| 108 | + appViewer.resourcesManager.on('assetsTexturesUpdated', onAssetsUpdated) |
| 109 | + |
| 110 | + // Cleanup |
| 111 | + return () => { |
| 112 | + appViewer.resourcesManager.off('assetsTexturesUpdated', onAssetsUpdated) |
| 113 | + // Cleanup texture URLs |
| 114 | + for (const url of fireTextures) URL.revokeObjectURL(url) |
| 115 | + } |
| 116 | + }, []) |
| 117 | + |
| 118 | + if (!onFire || fireTextures.length === 0 || perspective !== 'first_person') return null |
| 119 | + |
| 120 | + return ( |
| 121 | + <div |
| 122 | + className='fire-renderer-container' |
| 123 | + style={{ |
| 124 | + position: 'fixed', |
| 125 | + left: 0, |
| 126 | + right: 0, |
| 127 | + bottom: 0, |
| 128 | + height: '20dvh', |
| 129 | + pointerEvents: 'none', |
| 130 | + display: 'flex', |
| 131 | + justifyContent: 'center', |
| 132 | + alignItems: 'flex-end', |
| 133 | + overflow: 'hidden' |
| 134 | + }} |
| 135 | + > |
| 136 | + <div |
| 137 | + style={{ |
| 138 | + position: 'absolute', |
| 139 | + width: '100%', |
| 140 | + height: '100%', |
| 141 | + backgroundImage: `url(${fireTextures[currentTextureIndex]})`, |
| 142 | + backgroundSize: '50% 100%', |
| 143 | + backgroundPosition: 'center', |
| 144 | + backgroundRepeat: 'repeat-x', |
| 145 | + opacity: 0.7, |
| 146 | + filter: 'brightness(1.2) contrast(1.2)', |
| 147 | + mixBlendMode: 'screen' |
| 148 | + }} |
| 149 | + /> |
| 150 | + </div> |
| 151 | + ) |
| 152 | +} |
0 commit comments