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

LineSegments2 : Fix raytracing when the mesh has a transformation applied. #24405

Merged

Conversation

MixMasterMitch
Copy link
Contributor

Description

The LineSegments2 mesh has support for raytracing in "world units" mode. However, when checking for intersection with each line segment, the segments do not have the world matrix transformation applied. Therefore, the raytracing does not behave correctly if the mesh has any transformation applied to it. This PR fixes this by applying the world matrix to each line segment before checking for an intersection with the ray.

The behavior before and after can be reproduced by applying a transformation in the Line2 ray tracing example (/examples/#webgl_lines_fat_raycasting). Here is an example patch that can be used:

diff --git a/examples/webgl_lines_fat_raycasting.html b/examples/webgl_lines_fat_raycasting.html
index f923378abd..3067935181 100644
--- a/examples/webgl_lines_fat_raycasting.html
+++ b/examples/webgl_lines_fat_raycasting.html
@@ -327,7 +327,8 @@
                                        'visualize threshold': matThresholdLine.visible,
                                        'width': matLine.linewidth,
                                        'alphaToCoverage': matLine.alphaToCoverage,
-                                       'threshold': raycaster.params.Line2.threshold
+                                       'threshold': raycaster.params.Line2.threshold,
+                                       'translation': raycaster.params.Line2.threshold
                                };

                                gui.add( param, 'line type', { 'LineGeometry': 0, 'LineSegmentsGeometry': 1 } ).onChange( function ( val ) {
@@ -371,6 +372,14 @@

                                } );

+                               gui.add( param, 'translation', 0, 10 ).onChange( function ( val ) {
+
+                                       segments.position.x = val;
+                                       segments.updateMatrix();
+                                       segments.updateMatrixWorld();
+
+                               } );
+
                        }

                </script>

@MixMasterMitch
Copy link
Contributor Author

How do I get this merged? @gkjohnson I have seen you approve some similar PRs. Can you approve this one and merge it? Thanks in advance.

@Mugen87 Mugen87 added this to the r144 milestone Jul 30, 2022
@mrdoob mrdoob modified the milestones: r144, r145 Aug 31, 2022
@mrdoob mrdoob modified the milestones: r145, r146 Sep 29, 2022
@mrdoob mrdoob modified the milestones: r146, r147 Oct 27, 2022
@gkjohnson
Copy link
Collaborator

cc @ariel-resonai @bergden-resonai I think this change looks right but I'd appreciate if you guys can confirm.

@bergden-resonai
Copy link
Contributor

bergden-resonai commented Nov 29, 2022

@gkjohnson Works for me, I think the change to the example is great and should be added to make it easier to find future issues with this

https://github.com/MixMasterMitch/three.js/pull/1/files

Copy link
Collaborator

@gkjohnson gkjohnson left a comment

Choose a reason for hiding this comment

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

This looks like a good change - at the moment it looks like raycasting will only work with world units when the mesh has no transform.

I have not tested with a non identity world transform so I'm trusting the contributor has validated this but you can see at least that the example behavior is still correct:

https://raw.githack.com/MixMasterMitch/three.js/line-segments-2-world-units-raycast-fix/examples/webgl_lines_fat_raycasting.html

@Mugen87 Mugen87 merged commit fbace95 into mrdoob:dev Nov 29, 2022
@Mugen87 Mugen87 changed the title Fix raytracing of LineSegments2 when the mesh has a transformation applied. LineSegments2 : Fix raytracing when the mesh has a transformation applied. Nov 29, 2022
@bergden-resonai
Copy link
Contributor

@gkjohnson do you think adding the translation and/or rotation to the example is helpful?
Translation

Both

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants