I’m using three-mesh-bvh and I’ve run into some issues with its extensions to ‘three’. Since it’s exporting BufferGeometry and Raycaster as interfaces rather than classes, anywhere I construct new BufferGeometry() or new Raycaster() fails “compilation”.
Is there a new or specific way I’m supposed to construct these when using three-mesh-bvh?
@gkjohnson
Any chance you could provide a code snippet of how you’re importing and trying to create these as well as any console logs you’re getting? A minimal live repro on codepen, js fiddle or codesandbox would be ideal if possible
2 Likes
package.json
"dependencies": {
...
"@types/three": "^0.166.0"
"three": "^0.166.0",
"three-mesh-bvh": "^0.8.2",
}
In code
import { BufferAttribute, BufferGeometry, Vector3 } from 'three'
bufferGeometry(points: Vector3[], facets: Vector3[])
{
const geometry = new BufferGeometry();
geometry.setAttribute(
'position',
new BufferAttribute(new Float32Array(points.flatMap(point => [point.x, point.y, point.z])), 3),
);
geometry.setIndex(facets.flatMap(facet => [facet.x, facet.y, facet.z]));
if (points.length > 0)
{
geometry.computeVertexNormals();
geometry.computeBoundsTree();
}
return geometry;
}
Gives me the error
error TS2693: ‘BufferGeometry’ only refers to a type, but is being used as a value here.
17 const geometry = new BufferGeometry();
It’s fine if I import from ‘three/src/Three’ instead or don’t install three-mesh-bvh/use computeBoundsTree()
I can see the issue in three-mesh-bvh’s index.d.ts
declare module 'three' {
export interface BufferGeometry {
boundsTree?: MeshBVH;
computeBoundsTree: typeof computeBoundsTree;
disposeBoundsTree: typeof disposeBoundsTree;
}
export interface BatchedMesh {
boundsTrees?: Array<MeshBVH | null>;
computeBoundsTree: typeof computeBatchedBoundsTree;
disposeBoundsTree: typeof disposeBatchedBoundsTree;
}
export interface Raycaster {
firstHitOnly?: boolean;
}
}
So I’m wondering if this is intended and there’s a different way to do it when using three-mesh-bvh
I’m not a typescript expert but these types have been in the project for years and no one has complained and have otherwise been contributed to by typescript users so I’m not sure why this wouldn’t be working unless something has changed in newer versions of typescript. The typescript docs demonstrate modifying a type using “interface” here, as well.
1 Like