Search Unity

Deferred Rendering SceneView Albedo-view mode compatible shader.

Discussion in 'Shaders' started by Deleted User, Jun 19, 2017.

  1. Deleted User

    Deleted User

    Guest

    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#:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. #if UNITY_EDITOR
    7. [ExecuteInEditMode]
    8. #endif
    9. public class GLScript : MonoBehaviour {
    10.  
    11.     private Material lineMaterial;
    12.  
    13.     void Start() {
    14.         lineMaterial = AssetDatabase.LoadAssetAtPath<Material> ("Assets/Resources/Materials/LineMaterial2.mat");
    15.     }
    16.  
    17.     #if UNITY_EDITOR
    18.     void OnRenderObject() {
    19.         if (!Application.isEditor)
    20.             return;
    21.  
    22.         // Lots of getting and null-checking
    23.         if (lineMaterial == null)
    24.             return;
    25.         MeshFilter mf = GetComponent<MeshFilter> ();
    26.         if (mf == null)
    27.             return;
    28.         Mesh m = mf.sharedMesh;
    29.         if (m == null)
    30.             return;
    31.         int[] tris = m.triangles;
    32.         Vector3[] verts = m.vertices;
    33.         if (tris == null || verts == null)
    34.             return;
    35.  
    36.         // Actual rendering
    37.         GL.PushMatrix ();
    38.         GL.modelview = Camera.current.worldToCameraMatrix * transform.localToWorldMatrix;
    39.         lineMaterial.SetPass(0);
    40.         for (int i = 0; i < tris.Length; i += 3) {
    41.             GL.Begin (GL.TRIANGLES);
    42.             GL.Vertex (verts [tris [i]]);
    43.             GL.Vertex (verts [tris [i + 1]]);
    44.             GL.Vertex (verts [tris [i + 2]]);
    45.             GL.End ();
    46.         }
    47.         GL.PopMatrix ();
    48.     }
    49.     #endif
    50. }
    Shader:
    Code (csharp):
    1. Shader "Unlit/Transparent Color"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", COLOR) = (1, 1, 1, 1)
    6.     }
    7.     SubShader
    8.     {
    9. //        Tags { "RenderType"="Opaque" } // Whatever other RenderType values there are?
    10.         Tags { "Queue"="Transparent" }
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.             Blend DstColor SrcColor // 2x multiply
    16.             Cull Back
    17.             ZWrite Off
    18.             ZTest LEqual
    19.             Offset -1, -1
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.            
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata {
    27.                 float4 vertex : POSITION;
    28.             };
    29.  
    30.             struct v2f {
    31.                 float4 vertex : SV_POSITION;
    32.             };
    33.  
    34.             fixed4 _Color;
    35.            
    36.             v2f vert (appdata v) {
    37.                 v2f o;
    38.                 o.vertex = UnityObjectToClipPos(v.vertex);
    39.                 return o;
    40.             }
    41.            
    42.             fixed4 frag (v2f i) : SV_Target {
    43.                 return _Color;
    44.             }
    45.             ENDCG
    46.         }
    47.     }
    48. }
    Comparison of Shaded and Albedo view modes:


    How do I add support for rendering in this mode?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    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 :)
     
    xVergilx likes this.
  3. Deleted User

    Deleted User

    Guest

    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.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    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.
     
    Last edited: Jan 12, 2019