Download

How to draw different color on individual mesh without using stencil buffer

I am developing a project in which I have to draw different colors on individual static mesh for object segmentation purpose. I use the custom stencil buffer provided in the latest UE4 to tag different objects and use USceneCaptureComponent2D to capture the scene and draw it with a render target. But with this approach, it limits the number of objects I can tag to up to 256 only. I urgently need a solution to allow me to have more than 256 tags.

I found several threads talking about similar problems when people want to draw outline in different colors, but they don’t need more than 256 colors, so stencil buffer works for them. But in my case it won’t work.

I found the above thread about creating a custom GBuffer which could be one option for me, but that will need to change the unreal engine code and will cause merge issues if upgrade to newer version in the future, so I am trying to avoid doing this. Another option could be to change the custom stencil buffer to have more than 8 bits, but I believe that again needs lots of changes in unreal engine itself.

Is there any elegant way (or even just workaround) to achieve what I want without having to modifying the unreal engine?

For example, dynamically create a transparent material per mesh and set a custom parameter on that material and somehow access that custom parameter in the post process pass. Or any way to create a custom SceneTexture which can hold the value set per mesh (similar to the stencil buffer)? Or any way to make use of postprocessinput3 to postprocessinput6 (marked as not usually used by Unreal) to inject the values? Or create two render targets to render the same material for twice, so in the shader I can use one custom stencil value as the lower bits and the other as the higher bits, then combine them in the shader. Is any of these approaches possible?

Any suggestion would be highly appreciated.

BTW, anyone had the chance to merge Temaran’s Custom Tag Buffer patch to Unreal Engine 4.18 or 4.19?

https://github.com/Temaran/UnrealEngine/tree/4.9CustomTagBufferPreview4

Thanks a lot!