Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
object3D.traverse(obj => {
// note that we might already have a bounds tree if this was a clone of an object with one
const hasBufferGeometry = obj.isMesh && obj.geometry.isBufferGeometry;
const hasBoundsTree = hasBufferGeometry && obj.geometry.boundsTree;
if (hasBufferGeometry && !hasBoundsTree && obj.geometry.attributes.position) {
const geo = obj.geometry;
const triCount = geo.index ? geo.index.count / 3 : geo.attributes.position.count / 3;
// only bother using memory and time making a BVH if there are a reasonable number of tris,
// and if there are too many it's too painful and large to tolerate doing it (at least until
// we put this in a web worker)
if (triCount > 1000 && triCount < 1000000) {
// note that bounds tree construction creates an index as a side effect if one doesn't already exist
geo.boundsTree = new MeshBVH(obj.geometry, { strategy: 0, maxDepth: 30 });
}
}
});
}
function generateHeightfield(geometry, params) {
geometry.computeBoundingBox();
const size = new THREE.Vector3();
geometry.boundingBox.getSize(size);
const bvh = new MeshBVH(geometry);
const heightfieldMesh = new THREE.Mesh(geometry);
const maxSide = Math.max(size.x, size.z);
const distance = params.hasOwnProperty("distance") ? params.distance : Math.max(0.25, Math.pow(maxSide, 1 / 2) / 10);
const resolution = Math.ceil(maxSide / distance);
const data = [];
const offset = new THREE.Vector3();
geometry.boundingBox.getCenter(offset);
const down = new THREE.Vector3(0, -1, 0);
const position = new THREE.Vector3();
const raycaster = new THREE.Raycaster();