- Home /
how to quickly disable vertices from rendering in a Mesh?
Hello, i am using a mesh to speed the rendering of my lines (there are over 10,000 lines), but i would also like to quickly activate and deactivate individual lines within the mesh from rendering. is there a quick way to do this, provided, the mesh indices of the specific lines are known?
right now i'm setting the transparency to 0 on the lines i don't want to render, but it is giving strange opacity effects, and i think it would be much better to directly inactivate the points.
i think i have found a way to do this, i just set all the vertices i don't want to be on some far away location, so they don't show anymore. i guess its the same as setting their transparency to 0, but the shader i was using didn't like that approach.
Answer by Zarenityx · Jul 29, 2018 at 03:18 AM
Unfortunately, it isn't possible to remove specific vertices in a shader. There are, however, a couple things you can do:
Modify the mesh procedurally
Use Alpha Testing in a shader
The first option is the probably the best one if you are modifying these lines rather infrequently. If the indices of the line's vertices are known, you can just remove the vertices those indices refer to, and remove the indices themselves from the Mesh. If you are rendering lines, you know that there will be 2 indices for each line. Assuming the vertices aren't shared between segments, that's 2 vertices per line as well. So if you want to delete line, say, 4, you would be removing indices 6 and 7, and their corresponding vertices. If you are sharing vertices, you can just get rid of the indices (Sure, you're wasting vertices, but it's faster than going through and checking if another index references them).
If you already have a solution for changing transparency and want to stick with that, or if you are calculating what lines get shown in a compute shader and want to avoid a round-trip, you can use Alpha testing. This is a bit different from blending, as rather than blend between two pixels, you just completely kill a pixel who's alpha is too low. This is how all of the Alpha Cutoff shaders work to render grass for instance. The solution is the clip function, or its close relative, the discard statement. Clip will kill a pixel if it is given a negative value, so you can feed it your alpha minus a cutoff. If you have a lot of calculations to do, you might want to do a dynamic branch, where you use Discard on one side, and your long, complex calculations on the other. For short calculations, use Clip to avoid a branch. Then, setting your transparency to 0, assuming your cutoff is above zero, will cause your line to be clipped out.
thanks for the response. right now, i think i'm using #1 (modifying the mesh procedurally), here is my code:
bundle$$anonymous$$esh.SetVertices(newVertices);
bundle$$anonymous$$esh.RecalculateBounds();
it works for one time only effects like changing the color map of all my vertices, but i get the feeling when i need to do more real time effects like updating the mesh every frame, this will not be feasible, so i guess i'll use the alpha testing, you think this will work on effects where i need to modify the mesh on every frame?
thanks again