I want to get a mask of a game object in 3D built in rendering pipeline.
Which means, I want to get a texture which colorized the target game object as red and other parts as black.
To do this, I think I need to modify the fragment shader.
Here are my questions.
- How can I check whether each pixel is included in the target game object?
- Can I add a boolean variable to each pixel?
Thank you in advance.
Well, first you’ll need to make the texture. Assuming you’re using the built-in render pipeline, as a starting point, I’d put the object on its own layer, and get a Camera rendering just that object to a RenderTexture, then use the Camera’s shader replacement functionality to render it with the desired fragment shader.
Then, if I remember correctly, you can call ReadPixel(…) to check the colour of a pixel, so you just check if a pixel is the appropriate colour to see if it’s inside the object.
To encode boolean values, write different colours in the fragment shader. Exactly what the values might be will depend on the texture format. As an example, you could use the R channel to indicate what object is present, and the G channel to store an associated value. So (0, 0, 0) indicates no object, (1, 0, 0) indicates object #1 with a false value, and (1, 1, 0) indicates object #1 with a true value.
Once you have all of that working, if it’s of value then you can look at how to render the mask texture more efficiently.
Depending on what you’re doing with this, the Stencil Buffer may be a better approach than a mask texture, so I recommend checking that out.