Can you process a color with postprocessing stack's color grading?

Hello :slight_smile:

Is there an easy way to get the return color from the Post Processing v.2 Color Grading effect, given a source color? I’d like to color grade a bunch of objects that have vertex colors by using the Post Processing Stack Color Grading and then “bake” the results into the vertex colors so I can turn of post processing and still have the colors look like when they were “graded”.

I’d be happy to google my way to the final result but need some pointers on where to get started. Doesn’t need to be a very performant solution as I’d be doing this in editor only.

Thanks in advance!

Just have a look at Brackeys’ video on youtube about Post proccesing in Unity. Everything is explained there.

Found a somewhat dirty workaround, but for what I want to achieve, it gets the job done. I thought I’d share it here in case anyone stumbles across the same problem in the future.

To get the color graded result from an original color, I get the camera to which the color grading is applied (Camera.main in this case), set the original color as its background color and cull all layers so the camera renders only the background color with Post Processing on it. For this to work it’s super important that Color Grading is the only active Post Processing Effect when this method is executed.

I have the camera render into a RenderTexture from which I read back into a helper texture. The helper texture is needed because from what I understand, you can’t directly read the color of a pixel from a RenderTexture. The color of the helper texture is now the original color + color grading.

Here’s the code in case anyone wants to reference it:

    private Color ApplyColorGradingFromPostFX(Color originalColor)
    {
        // For this to work, the color grading post processing you want to "bake" needs
        // to be the only active post processing effect on the main camera. 

        // This entire method is a bit of a dirty workaround but it gets the job done. 
        // It's important, that the Color Grading is the ONLY active post processing effect
        // when this method is executed, otherwise effects like vignette or grain will 
        // interfere with the background color of the camera and generate faulty results.

        const int Res = 1;
        Texture2D helperTexture = new Texture2D(Res, Res);

        // Store current active RenderTexture
        RenderTexture lastActiveRT = RenderTexture.active;

        // Get the main camera
        Camera renderCamera = Camera.main;

        // Store original camerasSettings
        Color originalBackgroundColor = renderCamera.backgroundColor;
        CameraClearFlags originalClearFlags = renderCamera.clearFlags;
        int originalCullingMask = renderCamera.cullingMask;

        // Set camera up for rendering the original color 
        // as background color and cull everything else
        renderCamera.backgroundColor = originalColor;
        renderCamera.clearFlags = CameraClearFlags.Color;
        renderCamera.cullingMask = 0;
        // I don't think this is really necessary, right now the code works 
        // if you delete it, but I'm leaving it in for future proofing
        RenderTexture.active = renderCamera.targetTexture;

        // Render and copy a pixel from the corner of the screen 
        // into our helper texture, so we can read it's color value
        renderCamera.Render();
        helperTexture.ReadPixels(new Rect(0, 0, Res, Res), 0, 0, false);
        helperTexture.Apply();

        // Read the modified color from the helper texture
        Color returnColor = helperTexture.GetPixel(0, 0);

        // Restore original camera settings
        renderCamera.backgroundColor = originalBackgroundColor;
        renderCamera.clearFlags = originalClearFlags;
        renderCamera.cullingMask = originalCullingMask;

        // Restore last active RenderTexture
        RenderTexture.active = lastActiveRT;

        return returnColor;
    }