Greyscale Shader with Masking

I’m new to shader programming, but I want to achieve a greyscale effect based on a mask texture (similar to fog of war).
The first picture is the original scene with two layers of objects. The first layer contains the stars and the second
layer the other objects. The objects on the second layer (not the stars) should be greyscaled based on a mask with
different alpha values (second picture). The mask is attached to a plane and can be moved around the scene.

To seperate the layers I will add a second camera. I’ve looked into grayscaling the whole screen using a Rendertexture, but
I have no idea how to get the mask texture into it. The target platform will be iOS.

Thanks for help.

1448365--78161--$greyscaleShader.png

If you are only targeting iOS, then you should look into “GL_EXT_shader_framebuffer_fetch”. It lets you read directly from the screen so you can create a greyscale effect without having to allocate a RenderTexture.

http://forum.unity3d.com/threads/82105-Desaturation-grayscale-shader-for-iOS/page3

You will need to modify this to add your masking texture as a property and then assign the texture to the shader. Then do a texture lookup, and replace the masking value with the _Opacity value in the shader I linked you to. I don’t know the syntax for it (especially using a GLSLPROGRAM) off-hand, but you should be able to find a reference with a bit of searching.

To draw the quad you can use the GL class. You can specify where exactly where you want to draw the quad in screen coordinates with the corresponding UV coordinates of the texture (set. There will be a bit of math to get right, but you just need to write out a diagram and figure it out. It should be pretty straight forward.