Raycaster Inaccurate When Objects Have LineSegments

After loading my gltf file I apply a MeshLambertMaterial to all of the parts and I also create LineSegments for the geometry and add these to the meshes so that I can apply a LineBasicMaterial to them as follows:

              // Ensure the gltfPart is a Mesh.
              if (child instanceof THREE.Mesh) {

                // Check to see if the edges have already been calculated
                if (child.children[0] instanceof THREE.LineSegments) {

                  // Set the material of the lineSegments.
                  let childLineSegments: any = child.children[0];
                  childLineSegments.material = threeLineBasicMaterial;

                else {

                  // Remove any existing children.
                  child.children = [];

                  // Add the edges.
                  const edges = new THREE.EdgesGeometry(child.geometry, this.materialLineBreakAngle);
                  const lines = new THREE.LineSegments(edges, threeLineBasicMaterial);


                // Apply the material.
                child.material = threeMeshLambertMaterial;

I have found that this badly affects raycasting to identify which object the user has clicked on.

Currently I check the array of intersects and if the closest object clicked on is not a Mesh, often a LineSegement, I get the parent and test if this is a Mesh and if so use that. However it’s very hit and miss (excuse the pun) and many objects just aren’t detected.

I temporarily removed the LineSegments and the raycasting worked fine so it’s not my maths on the mouse position or anything else.

Is there any way to ignore the LineSegments which seem to be obstructing the ray from some objects please?

When you create an instance of Raycaster. Add this line: raycaster.params.Line.threshold = 0.1
The less value you set, the more precision you get. Play with this parameter.

1 Like

Hi, that’s interesting. I’ll give it a go and report back. Curiously I tried to create an array of simple Meshes with no LineSegment children to pass to the raycaster but it didn’t seem to help.