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.
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.