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

Snapping on Mesh3D #1

Closed
oparisy opened this issue May 7, 2024 · 22 comments
Closed

Snapping on Mesh3D #1

oparisy opened this issue May 7, 2024 · 22 comments

Comments

@oparisy
Copy link

oparisy commented May 7, 2024

First of all, thanks for sharing your work!

I've tried building levels with modular assets before, but if you look at an example such as this one, it becomes clear that the gridmap is not sufficient to place those, and that a snapping tool is an invaluable help:
image

I wanted to know if only being able to snap on collision objects was a strong constraint, or if you could consider snapping on arbitrary Mesh3Ds?

My reasoning being that on the scene above, it seems dispendious to have one collision body per modular asset, while a more coarse-grained collision shape (added after modeling) would usually suffice.

Regards!

@mharitsnf
Copy link
Owner

Hi, thanks for bringing this up!

The only reason I implemented the snapping only to collision objects is because it was the easiest to get it running (because I can just use ray cast to get the position and normal of the target object). But yes, I have been thinking about ways to get things to snap without having to rely on collision objects.

I would love to have this feature as well, so I'll try to find ways to implement this.

@oparisy
Copy link
Author

oparisy commented May 7, 2024

A possibly related issue: godotengine/godot-proposals#2863

Note the comment about tooling at the beginning of the discussion.

@ChildLearningClub
Copy link

ChildLearningClub commented Jul 29, 2024

The way to implement this would be to apply a temporary collision shape to the Mesh3D objects that you want to place other objects on. and of course to the Mesh3D object you are placing, after it is placed, if you want to continue to build off of that. The final step would be a box select the objects in the area and "commit build button" and either strip collisions or convert to lower resolution collisions.

@oparisy I can't seem to find that pack of Kenney's that you show, do you happen to have a link to that?

@oparisy
Copy link
Author

oparisy commented Jul 30, 2024 via email

@ChildLearningClub
Copy link

That's more of a workaround, excepted if you're suggesting the tool is adding those collision objects automatically?

I think the only alternative would be to have a reference to all the AABB bounding boxes in the environment stored in an array or something, which I think would be a much more difficult option compared to even just having the tool apply Area3Ds to the same AABB and using raycasting. additionally rather then each one being done individually when selected there is the option to use an import script and apply all the collision/areas at import time, which again if desired removed when building of the area is complete. What do you think?

@oparisy
Copy link
Author

oparisy commented Jul 31, 2024

@oparisy I can't seem to find that pack of Kenney's that you show, do you happen to have a link to that?

Indeed the preview image I used for this issue has been changed in recent version. It's the Modular Buildings pack.

Actually the preview is much more contorted now, and mostly aligned on the vertical axis (probably as an influence of his City Builder Starter Kit), so it does not illustate my point so well anymore 😅

@oparisy
Copy link
Author

oparisy commented Jul 31, 2024

@ChildLearningClub User experience wise, I feel this is lots of manipulation. I think the current "snap to the nearest object" behavior (as demonstrated by the README video) is a strong feature of this addon and should not be degraded.

Technically wise, if the tool can automatically apply and remove those collision objects "under the hood", user experience would be preserved. It reads like a chicken and egg problem (how do you find the nearest object to apply the collisition object to, since that collision object is used by the addon to detect the nearest object in the first place?), but I guess using AABBs could help by efficiently providing a coarse "view" of the surrounding.

