Deferred Rendering SceneView Albedo-view mode compatible shader.

I am trying to write a shader for purposes of rendering controls for an editor extension. It’s working pretty well, except when I use the Albedo-view mode in the SceneView (available with deferred rendering only) anything that uses my shader is not being rendered at all.

C#:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

#if UNITY_EDITOR
[ExecuteInEditMode]
#endif
public class GLScript : MonoBehaviour {

    private Material lineMaterial;

    void Start() {
        lineMaterial = AssetDatabase.LoadAssetAtPath<Material> ("Assets/Resources/Materials/LineMaterial2.mat");
    }

    #if UNITY_EDITOR
    void OnRenderObject() {
        if (!Application.isEditor)
            return;

        // Lots of getting and null-checking
        if (lineMaterial == null)
            return;
        MeshFilter mf = GetComponent<MeshFilter> ();
        if (mf == null)
            return;
        Mesh m = mf.sharedMesh;
        if (m == null)
            return;
        int[] tris = m.triangles;
        Vector3[] verts = m.vertices;
        if (tris == null || verts == null)
            return;

        // Actual rendering
        GL.PushMatrix ();
        GL.modelview = Camera.current.worldToCameraMatrix * transform.localToWorldMatrix;
        lineMaterial.SetPass(0);
        for (int i = 0; i < tris.Length; i += 3) {
            GL.Begin (GL.TRIANGLES);
            GL.Vertex (verts [tris [i]]);
            GL.Vertex (verts [tris [i + 1]]);
            GL.Vertex (verts [tris [i + 2]]);
            GL.End ();
        }
        GL.PopMatrix ();
    }
    #endif
}

Shader:

Shader "Unlit/Transparent Color"
{
    Properties
    {
        _Color ("Color", COLOR) = (1, 1, 1, 1)
    }
    SubShader
    {
//        Tags { "RenderType"="Opaque" } // Whatever other RenderType values there are?
        Tags { "Queue"="Transparent" }
        LOD 100

        Pass
        {
            Blend DstColor SrcColor // 2x multiply
            Cull Back
            ZWrite Off
            ZTest LEqual
            Offset -1, -1
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
           
            #include "UnityCG.cginc"

            struct appdata {
                float4 vertex : POSITION;
            };

            struct v2f {
                float4 vertex : SV_POSITION;
            };

            fixed4 _Color;
           
            v2f vert (appdata v) {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }
           
            fixed4 frag (v2f i) : SV_Target {
                return _Color;
            }
            ENDCG
        }
    }
}

Comparison of Shaded and Albedo view modes:

How do I add support for rendering in this mode?

Transparent materials are rendered in Forward-Mode, even if the Rendering Path is set to Deferred. You can verify that using the Frame Debugger.

Therefore, my guess is that these transparent materials simply don’t write to the GBuffer and since the “Albedo” view is most likely just a view on a particular part of the gbuffer, these objects are missing.

Hope it makes sense :slight_smile:

1 Like

I was able to confirm your theory with surface shaders. However, the default Unlit shader (supposedly “Queue”=“Geometry”) has the same problem, i.e. the Unlit/Texture shader included with Unity. I am not able to get a non-transparent shader that uses vertex and fragment functions (i.e. is not a surface-shader) to render in albedo view mode.

I take away from this that this is not my bug and will just toggle off scene lighting when working in dark areas for now.

Sorry for necroing this.
But maybe someone can suggest on how to make transparent object render in deferred mode?
Edit: Figured out that it’s not really possible to render transparent object to the GBuffer due to how deferred algorithm works. Well it’s possible, but that will mess up the result.