The ‘mesh’ position is always 0,0,0. But you can access it’s center position by accessing the bounding box parameters that define mesh extents within the world space. So within your script you could add additional variable ‘meshCenter’ like this:
After looking at your scene and your code I have noticed that you are using “extension.activate()” which is not correct. Proper extension activation is made by using “extension.trigger()”. Then your script should work:
<script>
const viewer = WALK.getViewer();
let extension = null;
let cameraWithinDistance = false;
function triggerExtension() {
if (extension) {
extension.trigger();
}
}
function checkProximity() {
const cameraPosition = viewer.getCameraPosition();
const nodes = viewer.findNodesOfType('Cube');
if (nodes.length > 0) {
const mesh = nodes[0].mesh;
const meshCenter = mesh.geometry.boundingSphere.center;
const distance = cameraPosition.distanceTo(meshCenter);
if (distance < 2 && !cameraWithinDistance) {
cameraWithinDistance = true;
triggerExtension();
}
if (distance >= 2 && cameraWithinDistance) {
cameraWithinDistance = false;
// Uncomment line below to triggerExtension again after moving away.
// triggerExtension();
}
}
requestAnimationFrame(checkProximity);
}
function onSceneLoadComplete() {
extension = viewer.getExtension('SwitchObjects', '1');
requestAnimationFrame(checkProximity);
}
viewer.onSceneLoadComplete(onSceneLoadComplete);
</script>