I still think the best solution would be to have some support from the physics server to raycast to non-collision objects (as evoked in godotengine/godot-proposals#2863).

Since this is not currently available, another solution would be for this plugin to use some efficient spatial queries data structure, and do the raycasting itself. But this is quite a heavy lifting for GDScript.

@mharitsnf
Copy link
Owner

One approach that I want to try is to use ray-triangle intersection (as seen here), which involves (1) getting all triangles in the scene, (2) finding an intersection between one of the triangles with the currently selected object, and (3) placing the selected object at the intersection point (and optionally align the orientation of the selected object with the triangle's normal).

Technically wise, if the tool can automatically apply and remove those collision objects "under the hood", user experience would be preserved. It reads like a chicken and egg problem (how do you find the nearest object to apply the collision object to, since that collision object is used by the addon to detect the nearest object in the first place?), but I guess using AABBs could help by efficiently providing a coarse "view" of the surrounding.

TBH, I think this approach is pretty interesting and I think it could work as well. Since the addon already have a function that can retrieve all mesh instances in a scene, we can firstly try to retrieve all mesh instances in the scene, create a temporary trimesh collision for all of those mesh instances, and removing them as soon as the snapping is finished.

I don't know how well these approaches will perform in a scene with a lot of meshes, I figure it might be pretty slow in that scenario. But I think we can resort to using AABBs like you mentioned or use the camera frustrum to limit the number of meshes to process to improve the performance.

@oparisy
Copy link
Author

oparisy commented Aug 1, 2024 via email

@ChildLearningClub
Copy link

Yeah, I think creating the temporary trimesh collision would be the way to go for the accurate snapping that would provide, The initial building of all the trimesh I can see slowing things down a bit (and this would just be for large static mesh projects imported), As new objects are brought in they would individually get the trimesh applied. All the collisions that are created can be added to a group can be stripped out later. What do you guys think?

I don’t know if there is a way through code to do this stripping out process, I imagine there is? So there is this extra step involved compared to what I think your suggesting @mharitsnf instantiating the collisions as they come into view and then freeing or removing them as they exit the view? Anyway, some great ideas!

@mharitsnf
Copy link
Owner

Yeah, as you mentioned, I think we can put the collision (StaticBody3Ds) in a group then delete them with queue_free once we are done.

For a quick update: I managed to make it work with ray-triangle intersection, but sometimes it just couldn't detect an intersection on some part of the mesh (take a look at the video). I've no clue why this happens lol but I'll try to look at it a bit closer later.

no.collision.snap.mp4

If this doesn't work, I'll probably try to create static bodies on the meshes, although I don't really like this approach because you can visibly see the static body nodes being added and removed from the scene tree.

@oparisy
Copy link
Author

oparisy commented Aug 2, 2024

For a quick update: I managed to make it work with ray-triangle intersection, but sometimes it just couldn't detect an intersection on some part of the mesh (take a look at the video). I've no clue why this happens lol but I'll try to look at it a bit closer later.

Maybe a Godot bug worthy of reporting, especially if you can reliably reproduce it? I had a quick look at their tracker but did not find a related issue.

@ChildLearningClub
Copy link

ChildLearningClub commented Aug 2, 2024

@mharitsnf Wow! Great job!. I never knew it was possible to get a meshes position from just the mesh itself. This might be of help with the issue you are having. Or at least give you some ideas? I hope it is useful.
https://forum.godotengine.org/t/get-face-of-colliding-mesh/2206/2

Godot 4.3
Geometry = Geometry3D
rad2deg = rad_to_deg

@ChildLearningClub
Copy link

ChildLearningClub commented Aug 3, 2024

Maybe not a bug but a limitation of the angle maybe? Not sure myself?

2024-08-04.07-25-21.mp4

@mharitsnf turns out it is a limitation of our brains ;). What's the length of your raycast? can bet it is too short.

2024-08-04.08-04-41.mp4

@mharitsnf
Copy link
Owner

mharitsnf commented Aug 4, 2024

Hmmm, I think we are using a different approach, as I'm not using raycasts. But your snippets look cool! I thought raycasts require collision bodies, but you only have meshes in the scene tree. Or perhaps there is a way without using collision bodies?

In the video I posted before, I used ray_intersects_triangle function from the Geometry3D class. It doesn't require ray length because it uses some mathematical calculations between a triangle and a ray origin and direction.

Maybe a Godot bug worthy of reporting, especially if you can reliably reproduce it? I had a quick look at their tracker but did not find a related issue.

It could be. I will try to figure out how to reproduce it.

Edit: wrong function name

@mharitsnf
Copy link
Owner

Nevermind... I was being dumb. My for loop was the main issue 😅 Anyways, here's how it looks like, currently works with CSGs and meshes:

no.snap.collision.2.mp4

I think it looks nice; though I'll go through it again to make sure it actually works well. Hopefully I can make a PR soon enough :D

@ChildLearningClub
Copy link

ChildLearningClub commented Aug 4, 2024

You are correct, I am not using the raycast, I'm pretty you are also right that it requires a collision body to function It is just to visualize the position and was using the position and raycast target information from it. I'm using the ray_intersects_triangle as well, so we are probably not to far off each other :). Yours looks smooth! were you able to figure out how to narrow down the process to only the mesh target, or are they all processed or just the ones in camera view? the AABB class has the intersects_ray () function which I think will solve any issues with that. Struggling now to get it to work through. think I have to convert the aabb to global space.

Edit: Oh, also I have to look at it more, but think the get_triangle_barycentric_coords from the Geometry class might also be useful with smoothing the transitions over the surfaces if that was desirable. godotengine/godot#71233

Edit 2: Took forever to find this. the xform in godot 4 visual_instance.global_transform * visual_instance.get_aabb(). https://stackoverflow.com/questions/76982738/how-to-convert-form-aabb-space-get-center-method-to-world-environment-in-godot

@ChildLearningClub
Copy link

@mharitsnf if you are not already limiting the process I found using the intersects_segment( ) function within the AABB class worked, I was having trouble with the intersects_ray ().

var aabb_global: AABB = mesh_instance.global_transform * mesh_instance.mesh.get_aabb()

if aabb_global.intersects_segment(editor_camera3d.project_ray_origin(mouse_pos), (editor_camera3d.project_ray_origin(mouse_pos) + editor_camera3d.project_ray_normal(mouse_pos) * 1000)):

https://docs.godotengine.org/en/stable/classes/class_aabb.html#class-aabb-method-intersects-segment

@mharitsnf
Copy link
Owner

mharitsnf commented Aug 6, 2024

Hi folks, I just made a pull request for this feature (check here: #8)! I ended up using AABB intersection to determine which object to process as mentioned by @ChildLearningClub, as it helps with the performance when I tested it (although it wasn't a comprehensive test at all)

Please give it a try, see how it works for you 😊

@ChildLearningClub
Copy link

ChildLearningClub commented Aug 6, 2024

It works well! again, great Job! Now it's time @oparisy to test it on that buildings pack :)

@mharitsnf
Copy link
Owner

Thanks all for the help and contributions! I'll close this issue as I'll merge the PR.

@ChildLearningClub
Copy link

ChildLearningClub commented Aug 13, 2024

@mharitsnf would possibly obscuring the option to switch between the two object types be desirable? I'm trying to think of when you would only want one or the other and if that could not be resolved by the user viewing which have collision shapes? What are your thoughts? And @oparisy?

2024-08-14.07-36-36.mp4

Edit: Now that I think about it, if the scene got really crowded, I guess it might be a good thing to have.

Edit2: @mharitsnf also wanted to say that implementing the snapping at the distance to the vertices rather then the vertices themselves was a great idea!

Edit 3. Okay, I see the Mesh option allows for you to snap to all mesh and the using raycasts will allow for just snapping to those objects with collision nodes. So you can completely disregard this post :) I guess the question now is whether the collision object snapping is even needed when the mesh snapping will do the job for both? Oh yeah, you could have custom collision shapes and want to snap to those, although not sure I know the real use cases? Anyway, Okay, I'm done.

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

No branches or pull requests

3 participants