- Home /
Render wireframe on cut section of polygons
Hi, after researching for a few days and trying everything free I came across I'm giving in and asking here. I'm looking for a way to render wireframe so that it'll complete the lines where geometry is cut off, either because of a cross section or from the near plane on a camera. I've found that the Source Engine does this which you can see in this picture and the video. Unlike source however I'd like it to be double sided, and though unlikely, if only the clipping edge could be isolated that'd be even better.
The reason I want this behavior is that I've been looking for any way to scan a models shape in layers from the top-down using an orthographic camera and reconstruct it using layers of textures. I've tried many ways, had a pretty good one but it relied on the model being well made, my system has to account for any model from the user. I've also tried using highlight shaders, which worked well for the most part, but if a polygon is perfectly vertical (like on a building wall) there will be no highlight since it'll be too thin.
I've come to think this wireframe method is going to be the cheapest way to obtain an outline of a models shape at any point, by sending an orthographic camera through the model with the near and far plane extremely close to eachother. I'll take any solution that isn't too heavy tho, needs to be able to make a scan relatively quick for models with millions of polygons.
Thanks for reading, hope there's a way to do something like this.
ok, so partial answer... ins$$anonymous$$d of using one ortho camera, you should use several with your highlight shader. From one direction, the shape will never be deter$$anonymous$$ed, but if you have two (or more) a few degrees apart, you shouldn't have that problem. Admittedly, that makes it all the more complicated - hence partial answer :P
Yeah have thought of something like this but doesn't seem all that practical, rendertime will be longer and I might have to try to combine the different angles result into one somehow, dunno how. $$anonymous$$aybe I'll look into that if I get really desperate but for now I hope there's a better way, thanks for the response though.
Well, to elaborate a little; the principle of triangulation and a c# script to combine the data. Or a shader, it really doesn't matter which way (ie, a volumetric shader). Rather than using slices, I would have a front-only and back-only shader that draws depth into the colour channels. Oh my...maybe I can give a decent answer. You see, if you have a shader like that, then you only need one camera. You'd take two images, compare the difference, and voila. But I think I need more info on what the end result has to be. And on what you mean by fast. Is this an editor solution or something that must happen at runtime? Well naturally that has limitations...I'm assu$$anonymous$$g there's no holes in the middle of the object, for instance. If you want it to be perfect you would indeed need to take slices.
Answer by Bunny83 · Oct 30, 2017 at 03:31 AM
I quickly created a script that will create a wireframe mesh (which consists of lines) based on a given object. In addition it will clip the edges against a plane.
The actual MonoBehaviour in this script is ment for demonstration. Modify it as you need it or just use the "WireframeClipMesh" to create / update the wireframe mesh on the fly. Unfortunately Unity still has no SetIndices variant that takes a generic List. So if the number of edges that has to be drawn changes we have to create a new index array. Though even when moving through an object that shouldn't happen too often.
When you attach this script to an object it will initialize the "WireframeClipMesh" with the mesh of the object it's attached to. It will create a child object "wireframe" which will actually draw the clipped wireframe mesh. The clipping plane is defined by a single Transform reference. The positive z-axis (forward) of that transform is the plane's normal vector. Note things in front of the plane get cut away. So it's not an "allow" plane (like the viewing frustum where the normals point inwards) but a cutting plane.
It's probably easier to look at this animated gif
Wow thank you very much for this, it works pretty darn well! There are some problems though, most importantly that on models that reach the max poly count of unity it gets errors. I don't expect you to do more for me for free here so I would like to contact you and offer some pay to do some fixes, additions and changes, if you're interested that is. $$anonymous$$y main specialty isn't program$$anonymous$$g and I don't think anyone on my $$anonymous$$m has much experience with this exact thing (or time to spend on it), so if you have time I'd be happy to give you something for your effort. If not thanks again for what you have done, didn't expect anyone to come with something so functional, or anything at all really. Thanks!
Unity doesn't have a max poly count, just a max vertex count as Unity only uses 16 bit index buffers so it's not possible to address more than 65k vertices.
Well, yes if the original mesh has many shared vertices my current method does duplicate those for each edge. When i started writing the script i actually thought if i may actually create triangles as output which was the main reason to keep the vertices for each triangle.
This problem can probably be solved by simply sharing all vertices possible. Since we only deal with lines we actually don't care about vertex colors, normals, uv, ... so sharing should be no issue. The easiest would probably be a mapping dictionary to automatically merge vertices with the same position.It's probably the best to do the merging as the last step. It could be done right in the beginning, however as new vertices are generated by the clipping they would need to be merged as well.
Currently my family is around for the next few days. I will have a look at it. If it can be fixed easily I'll do it right away ^^
It was faster than i expected ^^. Just added the merging code which is essentially just a few additional lines. I've updated the file on my dropbox. If i check the created mesh for the default sphere it actually ends up with less vertices than the original sphere.
$$anonymous$$an you're awesome, works nicely ^^. But sadly I still have a list of a few things that are needed before I can use it :c. Again I'd really like to reward you for what you have done and what I still need, and I'm all out of reputation points.
I'll put the list here, not because I want you to fix it for free but to give you an idea of what I need, not that I can stop you it seems :P. Show only the clip line (would probably fix the performance too), apply script to top parent only, size and orientation problems when not default I think, and nice to have would be to change line thickness and color.
Since you seem interested in helping please do give me any way to contact you so we can talk and maybe to give some back, even s$$anonymous$$m will do, assume you have a s$$anonymous$$m account to play Black $$anonymous$$esa at least, my name and avatar are the same on s$$anonymous$$m as here. Sorry if I'm pushy about rewarding you, just think you deserve it ^^.
Anyway thanks for everything you're awsome, glad you stumbled upon my question :D.
Answer by Fyphia · Apr 19, 2021 at 07:40 AM
@Bunny83 , Hi, thanks for your sharing! I applied the script, but it seems there is no cut happens. May I ask why? I think that if I don't close the mesh renderer, it should show part of the object when moving the clipping plane
@Bunny83 , I've checked the complete comments above and I found that you added a boolean parameter, now the cut of wireframe works well! May I ask, is there any way to perform the cut with mesh renderer instead of just wireframe so that parts of the object occurs with the clipping plane move?
Is there a way? sure, however it gets significantly more complex. When we clip a wireframe model we can work on each edge more or less seperately and just have to take care of adding the new edge at the cut. When you want to actually cut a triangle the result may need to split the original triangle into two new triangles. That's because a cut triangle may result either in a single smaller triangle (if two of three vertices are cut off) or in a trapezoid (if only one vertex is cut off). Of course a trapezoid has to be represented with two triangles. Also cutting a triangles that have textures applied also means you have to calculate the UVs for the new vertices. Also in 3d you have the additional problem of the newly created face at the cut. This face needs to be created from all the vertices around the cut and if the mesh was textured you have to find a reasonable way to texture this piece.
So there are a lot more variables to consider. It's not black magic and not overly complicated, though quite a bit of work. Currently I don't have any time to flesh out a solution for that. Also that's not what was asked here. So if you have a different question, you should ask a seperate question. However there are at least 5 questions on UA which ask for how to cut a mesh. There are (mosly paid) solutions on the asset store as well.
@Bunny83 , thanks for your reply! Maybe my expression is not accurate enough. I didn't mean the actual cut or slice. As shown in your gif, it is possible to show/render part of the object's wireframe (without its mesh renderer) when moving the plane, I wanna ask if it is possible to show part of the object with its mesh renderer.
Your answer
Follow this Question
Related Questions
How to see mesh in game mode ? 3 Answers
How to acheive wireframe effect with dots? 0 Answers
LineRenderer with nontrivial radius? 1 Answer