using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class PBRShaderGUI : ShaderGUI
{
    Material targetMat;
    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
    {
        targetMat = materialEditor.target as Material;

        GUIContent mainLabel = EditorGUIUtility.TrTextContent("Main(RGBA)", "Main:主贴图(RGBA)+主颜色(RGB)");
        materialEditor.TexturePropertySingleLine(mainLabel, FindProperty("_BaseMap", properties), FindProperty("_BaseColor", properties));
        materialEditor.TextureScaleOffsetProperty(FindProperty("_BaseMap", properties));

        if(KeywordGui("Alpha Test", "_ALPHATEST_ON", false))
        {
            materialEditor.RangeProperty(FindProperty("_Cutoff", properties), "threshold");
        }

        GUIContent normalLabel = EditorGUIUtility.TrTextContent("Normal", "Normal:法线贴图(RG)");
        materialEditor.TexturePropertySingleLine(normalLabel, FindProperty("_NormalMap", properties), FindProperty("_normalScale", properties));

        if (KeywordGui("Detail Normal", "_DETAILNOMAL", false))
        {
            GUIContent detailNormalLabel = EditorGUIUtility.TrTextContent("Detail Normal", "细节法线");
            GUIContent detailNormalTilingLabel = EditorGUIUtility.TrTextContent("Detail Normal Tiling", "细节法线uv缩放");
            materialEditor.TexturePropertyTwoLines( detailNormalLabel, FindProperty("_DetailNormalMap", properties), FindProperty("_detailNormalScale",properties),
                                                    detailNormalTilingLabel, FindProperty("_detailNormalTiling", properties));
        }

        if (KeywordGui("混合草地", "_USE_Blend", false))
        {
            GUIContent blendLabel = EditorGUIUtility.TrTextContent("Blend Texture", "草地贴图");
            materialEditor.TexturePropertySingleLine(blendLabel, FindProperty("_BlendTex", properties));
            materialEditor.FloatProperty(FindProperty("_BlendTexTilling", properties), "   Tilling");

            materialEditor.ColorProperty(FindProperty("_BlendColor", properties), "   颜色");
            materialEditor.RangeProperty(FindProperty("_BlendIntensity", properties), "   强度");
        }

        if (KeywordGui("视差贴图", "_POM_ON", false))
        {
            materialEditor.RangeProperty(FindProperty("_PomCount", properties), "   采样次数");
            materialEditor.RangeProperty(FindProperty("_PomScale", properties), "   高度缩放");
        }

        GUIContent packLabel = EditorGUIUtility.TrTextContent("Pack", "Metallic(R), Roughness(G), Emission(B)");
        materialEditor.TexturePropertySingleLine(packLabel, FindProperty("_PackMap", properties));
        materialEditor.RangeProperty(FindProperty("_Metallic", properties), "Metallic");
        materialEditor.RangeProperty(FindProperty("_Roughness", properties), "Roughness");
        materialEditor.RangeProperty(FindProperty("_Emission", properties), "Emission");


        KeywordGui("Specular Highlights", "_SPECULARHIGHLIGHTS_OFF", true);
        KeywordGui("Environment Reflections", "_ENVIRONMENTREFLECTIONS_OFF", true);
    }

    private bool KeywordGui(string label, string keyword, bool negate)
    {
        bool keywordEnable = (Array.IndexOf(targetMat.shaderKeywords, keyword) != -1) ^ negate;

        EditorGUI.BeginChangeCheck();
        keywordEnable = EditorGUILayout.Toggle(label, keywordEnable);
        if (EditorGUI.EndChangeCheck())
        {
            // enable or disable the keyword based on checkbox
            if (keywordEnable ^ negate)
                targetMat.EnableKeyword(keyword);
            else
                targetMat.DisableKeyword(keyword);
        }

        return keywordEnable;
    }
}
