You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

How to Get Coordinates/Three.js Vectors from a Forge Object by ID

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 dbId of the wall (or target object) you want to query. You can get this dbId by 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() with getLocalBounds().
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:12:47