{"id":1144,"date":"2026-11-02T17:01:15","date_gmt":"2026-11-02T17:01:15","guid":{"rendered":"https:\/\/networkyy.com\/building-interactive-3d-voxel-maps-threejs-webgl\/"},"modified":"2026-11-02T17:01:15","modified_gmt":"2026-11-02T17:01:15","slug":"building-interactive-3d-voxel-maps-threejs-webgl","status":"publish","type":"post","link":"https:\/\/networkyy.com\/fr\/building-interactive-3d-voxel-maps-threejs-webgl\/","title":{"rendered":"Building Interactive 3D Voxel Maps in the Browser with Three.js and WebGL"},"content":{"rendered":"<figure><img decoding=\"async\" src=\"https:\/\/images.pexels.com\/photos\/5380666\/pexels-photo-5380666.jpeg?auto=compress&#038;cs=tinysrgb&#038;dpr=2&#038;h=650&#038;w=940\" alt=\"Building Interactive 3D Voxel Maps in the Browser with Three.js and WebGL\" style=\"width:100%;height:auto;border-radius:8px;margin-bottom:24px;\" \/><figcaption>Photo by Tima Miroshnichenko on Pexels<\/figcaption><\/figure>\n<h1>Building Interactive 3D Voxel Maps in the Browser with Three.js and WebGL<\/h1>\n<p>A developer just released an interactive 3D voxel map of Warsaw&#8217;s city center that&#8217;s caught the attention of the Hacker News community, and for good reason. This isn&#8217;t some heavy Unity export requiring gigabytes of downloads\u2014it&#8217;s a browser-based experience built with web technologies that runs smoothly on desktop while remaining functional on mobile. Users can change the time of day, adjust weather conditions, and even click on pedestrians to take control of them, Dungeon Keeper style.<\/p>\n<p>What makes this particularly interesting from an IT professional&#8217;s perspective isn&#8217;t just the visual appeal\u2014it&#8217;s the technical execution. This project demonstrates how far browser-based 3D rendering has come and showcases practical techniques you can apply to data visualization, urban planning tools, architectural presentations, or any scenario where you need to represent spatial information interactively.<\/p>\n<p>Let&#8217;s break down how you can build similar interactive 3D experiences using modern web technologies, starting with the fundamentals and moving into optimization strategies that make these projects actually usable in production.<\/p>\n<h2>Table of Contents<\/h2>\n<ul>\n<li><a href=\"#three-js-foundation\">Setting Up Three.js for Voxel Rendering<\/a><\/li>\n<li><a href=\"#voxel-geometry\">Efficient Voxel Geometry and Instancing<\/a><\/li>\n<li><a href=\"#interaction\">Adding Raycasting and User Interaction<\/a><\/li>\n<li><a href=\"#optimization\">Performance Optimization for Large Scenes<\/a><\/li>\n<li><a href=\"#lighting-atmosphere\">Dynamic Lighting and Atmospheric Effects<\/a><\/li>\n<\/ul>\n<h2 id=\"three-js-foundation\">Setting Up Three.js for Voxel Rendering<\/h2>\n<p>Three.js is the de facto library for WebGL in the browser, abstracting away much of the low-level complexity while still giving you the control needed for performance-critical applications. Unlike game engines that require compilation steps, Three.js lets you iterate rapidly and deploy instantly\u2014critical advantages for web-based projects.<\/p>\n<p>Here&#8217;s a minimal scene setup that establishes the foundation for voxel rendering:<\/p>\n<pre><code>\/\/ Initialize scene, camera, and renderer for voxel environment\nconst scene = new THREE.Scene();\nconst camera = new THREE.PerspectiveCamera(75, window.innerWidth \/ window.innerHeight, 0.1, 2000);\nconst renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });\n\nrenderer.setSize(window.innerWidth, window.innerHeight);\nrenderer.shadowMap.enabled = true;\nrenderer.shadowMap.type = THREE.PCFSoftShadowMap;\ndocument.body.appendChild(renderer.domElement);\n\n\/\/ Position camera for city overview\ncamera.position.set(50, 80, 50);\ncamera.lookAt(0, 0, 0);\n\n\/\/ Add ambient and directional lighting\nconst ambientLight = new THREE.AmbientLight(0x404040, 0.6);\nscene.add(ambientLight);\n\nconst sunLight = new THREE.DirectionalLight(0xffffff, 0.8);\nsunLight.position.set(100, 200, 50);\nsunLight.castShadow = true;\nsunLight.shadow.camera.left = -100;\nsunLight.shadow.camera.right = 100;\nsunLight.shadow.camera.top = 100;\nsunLight.shadow.camera.bottom = -100;\nscene.add(sunLight);\n<\/code><\/pre>\n<p>This setup includes shadow mapping, which is essential for creating the depth and visual hierarchy that makes voxel scenes readable. The directional light represents the sun, and as you&#8217;ll see later, adjusting its position and color lets you simulate different times of day.<\/p>\n<h2 id=\"voxel-geometry\">Efficient Voxel Geometry and Instancing<\/h2>\n<p>The naive approach to voxels\u2014creating individual cube meshes for every block\u2014will destroy your frame rate faster than you can say &#8220;memory leak.&#8221; A city center might contain tens of thousands of voxels, and each traditional mesh carries significant overhead.<\/p>\n<p>The solution is geometry instancing, where you define a single cube geometry and material, then use InstancedMesh to render thousands of copies with different positions and colors. This dramatically reduces draw calls and memory usage.<\/p>\n<div style=\"background:#fef3c7;border-left:4px solid #f59e0b;padding:14px 18px;border-radius:6px;margin:20px 0;\"><strong>\ud83d\udca1 Pro Tip:<\/strong> When working with large voxel datasets, store your voxel data as a flat typed array (Uint8Array or Uint16Array) rather than nested JavaScript objects. This reduces memory overhead by 10-20x and enables faster iteration through your voxel data during updates.<\/div>\n<p>Here&#8217;s how to implement efficient voxel rendering with instancing:<\/p>\n<pre><code>\/\/ Create instanced mesh for efficient rendering of thousands of voxels\nconst voxelGeometry = new THREE.BoxGeometry(1, 1, 1);\nconst voxelMaterial = new THREE.MeshLambertMaterial();\n\nconst maxVoxels = 50000;\nconst instancedMesh = new THREE.InstancedMesh(voxelGeometry, voxelMaterial, maxVoxels);\ninstancedMesh.castShadow = true;\ninstancedMesh.receiveShadow = true;\n\n\/\/ Temporary objects for matrix transformations\nconst matrix = new THREE.Matrix4();\nconst color = new THREE.Color();\n\n\/\/ Function to add a voxel at specific coordinates\nfunction addVoxel(x, y, z, colorHex, index) {\n  matrix.setPosition(x, y, z);\n  instancedMesh.setMatrixAt(index, matrix);\n  color.setHex(colorHex);\n  instancedMesh.setColorAt(index, color);\n}\n\n\/\/ Example: Create a simple building structure\nlet voxelIndex = 0;\nfor (let x = 0; x < 10; x++) {\n  for (let y = 0; y < 20; y++) {\n    for (let z = 0; z < 10; z++) {\n      if (x === 0 || x === 9 || z === 0 || z === 9) {\n        addVoxel(x, y, z, 0xcccccc, voxelIndex++);\n      }\n    }\n  }\n}\n\ninstancedMesh.instanceMatrix.needsUpdate = true;\ninstancedMesh.instanceColor.needsUpdate = true;\nscene.add(instancedMesh);\n<\/code><\/pre>\n<p>This approach lets you render a basic building structure with hundreds of voxels while maintaining smooth performance. The InstancedMesh API gives you per-instance colors and transforms without the overhead of separate mesh objects.<\/p>\n<h2 id=\"interaction\">Adding Raycasting and User Interaction<\/h2>\n<p>The Warsaw voxel map's ability to click on pedestrians and control them demonstrates sophisticated interaction handling. In Three.js, this is accomplished through raycasting\u2014casting a virtual ray from the camera through the mouse position to detect intersections with 3D objects.<\/p>\n<p>For IT professionals building dashboards or visualization tools, this same technique enables clicking on data points in 3D space, selecting buildings in architectural models, or interacting with network topology visualizations. If you're looking to deepen your understanding of interactive web graphics and related front-end technologies, <a href=\"https:\/\/imp.i384100.net\/zxbRDr\" target=\"_blank\" rel=\"nofollow sponsored noopener\">Coursera<\/a> offers comprehensive courses on computer graphics and web development that cover these concepts in depth.<\/p>\n<p>Implementing raycasting requires careful consideration of performance, especially with instanced meshes where you need to determine not just which mesh was clicked, but which specific instance within that mesh.<\/p>\n<h3>Handling Click Events in 3D Space<\/h3>\n<p>The raycaster needs to convert 2D mouse coordinates into 3D world coordinates, then check for intersections:<\/p>\n<pre><code>const raycaster = new THREE.Raycaster();\nconst mouse = new THREE.Vector2();\n\n\/\/ Convert mouse coordinates and perform raycasting\nrenderer.domElement.addEventListener('click', (event) => {\n  mouse.x = (event.clientX \/ window.innerWidth) * 2 - 1;\n  mouse.y = -(event.clientY \/ window.innerHeight) * 2 + 1;\n  \n  raycaster.setFromCamera(mouse, camera);\n  const intersects = raycaster.intersectObject(instancedMesh);\n  \n  if (intersects.length > 0) {\n    const instanceId = intersects[0].instanceId;\n    console.log(`Clicked on voxel instance: ${instanceId}`);\n    \n    \/\/ Highlight or modify the clicked instance\n    const highlightColor = new THREE.Color(0xff0000);\n    instancedMesh.setColorAt(instanceId, highlightColor);\n    instancedMesh.instanceColor.needsUpdate = true;\n  }\n});\n<\/code><\/pre>\n<p>This code captures click events, translates them into 3D space, and identifies which specific voxel instance was clicked. You can extend this to trigger animations, open information panels, or change the camera focus\u2014exactly the kind of interaction patterns that make data visualization tools genuinely useful rather than just pretty.<\/p>\n<h2 id=\"optimization\">Performance Optimization for Large Scenes<\/h2>\n<p>The Warsaw map works smoothly because it implements several critical optimization strategies. When you're rendering city-scale scenes with potentially hundreds of thousands of voxels, performance becomes non-negotiable.<\/p>\n<div style=\"background:#fef3c7;border-left:4px solid #f59e0b;padding:14px 18px;border-radius:6px;margin:20px 0;\"><strong>\u26a0\ufe0f Common Mistake:<\/strong> Loading all voxel data at once will cause long initial load times and memory issues. Implement level-of-detail (LOD) systems where distant objects are simplified or chunk-based loading where only visible areas are rendered in full detail.<\/div>\n<p>Key optimization techniques include:<\/p>\n<ul>\n<li><strong>Frustum culling:<\/strong> Three.js handles this automatically, but you can optimize further by organizing voxels into spatial hierarchies<\/li>\n<li><strong>Texture atlasing:<\/strong> Combine multiple textures into a single atlas to reduce texture binding overhead<\/li>\n<li><strong>Geometry merging:<\/strong> For static structures, merge adjacent voxels into single optimized meshes<\/li>\n<li><strong>Web Workers:<\/strong> Offload voxel data processing and collision detection to separate threads<\/li>\n<\/ul>\n<p>For professionals looking to master these performance optimization techniques alongside data processing workflows, <a href=\"https:\/\/datacamp.pxf.io\/YR9dQK\" target=\"_blank\" rel=\"nofollow sponsored noopener\">DataCamp<\/a> provides practical courses on working with large datasets and efficient algorithms that translate well to 3D rendering contexts.<\/p>\n<h3>Implementing Chunk-Based Rendering<\/h3>\n<p>Divide your voxel world into chunks (typically 16x16x16 or 32x32x32 blocks) and only render chunks within view distance:<\/p>\n<pre><code>class VoxelChunkManager {\n  constructor(chunkSize = 16, viewDistance = 5) {\n    this.chunkSize = chunkSize;\n    this.viewDistance = viewDistance;\n    this.chunks = new Map();\n  }\n  \n  \/\/ Get chunk key from world coordinates\n  getChunkKey(x, y, z) {\n    const cx = Math.floor(x \/ this.chunkSize);\n    const cy = Math.floor(y \/ this.chunkSize);\n    const cz = Math.floor(z \/ this.chunkSize);\n    return `${cx},${cy},${cz}`;\n  }\n  \n  \/\/ Update visible chunks based on camera position\n  updateVisibleChunks(cameraPosition) {\n    const visibleKeys = new Set();\n    const camChunkX = Math.floor(cameraPosition.x \/ this.chunkSize);\n    const camChunkZ = Math.floor(cameraPosition.z \/ this.chunkSize);\n    \n    for (let x = -this.viewDistance; x <= this.viewDistance; x++) {\n      for (let z = -this.viewDistance; z <= this.viewDistance; z++) {\n        const key = `${camChunkX + x},0,${camChunkZ + z}`;\n        visibleKeys.add(key);\n        \n        if (!this.chunks.has(key)) {\n          this.loadChunk(key);\n        }\n      }\n    }\n    \n    \/\/ Unload distant chunks\n    for (const [key, chunk] of this.chunks) {\n      if (!visibleKeys.has(key)) {\n        this.unloadChunk(key);\n      }\n    }\n  }\n  \n  loadChunk(key) {\n    \/\/ Create and add chunk mesh to scene\n    console.log(`Loading chunk: ${key}`);\n  }\n  \n  unloadChunk(key) {\n    \/\/ Remove chunk mesh and free memory\n    console.log(`Unloading chunk: ${key}`);\n    this.chunks.delete(key);\n  }\n}\n<\/code><\/pre>\n<p>This chunk management system ensures you're only rendering what's visible, dramatically reducing the computational load for large scenes. The Warsaw map likely implements something similar to maintain its smooth performance across different hardware capabilities.<\/p>\n<h2 id=\"lighting-atmosphere\">Dynamic Lighting and Atmospheric Effects<\/h2>\n<p>One of the Warsaw map's most compelling features is the ability to change time of day and weather conditions. These aren't just cosmetic\u2014they demonstrate how dynamic lighting transforms the perception of 3D space and can be applied to any scenario where environmental context matters.<\/p>\n<p>Implementing time-of-day transitions involves smoothly interpolating light color, intensity, and position. The sun moves across the sky, its color shifts from warm orange at dawn to bright white at noon to deep red at sunset, and the ambient light adjusts accordingly.<\/p>\n<h3>Creating Dynamic Sky and Weather Systems<\/h3>\n<p>Here's a simplified approach to dynamic lighting that responds to a time parameter:<\/p>\n<pre><code>class TimeOfDayController {<br \/>\n  constructor(scene, sunLight) {<br \/>\n    this.scene = scene;<br \/>\n    this.sunLight = sunLight;<br \/>\n    this.time = 12; \/\/ Hour in 24-hour format<br \/>\n  }<\/p>\n<p>  \/\/ Update lighting based on time of day<br \/>\n  setTime(hour) {<br \/>\n    this.time = hour;<\/p>\n<p>    \/\/ Calculate sun position (simplified solar path)<br \/>\n    const angle = (hour - 6) * (Math.PI \/ 12);<br \/>\n    this.sunLight.position.set(<br \/>\n      Math.cos(angle) * 100,<br \/>\n      Math.sin(angle) * 100,<br \/>\n      50<br \/>\n    );<\/p>\n<p>    \/\/ Adjust sun color temperature<br \/>\n    if (hour < 6 || hour > 20) {<br \/>\n      \/\/ Night: dim blue<br \/>\n      this.sunLight.color.setHex(0x2244aa);<br \/>\n      this.sunLight.intensity = 0.1;<br \/>\n      this.scene.background = new THREE.Color(0x001133);<br \/>\n    } else if (hour < 8 || hour > 18) {<br \/>\n      \/\/ Dawn\/dusk: warm orange<br \/>\n      this.sunLight.color.setHex(0xff8844);<br \/>\n      this.sunLight.intensity = 0.5;<br \/>\n      this.scene.background = new THREE.Color(0xff6633);<br \/>\n    } else {<br \/>\n      \/\/ Day: bright white<br \/>\n      this.sunLight.color.setHex(0xffffff);<br \/>\n      this.sunLight.intensity = 0.9;<br \/>\n      this.scene.background =<\/p>","protected":false},"excerpt":{"rendered":"<p>Learn how a developer built an explorable 3D voxel map of Warsaw. Master Three.js, WebGL optimization, and interactive 3D rendering techniques.<\/p>","protected":false},"author":2,"featured_media":1143,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":"","_yoast_wpseo_title":"Building Interactive 3D Voxel Maps in the Browser with Three.js and WebGL - Networkyy","_yoast_wpseo_metadesc":"Learn how a developer built an explorable 3D voxel map of Warsaw. Master Three.js, WebGL optimization, and interactive 3D rendering techniques.","_yoast_wpseo_focuskw":"3D voxel maps","rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":""},"categories":[1],"tags":[],"class_list":["post-1144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"contentshake_article_id":"","brizy_media":[],"_links":{"self":[{"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/posts\/1144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/comments?post=1144"}],"version-history":[{"count":0,"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/posts\/1144\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/media\/1143"}],"wp:attachment":[{"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/media?parent=1144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/categories?post=1144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/networkyy.com\/fr\/wp-json\/wp\/v2\/tags?post=1144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}