Loading and Unloading entities with regard to camera distance

Hello,

I am attempting to implement a feature to bypass the lag/expense of loading in large 3D tilesets by displaying them as glTF files instead. The only issue is if I load many, they don’t unload like 3D tiles do when the camera position is past a certain distance away. I believe most of my idea is correct (see below), however, the distance is wildly off which I believe may be due to some inaccuracy in the position. I would appreciate any assistance.

const testy = Cesium.Cartesian3.fromDegrees(-80, 25,0);
let hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(0), Cesium.Math.toRadians(90), Cesium.Math.toRadians(-90));
var modelOr = Cesium.Transforms.headingPitchRollQuaternion(testy, hpr);

try {
const entity = viewer.entities.add({
id: ‘thingy’,
position: testy,
orientation: modelOr,
model: {
uri: ../../datasets/car-sift/odm_texturing/odm_textured_model_geo.glb,
},
});
viewer.trackedEntity = entity;
} catch (error) {
console.log(error);
}

function action(distance){
if (distance >= 65000){
viewer.entities.getById(‘thingy’).show = true;
}
else{
viewer.entities.getById(‘thingy’).show = true;
}
}

function calcDist(){
let camPos = viewer.camera.position;
console.log(Camera Pos: ${camPos});
console.log(Model Pos: ${testy});

var x1 = testy.x;
var y1 = testy.y;
var z1 = testy.z;

var x2 = camPos.x;
var y2 = camPos.y;
var z2 = camPos.z;

var dx = x2-x1;
var dy = y2-y1;
var dz = z2-z1;

console.log(dx, dy, dz: ${dx}, ${dy}, ${dz});

var sqrSum = Math.pow(dx, 2) + Math.pow(dy, 2) + Math.pow(dz, 2);
//console.log(sqrSum : ${sqrSum});

var distance = Math.sqrt(sqrSum);
console.log(Distance: ${distance});

action(distance);
return distance;
}

setInterval(calcDist, 5000);

Thank you,

I notice that when I select the entity and select the option ‘focus camera on object’ I appear to get correct distance readings. However, when this is not selected, the distance is off by orders of magnitude.

solved. viewer.flyTo(entity) rather than viewer.trackedEntity = entity