본문으로 바로가기

Custom Material GUI

category Technical Report/Unity Scripts 2019. 8. 14. 18:01
반응형



Editor Label


Unity GUILayout class

Unity EditorStyles class


CatLike coding 샘플 코드



using UnityEngine;
using UnityEditor;

// https://catlikecoding.com/unity/tutorials/rendering/part-9/

public class CustomShaderGUI : ShaderGUI
{
    Material target;
    MaterialEditor editor;
    MaterialProperty[] properties;

    public override void OnGUI(MaterialEditor editor, MaterialProperty[] properties)
    {
        this.target = editor.target as Material;
        this.editor = editor;
        this.properties = properties;
        Main();   
    }       

    MaterialProperty FindProperty(string name)
    {
        return FindProperty(name, properties);
    }

    static GUIContent staticLabel = new GUIContent();

    static GUIContent MakeLabel(MaterialProperty property, string tooltip = null)
    {
        staticLabel.text = property.displayName;
        staticLabel.tooltip = tooltip;
        return staticLabel;

        // it gets from shader properties name to custom editor


    }

    void SetKeyword(string keyword, bool state)
    {

        if (state)
        {
            target.EnableKeyword(keyword);
        }
        else
        {
            target.DisableKeyword(keyword);
        }

    }

    void Main( )
    {
        GUILayout.Space(20);
        GUILayout.Label("Custom GUI Sample Code shader", EditorStyles.boldLabel);

        GUILayout.Space(10);
        GUILayout.Label("Legacy Lambert Shader", EditorStyles.label);
        GUILayout.Space(10);

        //MaterialProperty tint = FindProperty("_TintColor", properties);
        MaterialProperty mainTex = FindProperty("_MainTex", properties);

        GUIContent albedoLabel = new GUIContent(mainTex.displayName, "Albedo(RGB)");        
      //editor.TexturePropertySingleLine(albedoLabel, mainTex, tint);
      //editor.TexturePropertySingleLine(albedoLabel, mainTex, FindProperty("_TintColor"));
        editor.TexturePropertySingleLine(MakeLabel(mainTex, "Albedo(RGB)"), mainTex, FindProperty("_TintColor"));        

        Normal( );
        Brightness( );
        editor.TextureScaleOffsetProperty(mainTex);
        Emissive( );
    }

    void Normal( )
    {
        MaterialProperty map = FindProperty("_NormalMap");
        //editor.TexturePropertySingleLine(MakeLabel(map, "Normal Texture"), map, FindProperty("_BumpScale"));
        
        //Hidden Normal Intensity value when texture does not has.
        editor.TexturePropertySingleLine(MakeLabel(map, "Normal Texture"), map, map.textureValue ? FindProperty("_BumpScale") : null);
        
        //offsite code
        //editor.TexturePropertySingleLine(MakeLabel(map, "Normal Texture"), map, map.textureValue ? null : FindProperty("_BumpScale"));
        
    }


    void Brightness( )
    {
        MaterialProperty bright = FindProperty("_Brightness");
        EditorGUI.indentLevel += 2;
        //editor.ShaderProperty(bright,"Brightness");
        editor.ShaderProperty(bright, MakeLabel(bright, "Brightness value control"));
        EditorGUI.indentLevel -= 2;       
    }


    void Emissive( )
    {        
        MaterialProperty map = FindProperty("_EmiTex");
        EditorGUI.BeginChangeCheck();
        editor.TexturePropertySingleLine(MakeLabel(map,"Emissive texture"), map, map.textureValue ? FindProperty("_EmiInten") : null);

        if (EditorGUI.EndChangeCheck())
        {
            SetKeyword("EMISSION_ON", map.textureValue);
        }
        
    }

}





Shader "LitBase" {

    Properties {       
        _TintColor("TintColor", color) = (1,1,1,1)
        _Brightness("Brightness", Range(0,3)) = 1
        _MainTex ("Texture", 2D) = "white" {}
        [Normal]_NormalMap("Normal Texture", 2D) = "Bump" {}        
        _BumpScale("Bump Scale", Int) = 1
        
        [Toggle(EMISSION_ON)] _emi("Emissve ON", Int) = 0
        _EmiTex("Emissvie", 2D) = "black" {}
        _EmiInten("Emissive Intensity", Range(0,3)) = 1
    }

    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        
        CGPROGRAM
        #pragma surface surf Lambert addshadow
        #pragma shader_feature EMISSION_ON

        sampler2D _MainTex;
        sampler2D _NormalMap;
        sampler2D _EmiTex;

        struct Input {
            float2 uv_MainTex;
        };

        fixed4 _TintColor;
        half _BumpScale;
        half _EmiInten;

        void surf (Input IN, inout SurfaceOutput o) {
           
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            fixed3 nm = UnpackScaleNormal(tex2D(_NormalMap, IN.uv_MainTex), _BumpScale);
            fixed4 e = tex2D(_EmiTex, IN.uv_MainTex);
            
            o.Albedo = c.rgb * _TintColor;
            o.Normal = nm;
            o.Alpha = c.a;

           #if EMISSION_ON
            o.Emission = e * _EmiInten;           
           #endif
    
        }
        ENDCG
    }
   CustomEditor "CustomShaderGUI"

   Fallback "Diffuse"

}






반응형

'Technical Report > Unity Scripts' 카테고리의 다른 글

PCSS Light script  (0) 2019.09.15
Unity Legacy shadow map resolution control  (0) 2019.09.15
Unity Custom tube, shere Light  (0) 2018.12.04
Custom Inspectors and Scriptable Objects for UDIM materials  (0) 2018.02.10
Texture2D.Encode  (0) 2017.08.17