Git Product home page Git Product logo

Comments (9)

Mathias-Fuchs avatar Mathias-Fuchs commented on May 21, 2024 3

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.

crabmusket avatar crabmusket commented on May 21, 2024 2

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.

crabmusket avatar crabmusket commented on May 21, 2024 1

Working branch: master...crabmusket:typechecking

Turns out the prototype modifications aren't a problem 👍

from three-mesh-bvh.

crabmusket avatar crabmusket commented on May 21, 2024 1

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.

gkjohnson avatar gkjohnson commented on May 21, 2024 1

Fixed in #338

from three-mesh-bvh.

crabmusket avatar crabmusket commented on May 21, 2024

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.

gkjohnson avatar gkjohnson commented on May 21, 2024

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.

gkjohnson avatar gkjohnson commented on May 21, 2024

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.

gkjohnson avatar gkjohnson commented on May 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.