This Chrome DevTools extension provides debugging capabilities for Three.js applications. It allows you to inspect scenes, objects, materials, and renderers.
-
Development Mode:
- Open Chrome and navigate to
chrome://extensions/ - Enable "Developer mode" (toggle in the top-right corner)
- Click "Load unpacked" and select the
devtoolsdirectory - The extension will now be available in Chrome DevTools when inspecting pages that use Three.js
- Open Chrome and navigate to
-
Usage:
- Open Chrome DevTools on a page using Three.js (F12 or Right-click > Inspect)
- Click on the "Three.js" tab in DevTools
- The panel will automatically detect and display Three.js scenes and renderers found on the page.
The extension follows a standard Chrome DevTools extension architecture:
- Background Script (
background.js): Manages the extension lifecycle and communication ports between the panel and content script. - DevTools Script (
devtools.js): Creates the panel when the DevTools window opens. - Panel UI (
panel/panel.html,panel/panel.js,panel/panel.css): The DevTools panel interface that displays the data. - Content Script (
content-script.js): Injected into the web page. Relays messages between the background script and the bridge script. - Bridge Script (
bridge.js): Injected into the page's main world via the manifest. Directly interacts with the Three.js instance, detects objects, gathers data, and communicates back via the content script.
- When a page loads, Chrome injects
bridge.jsinto the page's main world (including iframes). bridge.jscreates thewindow.__THREE_DEVTOOLS__global object.- When the DevTools panel is opened,
panel.jsconnects tobackground.js(init) and immediately requests the current state (request-state). background.jsrelays the state request tocontent-script.js, which posts it tobridge.js.bridge.jsresponds by sending back observed renderer data (renderermessage) and batched scene data (scenemessage).- Three.js detects
window.__THREE_DEVTOOLS__and sends registration/observation events to the bridge script as objects are created or the library initializes.
The bridge acts as the communication layer between the Three.js instance on the page and the DevTools panel:
-
Event Management: Creates a custom event target (
DevToolsEventTarget) to manage communication readiness and backlog events before the panel connects. -
Object Tracking:
getObjectData(): Extracts essential data (UUID, type, name, parent, children, etc.) from Three.js objects.- Maintains a local map (
devTools.objects) of all observed objects.
-
Initial Observation & Batching:
- When Three.js sends an
observeevent (viawindow.__THREE_DEVTOOLS__.dispatchEvent):- If it's a renderer, its data is collected and sent immediately via a
'renderer'message. - If it's a scene, the bridge traverses the entire scene graph, collects data for the scene and all descendants, stores them locally, and sends them to the panel in a single
'scene'batch message.
- If it's a renderer, its data is collected and sent immediately via a
- When Three.js sends an
-
State Request Handling:
- When the panel sends
request-state(on load/reload), the bridge iterates its known objects and sends back the current renderer data ('renderer') and scene data ('scene'batch).
- When the panel sends
-
Message Handling:
- Listens for messages from the panel (relayed via content script) like
request-state.
- Listens for messages from the panel (relayed via content script) like
The panel UI provides the visual representation of the Three.js objects:
- Tree View: Displays hierarchical representation of scenes and objects.
- Renderer Details: Shows properties and statistics for renderers in a collapsible section.
- Scene Hierarchy Visualization: Browse the complete scene graph.
- Object Inspection: View basic object properties (type, name).
- Renderer Details: View properties, render stats, and memory usage for
WebGLRendererinstances.
- Panel ↔ Background ↔ Content Script: Standard extension messaging for panel initialization and state requests (
init,request-state). - Three.js → Bridge: Three.js detects
window.__THREE_DEVTOOLS__and uses itsdispatchEventmethod (sending'register','observe'). - Bridge → Content Script: Bridge uses
window.postMessageto send data ('register','renderer','scene','update') to the content script. - Content Script → Background: Content script uses
chrome.runtime.sendMessageto relay messages from the bridge to the background. - Background → Panel: Background script uses the established port connection (
port.postMessage) to send data to the panel.
- DevToolsEventTarget: Custom event system with backlogging for async loading.
- Object Observation & Batching: Efficiently tracks and sends scene graph data.
- Renderer Property Display: Shows detailed statistics for renderers.
The extension relies on Three.js having built-in support for DevTools. When Three.js detects the presence of window.__THREE_DEVTOOLS__, it interacts with it, primarily by dispatching events.
The bridge script listens for these events, organizes the data, and provides it to the DevTools panel.
To modify the extension:
- Edit the relevant files in the
devtoolsdirectory. - Go to
chrome://extensions/, find the unpacked extension, and click the reload icon. - Close and reopen DevTools on the inspected page to see your changes.