I create mathematical geometries. I would like to create suitable boundingSperes for these. This works if I set the boundingSphere during initialization.
_Init(params) {
this.geometry_ = new THREE.BufferGeometry();
this.mesh_ = new THREE.Mesh(this.geometry_, params.material);
const boundingSphere = new THREE.Sphere(this.params_.boundingPosition, this.params_.width * 2);
this.mesh_.geometry.boundingSphere = boundingSphere;
this.mesh_.geometry.computeBoundingSphere();
}
But it would be even better if I could use the arithmetic mean of all vertices as the center of the boundingSphere. Since I can do this at the same time as creating the geometry, calculating the center of gravity of the geometry is a simple matter.
What doesn’t work is creating the boundingSphere after the init.
If I later call the exact same 3 lines as in the init just for a test:
const boundingSphere = new THREE.Sphere(this.params_.boundingPosition, this.params_.width * 2);
this.mesh_.geometry.boundingSphere = boundingSphere;
this.mesh_.geometry.computeBoundingSphere();
the boundigSperes no longer work.
I quickly notice this when I turn the camera a little and then geometries disappear from view. Do I need to set boundingSpheres in a different way after init?
P.S:
I had the values displayed in text fields so that I could better see what was going on.
const center = new THREE.Vector3(data.center[0], data.center[1], data.center[2]);
const boundingSphere = new THREE.Sphere(center.clone(), this.params_.width);
this.mesh_.geometry.boundingSphere = boundingSphere;
this.mesh_.geometry.computeBoundingSphere();
document.getElementById("testfield19").value = center.x;
document.getElementById("testfield20").value = center.y;
document.getElementById("testfield21").value = center.z;
document.getElementById("testfield25").value = this.mesh_.geometry.boundingSphere.center.x;
document.getElementById("testfield26").value = this.mesh_.geometry.boundingSphere.center.y;
document.getElementById("testfield27").value = this.mesh_.geometry.boundingSphere.center.z;
document.getElementById("testfield22").value = this.params_.width;
document.getElementById("testfield28").value = this.mesh_.geometry.boundingSphere.radius;
Top line center(xyz) testfield(19, 20, 21) and radius testfield(22).
The bottom line is the read values of the boundingSphere center(xyz) testfield(25, 26, 27) and radius testfield(28).
That doesn’t even begin to agree. And if I don’t clone the center vector, the center vector is changed by the boundingSphere instead of the boundingSphere taking over the values.
If I do the whole thing at the beginning in the init, the values match and my center vector and radius are not influenced by the boundingSphere. The same lines of code produce different results depending on where I execute them (init or update). The fact that my center values and radius are changed by the boundingSphere when I run it in the update instead of the boundingSphere taking them over is definitely not correct.