How can I get positions of an object within an imported object?

How can I get positions of an object within an imported object? The object itself has a few thousand children but since its a single import object, all the children have a position of 0,0,0 inherited from the parent I assume.

I tried to add the children to the scene one by one on import but it didn’t appear they all added to the scene correctly. Even the ones imported still had a position of 0,0,0.

I also tried to detach them from their parent but still some object didn’t seem to add.

The goal is to add menu options for TWEENing to an object if clicked. The imported object was created in 3DMax

Here is the code, quite basic at this point

import './style.css'
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
import {MTLLoader} from 'three/examples/jsm/loaders/MTLLoader';
import {OBJLoader} from 'three/examples/jsm/loaders/OBJLoader';

const mtlLoader = new MTLLoader()

/**
 * Base
 */
// Canvas
const canvas = document.querySelector('canvas.webgl')

// Scene
const scene = new THREE.Scene()

const light = new THREE.AmbientLight(0x404040); // soft white light
light.intensity = 15
scene.add(light)

/**
 * Sizes
 */
const sizes = {
    width: window.innerWidth,
    height: window.innerHeight
}

window.addEventListener('resize', () => {
    // Update sizes
    sizes.width = window.innerWidth
    sizes.height = window.innerHeight

    // Update camera
    camera.aspect = sizes.width / sizes.height
    camera.updateProjectionMatrix()

    // Update renderer
    renderer.setSize(sizes.width, sizes.height)
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})

/**
 * Camera
 */

const camera = new THREE.PerspectiveCamera(45, sizes.width / sizes.height, 0.1, 10500000)
camera.position.z = 2000
camera.position.y = 15
scene.add(camera)


// Controls
const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true


/**
 * Renderer
 */
const renderer = new THREE.WebGLRenderer({
    canvas: canvas,
    logarithmicDepthBuffer: true
})

renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))

//Load OBJ

window.addEventListener('load', function () {

    mtlLoader.load(
        '/import/stadium.mtl',
        (materials) => {
            materials.preload()

            const objLoader = new OBJLoader()
            objLoader.setMaterials(materials)
            objLoader.load(
                '/import/stadium.obj',
                (object) => {
                    console.log(object)
                    scene.add(object)
                },
                (xhr) => {
                    console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
                },
                (error) => {
                    console.log(error)
                }
            )
        },
        (xhr) => {
            console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
        },
        (error) => {
            console.log(error)
        }
    )
})

const clock = new THREE.Clock()

const tick = () => {
    const elapsedTime = clock.getElapsedTime()

    // Update controls
    controls.update()

    // Render
    renderer.render(scene, camera)

    // Call tick again on the next frame
    window.requestAnimationFrame(tick)
}

tick()


How can I get positions of an object within an imported object?

Normally you would use Object3D.getWorldPosition() to extract the position of a 3D object in world space. This position will honor the transformation of the object itself as well as the transformation of all ancestor nodes.

If you don’t get the expected result, the vertices of the object’s geometry are probably shifted (meaning an offset is applied).

I tried to get world position from the children after object load but still got 0,0,0

const getWorld = () =>{
    scene.children[2].children.forEach(child => {
        let vector = new THREE.Vector3()
        child.getWorldPosition(vector)
        console.log(vector)

             {
          "x": 0,
          "y": 0,
          "z": 0
             }

    })
}

I will look into the offset, Thanks

I was able to get positions from the buffer attributes, although it wouldn’t be centered.

const getWorld = () =>{
    scene.children[2].children.forEach(child => {
        let vector = new THREE.Vector3()
        vector.fromBufferAttribute(child.geometry.getAttribute('position'), 0)
        console.log(vector)

          {
              "x": 94165.53125,
              "y": 49701.47265625,
              "z": 2253.197021484375
          }
    })
}

I think maybe using boundSphere.center would work so no offset would be applied?