Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

unity3d - Unity: Mesh Renderer seems to not fit the Edge perfectly

We are currently trying to lay mesh colliders onto our edges as shown in the pictures. The problem is that the meshes sometimes seem to be 2D instead of 3D (shown in Picture 2 and Picture 3), which makes them unselectable from certain camera-angles. Sometimes the meshes even seem to disappear through some parts of the Edge(Picture 1). Turning convex on for the colliders makes them way easier to select, but we dont really want to do that because that makes it realy unclear which edge you are selecting right now. We are creating our meshes through bakeMesh from our previously created Edges as shown below:

LineRenderer lineRenderer = gameEdge.GetComponent<LineRenderer>();
MeshCollider meshCollider = gameEdge.AddComponent<MeshCollider>();
Mesh mesh = new Mesh();
lineRenderer.BakeMesh(mesh, Camera.main, false);
meshCollider.sharedMesh = mesh;
meshCollider.convex = false;

Edit: We used this https://github.com/mattatz/unity-tubular to generate tube meshes around our edges, working pretty well now!

question from:https://stackoverflow.com/questions/65862116/unity-mesh-renderer-seems-to-not-fit-the-edge-perfectly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The mesh generated by the line renderer is actually 2D. I think that your best chance is to update the mesh orientation to face to the camera so that the mesh will always be facing to the camera wherever you are looking from. That way you'll allways be able to click on it.

You have 2 options:

1.- Passing the new camera (with its new position) all the time. This would be in the Update. Be carefull to only use the code that bakes the mesh and not the Mesh mesh = new Mesh() in the update. Because if you create a new mesh in the Update passed some time you'll have an stack overflow error. I recomend if you do this, to make the Mesh mesh; a class variable and start it only in the class initialization (mesh = new Mesh();) so that you use only the created instance to update the mesh all the time, not creating a new one for each update.

2.- If you are concerned about efficiency, you can handle when your camera is moving, so that when it stops moving, you pass in the new camera along with its position. For this you would need to handle the camera OnStopMoving event yourself to pass the new camera in, so whenever the Main camera stops moving, the mesh will always be facing it. This makes sense because its easier for the user to click on things while stopped, so it can be presumed that the user will try to click on the lines while not moving

There is one third option I did not mention due to feasability, that is wrapping your line renderer with primitive colliders with the points of your line as an starting point for the procedural collider wrapping logic. However you would need to code all that out, which might take a while.

On the other hand making it a convex collider as far as I checked is not feasable, as the behaviour and shape of the collider itself changes on the mesh cooking by the MeshRenderer component. Check if this might be of help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...