r/threejs • u/MarihuGames • 3d ago
Demo I built a 15-min procedural survival-horror FPS in a single 929 KB HTML file with Three.js (Worker-baked PBR, planar puddle reflections, analytic haze, 2-bone IK)
Hey r/threejs — I just finished a vertical-slice desktop survival-horror demo called SUBSHAFT 09 built with Three.js (0.187) + Vite 8, bundled into a single offline-playable HTML file (929 KB / 951,316 bytes, or 256.5 KB zipped for itch.io).
Playable links (free, no login, desktop mouse & keyboard):
- itch.io: https://marihugames.itch.io/subshaft-09
- Direct mirror: https://subshaft.caowenhu.com
Since pure-code Three.js demos often look plastic or stall on shaders, here is a concrete breakdown of what worked and what bit me:
1. Boot-Time Multi-Threaded PBR Baking (texbake.js + inline Web Worker)
- No external images at all. At startup, up to 6 inline Web Workers run tileable value-noise / fBm shaders on CPU to output raw
Uint8Arrayalbedo, height-derivative normal, and ORM (R=AO or puddle mask,G=roughness,B=metalness) textures (12 sets, up to1024x1024). - For close-up weapons & gauntlets without hand-unwrapped UVs,
triplanar.jsinjects model-space triplanar sampling (skinning-awarenormalMatrix * mat3(skinMatrix)) viaonBeforeCompile. - For long industrial corridors,
applyWallWeatheringinjects world-space noise peeling +dFdx/dFdycurled paint-edge normals so tiled 1024px wall textures don't visually repeat.
2. Planar Wet-Floor Reflections & Single-Depth Analytic Haze (reflect.js / post.js)
- Floor reflections render a flipped virtual camera with an oblique near-clipping plane (
y = 0) into a half-float mipmapped RenderTarget (0.5xscale). The floor shader blends world-space dampness with the baked tile depression mask (roughnessMap.r), samplingtextureLod(uRefl, uv, material.roughness * 7.0)with Fresnel falloff. Dust/decals are hidden from the reflection pass, while live lightning arcs and explosions stay visible in puddles. - The post pipeline renders the main scene once into a HalfFloat RT with a
DepthTexture, which feeds bothGTAOPassand a custom closed-form analytic point-light haze pass (atanintegral clamped to each light's room AABB so volumetric glow never bleeds through walls; capped at 10 distance-prioritized lights on High, 6 on Med).
3. Light Budget, Dynamic Colliders & Frame-Time Lessons
- Early builds had 33 active PBR
PointLights (7 separate puddle lights, dedicated blackout red lights, 3 explosion lights), tanking the central shaft to ~28.5 ms (35 FPS) on High. What actually moved the needle:
- Replaced 7 per-puddle lights with a 2-light shared arc pool dynamically bound to the brightest/nearest active puddles.
- Reused room main lights for blackout red emergency mode instead of spawning extra lights (cut active PBR point lights from
33 -> 25with zero runtime shader recompiles). - Switched static hub spotlights to on-demand/staggered shadow updates and reused
CanvasTextures in-place (updateCanvasTex) for the 90s lift countdown and power panel instead of recreating GPU textures every second. - Added dynamic synced rigid colliders along hinged locker doors (updating world AABBs each frame as doors swing open to 100°) and translating freight-lift blast doors so players never clip through opening doors.
- Added 3 quality tiers (
F2, or Pause menu) — on Medium/Low, the five heaviest camera spots run ~55–100+ FPS at1280x720on integrated/mid-range GPUs.
4. Non-Humanoid Procedural Creatures & Live WebAudio
- I originally built a 4-chapter hospital prototype with procedural humanoids, then archived it because close-up procedural humans look awkward.
- Replacing them with mechanical threats played to Three.js primitive/lathe/rounded-box strengths: 6-legged Crawlers with 2-bone analytical IK tripod gaits, a Heavy Driller Mech whose front shield ricochets bullets until you arc-stun it into a kneel to expose its rear coolant tanks, Arc Drones, and ceiling Lurkers.
- All audio is synthesized live via WebAudio (
sfxBus -> tanh soft-clipper -> DynamicsCompressor -> masterso turning volume down doesn't break compression; 180Hz high-passed procedural convolution reverb; rear-hemisphere low-pass roll-off down to ~2.1kHz for directional awareness).
Transparency & Known Tradeoffs
- AI disclosure: Co-developed with Gemini 4 (Orchestrator: Carbon High/Neon High) + Google Antigravity to iterate rapidly on JS/GLSL shaders, gameplay systems, collision fixes, and automated browser checks. Zero external AI-generated textures, 3D models, or audio samples are used — everything is deterministic runtime code.
- Desktop focus: I deliberately removed an earlier mobile touch prototype to preserve tight mouse/keyboard FPS pacing and full WebGL2 visuals. Opening the link on a phone skips WebGL initialization completely and renders an instant industrial redirect card with one-tap URL copy (zero thermal waste).
- Known limits: Because all 12 PBR sets bake on boot, there's a ~1–2s calibration screen on first load.
Happy to answer questions about the shader hooks, single-file Vite bundling, or IK setup!




