Comments (9)
In case anyone trying to make raycasts work from Typescript reads this, here's a simple and quick solution:
import { MeshBVH, acceleratedRaycast } from 'three-mesh-bvh/build/index.module.js';
THREE.Mesh.prototype.raycast = acceleratedRaycast;
class MBG extends THREE.BufferGeometry {
boundsTree: any;
constructor(bg: THREE.BufferGeometry) {
super();
this.copy(bg);
this.boundsTree = new MeshBVH(this as THREE.BufferGeometry);
};
}
instead of using Typescript's awful "declare module" -syntax, and then, for instance:
const tkbg = new THREE.TorusKnotBufferGeometry();
const tkbgm = new MBG(tkbg);
const testmesh = new THREE.Mesh(tkbgm, new THREE.MeshBasicMaterial({ color: "red", wireframe: true }));
scene.add(testmesh);
and then one can testmesh.raycast(...) as usual, it will pick up the attached boundsTree.
from three-mesh-bvh.
Adding a noEmit
compiler check sounds like a good way to get started with tests. I'm no expert either! I think the only challenge will be staying compatible with THREE itself as it progresses through versions.
The prototype modification shouldn't be an issue, but I'll investigate whether it's possible to modify the Raycaster
type when this library is installed.
I'll see what I can do over the weekend :)
from three-mesh-bvh.
Working branch: master...crabmusket:typechecking
Turns out the prototype modifications aren't a problem 👍
from three-mesh-bvh.
Go ahead and merge, I've had an extra busy time at work so haven't been able to progress on this for a bit. I'll adapt once those changes are merged :)
from three-mesh-bvh.
Fixed in #338
from three-mesh-bvh.
I'm happy to take ownership of this one. What's your preferred strategy for dealing with types? I assume just adding a src/index.d.ts
file covering the main exports, making sure to be compatible with THREE's core types? Do you have any thoughts on testing types?
from three-mesh-bvh.
Excellent! Thank you for the offer.
I assume just adding a src/index.d.ts file covering the main exports, making sure to be compatible with THREE's core types
That sounds like a good place to start. In other projects I've created d.ts sibling files for everything that was part of the public interface but I think the interface is small enough here that it's manageable in one file. There should be a separate types file for src/workers/GenerateMeshBVHWorker.js
, though, which isn't currently exported from index.js because Workers are not compatible with most build processes by default.
Do you have any thoughts on testing types?
I am not a typescript expert so I'm open to suggestions for validating types but on other projects I've just added a tsc
command to the lint script in the package.json with a noEmit
ts config file to make sure all the syntax is valid.
And you'll know better than me but if there's anything special that needs to be done in order to enable the prototype modification that the docs suggest or setting custom parameters on existing three.js types such as Raycaster.firstHitOnly it might be nice to add documentation for ts-specific use. Of course not required and anything you can contribute is appreciated!
from three-mesh-bvh.
Awesome! It's looking good. I didn't know about the declare module
syntax for modifying existing type definitions -- seems useful
from three-mesh-bvh.
Hey @crabmusket what are your plans for the type definitions? No rush I was just planning to merge the v0.4.0 (#205) possibly in the next few weeks which introduces some API adjustments so I didn't want to make changes out from under you if you were planning to make a PR!
from three-mesh-bvh.
Related Issues (20)
- Fix validation function for use with "indirect" bvh
- "Depth" can be stored in the node buffer which may allow for finding parent?
- MeshBVH: Allow for parallelized refit
- Perf test linear refitting HOT 1
- Make including / copying the workers easier HOT 1
- I encounter an unknown error when creating ParallelMeshBVHWorker in a Vite environment. HOT 3
- Iterative closest points algorithm implemantion using three-mesh-bvh HOT 1
- Adjusting bvh options in inspector demo seems to do nothing
- Triangle Painting - How does it work? HOT 1
- Some examples throwing an exception (Failed to construct 'URL') HOT 1
- StaticSceneGenerator: Fail gracefully when there's no geometry HOT 1
- StaticGeometryGenerator: Invert triangles if the scale is negative
- StaticGeometryGenerator: Simplify by using Mesh.getVertexPosition
- Help wanted. HOT 5
- StaticGeometryGenerator: Auto reset if geometry changes are detected
- StaticGeometryGenerator: Make handling of attributes more robust
- StaticGeometryGenerator: Support multimaterial meshes
- Does `MeshBVH.serialize` generate data for saving in `.bvh` file format? HOT 1
- Provide a CompoundMeshBVH class
- You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from three-mesh-bvh.