- Home /
Set dynamic generated alpha mask on shader at runtime
Here is my end goal: to generate an alpha mask texture at runtime and be able to keep updating it and sending it to a texture's shader. Performance is a concern for me. I want to be able to take touch input and 'erase' the texture by setting the alpha to 0 in that section.
So I already have a shader and material that renders a texture with an alpha mask, and I think I just need to do myMaterial.SetTexture("_AlphaTex",myRenderTexture);
But how can I generate the RenderTexture and manually set each pixel. Will changing the texture and sending it to the shader every frame there is touch input be a performance issue? Is there a better way to achieve this effect? (I've read that Texture2D.SetPixels is super slow)
So, your alpha mask is a render texture? Or are you playing fast and loose with the ter$$anonymous$$ology? ;)
In whatever case, I see you have two sensible options:
$$anonymous$$anipulate an alpha texture's Color[] array with touches or manipulate the mesh's vertex colors with touches and use a shader which respects vertex alpha.
It's my understanding that the vertex method would be somewhat more performant, simply because it's less expensive than SetPixels. Even so, frankly I'd just go with what you've got unless you find that it's a bottleneck.
$$anonymous$$eep in $$anonymous$$d that your alpha mask's resolution (or vertex density with the vertex color method) can-and-should be as sparse as possible. Writing 32x32 pixels or vertex colors is (literally!) exponentially cheaper than 256x256 or whatever.
Yeah I don't know all my ter$$anonymous$$ology :) this is the first time I've tried messing with anything graphics related in Unity. So how would I go about doing the mesh vertex method? Can you show me where I can access a mesh's vertex colors in code?
Because you've already got your texture method all set up and ready to roll, you might as well stick with it. I imagine the performance discrepancy between the two methods will be negligible if your expectations (resolutions) are reasonable.
Your biggest optimizations will be keeping the operation as svelte as possible by reducing resolution, maybe even spreading your change requests over several frames.
Here's the $$anonymous$$esh API link in the docs. You can do some cool stuff so it's worth digging into, but then you'll still need to write a shader to respect vertex colors. Gotta pace yourself! ;)
http://docs.unity3d.com/ScriptReference/$$anonymous$$esh.html
Thanks, I'll take a look at that later and let you know how it goes!