I'll build a Minecraft-style voxel game using Three.js. Let me start by exploring the project structure.
Now I'll install Three.js and start building the voxel game. Let me plan the architecture:
- App.tsx - Main React component that hosts the Three.js canvas and UI
- game/Engine.ts - Core game engine (Three.js setup, render loop)
- game/World.ts - Chunk-based world with procedural generation
- game/Player.ts - First-person controls with physics
- game/BlockTypes.ts - Block definitions
- game/Input.ts - Keyboard/mouse input handling
- game/Noise.ts - Simplex/value noise for terrain
Let me install Three.js first:
Now let me create the noise/terrain generation system:
Now let me create the block types and material system:
Let me fix the BlockTypes.ts file. I'll restructure to avoid the forward reference and use clean tile indices:
Now let me create the chunk-based world system:
Let me fix the unused variable warning:
Let me just remove the unused field:
Let me read the current state and clean it up properly:
The edit_file tool seems to have only updated one of the two locations. Let me fix it again:
Now let me create the player/input system:
Now the player class with first-person controls and physics:
Now the main engine that ties everything together:
Let me fix these issues:
Now let me create the React UI for hotbar, crosshair, and overlays:
Let me also remove the dependency on a separate atlas.png file. I'll change the BlockIcon to use a CSS variable for the atlas URL:
Now let me also fix the BlockIcon - the faceTextures logic was odd. Let me improve it to show a more representative face:
Let me also check the main.tsx to ensure it's set up properly:
Now let me try to build the project to see if everything compiles:
Build succeeded! Let me fix the CSS import order warning and review the game. Let me also think about some issues:
- The atlas background uses
atlas-bg class but it's defined only after engine starts - that means the hotbar won't show icons until the engine is up. But actually since started becomes true after onStart, and the atlas-bg style is injected right after, it should work. Let me fix the CSS order issue:
Now let me think more carefully about a few things:
-
Faster initial load: The pregenerate builds all chunks synchronously. For VIEW_DISTANCE=5, that's 121 chunks, each with 16x16x64=16384 blocks. That's ~2M blocks. Generation is fast in JS, but mesh building may be slow. Let me make it async with progressive loading.
-
Breaking animation: Currently there's no visual feedback for breaking. Let me add a crack overlay or some visual.
-
Player spawn: When the terrain has water, the player should spawn above the water on a hill. Let me improve spawn finding.
-
The atlas-bg class won't work before engine starts - but the start screen doesn't show hotbar so that's fine.
Let me look at potential issues more carefully:
Let me improve the engine for better startup and also add breaking visual feedback. Let me also check if the atlas-bg approach works correctly. Actually, the simpler approach: I can have the atlas built once and use a canvas to dataURL right in the BlockIcon directly via React state.
Let me refactor to make this cleaner and also improve some game aspects:
I need to expose breakProgress from the Engine:
Now I need to update breakProgressVisible whenever breakProgress changes:
Build succeeded. Now let me think about runtime issues. Let me re-examine the engine for some potential runtime problems:
-
Mesh building performance: Building all 121 chunks synchronously at startup could freeze the page. Let me check that.
-
Breaking block on a selectedBlock that's BLOCK.AIR: That should be filtered, but let me make sure.
-
The fog declaration is fog!: THREE.Fog - that's a definite assignment assertion, fine.
-
The fog is being recreated every frame in updateLighting. Let me reuse it.
Let me also think about the atlas canvas being modified after creation - that could cause issues with the texture. Once we use it as a THREE texture, we shouldn't modify the canvas. The Math.random() calls in buildTextureAtlas use the regular Math.random, not a seeded one, so we get random atlas each time. That's fine but maybe not deterministic.
Let me also look at the pregenerate more carefully:
The pregenerate is async but doesn't actually defer. The synchronous building of 121 chunks will block. Let me make it actually progress-friendly:
Now let me also check the unused fog! and a few other issues. Let me also think about whether the fog needs to be recreated. Actually three.js Fog's color is mutable, so we can update this.scene.fog.color and this.scene.fog.far:
Now let me also think about the renderer's color management. I have outputColorSpace defaults which is sRGB. The atlas texture is set to SRGBColorSpace. The MeshLambertMaterial should display colors correctly. But our colors in BLOCKS use raw hex values which are intended to be sRGB. They will be slightly off, but acceptable.
Now let me build again to verify:
Now let me also add a few more improvements:
- Improve tree generation - need to ensure trees can be in inner chunk areas only and not on edges, AND handle chunk boundaries
- Add a small "third-person" or just a hand preview in the corner? - skip for now
- Better feedback for the current hotbar slot name - show the block name above the hotbar
Let me look at the world streaming issue: when the player walks, the chunks are loaded/unloaded, but we should make sure not to unload too aggressively. Let me also think about a possible issue where the player spawns and falls through the world if terrain height computation is too slow.
Let me also test: what happens if the player respawns by dying? Currently no death system. Let me add a simple "fall protection" - if y < -10, respawn at the spawn point.
Let me also add a block name display:
Now let me add a death/respawn system. If the player falls below y = -20, respawn at the original spawn point:
Let me also think about the tree generation. There's a potential issue where trees on chunk boundaries could have leaves in the neighboring chunk that don't get added. Let me check:
Good, the tree generation already skips leaves outside the chunk boundaries. So we won't write to the neighbor chunk. But this means trees near chunk edges have clipped leaves. That's acceptable for now.
Now let me think about one more potential issue: when the player is in water and tries to place a block, water is non-solid so we can't build on it. That's correct behavior. But what about when the player wants to replace water with a block? We should let them place blocks in water. Let me check the logic:
In tryPlace, we check if (this.world.getBlock(px, py, pz) !== BLOCK.AIR) return; - that means we can't place on water. We should allow placing water in air and other blocks in water. Let me fix:
Also, the raycast skips water (so the player can't target water with the crosshair). Let me also make the crosshair skip bedrock but allow targeting water. Actually let me change the raycast to also return water, so the player can interact with water:
Wait, but then water would block the ray from hitting solids behind it. That's probably OK in Minecraft-style - if you aim at water you hit water, if there's solid behind you can still break water first. Actually Minecraft allows you to see through water for breaking purposes, but solid blocks adjacent to water are still breakable. Let me think...
Actually, the simpler approach: only return water if it's adjacent to the source (i.e., visible to the player). For now, let's allow hitting water but in updateHighlight we don't show the outline for water:
Also, in continueBreaking, we should not allow breaking water or bedrock:
Let me also make sure water hardness is infinite:
Now let me build and verify:
Now let me think about a few more issues. Let me check the Water mesh issue: Currently I draw all 6 faces of water adjacent to air, which might cause weird visuals at water surfaces (top face drawn but bottom and sides also drawn). Let me only draw the top face of water:
Now let me also think about the leaves - they have alphaTest but no actual transparency in the texture. Actually they're solid color. We can render them as fully solid:
Now let me also think about whether the tree generation is correct for being able to walk under trees. The trunk is 4-6 blocks tall, leaves start at top. The player is 1.8 blocks tall, so they can usually walk under a 4-block-tall tree if it's on a flat surface (1 block of trunk above ground = 3 blocks of headroom).
Let me also verify by checking the sea-level water setup:
- Sea level = 18
- Terrain at y=15 means water fills y=16, 17, 18
- Player can swim up out of water
Let me also check whether the fog distance is reasonable. View distance = 5 chunks = 80 blocks. Fog from 30 to 80. That should look nice.
Now let me also check one thing: in the world gen, the base terrain height function has some issues. Let me trace:
fbm2D(this.seed, wx * 0.012, wz * 0.012, 4, 2.0, 0.5) returns 0..1
Base = base * 18 + detail * 4
Plus fbm2D(this.seed + 7, wx * 0.005, wz * 0.005, 2, 2.0, 0.5) * 12 (the "hills" function)
= 0..1 * 12 = 0..12
Total: 0..12 + 0..18 + 0..4 = 0..34, then + 22 = 22..56
So terrain is at y=22 to y=56, with sea level at 18. Wait, that means almost no water? Let me recheck the surface values... if surface is always above 22, then water is never seen. Let me adjust the terrain to have more variety and lower minimum:
Now terrain can be in range roughly 0..30, centered at sea level (18). This should give lakes, beaches, hills, and mountains. Let me also test by computing at (0,0): continent ~ 0.5, mountains ~ 0.5, surface ~ 0.5, h = 8 + 7 + 1.25 - 8 = 8.25. Hmm that's too low. Let me adjust:
Now h is roughly -4 to 34, mostly around 0..30 with sea level 18. The terrain will have water in some low areas.
Let me also ensure the spawn point is good. The App.tsx code I wrote searches for a high point near origin. Let me check it:
Good. Now let me build and verify:
Now let me also do a final review. Let me look at the actual file listing:
Files built: index.html, src/index.css, src/game/Noise.ts, src/game/BlockTypes.ts, src/game/World.ts, src/game/Input.ts, src/game/Player.ts, src/game/Engine.ts, src/App.tsx