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?