Mesh exists in the scene but is not intersectable #2179
Replies: 3 comments
-
Refs are populated in time for useEffect and (newer) three versions should recursively intersect your model. Can you create a codesandbox with your setTimeout workaround? This is 100% a react/three issue if you're not using our built-in events. |
Beta Was this translation helpful? Give feedback.
-
similar problem here: #2159 you need to make sure the matrices are calculated, gl.render calls updateMatrixWorld. i believe react hooks are scheduled to fire first, so the meshes exist, and they are part of the scene (you can easily verify that with a log) but they haven't been touched by the renderer yet. i believe updateMatrix is not enough, isn't that just he relative position? |
Beta Was this translation helpful? Give feedback.
-
Unfortunately I'm unable to do a sandbox currently but I can show the relevant code useEffect(() => {
function findBubbles(): void {
const mesh = getMeshRef();
const startPosition = new Vector3();
const rcDirection = new Vector3();
const bubblePairs = bubbleIds
.map((id) => {
const bubbleMesh = scene.getObjectByName(bubbleId);
const bubble= bubbleById[bubbleId];
startPosition.fromArray(/* arbitrary Vector3 from other part of system */);
rcDirection.subVectors(bubbleMesh.position, startPosition).normalize();
raycaster.set(startPosition, rcDirection);
const intersects = raycaster.intersectObjects([bubbleMesh, mesh]);
// more code that decides what to do based on raycast results
})
}
// setTimeout(findBubbles, 0);
findBubbles();
}, [...]); Neither const getMeshRef = useCallback(() => {
return meshRef.current;
}, []);
return (
<>
<mesh
ref={meshRef}
name="model"
material={ctx.assets.material}
geometry={ctx.assets.geometry}
matrixAutoUpdate={false}
visible={isMeshVisible}
onClick={handleClick}
onPointerDown={handlePointerDown}
/>
{children && children(getMeshRef)}
</>
); The first code block is a child of the second, and receives As an aside, I guess there isn't a better approach than this? I couldn't find a way to do this reactively instead of imperatively |
Beta Was this translation helpful? Give feedback.
-
Hello. I'm maybe 90% sure I'm having some trouble with a r3f interaction and not a three.js interaction.
I have a not huge obj mesh of about 33.5 MB that I'm trying to imperatively raycast on as soon as it's available via a ref and a
useEffect
hook. At the time of callingintersectObject
, I am confirming that the ref is pointing at aMesh
instance and I've also rendered anarrowHelper
to confirm that the ray is properly placed. Unfortunately I'm not getting an intersection despite thearrowHelper
shooting right through it. If I wrap the intersection function insetTimeout
with a 0 delay, it solves the problem.Seems fairly clear that it's a race condition of some sort. Is it possible that a
<mesh ref={...} />
would populate the ref without being in the scene yet? I'd like to determine if there's a shortcoming my code and if there's a geometry/mesh load event that I can use, or if it's something with the library itselfSome side details:
6.2.3
currently with no option to upgradematrixAutoUpdate
as false on the mesh but I've confirmed I callupdateMatrix
on the mesh ref before raycastingApologies if this is the wrong place for this, I thought it sat on the border of issue and tutorial help. Thanks for the library you've put out, it's vastly improved the OOP spaghetti I had before
Beta Was this translation helpful? Give feedback.
All reactions