Teymoor
November 21, 2022, 5:22pm
1
I used this method to copy gltfmodel box to outside of gltfloader function
live example
const gltfBox2 = new THREE.Box3();
gltfLoader.load('https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Fox/glTF/Fox.gltf',(gltf) => {
gltf.scene.traverse((child) => {
gltf.scene.updateMatrixWorld(true);
if (child.isMesh) {
.
.
.
const gltfBox = new THREE.Box3().setFromObject(m);
gltfBox2.copy(gltfBox);
.
.
.
}
});
});
function updateBoxes() {
dummies.forEach((d, idx) => {
.
.
.
let box = new THREE.Box3().setFromObject(cube);
const intersectBox = gltfBox2.intersectsBox(box);
console.log(intersectBox);
});
}
Why intersectBox return false?
Teymoor
November 22, 2022, 9:06pm
2
Does anyone have any ideas?
Teymoor
November 23, 2022, 1:03am
3
I have updated the example,
As you can see in the example, .intersectBox returns true when the blue cube and other cubes intersect. The problem is with the gltf model, which returns false when it intersects with cubes
How can I solve it?
hey teymoor, I havne’t had a chance to look in any detail, but is it possible that you run your intersectBox function before the fox is loaded? you might have to change your logic so that you run intersectBox as a callback
2 Likes
Teymoor
November 23, 2022, 2:17pm
5
intersectBox It currently runs before loading the model.
I think I need to run “intersectBox” in the “updateBoxes” function to check the intersection of all cubes with the model. As I did now. Currently, this happens before the model is loaded.
Teymoor
November 23, 2022, 10:28pm
7
function updateBoxes(gltfBox) {
dummies.forEach((d, idx) => {
let box = new THREE.Box3().setFromObject(cube);
const intersectBox = box.intersectsBox(gltfBox);
console.log(intersectBox);
});
}
gltfLoader.load('https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Fox/glTF/Fox.gltf',(gltf) => {
gltf.scene.traverse((child) => {
gltf.scene.updateMatrixWorld(true);
if (child.isMesh) {
const gltfBox = new THREE.Box3().setFromObject(m);
updateBoxes(gltfBox);
}
});
});
I have update the code with your point, now “console.log(Intersect Box);” Returns true when cubes collide with the model, but I get this error :
it looks like you still call updateBoxes() outside of the scope? what happens if you remove that
Teymoor
November 23, 2022, 11:08pm
10
gsap.to(x, {
onUpdate: updateBoxes,
});
I have this code in the model click function
If I remove “updateBoxes()” outside the scope, animation does not work
You call the updateBoxes function outside the callback scope
In any case, I think we’ve solved your original problem
updateBoxes(); //try removing this function call
/**
* Renderer
*/
const renderer = new THREE.WebGLRenderer({
canvas: canvas
})
1 Like