Hi there, I’m using this
var nearPlacements = [];
var closestDistance = 30;
playerPosition = this.player.getPosition();
var placements = this.app.root.findByTag('Placement');
placements.forEach( function(placementsEntity){
var distance = playerPosition.distance(placementsEntity.getPosition());
if (distance < closestDistance) {
nearPlacements.push(placementsEntity);
}
}.bind(this));
if (nearPlacements.length == 0) {
console.log('No near entities');
//Set checker variable to 'True'
} else {
var totalTriangles = 0;
for (var i = 0; i < nearPlacements.length; i++) {
if (nearPlacements[i].model) {
var meshInstances = nearPlacements[i].model.model.meshInstances;
var mat;
var matEach = 0;
for (var i2 = 0; i2 < meshInstances.length; i2++) {
mat = parseFloat(meshInstances[i2].mesh.indexBuffer[0].numIndices / 3);
matEach += mat;
}
totalTriangles += matEach;
}}
console.log(totalTriangles);
}
To calculate meshInstances of objects near the player, the problem it fails sometimes if models are unloading I think
Uncaught TypeError: Cannot read properties of null (reading ‘meshInstances’)
On this line:
var meshInstances = nearPlacements[i].model.model.meshInstances;
Shouldn’t if (nearPlacements[i].model)
{ fix the problem?