Let's read the rest of textures.ts and see where we can add Crafting Table top/side textures and Crack textures! Wait, if we increase ATLAS_SIZE to 4, we have 16 slots (0 to 15). Right now indices go up to 11 (COBBLESTONE: 11).
We can add:
CRAFTING_TABLE_TOP: 12
CRAFTING_TABLE_SIDE: 13
Wait, what about the crack texture stages? Or we can create a separate texture or atlas just for crack textures (or 10 stages of cracks), or generate a separate texture for the crack overlay mesh!
Let's think: having a dedicated createCrackTextures() function that returns 10 THREE.CanvasTextures (or 1 texture atlas with 10 stages) for the crack overlay mesh is super clean!
Let's see: how should the 3D crack overlay work?
When the player is breaking a block at targetBlock = {x, y, z}, we calculate stage = Math.floor(progress * 10) (from 0 to 9).
If stage >= 0 && progress > 0, we show a THREE.Mesh(boxGeometry, crackMaterials[stage]) positioned at (x + 0.5, y + 0.5, z + 0.5) with scale (1.003, 1.003, 1.003).
The crack texture will have transparent background (rgba(0,0,0,0)) and black/dark gray crack lines (rgba(0,0,0,0.85) or rgba(30,30,30,0.9)) that branch across the 16x16 pixel face!
Let's design realistic Minecraft 16x16 crack patterns for stages 0 to 9! That will look authentic and feel like real Minecraft mining!
Let's also look at BlockType in world.ts and items.ts. Let's read world.ts lines 1 to 60.
Let's check what we need for Crafting Table as a real block:
- In
BlockType, add CRAFTING_TABLE = 11.
- In
textures.ts:
Add CRAFTING_TABLE_TOP: 12 and CRAFTING_TABLE_SIDE: 13.
Draw them nicely (with grid/tools pixel art pattern).
- In
BLOCK_FACES:
[BlockType.CRAFTING_TABLE]: [TEX.CRAFTING_TABLE_TOP, TEX.OAK_PLANKS, TEX.CRAFTING_TABLE_SIDE]
- In
items.ts:
Update ItemType.CRAFTING_TABLE = 11 so ItemType.CRAFTING_TABLE === BlockType.CRAFTING_TABLE.
And in ITEMS[ItemType.CRAFTING_TABLE], set placeable: BlockType.CRAFTING_TABLE.
Set BLOCK_HARDNESS[BlockType.CRAFTING_TABLE] = 2.5, BLOCK_TOOL[BlockType.CRAFTING_TABLE] = 'axe', BLOCK_DROPS[BlockType.CRAFTING_TABLE] = [{ item: ItemType.CRAFTING_TABLE, count: 1, chance: 1 }].
- How does the player use the Crafting Table now?
In
engine.isNearCraftingTable(), check if any block within radius 3 around player is BlockType.CRAFTING_TABLE!
ALSO: If the player right-clicks (placing/interacting) on a placed BlockType.CRAFTING_TABLE block, or presses E while near one, or taps the table directly, open the Crafting Table interface!
Let's check handleBlockInteraction(): if player right clicks on BlockType.CRAFTING_TABLE, we trigger this.callbacks.onOpenCrafting?.(). That opens the inventory/crafting menu directly!
Let's check Task 2: Real 3D Block Breaking Overlay.
Instead of the 2D ring in App.tsx, let's create a 3D mesh in Game:
private breakOverlayMesh: THREE.Mesh | null = null;
Let's generate 10 crack textures (createCrackTextures()).
When mining (this.miningTime > 0 and this.targetBlock exists):
We set breakOverlayMesh.position.set(x + 0.5, y + 0.5, z + 0.5).
breakOverlayMesh.visible = true.
We calculate stage = Math.min(9, Math.floor((this.miningTime / this.miningTotal) * 10)).
We set (breakOverlayMesh.material as THREE.MeshBasicMaterial).map = crackTextures[stage].
When mining resets or stops (resetMining()), we set breakOverlayMesh.visible = false.
And let's remove the spinning SVG ring from App.tsx. That will look like authentic Minecraft mining!
Let's check Task 3: Water Physics & Buoyancy.
Right now, what happens when you go into water? Let's check updatePlayer(dt) in engine.ts. Let's read lines 215 to 380 of engine.ts.
Let's check lines 310 to 380 of engine.ts where updatePlayer is defined.
Let's see: how should water physics work?
Let's check if the player's feet or eyes are inside water:
const inWater = this.world.getBlock(Math.floor(this.posX), Math.floor(this.posY + 0.3), Math.floor(this.posZ)) === BlockType.WATER;
const eyesInWater = this.world.getBlock(Math.floor(this.posX), Math.floor(this.posY + PLAYER_EYE_HEIGHT), Math.floor(this.posZ)) === BlockType.WATER;
If inWater is true:
- Movement speed is slowed down (
speed = MOVE_SPEED * 0.55).
- Gravity is greatly reduced (
this.velY -= GRAVITY * 0.15 * dt).
- If
inputState.jump is true (or jump button held), player swims upwards (this.velY = 3.5).
- Terminal falling velocity in water:
if (this.velY < -3) this.velY = -3;
- No fall damage when landing in water (
if (inWater) this.fallStartY = this.posY).
- If
eyesInWater is true:
We can adjust fog color or background color or apply a blue underwater tint! Or pass underwater: boolean in SurvivalState so React can render a soft blue overlay screen when underwater!
Let's check why lag is happening and how we can achieve 60 FPS! Let's read updateChunks and chunk meshing in world.ts.
Let's read lines 410 to 470 of engine.ts.
Let's examine why FPS drops:
generateChunk(cx, cz) runs noise calculations for 16x16 = 256 columns x 64 height.
buildChunkMesh(chunk) iterates over 16x64x16 = 16,384 blocks! For every block, it checks neighbors using this.getBlock(nx, ny, nz).
Let's look at this.getBlock(wx, wy, wz) inside buildChunkMesh:
getBlock calculates cx = Math.floor(wx / CHUNK_SIZE), cz = Math.floor(wz / CHUNK_SIZE), looks up chunks.get(key) in a JavaScript Map<string, Chunk> using string allocation ${cx},${cz}!
Inside a nested loop of 16x64x16 blocks x 6 faces = 98,304 face checks per chunk rebuild! Doing Map.get("${cx},${cz}") ~100,000 times creates massive string garbage and Map lookup overhead!
And if loaded < 2 or rebuilt < 3, doing 2 chunk generations and 3 chunk rebuilds in ONE frame means doing 500,000 string allocations and Map lookups in a single 16ms animation frame! No wonder it lags or stutters!
Let's verify this insight! In buildChunkMesh(chunk), for lx from 0..15, ly from 0..63, lz from 0..15:
If lx > 0 && lx < 15 && lz > 0 && lz < 15 && ly > 0 && ly < 63:
All neighbors are guaranteed to be inside chunk.blocks! We DO NOT need to call world.getBlock(wx, wy, wz) or allocate strings for neighbors that are inside the same chunk!
Inside the chunk, we can directly do chunk.blocks[ny * 256 + nz * 16 + nx]! That is an O(1) direct typed array access!
Only when lx === 0 || lx === 15 || lz === 0 || lz === 15 || ly === 0 || ly === 63 do we need to check across chunk boundaries (and even then we can pre-fetch the 4 neighbor chunks chunkXMinus1, chunkXPlus1, chunkZMinus1, chunkZPlus1 ONCE before the loop instead of creating strings inside the loop)!
Let's calculate the speedup:
Instead of ~100,000 string allocations and Map lookups per chunk mesh, we do 0 string allocations and 0 Map lookups during mesh building! This will make buildChunkMesh at least 20x to 50x faster!
Furthermore:
In updateChunks(), limit chunk loading and rebuilding per frame:
Instead of loaded < 2 and rebuilt < 3 per frame, let's limit total heavy operations per frame:
If we load a new chunk or build a mesh, only do 1 per frame (if (ops >= 1) break;). Since our optimized buildChunkMesh takes <1ms, 1 per frame guarantees solid 60 FPS without any frame drops!
And let's optimize renderer.setPixelRatio:
On mobile devices (navigator.maxTouchPoints > 0), devices often have devicePixelRatio = 3. Rendering a 3D scene at 3x retina resolution on a mobile GPU destroys framerate. Let's set renderer.setPixelRatio(Math.min(window.devicePixelRatio, isMobile ? 1.0 : 1.5)).
Let's also look at raycast in world.ts. Let's check lines 400 to 490 of world.ts.
Look at raycast: for i = 0 to 200, calling getBlock(x, y, z) does string formatting and Map lookup at every single step! And REACH_DISTANCE is 7, so maximum steps needed is ~30 (not 200). Furthermore, we can cache the chunk lookup inside raycast or let getBlock use a fast numeric coordinate cache (lastCx, lastCz, lastChunk)!
Let's check getBlock(wx, wy, wz) in world.ts. Let's read lines 190 to 220 of world.ts.
Adding a 1-entry numeric cache to getChunk(cx, cz):
private lastCx = -999999;
private lastCz = -999999;
private lastChunk: Chunk | undefined;
getChunk(cx: number, cz: number): Chunk | undefined {
if (cx === this.lastCx && cz === this.lastCz) return this.lastChunk;
const c = this.chunks.get(this.chunkKey(cx, cz));
this.lastCx = cx;
this.lastCz = cz;
this.lastChunk = c;
return c;
}
And whenever removeChunk happens or chunks change, reset this.lastCx = -999999.
Just adding this 1-line cache speeds up getBlock and raycasting by ~10x across the entire game engine!
Let's start implementing step-by-step:
First, let's update textures.ts to add Crafting Table top & side textures, and also add createCrackTextures() that returns 10 stages of breaking textures!
Let's explain what we are about to do and edit textures.ts.I will now update src/game/textures.ts to add the Crafting Table top and side textures to the texture atlas, and create a createCrackTextures() function that generates 10 progressive block breaking crack overlay textures for rendering 3D block breaking animations directly on the blocks.
Now let's add the drawing for CRAFTING_TABLE_TOP and CRAFTING_TABLE_SIDE, and also add createCrackTextures() to src/game/textures.ts. Let's check where COBBLESTONE is drawn in textures.ts.
Let's edit src/game/textures.ts to add the crafting table textures and the crack overlay textures generator.I will now add the Crafting Table top and side texture rendering rules and create the createCrackTextures() function in src/game/textures.ts.
Now let's add createCrackTextures() to src/game/textures.ts. Let's check the end of textures.ts.
Let's add createCrackTextures() at the bottom of textures.ts.I will now append the createCrackTextures() function to src/game/textures.ts to generate 10 progressive crack stage textures.
Now let's update world.ts:
- Add
CRAFTING_TABLE = 11 to BlockType.
- Update
BLOCK_NAMES and BLOCK_FACES.
- Optimize
buildChunkMesh(chunk) by using typed array directly for neighbors inside the chunk and avoiding string allocations/Map lookups inside the loop!
Let's read world.ts around BlockType first.
Let's edit lines 1 to 45 of world.ts to add CRAFTING_TABLE.I will now update BlockType, BLOCK_NAMES, and BLOCK_FACES in src/game/world.ts to include the Crafting Table block type.
Now let's add the chunk lookup cache to World in world.ts to dramatically speed up block lookups and raycasting. Let's read lines 180 to 220 of world.ts.
Let's update getChunk and removeChunk in world.ts with the numeric cache. Also let's check buildChunkMesh in world.ts to optimize it with direct array lookups inside the chunk! Let's read lines 320 to 395 of world.ts.
Let's edit world.ts to add the cache in getChunk and removeChunk, and optimize buildChunkMesh to fetch internal neighbor blocks directly from chunk.blocks (and pre-fetch neighbor chunks for boundaries!).
Let's see: inside buildChunkMesh:
If nx >= wx - lx and nx < wx - lx + 16 and nz >= wz - lz and nz < wz - lz + 16 and ny >= 0 and ny < 64:
Wait, inside the chunk, nx relative to chunk start is lx + face.dir[0].
So if lx + face.dir[0] >= 0 && lx + face.dir[0] < CHUNK_SIZE && lz + face.dir[2] >= 0 && lz + face.dir[2] < CHUNK_SIZE && ny >= 0 && ny < CHUNK_HEIGHT:
Then neighbor block is simply chunk.blocks[ny * CHUNK_SIZE * CHUNK_SIZE + (lz + face.dir[2]) * CHUNK_SIZE + (lx + face.dir[0])]!
Let's check: if ny < 0 || ny >= CHUNK_HEIGHT, neighbor is BlockType.AIR.
If lx + face.dir[0] < 0, neighbor chunk is chunkXMinus1 (pre-fetched!).
If lx + face.dir[0] >= CHUNK_SIZE, neighbor chunk is chunkXPlus1 (pre-fetched!).
If lz + face.dir[2] < 0, neighbor chunk is chunkZMinus1 (pre-fetched!).
If lz + face.dir[2] >= CHUNK_SIZE, neighbor chunk is chunkZPlus1 (pre-fetched!).
With these 4 pre-fetched chunks (cXM, cXP, cZM, cZP), neighbor block lookup requires 0 Map lookups and 0 string allocations across all 98,304 face checks!
Let's implement this super-fast getNeighbor inside buildChunkMesh!I will now update src/game/world.ts to add the numeric chunk lookup cache and optimize buildChunkMesh with zero-allocation O(1) neighbor lookups.
Now let's update removeChunk and buildChunkMesh in world.ts. Let's check removeChunk first.
Let's edit removeChunk to reset the cache this.lastCx = -999999.I will now update removeChunk in src/game/world.ts to invalidate the cache.
Now let's replace buildChunkMesh(chunk) in world.ts with the zero-allocation O(1) pre-fetched neighbor lookup! Let's check lines 330 to 415 of world.ts.
Let's update buildChunkMesh(chunk) in src/game/world.ts to pre-fetch the 4 border chunks and do fast local lookups.I will now optimize buildChunkMesh in src/game/world.ts to pre-fetch neighbor chunks and perform 100% zero-allocation array lookups.
Let's check items.ts to add CRAFTING_TABLE = 11 to ItemType and update recipe outputs/inputs if needed. Let's read items.ts.
Let's read lines 60 to 120 of items.ts.
Let's update items.ts:
- Change
ItemType.CRAFTING_TABLE = 11 (instead of 107) so that block type and item type match!
- In
ITEMS[ItemType.CRAFTING_TABLE], set placeable: BlockType.CRAFTING_TABLE.
- Add
BLOCK_HARDNESS[BlockType.CRAFTING_TABLE] = 2.5.
- Add
BLOCK_TOOL[BlockType.CRAFTING_TABLE] = 'axe'.
- Add
BLOCK_DROPS[BlockType.CRAFTING_TABLE] = [{ item: ItemType.CRAFTING_TABLE, count: 1, chance: 1 }].
Let's do this edit on items.ts.I will now update src/game/items.ts so ItemType.CRAFTING_TABLE matches BlockType.CRAFTING_TABLE (ID 11) and add its hardness, tool, and drop definitions.
Let's update ITEMS[ItemType.CRAFTING_TABLE] and block properties in src/game/items.ts.Let's read lines 50 to 90 of items.ts.
Let's edit items.ts to update placeable: BlockType.CRAFTING_TABLE, BLOCK_HARDNESS, BLOCK_TOOL, and BLOCK_DROPS.I will now update the crafting table item definition and block mining properties in src/game/items.ts.
Let's update BLOCK_HARDNESS, BLOCK_TOOL, and BLOCK_DROPS in items.ts. Let's read lines 70 to 125 of items.ts.
Let's edit items.ts.I will now update BLOCK_HARDNESS, BLOCK_TOOL, and BLOCK_DROPS in src/game/items.ts for the Crafting Table.
Let's also update BLOCK_DROPS and HOTBAR_BLOCKS / recipes in items.ts. Let's read lines 110 to 145 of items.ts.
Let's add [BlockType.CRAFTING_TABLE]: [{ item: ItemType.CRAFTING_TABLE, count: 1, chance: 1 }], to BLOCK_DROPS.I will now update BLOCK_DROPS in src/game/items.ts to include Crafting Table drops.
Now let's check engine.ts! We need to:
- Limit heavy chunk operations (
loaded + rebuilt) to 1 per frame for super smooth 60 FPS.
- Set pixel ratio to
1.0 or Math.min(window.devicePixelRatio, isMobile ? 1.0 : 1.5) for high FPS.
- Add the 3D block break overlay mesh (
breakOverlayMesh) using createCrackTextures(). When breaking a block, position and scale breakOverlayMesh slightly larger than the target block (1.002 scale) and update its texture!
- Check if near
BlockType.CRAFTING_TABLE in isNearCraftingTable().
- If player right-clicks (
placing) on a BlockType.CRAFTING_TABLE block, trigger callbacks.onOpenCrafting?.().
- Add real water physics (swimming, buoyancy, slow movement when in water).
Let's read engine.ts around GameCallbacks and constructor first.
Let's read lines 85 to 135 of engine.ts.
Let's import createCrackTextures from ./textures in engine.ts and set up breakOverlayMesh and crackTextures. Let's check imports at top of engine.ts.
Let's edit GameCallbacks to add onOpenCrafting?: () => void; and import createCrackTextures.I will now update src/game/engine.ts to import createCrackTextures, add onOpenCrafting to GameCallbacks, and add fields for the 3D block breaking overlay mesh.
Let's add onOpenCrafting to GameCallbacks and initialize breakOverlayMesh and crackTextures. Let's read lines 35 to 65 of engine.ts.
Let's edit GameCallbacks and Game fields.I will now update GameCallbacks and add the break overlay mesh and textures to the Game class.
Let's initialize breakOverlayMesh and crackTextures in the constructor, and set up pixelRatio optimization for mobile. Let's read lines 100 to 140 of engine.ts.
Let's edit the constructor in engine.ts.I will now initialize the break overlay mesh and optimize the renderer pixel ratio in the Game constructor.
Now let's check dispose() in engine.ts to make sure we clean up breakOverlayMesh. Let's read the end of engine.ts.
Let's update isNearCraftingTable() to check for BlockType.CRAFTING_TABLE. And let's update dispose().I will now update isNearCraftingTable to check for BlockType.CRAFTING_TABLE and dispose resources properly.
Now let's update updateChunks() to limit operations to 1 per frame, update updatePlayer(dt) with water buoyancy and physics, and update handleBlockInteraction with the 3D overlay breaking animation and crafting table interaction! Let's read updatePlayer in engine.ts (lines 320 to 380).
Let's edit updatePlayer(dt) to add water buoyancy, swimming up when holding jump, slowed falling, reduced movement speed in water, and underwater fog tint!I will now update updatePlayer in src/game/engine.ts with water physics and swimming mechanics.
Now let's update updateChunks() to limit heavy operations to 1 per frame, ensuring steady 60 FPS. Let's read lines 440 to 480 of engine.ts.
Let's read lines 470 to 510 of engine.ts.
Let's update updateChunks() to budget 1 heavy operation per frame.I will now update updateChunks in src/game/engine.ts to strictly budget chunk generation and mesh rebuilding to 1 operation per frame for smooth 60 FPS.
Now let's check handleBlockInteraction() and resetMining() in engine.ts to add the 3D block breaking overlay animation and the Crafting Table interaction! Let's read lines 520 to 600 of engine.ts.
Let's read lines 585 to 635 of engine.ts.
Let's edit handleBlockInteraction() and resetMining() in engine.ts:
- Check if right-clicking (
inputState.placing) on a block that is BlockType.CRAFTING_TABLE. If so, call this.callbacks.onOpenCrafting?.(), set this.placeCooldown = 0.5, and return!
- When mining, position
this.breakOverlayMesh at (x + 0.5, y + 0.5, z + 0.5) and set texture to this.crackTextures[stage].
- When
resetMining() is called, set this.breakOverlayMesh.visible = false.
Let's do this edit on engine.ts.I will now update handleBlockInteraction and resetMining in src/game/engine.ts to show the 3D block breaking overlay animation directly on the block and open the crafting menu when interacting with a placed Crafting Table.
Now let's check App.tsx!
Let's see:
- In
App.tsx, remove the spinning SVG break progress ring around the crosshair since breaking is now shown on the real 3D block itself!
- Hook up
onOpenCrafting: () => { setInvOpen(true); if (document.pointerLockElement) document.exitPointerLock(); } when initializing Game!
- Add starter crafting tables or let the player craft one and place it to use it!
Let's read
App.tsx around the useEffect where Game is initialized and around the crosshair.
Let's read lines 260 to 320 of App.tsx where the crosshair and UI elements are rendered.
Let's read lines 315 to 350 of App.tsx.
Files built: src/game/textures.ts, src/game/world.ts, src/game/items.ts, src/game/engine.ts