Forge新手求助:如何通过ID获取对象坐标或Three.js Vector?
Hey there! As someone who’s spent a fair amount of time tinkering with Forge Viewer, I totally get the confusion when you’re just starting out and need to grab spatial data like coordinates or Three.js vectors from specific objects (like those house walls you mentioned). Let’s walk through exactly how to do this, step by step.
Step 1: Confirm your setup
First, you’ll need two key things:
- Access to your Forge Viewer instance (this is the main object that renders your model)
- The
dbIdof the wall (or target object) you want to query. You can get thisdbIdby selecting the object with the viewer’s default selection tool, or by running model property queries if you know the object’s metadata.
Step 2: Fetch the object’s bounding box
Forge stores object geometry in fragments, so to get the world-space bounds of your object, you’ll use the viewer’s fragment list. We’ll leverage Three.js’s Box3 class (which Forge uses under the hood) to capture these bounds:
// Replace with your actual Viewer instance and target object's dbId const viewer = window.myForgeViewer; const targetDbId = 456; // Example dbId for your wall // Create an empty Three.js Box3 to hold the bounding box data const worldBounds = new THREE.Box3(); // Retrieve the world-space bounding box for the target object viewer.model.getFragmentList().getWorldBounds(targetDbId, worldBounds);
Step 3: Extract coordinates as Three.js Vectors
Once you have the bounding box, you can pull out all kinds of useful vector data for your wall:
- Center point: Get the exact center of the wall using
getCenter():const centerVector = worldBounds.getCenter(new THREE.Vector3()); console.log("Wall center coordinates:", centerVector); - Extreme corners: Grab the min/max corners to understand the full extent of the wall:
const minCorner = worldBounds.min; // Bottom-left-back corner of the wall const maxCorner = worldBounds.max; // Top-right-front corner of the wall console.log("Wall min coordinates:", minCorner); console.log("Wall max coordinates:", maxCorner);
Quick additional tips
- Wait for model loading: Always run this code after the model has fully loaded. You can wrap it in a listener to avoid errors:
viewer.addEventListener('geometry.loaded', () => { // Place your bounding box/vector code here }); - Local vs. World space: If you need coordinates relative to the wall itself (not the entire scene), swap
getWorldBounds()withgetLocalBounds(). - Complex objects: Even if your wall is made up of multiple geometry fragments,
getWorldBounds()will combine them into a single bounding box for the entire object.
内容的提问来源于stack exchange,提问作者Artem Van

