I am interested in storing some values from opaque objects into the alpha channel of a render texture. I got the idea from listening to a Unite talk where they used the trick to crate a bloom post effect without using hdr. Here is the link
.
I tried using CommandBuffers to get the texture right after Opaque geometry is rendered:
myCam = this.gameObject.GetComponent<Camera>();
if (XRSettings.enabled)
originalTextureDescriptor = XRSettings.eyeTextureDesc;
else
originalTextureDescriptor = new RenderTextureDescriptor(Screen.width, Screen.height); // Not XR
originalTextureDescriptor.colorFormat = RenderTextureFormat.ARGB32;
CommandBuffer buf = new CommandBuffer();
buf.name = "grab alpha";
int screenCopyID = Shader.PropertyToID("_AlphaMask");
buf.GetTemporaryRT(screenCopyID, originalTextureDescriptor);
buf.Blit(BuiltinRenderTextureType.CurrentActive, screenCopyID);
buf.SetGlobalTexture("_HDRmask", screenCopyID);
myCam.AddCommandBuffer(CameraEvent.AfterForwardOpaque, buf);
And use this test surface shader for opaque objects:
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
Blend SrcAlpha OneMinusSrcAlpha, One One
ColorMask RGBA
ZWrite On
CGPROGRAM
#pragma surface surf Lambert keepalpha
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
Even though i use ColorMask RGBA and keepalpha tag all the opaque geometry has alpha 1 in the render texture. My cameras clear flag is set to solid color (0,0,0,0). Any advice would be appreciated.