Search Unity

SubShader Tags: Rendering Order - Variable Queue tag

Discussion in 'Shaders' started by PvtHudson, Oct 21, 2010.

  1. PvtHudson

    PvtHudson

    Joined:
    Oct 8, 2010
    Posts:
    20
    Hello,

    Id like to know how can I define a Queue.

    I have a shader with this property:
    Code (csharp):
    1.  
    2. Shader "MyShader" {
    3.     Properties {
    4.         _RenderQueue ("RenderQueue Increment", float) = 0
    5.     }
    6.  
    And, Id like use this property to define the Rendering Order. Like this:

    Code (csharp):
    1.  
    2. SubShader {
    3.         Tags { "Queue" = "Transparent + [_RenderQueue]"  } //Gives me a Undefined Queue: 'Transparent + [_RenderQueue]'
    4.  
    I tried a "#define RQ [_RenderQueue]" with no luck, maybe the syntax is wrong?

    How can I solve this?

    Also bonus question, can I force _RenderQueue to be an integer?
     
  2. PvtHudson

    PvtHudson

    Joined:
    Oct 8, 2010
    Posts:
    20
    Anyone knows how can I do this?
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    You can only specify constant render queues in a given shader. However, you can set Material.renderQueue for material instances.
     
  4. PvtHudson

    PvtHudson

    Joined:
    Oct 8, 2010
    Posts:
    20
    Ok, looks like I'll be using renderer.sharedMaterial.renderQueue then.

    Thanks Daniel.
     
  5. Draco18s

    Draco18s

    Joined:
    Aug 15, 2011
    Posts:
    110
    "Come, my minions! Rise, thread, for your master! Let your evil shine!"

    Found this thread trying to accomplish the same thing and thought I'd share what looks like a solution utilizing a custom editor script. Figured I'd necro this thread and supply a good solution for anyone from The Future.

    First is the custom inspector script. This is identical to the default one in the documentation except for lines 78-81 (last four lines of code).

    Following it is a duplicate of a default unlit transparent shader, with the exception of an additional Int parameter to control the render queue value. Its this property that is read out by line 80 and sets the renderQueue on the material itself. Default value of 3000 is identical to the default Transparent queue value, according to Unity's documentation.

    The last line of the shader (line 52) is what connects the custom inspector script, by class name. The same inspector class can be used for multiple shaders, as long as they include that linkage (which, apparently, must come after the subshader; specifying it first causes a parsing error). Because the shader references the inspector class, the queue depth value isn't specified in the inspector, but the shader: even if it was part of the inspector class, you'd still need the custom shader anyway.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Linq;
    5.  
    6. public class CustomMatInspector : MaterialEditor {
    7.  
    8.     // this is the same as the ShaderProperty function, show here so
    9.     // you can see how it works
    10.     private void ShaderPropertyImpl(Shader shader, int propertyIndex)
    11.     {
    12.         int i = propertyIndex;
    13.         string label = ShaderUtil.GetPropertyDescription(shader, i);
    14.         string propertyName = ShaderUtil.GetPropertyName(shader, i);
    15.         switch (ShaderUtil.GetPropertyType(shader, i))
    16.         {
    17.             case ShaderUtil.ShaderPropertyType.Range: // float ranges
    18.             {
    19.                 GUILayout.BeginHorizontal();
    20.                 float v2 = ShaderUtil.GetRangeLimits(shader, i, 1);
    21.                 float v3 = ShaderUtil.GetRangeLimits(shader, i, 2);
    22.                 RangeProperty(propertyName, label, v2, v3);
    23.                 GUILayout.EndHorizontal();
    24.  
    25.                 break;
    26.             }
    27.             case ShaderUtil.ShaderPropertyType.Float: // floats
    28.             {
    29.                 FloatProperty(propertyName, label);
    30.                 break;
    31.             }
    32.             case ShaderUtil.ShaderPropertyType.Color: // colors
    33.             {
    34.                 ColorProperty(propertyName, label);
    35.                 break;
    36.             }
    37.             case ShaderUtil.ShaderPropertyType.TexEnv: // textures
    38.             {
    39.                 ShaderUtil.ShaderPropertyTexDim desiredTexdim = ShaderUtil.GetTexDim(shader, i);
    40.                 TextureProperty(propertyName, label, desiredTexdim);
    41.  
    42.                 GUILayout.Space(6);
    43.                 break;
    44.             }
    45.             case ShaderUtil.ShaderPropertyType.Vector: // vectors
    46.             {
    47.                 VectorProperty(propertyName, label);
    48.                 break;
    49.             }
    50.             default:
    51.             {
    52.                 GUILayout.Label("ARGH" + label + " : " + ShaderUtil.GetPropertyType(shader, i));
    53.                 break;
    54.             }
    55.         }
    56.     }
    57.  
    58.     public override void OnInspectorGUI ()
    59.     {
    60.         serializedObject.Update ();
    61.         var theShader = serializedObject.FindProperty ("m_Shader");
    62.         if (isVisible && !theShader.hasMultipleDifferentValues && theShader.objectReferenceValue != null)
    63.         {
    64.             float controlSize = 64;
    65.  
    66.             EditorGUIUtility.LookLikeControls(Screen.width - controlSize - 20);
    67.  
    68.             EditorGUI.BeginChangeCheck();
    69.             Shader shader = theShader.objectReferenceValue as Shader;
    70.  
    71.             for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); i++)
    72.             {
    73.                 ShaderPropertyImpl(shader, i);
    74.             }
    75.  
    76.             if (EditorGUI.EndChangeCheck())
    77.                 PropertiesChanged ();
    78.             if(target is Material) {
    79.                 Material m = (Material)target;
    80.                 m.renderQueue = m.GetInt("_Depth");
    81.             }
    82.         }
    83.     }
    84. }
    Code (csharp):
    1.  
    2. Shader "Unlit/Transparent Queued" {
    3.    Properties {
    4.     _Color ("Color Tint (A = Opacity)", Color) = (1,1,1,1)
    5.      _MainTex ("Base (RGB)", 2D) = "white" {}
    6.      _Depth ("Render Depth", Int) = 3000
    7.    }
    8.    SubShader {
    9.      Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.      Blend SrcAlpha OneMinusSrcAlpha
    11.      LOD 100
    12.  
    13.      Lighting Off
    14.  
    15.      Pass {
    16.        CGPROGRAM
    17.          #pragma vertex vert
    18.          #pragma fragment frag
    19.        
    20.          #include "UnityCG.cginc"
    21.  
    22.          struct appdata_t {
    23.            float4 vertex : POSITION;
    24.            float2 texcoord : TEXCOORD0;
    25.          };
    26.  
    27.          struct v2f {
    28.            float4 vertex : SV_POSITION;
    29.            half2 texcoord : TEXCOORD0;
    30.          };
    31.  
    32.          sampler2D _MainTex;
    33.          float4 _MainTex_ST;
    34.          fixed4 _Color;
    35.  
    36.          v2f vert (appdata_t v)
    37.          {
    38.            v2f o;
    39.            o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    40.            o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    41.            return o;
    42.          }
    43.        
    44.          fixed4 frag (v2f i) : COLOR
    45.          {
    46.            fixed4 col = tex2D(_MainTex, i.texcoord) * _Color;
    47.            return col;
    48.          }
    49.        ENDCG
    50.      }
    51.    }
    52.    CustomEditor "CustomMatInspector"
    53. }
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Since Material.renderQueue is a serialized property, I think you'd be better off just using the debug inspector to change it for your material asset.