Cannot read property 'subtractToRef' of undefined


With some models I get the following error when trying to move the camera with my mouse

ray.ts:188 Uncaught TypeError: Cannot read property 'subtractToRef' of undefined
    at Ray.intersectsTriangle (ray.ts:188)
    at SubMesh._intersectTriangles (subMesh.ts:490)
    at SubMesh.intersects (subMesh.ts:408)
    at Mesh.AbstractMesh.intersects (abstractMesh.ts:1497)
    at Scene.push../node_modules/@babylonjs/core/Culling/ray.js._scene__WEBPACK_IMPORTED_MODULE_4__.Scene._internalPick (ray.ts:680)
    at Scene.push../node_modules/@babylonjs/core/Culling/ray.js._scene__WEBPACK_IMPORTED_MODULE_4__.Scene.pick (ray.ts:738)
    at HTMLCanvasElement._onPointerDown (scene.inputManager.ts:661)

I tried recreating it in a playground, but I wasn’t able to recreate it which leads me to believe that it’s a lifecycle issue issue in React or a BabylonJS engine issue.

Is anybody able to give me a hint to lead me in the right direction?

Update: I found a similar issue (Error when clicking on point-cloud mesh without indices), but there was no mention of what the fix was. Maybe it’s related to that?

Also I realized that this issue usually occurs when clicking on the model when trying to rotate the camera. Even though my playground doesn’t reproduce the error, it should give you an idea of what my code is doing.

Lastly I should mention that I’m using views since I’m using multiple canvases so maybe that has something to do with it

Nevermind I figured it out. It was an oversight. The crash was caused by this line in my code:


I add those 2 lines to reduce the memory footprint as per the documentation (Reduce memory footprint - Babylon.js Documentation), but I didn’t realize it would cause this error.

1 Like