Render Post Processing effects using Commandbuffer in Unity

Asked

Viewed 11 times

0

Good afternoon. I would like help to make a post-processing system. I don’t know much about commandBuffer, so I’m not getting it to work.

Here’s what I need to do. Render the first effect on top of the camera image, and the second effect on top of the first image, and after that throw the rendered image on the camera.

Code used:

using UnityEngine;
using UnityEngine.Rendering;
using System.Collections;
using System.Collections.Generic;
public partial class PostProcessingStack
{

    const string bufferName = "PostProcessingEffects";
    int fxSourceId = Shader.PropertyToID("_PostFXSource");
    CommandBuffer buffer = new CommandBuffer
    {
        name = bufferName
    };

    ScriptableRenderContext context;

    Camera camera;

    public bool IsActive = true;

    public void Setup(ScriptableRenderContext context, Camera camera)
    {
        this.context = context;
        this.camera = camera;
    }

    public void PostProcessingDrawing(PostProcessingCamera PPC, int sourceId)
    {
        //Drawing(sourceId, to, material, pass);
        createStack(PPC, sourceId);
        context.ExecuteCommandBuffer(buffer);
        buffer.Clear();
    }

    void Drawing(RenderTargetIdentifier from, RenderTargetIdentifier to, Material material, int pass)
    {
        //BuiltinRenderTextureType.CameraTarget
        buffer.SetGlobalTexture(fxSourceId, from);
        buffer.SetRenderTarget(to, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
        buffer.DrawProcedural(Matrix4x4.identity, material, pass,MeshTopology.Triangles, 3);
        
        buffer.ReleaseTemporaryRT(fxSourceId);
    }
    void createStack(PostProcessingCamera PPC, int sourceId)
    {

        int source = sourceId;
        int toID = (int)BuiltinRenderTextureType.CameraTarget;

        buffer.BeginSample("ProcessingEffects");
        List<Effect> effects = PPC.getMaterialList();
        for (int i = 0;i < effects.Count;i++ )
        {

                for (int pass = 0; pass < effects[i].passNumber; pass++)
                {

                buffer.GetTemporaryRT(source, camera.pixelWidth, camera.pixelHeight, 32, FilterMode.Bilinear, RenderTextureFormat.DefaultHDR);
                if (i ==0 && pass == 0)
                    Drawing(source, BuiltinRenderTextureType.CameraTarget, effects[i].material, pass);
                else
                    Drawing(source, BuiltinRenderTextureType.CameraTarget, effects[i].material, pass);

                    // buffer.Blit(source, toID);
                    source = source + 1;
                    toID += 1;
                    if (PPC.ConsoleDebug)
                        Debug.Log(effects[i].material.name +" | Pass: " + pass);
                }
            //}//
            //PPC.resetMaterialList();
        }

        buffer.EndSample("ProcessingEffects");
    }


}

Render effect:

void createStack(PostProcessingCamera PPC, int sourceId)
    {

        int source = sourceId;
        int toID = (int)BuiltinRenderTextureType.CameraTarget;

        buffer.BeginSample("ProcessingEffects");
        List<Effect> effects = PPC.getMaterialList();
        for (int i = 0;i < effects.Count;i++ )
        {

                for (int pass = 0; pass < effects[i].passNumber; pass++)
                {

                buffer.GetTemporaryRT(source, camera.pixelWidth, camera.pixelHeight, 32, FilterMode.Bilinear, RenderTextureFormat.DefaultHDR);
                if (i ==0 && pass == 0)
                    Drawing(source, BuiltinRenderTextureType.CameraTarget, effects[i].material, pass);
                else
                    Drawing(source, BuiltinRenderTextureType.CameraTarget, effects[i].material, pass);

                    // buffer.Blit(source, toID);
                    source = source + 1;
                    toID += 1;
                    if (PPC.ConsoleDebug)
                        Debug.Log(effects[i].material.name +" | Pass: " + pass);
                }
            //}//
            //PPC.resetMaterialList();
        }

        buffer.EndSample("ProcessingEffects");
    }

Draw effect:

 void Drawing(RenderTargetIdentifier from, RenderTargetIdentifier to, Material material, int pass)
    {
        //BuiltinRenderTextureType.CameraTarget
        buffer.SetGlobalTexture(fxSourceId, from);
        buffer.SetRenderTarget(to, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
        buffer.DrawProcedural(Matrix4x4.identity, material, pass,MeshTopology.Triangles, 3);
        
        buffer.ReleaseTemporaryRT(fxSourceId);
    }

Video of the problem: https://youtu.be/IRg2NdgICdo

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.