Search Unity

Question How to obtain HD Camera's render average color?

Discussion in 'High Definition Render Pipeline' started by alexandre-fiset, Nov 2, 2022.

  1. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    I would like to obtain the average color of my main camera render in order to, among other things, dim the user interface in dark areas and vice versa for light area.

    I thought of using Texture2D.GetPixel to get three pixels (or more with a precision setting) on screen and then average these over multiple frames, but in order to do that I'd need the render texture of the camera.
    1. Is there way to get a render texture from the HD Camera without rendering again on a render texture via camera.target?
    2. Is GetPixel the right method for the job?
     
  2. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Hi, I'm not an expert on this but I know that there might be a better way to do this in HDRP.

    There's a special render texture called Color Pyramid in HDRP.

    It does not include transparent objects but I suppose you are not looking for a 100% accurate average color?
    HDRPColorPyramid.jpg

    You can access the highest mipmap level of Color Pyramid to get the average scene color.
    (Unity-Documentation-Mipmaps)

    You can, but I think a better way is to use compute shader.
    • Compute shaders can easily access global render textures (Color Pyramid).
    • Can use something like AsyncGPUReadback (or even don't need if outputting one value from compute shader).
     
  3. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    You can still sample other mipmap levels of Color Pyramid (with different UV, which is a 2D screen coordinate) in a compute shader.

    I think it should be more average than reading several pixels.
     
  4. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Thanks for the very valuable answer :) Will check this out, never played around with AsyncGPUReadback.
     
    wwWwwwW1 likes this.