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()