- Home /
Conform mesh to arbitrary surfaces?
Hi there, wondering whether this is feasible or not. I'm looking to create a planar mesh mouse cursor that conforms (folds) to follow adjacent surface normals. See image and code below to see the start of my logic - the red area being the photoshopped goal.
I can't imagine how to do this without checking each vertex of the cursor against colliders, starting from the original hit point and radiating out. First problem with this logic is that it's doing my head in simply considering how to start. Second problem is whether it's feasible to shoot out that many rays each frame.
Any suggestions on how to proceed would be greatly appreciated.
void Update () {
RaycastHit cursorHit;
Ray cursorRay = cam.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(cursorRay,out cursorHit,500,hitLayer)) {
materialMarker.transform.position = cursorHit.point;
materialMarker.transform.rotation = Quaternion.LookRotation(cursorHit.normal);
materialMarker.transform.Rotate(90,0,0);
materialMarker.transform.Translate(Vector3.up*0.01f);
}
}
Answer by Bunny83 · May 16, 2013 at 10:54 PM
Well an arbitrary approach is not really easy. You have to create the geometry procedural. The tricky thing is the edge clipping. The easiest approach would be to use MeshCollider for all of the surfaces. That way you get a lot of information from the hit point when you raycast at the object. You get the triangle index you hit as well as the barycentric coordinate of the hit. With those you can create a sub-piece on this triangle. Now you would check if your desired size of your projected plane is larger than the distance from the hitpoint to the 3 edges of the triangle. If it is larger you have to find the neighbor triangle and create the next piece of mesh there.
As i said it's not easy to get this working with arbitrary gemoetry.
btw: Do you want a linear projection onto the object or should it really be "folded" around the edge so it doesn't keep it's original shape?
No, an arbitrary approach is not easy. I was hoping it would be slightly easier than the method you suggest. A lot to learn. Found this example, and in slow motion I still cannot quite work it out...perhaps it's all produced with normal averaging, depth of fielding projectors.
Anyway, a project for the weekend! Thanks!
Well, if you just want a "highlight" it might work with a custom shader were you feed in a worldposition of your mouse target and in the fragment shader you simply highlight every "fragment" that's within a certain radius(in 3d space). This should give this effect without any additional geometry ;) You might be able to simply add another material to the mesh under the cursor (the meshes around the cursor) and let this material just draw the highlight. It would actually be a similar approach to the projector, but without any special projection math ;)
Answer by Graham-Dunnett · May 16, 2013 at 09:36 PM
Sounds like something the Projector could do for you.
Thanks for the suggestion, I was looking into it earlier but unfortunately I don't see it being quite that simple...unless my illustration below is way off base?
Answer by adrien_grs · Sep 07, 2015 at 01:20 PM
Hey!
Did you managed to solve this problem ? I am very curious about this issue :)
Thank you in advance,