How can I properly implement HDRI on-the-fly tone adjustment and bloom in Unity?
I am currently working on the same topic. Let your main camera render into a RenderTexture which is in ARGBHalf format (you can create these RenderTextures from script, see below).
Then create another camera which renderes nothing (Culling Mask: Nothing), but in the OnRenderImage() function you replace the source image with the RenderTexture from your mainCamera and render this with a tonemapping shader.
Then you could add a bloom filter effect to the second camera...
Here is the script I attach to the MainCamera (it's just a little test):
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class OutputCamera : MonoBehaviour {
public Shader toneMapShader;
[HideInInspector]
public RenderTexture outputTexture;
public Camera mainCam;
public float exposure = 1.0f;
private Material ToneMapMaterial = null;
private Material GetMaterial()
{
if (ToneMapMaterial == null)
{
ToneMapMaterial = new Material(toneMapShader);
ToneMapMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return ToneMapMaterial;
}
void Start () {
outputTexture = new RenderTexture(Screen.width, Screen.height, 1);
outputTexture.hideFlags = HideFlags.DontSave;
//Set output texture to 16bit Format
outputTexture.format = RenderTextureFormat.ARGBHalf;
if(mainCam != null)
mainCam.targetTexture = outputTexture;
}
void OnRenderImage(RenderTexture source, RenderTexture dest)
{
GetMaterial().SetFloat("_exposure", exposure);
ImageEffects.BlitWithMaterial(GetMaterial(), outputTexture, dest);
}
}
It seems like you cannot insert fullscreen effects on the mainCamera, because then the HDR information gets lost (the input will be ARGB32), but I am not sure about that...
where can i get the bloom filter in the second camera?