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

useBVH (Bounding Volume Hierarchy) composable #604

Open
4 tasks done
alvarosabu opened this issue Mar 24, 2025 · 0 comments
Open
4 tasks done

useBVH (Bounding Volume Hierarchy) composable #604

alvarosabu opened this issue Mar 24, 2025 · 0 comments
Labels
p4-important-bug Violate documented behavior or significantly improve performance (priority)

Comments

@alvarosabu
Copy link
Member

alvarosabu commented Mar 24, 2025

Description

As a developer using TresJS,
I want a straightforward way to enable a bounding volume hierarchy (BVH) on my 3D meshes and geometries,
so that I can benefit from drastically improved raycasting performance and more efficient collision detection in complex scenes.

Reason / Use Cases

  • Performance Gains: A BVH can accelerate raycasting operations, especially for large/complex geometry.

  • Collision Detection: Many real-time applications (e.g. games, simulations) need fast collision checks; BVH can help optimize these.

  • Simplicity & Consistency: Having a composable approach in TresJS aligns with the rest of the API, keeping logic encapsulated and Vue-friendly.

Suggested solution

In the appropriate module (e.g.,/useBVH.ts or similar), we could provide:

Tip

Check R3F Drei's implementation https://drei.docs.pmnd.rs/performances/bvh

import { ref, onMounted } from 'vue'
// Possibly import { MeshBVH, MeshBVHVisualizer } from 'three-mesh-bvh' 
// or any relevant utility from three-mesh-bvh

export function useBVH(geometryRef) {
  const bvhRef = ref(null)

  onMounted(() => {
    // Pseudocode: 
    // 1. Create or update the BVH
    // 2. Store the result in bvhRef
    // 3. Optionally expose utilities for intersection, collision checks, etc.

    // Example:
    // bvhRef.value = new MeshBVH(geometryRef.value)
  })

  // Return whatever is helpful for the user:
  // e.g. the BVH instance, methods for raycasting, collision checks, etc.
  return {
    bvhRef,
    // raycastWithBVH, collisionCheck, etc.
  }
}

Reactive Updates

Optionally watch for changes in the provided geometry or the mesh and re-build BVH if necessary.

Alternative

No response

Additional context

  • Potentially wrap three-mesh-bvh or another library under the hood.
  • Consider how to handle dynamic geometries versus static ones (e.g., partial updates vs. full rebuilds).
  • Align the API style with other TresJS composables (useXyz naming, returning refs or methods, etc.).

Validations

@alvarosabu alvarosabu added the p4-important-bug Violate documented behavior or significantly improve performance (priority) label Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-important-bug Violate documented behavior or significantly improve performance (priority)
Projects
Status: No status
Development

No branches or pull requests

1 participant