using System; using UnityEngine.Rendering; using System.Reflection; namespace UnityEngine.ProBuilder { /// /// Provides access to the built-in materials that ProBuilder uses. In the Editor, see /// for access to the full array of materials /// and shaders. /// public static class BuiltinMaterials { static bool s_IsInitialized; /// /// Represents the shader used to highlight selections. /// public const string faceShader = "Hidden/ProBuilder/FaceHighlight"; /// /// Represents the shader used to highlight selections. /// /// /// If the graphics device does not support geometry shaders, this shader doesn't compile. /// In that case, use . /// public const string lineShader = "Hidden/ProBuilder/LineBillboard"; /// /// Represents a line shader for use with `CreateEdgeBillboardMesh` when geometry /// shaders are not available. Use where possible. /// public const string lineShaderMetal = "Hidden/ProBuilder/LineBillboardMetal"; /// /// Represents the shader used to draw camera facing billboards from a single vertex. /// /// /// If the graphics device does not support geometry shaders, this shader doesn't compile. /// In that case, use . /// public const string pointShader = "Hidden/ProBuilder/PointBillboard"; /// /// Represents the fallback shader used to draw lines when the graphics device /// does not support geometry shaders. /// /// public const string wireShader = "Hidden/ProBuilder/FaceHighlight"; /// /// Represents a fallback shader used to draw billboards when the graphics /// device does not support geometry shaders. /// /// public const string dotShader = "Hidden/ProBuilder/VertexShader"; internal static readonly Color previewColor = new Color(.5f, .9f, 1f, .56f); static Shader s_SelectionPickerShader; static bool s_GeometryShadersSupported; static Material s_DefaultMaterial; static Material s_FacePickerMaterial; static Material s_VertexPickerMaterial; static Material s_EdgePickerMaterial; static Material s_UnityDefaultDiffuse; static Material s_ShapePreviewMaterial; #if PB_URP_MODE static string k_EdgePickerShader = "Hidden/ProBuilder/EdgePickerURP"; static string k_VertexPickerShader = "Hidden/ProBuilder/VertexPickerURP"; static string k_FacePickerShader = "Hidden/ProBuilder/FacePickerURP"; static string k_EdgePickerMaterial = "com.unity.probuilder/Materials/EdgePickerURP"; static string k_FacePickerMaterial = "com.unity.probuilder/Materials/FacePickerURP"; static string k_VertexPickerMaterial = "com.unity.probuilder/Materials/VertexPickerURP"; const string k_SelectionPickerShader = "Hidden/ProBuilder/SelectionPickerURP"; #else static string k_EdgePickerShader = "Hidden/ProBuilder/EdgePicker"; static string k_FacePickerShader = "Hidden/ProBuilder/FacePicker"; static string k_VertexPickerShader = "Hidden/ProBuilder/VertexPicker"; static string k_EdgePickerMaterial = "com.unity.probuilder/Materials/EdgePicker"; static string k_FacePickerMaterial = "com.unity.probuilder/Materials/FacePicker"; static string k_VertexPickerMaterial = "com.unity.probuilder/Materials/VertexPicker"; const string k_SelectionPickerShader = "Hidden/ProBuilder/SelectionPicker"; #endif #if UNITY_EDITOR [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] static void ResetStaticsOnLoad() { // Reset the static variables to their initial values, these 3 static variables will be re-initialized when accessed s_DefaultMaterial = null; s_UnityDefaultDiffuse = null; s_ShapePreviewMaterial = null; // Re-initialize the rest of the static variables s_IsInitialized = false; Init(); } #endif static void Init() { if (s_IsInitialized) return; s_IsInitialized = true; var geo = Shader.Find(lineShader); s_GeometryShadersSupported = geo != null && geo.isSupported; // SelectionPicker shader s_SelectionPickerShader = (Shader)Shader.Find(k_SelectionPickerShader); if ((s_FacePickerMaterial = Resources.Load(k_FacePickerMaterial)) == null) { Log.Error("FacePicker material not loaded... please re-install ProBuilder to fix this error."); s_FacePickerMaterial = new Material(Shader.Find(k_FacePickerShader)); } if ((s_VertexPickerMaterial = Resources.Load(k_VertexPickerMaterial)) == null) { Log.Error("VertexPicker material not loaded... please re-install ProBuilder to fix this error."); s_VertexPickerMaterial = new Material(Shader.Find(k_VertexPickerShader)); } if ((s_EdgePickerMaterial = Resources.Load(k_EdgePickerMaterial)) == null) { Log.Error("EdgePicker material not loaded... please re-install ProBuilder to fix this error."); s_EdgePickerMaterial = new Material(Shader.Find(k_EdgePickerShader)); } } /// /// Tests whether the current graphics device supports geometry shaders. /// /// True if the current graphics device supports geometry shaders, and false if it does not. public static bool geometryShadersSupported { get { Init(); return s_GeometryShadersSupported; } } /// /// Represents the default ProBuilder material. /// /// /// When using the Scriptable Render Pipeline this returns the default material for that pipeline. /// public static Material defaultMaterial { get { Init(); if(s_DefaultMaterial == null) s_DefaultMaterial = GetDefaultMaterial(); return s_DefaultMaterial; } } /// /// Represents the shader used in selection picking functions. /// internal static Shader selectionPickerShader { get { Init(); return s_SelectionPickerShader; } } /// /// Represents the material used for face picking functions. /// internal static Material facePickerMaterial { get { Init(); return s_FacePickerMaterial; } } /// /// Represents the material used for vertex picking functions. /// internal static Material vertexPickerMaterial { get { Init(); return s_VertexPickerMaterial; } } /// /// Represents the material used for edge picking functions. /// internal static Material edgePickerMaterial { get { Init(); return s_EdgePickerMaterial; } } /// /// Represents the material used for the ProBuilder Trigger /// entity type material. /// internal static Material triggerMaterial { get { Init(); return (Material)Resources.Load("Materials/Trigger", typeof(Material)); } } /// /// Represents the material used for the ProBuilder Collider /// entity type material. /// internal static Material colliderMaterial { get { Init(); return (Material)Resources.Load("Materials/Collider", typeof(Material)); } } /// /// Represents the default Unity diffuse material. /// internal static Material GetLegacyDiffuse() { Init(); if (s_UnityDefaultDiffuse == null) { var mi = typeof(Material).GetMethod("GetDefaultMaterial", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); if (mi != null) s_UnityDefaultDiffuse = mi.Invoke(null, null) as Material; if (s_UnityDefaultDiffuse == null) { var go = GameObject.CreatePrimitive(PrimitiveType.Cube); s_UnityDefaultDiffuse = go.GetComponent().sharedMaterial; Object.DestroyImmediate(go); } } return s_UnityDefaultDiffuse; } internal static Material GetDefaultMaterial() { var material = (Material) Resources.Load("Materials/ProBuilderDefault", typeof(Material)); material.shader = Shader.Find("ProBuilder6/Standard Vertex Color"); if (material == null || !material.shader.isSupported) material = GetLegacyDiffuse(); return material; } static Material GetPreviewMaterial() { if (defaultMaterial == null) return null; var material = new Material(defaultMaterial.shader); material.hideFlags = HideFlags.HideAndDontSave; if (material.HasProperty("_MainTex")) material.mainTexture = (Texture2D)Resources.Load("Textures/GridBox_Default"); if (material.HasProperty("_Color")) material.SetColor("_Color", previewColor); return material; } internal static Material ShapePreviewMaterial { get { if(s_ShapePreviewMaterial == null) s_ShapePreviewMaterial = GetPreviewMaterial(); return s_ShapePreviewMaterial; } } } }