Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ray intersects mesh instances #989

Closed

Conversation

scarletsky
Copy link
Contributor

@scarletsky scarletsky commented Aug 22, 2017

This is a solution for https://forum.playcanvas.com/t/way-to-get-mesh-instance/4646 .
And the code is migrated from THREE.js 😃

Usage is learned from app.systems.rigidbody.raycastFirst:

var meshInstances = entity.model.model.meshInstances;
app.mouse.on('mousemove', function(e) {
    camera.camera.screenToWorld(e.x, e.y, camera.camera.nearClip, fromPoint);
    camera.camera.screenToWorld(e.x, e.y, camera.camera.farClip, toPoint);
    ray.origin.copy(fromPoint);
    ray.direction.copy(toPoint).sub(ray.origin).normalize();
  
    var results = app.systems.camera.raycastMeshInstances(ray, meshInstances);
    console.log(results);
});

The result:

[
    {
        index: Number, // the index of triangle based on IndexBuffer
        distance: Number, // the distance between ray.origin and the hit point
        point: Vec3, // the hit point of world
        localPoint: Vec3, // the hit point of local
        normal: Vec3, // the triangle's normal
        vertices: [Vec3, Vec3, Vec3], // the triangles's vertices
        meshInstance: meshInstance // the meshInstance of this triangle
    }
]

I've made a demo in https://scarletsky.github.io/engine/, you can open the console and see the performance and the result.

@scarletsky scarletsky force-pushed the ray-intersects-meshinstance branch 2 times, most recently from 21fc044 to ad84c0d Compare August 23, 2017 12:59
@scarletsky scarletsky force-pushed the ray-intersects-meshinstance branch from 987a57c to 19d239c Compare November 3, 2017 01:52
@willeastcott willeastcott added area: physics Physics related issue and removed area: physics Physics related issue labels Apr 15, 2020
@willeastcott willeastcott added area: graphics Graphics related issue enhancement labels Apr 28, 2020
Comment on lines +302 to +303
vertexBuffer.unlock();
indexBuffer.unlock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to unlock, as we read only from it .. otherwise gpu version is updated

@willeastcott
Copy link
Contributor

@mvaligursky + @slimbuck: This is our oldest PR (nearly 3 years!!). We need to figure out what to do with it.

My personal positive feelings are:

  1. It's cool to be able to raycast without having to depend on the physics engine.
  2. It's cool that this PR works with skinned mesh instances.
  3. Code looks reasonably straightforward and clean.

And negative feelings are:

  1. What about batched stuff?
  2. What about morphed mesh instances?
  3. Is performance good enough?

@mvaligursky mvaligursky self-assigned this Apr 29, 2020
@scarletsky
Copy link
Contributor Author

@willeastcott
This PR just scan all triangles of mesh, and its performance may not so good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: graphics Graphics related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants