Full analysis of Unity ShaderLab framework 3 -- Custom Shader GUI

Posted by embtech on Sun, 01 Mar 2020 13:20:30 +0100

Full analysis of Unity ShaderLab framework 3

This series mainly summarizes the framework of Unity ShaderLab according to the official documents of Unity and the knowledge collected by individuals, and compares the basis for improving the efficiency of knowledge learning and review. Thank you to the author of the link

Custom Shader GUI

Sometimes some attribute data of shaders cannot be displayed well on the editor panel. All Unity allows users to customize the display panel of shaders, such as adding other control methods.

Write the following statement outside the Subshader of ShaderLab:

CustomEditor "name"

Unity will automatically find a class named "name", which needs to inherit from the ShaderGUI class. Every shader that defines the above statement will instantiate an instance of "name" class and execute the code inside.

Let's explain the official case based on personal understanding:

Custom surface shaders:

Shader "Custom/Redify" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    __SubShader__ {
        Tags { "RenderType"="Opaque" }
        __LOD__ 200
        
        CGPROGRAM
        #pragma surface surf Lambert addshadow
        #pragma shader_feature REDIFY_ON //Defining keywords by compiling instructions with shader feature

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;

            #if REDIFY_ON				//If you define a keyword, change the Albedo color of the output
            o.Albedo.gb *= 0.5;
            #endif
        }
        ENDCG
    } 
    CustomEditor "CustomShaderGUI"
}

Custom ShaderGUI class, inherited from ShaderGUI:

using UnityEngine;
using UnityEditor;
using System;

public class CustomShaderGUI : ShaderGUI
{
    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
    {
        // render the default gui
        base.OnGUI(materialEditor, properties);

        Material targetMat = materialEditor.target as Material;

        // see if redify is set, and show a checkbox
        bool redify = Array.IndexOf(targetMat.shaderKeywords, "REDIFY_ON") != -1;
        EditorGUI.BeginChangeCheck();
        redify = EditorGUILayout.Toggle("Redify material", redify);
        if (EditorGUI.EndChangeCheck())
        {
            // enable or disable the keyword based on checkbox
            if (redify)
                targetMat.EnableKeyword("REDIFY_ON");
            else
                targetMat.DisableKeyword("REDIFY_ON");
        }
    }
}

As mentioned before, write the following statement in the shader file:

CustomEditor "CustomShaderGUI"

Unity finds the corresponding class and executes the code. In addition, the key is that the compiler is used in the shader to define the keyword redify UUN (this part is related to the compiler and the shader argument. For details, refer to this article: Getting started with Shader (21) Multiple Variants).

In a word, by controlling the definition of this keyword, we can produce shaders with no effect. Check in the OnGUI function of the CustomShaderGUI class. If the GUI changes (that is, the checkbox is manually changed), then according to the value of verify, control whether to define the redify UUN keyword in the shader, so as to control the output.

At present, it has not been applied to this knowledge, and will be implemented more complex later.

Published 7 original articles, won praise 7, visited 1208
Private letter follow

Topics: Unity Attribute