Broxxar / GlowingObjectOutlines

A technique for Glowing Object Outlines in Unity.
The Unlicense
161 stars 50 forks source link

Glow not working when using a Post Processing Layer #11

Closed Haapavuo closed 5 years ago

Haapavuo commented 5 years ago

If I add a Post Processing Layer component to my Camera, the glow no more works. What could cause this and how to fix it?

qq995002966 commented 5 years ago

hi, Haapavuo. I have been tring to modify this glowoutline repository recent days to be capacity with PostProcessing. The follow this thePostProcessingVersion GlowController of my work.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using System;
#if UNITY_EDITOR
using UnityEditor.Rendering.PostProcessing;

#endif

[Serializable]
[PostProcess(typeof(GlowOutlinePPRenderer), PostProcessEvent.BeforeStack, "Custom/GlowOutline")]
public class GlowOutlinePP : PostProcessEffectSettings {
    [Range(0.0f, 2.0f), Tooltip("Intensity")]
    public FloatParameter intensity = new FloatParameter{value = 1f};
}

public sealed class GlowOutlinePPRenderer : PostProcessEffectRenderer<GlowOutlinePP> {
    private static List<GlowObjectCmd> glowableObjects = new List<GlowObjectCmd>();

    private Vector2 blurTexelSize;
    private Shader glowCompositeShader = Shader.Find("Hidden/GlowComposite");
    private Shader glowCmdShader = Shader.Find("Hidden/GlowCmdShader");
    private Shader blurShader = Shader.Find("Hidden/Blur");
    private Material glowMat = new Material(Shader.Find("Hidden/GlowCmdShader"));
    private Material blurMaterial = new Material(Shader.Find("Hidden/Blur"));

    private int prePassRenderTexID = Shader.PropertyToID("_GlowPrePassTex");
    private int blurPassRenderTexID = Shader.PropertyToID("_GlowBlurredTex");
    private int tempRenderTexID = Shader.PropertyToID("_TempTex0");
    private int blurSizeID = Shader.PropertyToID("_BlurSize");
    private int glowColorID = Shader.PropertyToID("_GlowColor");
    private readonly float colorWriteThres = 0.01f;

    public static void RegisterObject(GlowObjectCmd glowObject){
        glowableObjects.Add(glowObject);
    }

    public static void UnregisterObject(GlowObjectCmd glowObject){
        glowableObjects.Remove(glowObject);
    }

    public override void Render(PostProcessRenderContext iContext){
        if (glowCompositeShader == null)
            return;
        var sheet = iContext.propertySheets.Get(glowCompositeShader);
        var cmd = iContext.command;

        cmd.BeginSample("GlowOutlinePP");

        cmd.GetTemporaryRT(prePassRenderTexID, Screen.width, Screen.height, 0, FilterMode.Bilinear,
            RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default, QualitySettings.antiAliasing);
        cmd.SetRenderTarget(prePassRenderTexID);
        cmd.ClearRenderTarget(true, true, Color.clear);

        foreach (var glowableObject in glowableObjects) {
            var curColor = glowableObject.currentColor;
            if (!(curColor.r < colorWriteThres && curColor.g < colorWriteThres && curColor.b < colorWriteThres)) {
                cmd.SetGlobalColor(glowColorID, glowableObject.currentColor);

                foreach (var render in glowableObject.Renderers) {
                    cmd.DrawRenderer(render, glowMat);
                }
            }
        }

        cmd.GetTemporaryRT(blurPassRenderTexID, Screen.width >> 1, Screen.height >> 1, 0,
            FilterMode.Bilinear);
        cmd.GetTemporaryRT(tempRenderTexID, Screen.width >> 1, Screen.height >> 1, 0, FilterMode.Bilinear);
        cmd.Blit(prePassRenderTexID, blurPassRenderTexID);

        blurTexelSize = new Vector2(1.5f / (Screen.width >> 1), 1.5f / (Screen.height >> 1));
        cmd.SetGlobalVector(blurSizeID, blurTexelSize);

        for (int i = 0; i < 4; i++) {
            cmd.Blit(blurPassRenderTexID, tempRenderTexID, blurMaterial, 0);
            cmd.Blit(tempRenderTexID, blurPassRenderTexID, blurMaterial, 1);
        }

        cmd.SetGlobalTexture("_GlowPrePassTex", prePassRenderTexID);
        cmd.SetGlobalTexture("_GlowBlurredTex", blurPassRenderTexID);

        sheet.properties.SetFloat("_Intensity", settings.intensity.value);
        cmd.BlitFullscreenTriangle(iContext.source, iContext.destination, sheet, 0);
        cmd.EndSample("GlowOutlinePP");
    }
}

#if UNITY_EDITOR
[PostProcessEditor(typeof(GlowOutlinePP))]
public class GlowOutlinePPEditor : PostProcessEffectEditor<GlowOutlinePP> {
    SerializedParameterOverride intensity;

    public override void OnEnable(){
        base.OnEnable();

        intensity = FindParameterOverride(x => x.intensity);
    }

    public override void OnInspectorGUI(){
        base.OnInspectorGUI();

        PropertyField(intensity);
    }
}
#endif

With this, you could add PostProcess GlowOutlinePP effect on PostProcessVolume. avatar Because of the busy work, I don't have enough time to explian the whole file and there must be some bugs. You could just read the source code and google it. Thank you.

Broxxar commented 5 years ago

Hi @Haapavuo, indeed this system was not built to work with the Post Processing Stack (in fact I believe the video I made and this code release before the v2 version of the stack). As such this isn't really an issue per se. With all new version of Unity, there will be new ways to do things. Even the Post FX Stack v2 can be considered out of date as it is not supported by the new HDRP. Hopefully @qq995002966's example can guide you in the right direction!