From dfe381773a17a4b6b7d44012d7439f9eafe94189 Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Sun, 27 Sep 2020 23:36:07 -0500 Subject: [PATCH 01/44] Implement material browser window + Adds material browser window * Moves preview materials to their own folder (allows simplified filtering in search) * Modified labels on default materials --- .../Package Resources/Floor.mat.meta | 5 +- .../Package Resources/Preview Materials.meta | 8 + .../{ => Preview Materials}/CastShadows.mat | 0 .../CastShadows.mat.meta | 0 .../{ => Preview Materials}/CastShadows.png | Bin .../CastShadows.png.meta | 0 .../{ => Preview Materials}/Collider.mat | 0 .../{ => Preview Materials}/Collider.mat.meta | 0 .../{ => Preview Materials}/Collider.png | Bin .../{ => Preview Materials}/Collider.png.meta | 0 .../{ => Preview Materials}/Culled.mat | 0 .../{ => Preview Materials}/Culled.mat.meta | 0 .../{ => Preview Materials}/Culled.png | Bin .../{ => Preview Materials}/Culled.png.meta | 0 .../{ => Preview Materials}/Discarded.mat | 0 .../Discarded.mat.meta | 0 .../{ => Preview Materials}/Discarded.png | Bin .../Discarded.png.meta | 0 .../{ => Preview Materials}/Invisible.mat | 0 .../Invisible.mat.meta | 0 .../{ => Preview Materials}/Invisible.png | Bin .../Invisible.png.meta | 0 .../ReceiveShadows.mat | 0 .../ReceiveShadows.mat.meta | 0 .../ReceiveShadows.png | Bin .../ReceiveShadows.png.meta | 0 .../{ => Preview Materials}/ShadowOnly.mat | 0 .../ShadowOnly.mat.meta | 0 .../{ => Preview Materials}/ShadowOnly.png | Bin .../ShadowOnly.png.meta | 0 .../{ => Preview Materials}/Trigger.mat | 0 .../{ => Preview Materials}/Trigger.mat.meta | 0 .../{ => Preview Materials}/Trigger.png | Bin .../{ => Preview Materials}/Trigger.png.meta | 0 .../Package Resources/Step.mat.meta | 5 +- .../Package Resources/Tread.mat.meta | 5 +- .../Package Resources/Wall.mat.meta | 6 +- .../Chisel/Editor/Editor/MaterialBrowser.meta | 3 + .../MaterialBrowser/MaterialBrowserTile.cs | 20 ++ .../MaterialBrowserTile.cs.meta | 3 + .../MaterialBrowser/MaterialBrowserWindow.cs | 258 ++++++++++++++++++ .../MaterialBrowserWindow.cs.meta | 3 + Packages/manifest.json | 1 + 43 files changed, 309 insertions(+), 8 deletions(-) create mode 100644 Packages/com.chisel.components/Package Resources/Preview Materials.meta rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/CastShadows.mat (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/CastShadows.mat.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/CastShadows.png (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/CastShadows.png.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Collider.mat (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Collider.mat.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Collider.png (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Collider.png.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Culled.mat (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Culled.mat.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Culled.png (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Culled.png.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Discarded.mat (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Discarded.mat.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Discarded.png (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Discarded.png.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Invisible.mat (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Invisible.mat.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Invisible.png (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Invisible.png.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/ReceiveShadows.mat (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/ReceiveShadows.mat.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/ReceiveShadows.png (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/ReceiveShadows.png.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/ShadowOnly.mat (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/ShadowOnly.mat.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/ShadowOnly.png (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/ShadowOnly.png.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Trigger.mat (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Trigger.mat.meta (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Trigger.png (100%) rename Packages/com.chisel.components/Package Resources/{ => Preview Materials}/Trigger.png.meta (100%) create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs.meta diff --git a/Packages/com.chisel.components/Package Resources/Floor.mat.meta b/Packages/com.chisel.components/Package Resources/Floor.mat.meta index 7756b0d8c..2745c115a 100644 --- a/Packages/com.chisel.components/Package Resources/Floor.mat.meta +++ b/Packages/com.chisel.components/Package Resources/Floor.mat.meta @@ -1,8 +1,9 @@ fileFormatVersion: 2 guid: 15039fb859f8cd64d824af5a2743f20f -timeCreated: 1532081897 -licenseType: Free +labels: +- Chisel NativeFormatImporter: + externalObjects: {} mainObjectFileID: -1 userData: assetBundleName: diff --git a/Packages/com.chisel.components/Package Resources/Preview Materials.meta b/Packages/com.chisel.components/Package Resources/Preview Materials.meta new file mode 100644 index 000000000..80cf9ff64 --- /dev/null +++ b/Packages/com.chisel.components/Package Resources/Preview Materials.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cb07b700fbffd8c4bb55d56b76aa7aea +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.chisel.components/Package Resources/CastShadows.mat b/Packages/com.chisel.components/Package Resources/Preview Materials/CastShadows.mat similarity index 100% rename from Packages/com.chisel.components/Package Resources/CastShadows.mat rename to Packages/com.chisel.components/Package Resources/Preview Materials/CastShadows.mat diff --git a/Packages/com.chisel.components/Package Resources/CastShadows.mat.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/CastShadows.mat.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/CastShadows.mat.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/CastShadows.mat.meta diff --git a/Packages/com.chisel.components/Package Resources/CastShadows.png b/Packages/com.chisel.components/Package Resources/Preview Materials/CastShadows.png similarity index 100% rename from Packages/com.chisel.components/Package Resources/CastShadows.png rename to Packages/com.chisel.components/Package Resources/Preview Materials/CastShadows.png diff --git a/Packages/com.chisel.components/Package Resources/CastShadows.png.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/CastShadows.png.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/CastShadows.png.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/CastShadows.png.meta diff --git a/Packages/com.chisel.components/Package Resources/Collider.mat b/Packages/com.chisel.components/Package Resources/Preview Materials/Collider.mat similarity index 100% rename from Packages/com.chisel.components/Package Resources/Collider.mat rename to Packages/com.chisel.components/Package Resources/Preview Materials/Collider.mat diff --git a/Packages/com.chisel.components/Package Resources/Collider.mat.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Collider.mat.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Collider.mat.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Collider.mat.meta diff --git a/Packages/com.chisel.components/Package Resources/Collider.png b/Packages/com.chisel.components/Package Resources/Preview Materials/Collider.png similarity index 100% rename from Packages/com.chisel.components/Package Resources/Collider.png rename to Packages/com.chisel.components/Package Resources/Preview Materials/Collider.png diff --git a/Packages/com.chisel.components/Package Resources/Collider.png.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Collider.png.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Collider.png.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Collider.png.meta diff --git a/Packages/com.chisel.components/Package Resources/Culled.mat b/Packages/com.chisel.components/Package Resources/Preview Materials/Culled.mat similarity index 100% rename from Packages/com.chisel.components/Package Resources/Culled.mat rename to Packages/com.chisel.components/Package Resources/Preview Materials/Culled.mat diff --git a/Packages/com.chisel.components/Package Resources/Culled.mat.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Culled.mat.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Culled.mat.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Culled.mat.meta diff --git a/Packages/com.chisel.components/Package Resources/Culled.png b/Packages/com.chisel.components/Package Resources/Preview Materials/Culled.png similarity index 100% rename from Packages/com.chisel.components/Package Resources/Culled.png rename to Packages/com.chisel.components/Package Resources/Preview Materials/Culled.png diff --git a/Packages/com.chisel.components/Package Resources/Culled.png.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Culled.png.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Culled.png.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Culled.png.meta diff --git a/Packages/com.chisel.components/Package Resources/Discarded.mat b/Packages/com.chisel.components/Package Resources/Preview Materials/Discarded.mat similarity index 100% rename from Packages/com.chisel.components/Package Resources/Discarded.mat rename to Packages/com.chisel.components/Package Resources/Preview Materials/Discarded.mat diff --git a/Packages/com.chisel.components/Package Resources/Discarded.mat.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Discarded.mat.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Discarded.mat.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Discarded.mat.meta diff --git a/Packages/com.chisel.components/Package Resources/Discarded.png b/Packages/com.chisel.components/Package Resources/Preview Materials/Discarded.png similarity index 100% rename from Packages/com.chisel.components/Package Resources/Discarded.png rename to Packages/com.chisel.components/Package Resources/Preview Materials/Discarded.png diff --git a/Packages/com.chisel.components/Package Resources/Discarded.png.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Discarded.png.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Discarded.png.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Discarded.png.meta diff --git a/Packages/com.chisel.components/Package Resources/Invisible.mat b/Packages/com.chisel.components/Package Resources/Preview Materials/Invisible.mat similarity index 100% rename from Packages/com.chisel.components/Package Resources/Invisible.mat rename to Packages/com.chisel.components/Package Resources/Preview Materials/Invisible.mat diff --git a/Packages/com.chisel.components/Package Resources/Invisible.mat.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Invisible.mat.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Invisible.mat.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Invisible.mat.meta diff --git a/Packages/com.chisel.components/Package Resources/Invisible.png b/Packages/com.chisel.components/Package Resources/Preview Materials/Invisible.png similarity index 100% rename from Packages/com.chisel.components/Package Resources/Invisible.png rename to Packages/com.chisel.components/Package Resources/Preview Materials/Invisible.png diff --git a/Packages/com.chisel.components/Package Resources/Invisible.png.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Invisible.png.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Invisible.png.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Invisible.png.meta diff --git a/Packages/com.chisel.components/Package Resources/ReceiveShadows.mat b/Packages/com.chisel.components/Package Resources/Preview Materials/ReceiveShadows.mat similarity index 100% rename from Packages/com.chisel.components/Package Resources/ReceiveShadows.mat rename to Packages/com.chisel.components/Package Resources/Preview Materials/ReceiveShadows.mat diff --git a/Packages/com.chisel.components/Package Resources/ReceiveShadows.mat.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/ReceiveShadows.mat.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/ReceiveShadows.mat.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/ReceiveShadows.mat.meta diff --git a/Packages/com.chisel.components/Package Resources/ReceiveShadows.png b/Packages/com.chisel.components/Package Resources/Preview Materials/ReceiveShadows.png similarity index 100% rename from Packages/com.chisel.components/Package Resources/ReceiveShadows.png rename to Packages/com.chisel.components/Package Resources/Preview Materials/ReceiveShadows.png diff --git a/Packages/com.chisel.components/Package Resources/ReceiveShadows.png.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/ReceiveShadows.png.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/ReceiveShadows.png.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/ReceiveShadows.png.meta diff --git a/Packages/com.chisel.components/Package Resources/ShadowOnly.mat b/Packages/com.chisel.components/Package Resources/Preview Materials/ShadowOnly.mat similarity index 100% rename from Packages/com.chisel.components/Package Resources/ShadowOnly.mat rename to Packages/com.chisel.components/Package Resources/Preview Materials/ShadowOnly.mat diff --git a/Packages/com.chisel.components/Package Resources/ShadowOnly.mat.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/ShadowOnly.mat.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/ShadowOnly.mat.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/ShadowOnly.mat.meta diff --git a/Packages/com.chisel.components/Package Resources/ShadowOnly.png b/Packages/com.chisel.components/Package Resources/Preview Materials/ShadowOnly.png similarity index 100% rename from Packages/com.chisel.components/Package Resources/ShadowOnly.png rename to Packages/com.chisel.components/Package Resources/Preview Materials/ShadowOnly.png diff --git a/Packages/com.chisel.components/Package Resources/ShadowOnly.png.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/ShadowOnly.png.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/ShadowOnly.png.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/ShadowOnly.png.meta diff --git a/Packages/com.chisel.components/Package Resources/Trigger.mat b/Packages/com.chisel.components/Package Resources/Preview Materials/Trigger.mat similarity index 100% rename from Packages/com.chisel.components/Package Resources/Trigger.mat rename to Packages/com.chisel.components/Package Resources/Preview Materials/Trigger.mat diff --git a/Packages/com.chisel.components/Package Resources/Trigger.mat.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Trigger.mat.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Trigger.mat.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Trigger.mat.meta diff --git a/Packages/com.chisel.components/Package Resources/Trigger.png b/Packages/com.chisel.components/Package Resources/Preview Materials/Trigger.png similarity index 100% rename from Packages/com.chisel.components/Package Resources/Trigger.png rename to Packages/com.chisel.components/Package Resources/Preview Materials/Trigger.png diff --git a/Packages/com.chisel.components/Package Resources/Trigger.png.meta b/Packages/com.chisel.components/Package Resources/Preview Materials/Trigger.png.meta similarity index 100% rename from Packages/com.chisel.components/Package Resources/Trigger.png.meta rename to Packages/com.chisel.components/Package Resources/Preview Materials/Trigger.png.meta diff --git a/Packages/com.chisel.components/Package Resources/Step.mat.meta b/Packages/com.chisel.components/Package Resources/Step.mat.meta index da7daa58a..e77e6034e 100644 --- a/Packages/com.chisel.components/Package Resources/Step.mat.meta +++ b/Packages/com.chisel.components/Package Resources/Step.mat.meta @@ -1,8 +1,9 @@ fileFormatVersion: 2 guid: d46a258d2f81e1c42bc91a293e9c0ed0 -timeCreated: 1542225161 -licenseType: Free +labels: +- Chisel NativeFormatImporter: + externalObjects: {} mainObjectFileID: 2100000 userData: assetBundleName: diff --git a/Packages/com.chisel.components/Package Resources/Tread.mat.meta b/Packages/com.chisel.components/Package Resources/Tread.mat.meta index b3fd8cc9a..ec4239c5e 100644 --- a/Packages/com.chisel.components/Package Resources/Tread.mat.meta +++ b/Packages/com.chisel.components/Package Resources/Tread.mat.meta @@ -1,8 +1,9 @@ fileFormatVersion: 2 guid: 6060f6e211f81ee4eb7d3f50d73265fc -timeCreated: 1542225176 -licenseType: Free +labels: +- Chisel NativeFormatImporter: + externalObjects: {} mainObjectFileID: 2100000 userData: assetBundleName: diff --git a/Packages/com.chisel.components/Package Resources/Wall.mat.meta b/Packages/com.chisel.components/Package Resources/Wall.mat.meta index 28611fd5d..6ab676ded 100644 --- a/Packages/com.chisel.components/Package Resources/Wall.mat.meta +++ b/Packages/com.chisel.components/Package Resources/Wall.mat.meta @@ -1,8 +1,10 @@ fileFormatVersion: 2 guid: ca37785d773692b47bd7339c9dd802ce -timeCreated: 1452705603 -licenseType: Pro +labels: +- Chisel NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 userData: assetBundleName: assetBundleVariant: diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser.meta new file mode 100644 index 000000000..c58781eaa --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 08acfa0371d942ccaf74e4a71aec087c +timeCreated: 1601158709 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs new file mode 100644 index 000000000..3150b3f5b --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs @@ -0,0 +1,20 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.MaterialBrowserTile.cs + +License: MIT (https://tldrlegal.com/license/mit-license) +Author: Daniel Cornelius + +* * * * * * * * * * * * * * * * * * * * * */ + +using UnityEngine; + +namespace Chisel.Editors +{ + internal struct MaterialBrowserTile + { + public string path; + public Material material; + public Texture2D preview; + public string[] labels; + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs.meta new file mode 100644 index 000000000..dcbcf17d0 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9ee8077204fd4eb58fc337860cc79f21 +timeCreated: 1601238411 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs new file mode 100644 index 000000000..0f27e00c2 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs @@ -0,0 +1,258 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.MaterialBrowserWindow.cs + +License: MIT (https://tldrlegal.com/license/mit-license) +Author: Daniel Cornelius + +$TODO: Optimize away all the GUILayout logic +$TODO: Implement culling for tiles not visible (hide them if they arent within the viewable window area) +$TODO: Do we want to filter by label, too? it would allow user-ignored materials. +$TODO: Optimize optimize optimize + +* * * * * * * * * * * * * * * * * * * * * */ + +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +namespace Chisel.Editors +{ + internal class MaterialBrowserWindow : EditorWindow + { + private Vector2 m_PreviewsScrollPosition = Vector2.zero; + private Vector2 m_LabelsScrollPosition = Vector2.zero; + + private static int m_PreviewSize = 128; + private static string m_SearchFieldText = string.Empty; + private static string m_LabelSearchText = string.Empty; + + private static List m_Materials = new List(); + private static List m_Labels = new List(); + + private const string PREVIEW_SIZE_PREF_NAME = "chisel_matbrowser_pviewSize"; + + [MenuItem( "Window/Chisel/Material Browser" )] + private static void Init() + { + MaterialBrowserWindow window = EditorWindow.GetWindow( false, "Material Browser" ); + window.maxSize = new Vector2( 1920, 2000 ); + window.minSize = new Vector2( 200, 100 ); + + m_PreviewSize = EditorPrefs.GetInt( PREVIEW_SIZE_PREF_NAME, 128 ); + + GetMaterials(); + } + + private void OnEnable() + { + m_PreviewSize = EditorPrefs.GetInt( PREVIEW_SIZE_PREF_NAME, 128 ); + + if( m_Materials == null || m_Materials.Count < 1 ) + GetMaterials(); + } + + private void OnGUI() + { + Rect rect = this.position; + + // toolbar + using( GUILayout.HorizontalScope hScope = new GUILayout.HorizontalScope( EditorStyles.toolbar, GUILayout.ExpandWidth( true ) ) ) + { + if( GUILayout.Button( "Refresh", EditorStyles.toolbarButton ) ) + { + m_LabelSearchText = string.Empty; + GetMaterials(); + } + + GUILayout.FlexibleSpace(); + if( m_LabelSearchText.Length > 0 ) + { + if( GUILayout.Button( "x", EditorStyles.toolbarButton, GUILayout.Width( 24 ) ) ) + { + m_LabelSearchText = string.Empty; + GetMaterials( false ); + } + + GUILayout.Label( $"Label Search: {m_LabelSearchText}", EditorStyles.toolbarButton, GUILayout.Width( 160 ) ); + } + + string lastText = ""; + m_SearchFieldText = EditorGUILayout.DelayedTextField( lastText = m_SearchFieldText, EditorStyles.toolbarSearchField ); + + if( m_SearchFieldText != lastText ) + GetMaterials(); + } + + // header bar + GUILayout.BeginHorizontal(); + GUILayout.Label( "Asset Labels (used)", EditorStyles.toolbarButton, GUILayout.Width( 120 ) ); + GUILayout.Label( "", EditorStyles.toolbarButton, GUILayout.ExpandWidth( true ) ); + GUILayout.EndHorizontal(); + + GUILayout.BeginHorizontal(); + { + // tag bar + using( GUILayout.ScrollViewScope lScope = new GUILayout.ScrollViewScope( m_LabelsScrollPosition, false, false, GUILayout.ExpandHeight( true ), GUILayout.Width( 120 ) ) ) + { + m_LabelsScrollPosition = lScope.scrollPosition; + GUILayout.BeginVertical( GUILayout.ExpandHeight( true ), GUILayout.ExpandWidth( true ) ); + { + foreach( string label in m_Labels ) + { + if( GUILayout.Button( label ) ) + { + m_LabelSearchText = label; + GetMaterials( true ); + } + } + } + GUILayout.EndVertical(); + } + + // previews area + GUILayout.BeginVertical( "GameViewBackground" ); + { + // view window + using( GUILayout.ScrollViewScope svScope = new GUILayout.ScrollViewScope( m_PreviewsScrollPosition, false, true, GUILayout.ExpandHeight( true ), GUILayout.ExpandWidth( true ) ) ) + { + m_PreviewsScrollPosition = svScope.scrollPosition; + + int idx = 0; + int numColumns = (int) ( ( rect.width - 130 ) / m_PreviewSize ); + + foreach( var entry in m_Materials ) + { + if( idx == m_Materials.Count ) break; + + GUILayout.BeginHorizontal(); + for( int x = 0; x < numColumns; x++ ) + { + if( idx == m_Materials.Count ) break; + + if( m_Materials[idx].preview != null && m_Materials[idx].material != null ) + { + GUIContent previewContent = new GUIContent( m_Materials[idx].preview, $"{m_Materials[idx].material.name}\nIn: [{m_Materials[idx].path}]" ); + + if( GUILayout.Button( previewContent, GUILayout.Height( m_PreviewSize ), GUILayout.Width( m_PreviewSize ) ) ) {} + } + + idx++; + } + + GUILayout.EndHorizontal(); + } + } + } + GUILayout.EndVertical(); // previews area + } + GUILayout.EndHorizontal(); // tag & previews area + + // bottom toolbar + using( GUILayout.HorizontalScope toolbarScope = new GUILayout.HorizontalScope( EditorStyles.toolbar, GUILayout.ExpandWidth( true ) ) ) + { + GUILayout.Label( $"Materials: {m_Materials.Count}" ); + + GUILayout.FlexibleSpace(); + + int lastSize; + m_PreviewSize = EditorGUILayout.IntSlider( new GUIContent( "", "Preview Size" ), lastSize = m_PreviewSize, 32, 128, GUILayout.Width( 200 ) ); + m_PreviewSize = GetPow2( m_PreviewSize ); + + if( m_PreviewSize != lastSize ) + EditorPrefs.SetInt( PREVIEW_SIZE_PREF_NAME, m_PreviewSize ); + } + } + + // gets all materials and the labels on them in the project, compares them against a filter, + // and then adds them to the list of materials to be used in this window + private static void GetMaterials( bool usingLabel = false ) + { + m_Materials.Clear(); + + // exclude the label search tag if we arent searching for a specific label right now + string search = usingLabel ? $"l:{m_LabelSearchText} {m_SearchFieldText}" : $"{m_SearchFieldText}"; + + string[] guids = AssetDatabase.FindAssets( $"t:Material {search}" ); + + // assemble preview tiles + foreach( var id in guids ) + { + MaterialBrowserTile browserTile = new MaterialBrowserTile(); + browserTile.path = AssetDatabase.GUIDToAssetPath( id ); + browserTile.material = AssetDatabase.LoadAssetAtPath( browserTile.path ); + browserTile.preview = AssetPreview.GetAssetPreview( browserTile.material ); + browserTile.labels = AssetDatabase.GetLabels( browserTile.material ); + + // add any used labels we arent currently storing + foreach( string label in browserTile.labels ) + { + if( !m_Labels.Contains( label ) ) + m_Labels.Add( label ); + } + + // check each entry against a filter to exclude certain entries + if( IsValidEntry( browserTile ) ) + { + // if we have the material already, skip, else add it + if( m_Materials.Contains( browserTile ) ) break; + else m_Materials.Add( browserTile ); + } + } + } + + // checks a path and returns true/false if a material is ignored or not + private static bool IsValidEntry( MaterialBrowserTile tile ) + { + // these are here to clean things up a little bit and make it easier to read + + bool PathContains( string path ) + { + return tile.path.ToLower().Contains( path ); + } + + // checks for any shaders we want to exclude + bool HasInvalidShader() + { + string shader = tile.material.shader.name.ToLower(); + + string[] excludedShaders = new string[] + { + "skybox/" + }; + + return shader.Contains( excludedShaders[0] ); + } + + string chiselPath = "packages/com.chisel.components/package resources/"; + + string[] ignoredEntries = new string[] + { + "packages/com.unity.searcher/", // 0, we ignore this to get rid of the built-in font materials + "packages/com.unity.entities/", // 1, we ignore this to get rid of the entities materials + $"{chiselPath}preview materials/", // 2, these are tool textures, so we are ignoring them + }; + + // if the path contains any of the ignored paths, then this will return false + bool valid = !PathContains( ignoredEntries[0] ) + && !PathContains( ignoredEntries[1] ) + && !PathContains( ignoredEntries[2] ) + && !HasInvalidShader(); // also check the shader + + return valid; + } + + // used by the preview size slider to step the preview size by powers of two + private static int GetPow2( int val ) + { + val--; + val |= val >> 1; + val |= val >> 2; + val |= val >> 4; + val |= val >> 8; + val |= val >> 16; + val++; + + return val; + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs.meta new file mode 100644 index 000000000..cfcc6cb59 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 374c5ba4ea614dd3a4eaf615c8f03eb4 +timeCreated: 1601158763 \ No newline at end of file diff --git a/Packages/manifest.json b/Packages/manifest.json index 330e72fa5..7c6f3e3e5 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -2,6 +2,7 @@ "dependencies": { "com.unity.burst": "1.3.6", "com.unity.entities": "0.14.0-preview.19", + "com.unity.ide.rider": "2.0.7", "com.unity.ide.visualstudio": "2.0.2", "com.unity.nuget.newtonsoft-json": "2.0.0", "com.unity.performance.profile-analyzer": "1.0.3", From de25960a0a8e29a0c0bf459a8918daa07299db4b Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Wed, 30 Sep 2020 01:58:48 -0500 Subject: [PATCH 02/44] Use a deferred job system to render asset thumbnails and start work on thumbnail caching --- .../MaterialBrowser/MaterialBrowserCache.cs | 68 +++++++++ .../MaterialBrowserCache.cs.meta | 3 + .../MaterialBrowser/MaterialBrowserTile.cs | 138 +++++++++++++++++- .../MaterialBrowser/MaterialBrowserWindow.cs | 23 +-- .../MaterialBrowserCache.asset | 14 ++ .../MaterialBrowserCache.asset.meta | 8 + 6 files changed, 238 insertions(+), 16 deletions(-) create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs.meta create mode 100644 Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset create mode 100644 Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs new file mode 100644 index 000000000..564975264 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs @@ -0,0 +1,68 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.MaterialBrowserCache.cs + +License: +Author: Daniel Cornelius + +* * * * * * * * * * * * * * * * * * * * * */ + +using System; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +namespace Chisel.Editors +{ + [CreateAssetMenu( fileName = "MaterialBrowserCache", menuName = "Material Browser Cache", order = int.MaxValue )] + internal class MaterialBrowserCache : ScriptableObject + { + [Serializable] + public struct StoredEntry + { + public int instanceID; + public string name; + public string data; + + public StoredEntry( string name, int instanceID, Texture2D data ) + { + this.name = name; + this.instanceID = instanceID; + this.data = Convert.ToBase64String( data.EncodeToPNG() ); + } + + public Texture2D GetThumbnail() + { + Texture2D temp = EmbeddedTextures.TemporaryTexture; + + if( data.Length > 0 ) + { + temp.LoadImage( Convert.FromBase64String( data ) ); + return temp; + } + + return temp; + } + } + + // Key = material name, Value = struct containing data + texture information + private List storedPreviewTextures = new List(); + + + public void AddEntry( StoredEntry entry ) + { + if( !TryGetEntry( entry.name, out StoredEntry e ) ) storedPreviewTextures.Add( entry ); + } + + public bool TryGetEntry( string materialName, out StoredEntry entry ) + { + entry = storedPreviewTextures.Find( e => e.name == materialName ); + + return !entry.Equals( null ); + } + + public Texture2D GetThumbnail( string materialName ) + { + return TryGetEntry( materialName, out StoredEntry entry ) ? entry.GetThumbnail() : EmbeddedTextures.TemporaryTexture; + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs.meta new file mode 100644 index 000000000..561e669c1 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 566b787ede4f4f8fa61fe7ca97f6c911 +timeCreated: 1601431645 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs index 3150b3f5b..c227f76f6 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs @@ -6,15 +6,143 @@ * * * * * * * * * * * * * * * * * * * * * */ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; using UnityEngine; namespace Chisel.Editors { - internal struct MaterialBrowserTile + internal static class EmbeddedTextures { - public string path; - public Material material; - public Texture2D preview; - public string[] labels; + private static string m_TempTexB64 = + @"iVBORw0KGgoAAAANSUhE + UgAAAIAAAACACAYAAADDPmHLAAACQ0lEQVR4Ae3bwW + 3FMAxEQRetLlz0D3QwsEgLbw5BeCXsHZGK87zv+yv/ + nHN+5Z+n/PBv7+WHf3v3AhDAEVBWgAAEIAABwptA+e + EbAm0BtgAChPl3D+AewEWQm0BXwf4WEL4LcBEUfvjW + QGugNdAaaA1MbwJmADOAPwaVjwECEIAABAgPguWHf3 + t3BDgCHAFlBQhAAAIQwBCYvQ10BDgCHAGOAEeAI6D6 + aVg5/bd3M4AZwAxQVoAABCAAAWwBtgBbQPMfRMwAZg + AzgBnADGAGMAOYAZL/JFrm//ZuCDQEGgLLChCAAAQg + gDXQGmgNtAZaA4PzgCEw+NB35vECeAFsAZuIWk0AAh + CglvrtlwAEIMAmolYTgAAEqKV++yUAAQiwiajVBCAA + AWqp334JQAACbCJq9VNr+H+/1Q9hvr69AOHP4e5L4A + XwAjS/hfuOgo/C6m8CEIAA1fSbAc5Jfgm9L7wjwBHg + CNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE + CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA + BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE + CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA + BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE + CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA + BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8B4g + L8ARmZaVKQSMMPAAAAAElFTkSuQmCC"; + + private static Texture2D m_TemporaryTexture = null; + + public static Texture2D TemporaryTexture + { + get + { + if( m_TemporaryTexture == null ) + { + m_TemporaryTexture = new Texture2D( 128, 128, TextureFormat.RGBA32, false, PlayerSettings.colorSpace == ColorSpace.Linear ); + + m_TemporaryTexture.LoadImage( Convert.FromBase64String( m_TempTexB64 ) ); + + m_TemporaryTexture.Apply(); + } + + return m_TemporaryTexture; + } + } + } + + internal class MaterialBrowserTile + { + public string path; + public string shaderName; + public string materialName; + public string[] labels; + + public Texture2D Preview + { + get + { + if( m_Preview == null ) { return EmbeddedTextures.TemporaryTexture; } + + return m_Preview; + } + } + + public int InstanceID => m_InstanceID; + + private int m_InstanceID = 0; + private Texture2D m_Preview = null; + + public MaterialBrowserTile( string instID ) + { + path = AssetDatabase.GUIDToAssetPath( instID ); + + Material m = AssetDatabase.LoadAssetAtPath( path ); + + m_InstanceID = m.GetInstanceID(); + labels = AssetDatabase.GetLabels( m ); + shaderName = m.shader.name; + materialName = m.name; + + if( !materialName.Contains( "Font Material" ) ) // dont even consider font materials + DelayedThumbnailRenderHandler.Add( materialName, () => !AssetPreview.IsLoadingAssetPreview( m_InstanceID ), () => { m_Preview = AssetPreview.GetAssetPreview( m ); } ); + } + + public void Draw( RectOffset offset ) + { + } + } + + // slightly modified from http://answers.unity.com/answers/243291/view.html + internal static class DelayedThumbnailRenderHandler + { + private class RenderJob + { + // used for debug + public string taskName = ""; + + public Func Completed { get; } + public Action ContinueWith { get; } + + public RenderJob( string name, Func completed, Action continueWith ) + { + Completed = completed; + ContinueWith = continueWith; + taskName = name; + } + } + + private static readonly List jobs = new List(); + + public static void Add( string name, Func completed, Action continueWith ) + { + if( !jobs.Any() ) EditorApplication.update += Update; + jobs.Add( new RenderJob( name, completed, continueWith ) ); + } + + private static void Update() + { + int i = 0; + for( i = 0; i >= 0; i-- ) + { + if( jobs[i].Completed() ) + { + //Debug.Log( $"Completed thumbnail render task for [{jobs[i].taskName}]" ); + + jobs[i].ContinueWith(); + jobs.RemoveAt( i ); + } + } + + if( !jobs.Any() ) EditorApplication.update -= Update; + } } } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs index 0f27e00c2..3088cdbcc 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs @@ -7,6 +7,7 @@ $TODO: Optimize away all the GUILayout logic $TODO: Implement culling for tiles not visible (hide them if they arent within the viewable window area) $TODO: Do we want to filter by label, too? it would allow user-ignored materials. +$TODO: Implement preview caching $TODO: Optimize optimize optimize * * * * * * * * * * * * * * * * * * * * * */ @@ -117,7 +118,7 @@ private void OnGUI() { m_PreviewsScrollPosition = svScope.scrollPosition; - int idx = 0; + int idx = 0; int numColumns = (int) ( ( rect.width - 130 ) / m_PreviewSize ); foreach( var entry in m_Materials ) @@ -129,9 +130,9 @@ private void OnGUI() { if( idx == m_Materials.Count ) break; - if( m_Materials[idx].preview != null && m_Materials[idx].material != null ) + if( m_Materials[idx].Preview != null ) { - GUIContent previewContent = new GUIContent( m_Materials[idx].preview, $"{m_Materials[idx].material.name}\nIn: [{m_Materials[idx].path}]" ); + GUIContent previewContent = new GUIContent( m_Materials[idx].Preview, $"{m_Materials[idx].materialName}\nIn: [{m_Materials[idx].path}]" ); if( GUILayout.Button( previewContent, GUILayout.Height( m_PreviewSize ), GUILayout.Width( m_PreviewSize ) ) ) {} } @@ -155,12 +156,15 @@ private void OnGUI() GUILayout.FlexibleSpace(); int lastSize; - m_PreviewSize = EditorGUILayout.IntSlider( new GUIContent( "", "Preview Size" ), lastSize = m_PreviewSize, 32, 128, GUILayout.Width( 200 ) ); - m_PreviewSize = GetPow2( m_PreviewSize ); + m_PreviewSize = EditorGUILayout.IntSlider( new GUIContent( "", "Preview Size" ), lastSize = m_PreviewSize, 64, 128, GUILayout.Width( 200 ) ); + //m_PreviewSize = GetPow2( m_PreviewSize ); if( m_PreviewSize != lastSize ) EditorPrefs.SetInt( PREVIEW_SIZE_PREF_NAME, m_PreviewSize ); } + + if( focusedWindow == this ) + Repaint(); } // gets all materials and the labels on them in the project, compares them against a filter, @@ -168,6 +172,7 @@ private void OnGUI() private static void GetMaterials( bool usingLabel = false ) { m_Materials.Clear(); + m_Labels.Clear(); // exclude the label search tag if we arent searching for a specific label right now string search = usingLabel ? $"l:{m_LabelSearchText} {m_SearchFieldText}" : $"{m_SearchFieldText}"; @@ -177,11 +182,7 @@ private static void GetMaterials( bool usingLabel = false ) // assemble preview tiles foreach( var id in guids ) { - MaterialBrowserTile browserTile = new MaterialBrowserTile(); - browserTile.path = AssetDatabase.GUIDToAssetPath( id ); - browserTile.material = AssetDatabase.LoadAssetAtPath( browserTile.path ); - browserTile.preview = AssetPreview.GetAssetPreview( browserTile.material ); - browserTile.labels = AssetDatabase.GetLabels( browserTile.material ); + MaterialBrowserTile browserTile = new MaterialBrowserTile( id ); // add any used labels we arent currently storing foreach( string label in browserTile.labels ) @@ -213,7 +214,7 @@ bool PathContains( string path ) // checks for any shaders we want to exclude bool HasInvalidShader() { - string shader = tile.material.shader.name.ToLower(); + string shader = tile.shaderName.ToLower(); string[] excludedShaders = new string[] { diff --git a/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset b/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset new file mode 100644 index 000000000..59b99e076 --- /dev/null +++ b/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset @@ -0,0 +1,14 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 566b787ede4f4f8fa61fe7ca97f6c911, type: 3} + m_Name: MaterialBrowserCache + m_EditorClassIdentifier: diff --git a/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta b/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta new file mode 100644 index 000000000..e1a389703 --- /dev/null +++ b/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bd2b8a2b8faebe34288757a66a20525b +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 0 + userData: + assetBundleName: + assetBundleVariant: From 4919d50859d1c114fa53f3b2e8ad3f0d5ec8d3af Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Thu, 1 Oct 2020 21:40:35 -0500 Subject: [PATCH 03/44] rename StoredEntry -> Cached thumbnail. naming makes more sense also modified .gitignore to ignore a material package, this can be removed before this PR is merged. --- .gitignore | 4 ++++ .../MaterialBrowser/MaterialBrowserCache.cs | 15 +++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 8f1afd81b..7ba4f388b 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,7 @@ UserSettings/EditorUserSettings.asset /Assets/Experiments.meta Packages/packages-lock.json + +# temporary (remove me before merging material browser PR) +/Assets/Real Materials/ +/Assets/Real Materials.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs index 564975264..94bbf609b 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs @@ -17,13 +17,13 @@ namespace Chisel.Editors internal class MaterialBrowserCache : ScriptableObject { [Serializable] - public struct StoredEntry + public struct CachedThumbnail { public int instanceID; public string name; public string data; - public StoredEntry( string name, int instanceID, Texture2D data ) + public CachedThumbnail( string name, int instanceID, Texture2D data ) { this.name = name; this.instanceID = instanceID; @@ -44,16 +44,15 @@ public Texture2D GetThumbnail() } } - // Key = material name, Value = struct containing data + texture information - private List storedPreviewTextures = new List(); + private List storedPreviewTextures = new List(); - public void AddEntry( StoredEntry entry ) + public void AddEntry( CachedThumbnail entry ) { - if( !TryGetEntry( entry.name, out StoredEntry e ) ) storedPreviewTextures.Add( entry ); + if( !TryGetEntry( entry.name, out CachedThumbnail e ) ) storedPreviewTextures.Add( entry ); } - public bool TryGetEntry( string materialName, out StoredEntry entry ) + public bool TryGetEntry( string materialName, out CachedThumbnail entry ) { entry = storedPreviewTextures.Find( e => e.name == materialName ); @@ -62,7 +61,7 @@ public bool TryGetEntry( string materialName, out StoredEntry entry ) public Texture2D GetThumbnail( string materialName ) { - return TryGetEntry( materialName, out StoredEntry entry ) ? entry.GetThumbnail() : EmbeddedTextures.TemporaryTexture; + return TryGetEntry( materialName, out CachedThumbnail entry ) ? entry.GetThumbnail() : EmbeddedTextures.TemporaryTexture; } } } From 44cadadb3b21d12054930b4b7217a4179d2697f1 Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Sat, 3 Oct 2020 19:55:35 -0500 Subject: [PATCH 04/44] squish a bug, now loads the cache --- .../MaterialBrowser/MaterialBrowserCache.cs | 2 + .../MaterialBrowser/MaterialBrowserWindow.cs | 51 ++++++++++++++++--- .../MaterialBrowserCache.asset.meta | 2 +- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs index 94bbf609b..f54e6dba1 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs @@ -37,6 +37,8 @@ public Texture2D GetThumbnail() if( data.Length > 0 ) { temp.LoadImage( Convert.FromBase64String( data ) ); + temp.Apply(); + return temp; } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs index 3088cdbcc..c6450a9f5 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs @@ -20,6 +20,38 @@ namespace Chisel.Editors { internal class MaterialBrowserWindow : EditorWindow { + public static MaterialBrowserCache CachedTiles + { + get + { + if( m_CachedTiles == null ) + { + Debug.Log( $"No thumbnail cache loaded, finding one." ); + string[] foundCaches = AssetDatabase.FindAssets( "t:MaterialBrowserCache" ); + + if( foundCaches.Length > 0 ) { m_CachedTiles = AssetDatabase.LoadAssetAtPath( AssetDatabase.GUIDToAssetPath( foundCaches[0] ) ); } + + if( m_CachedTiles != null ) + Debug.Log( $"Loaded thumbnail cache [{m_CachedTiles.name}]" ); + else + { + Debug.Log( $"Could not find thumbnail cache, creating one." ); + + MaterialBrowserCache temp = ScriptableObject.CreateInstance(); + AssetDatabase.CreateAsset( temp, $"Assets/MaterialBrowserCache.asset" ); + EditorUtility.SetDirty( temp ); + + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + + m_CachedTiles = temp; + } + } + + return m_CachedTiles; + } + } + private Vector2 m_PreviewsScrollPosition = Vector2.zero; private Vector2 m_LabelsScrollPosition = Vector2.zero; @@ -30,11 +62,15 @@ internal class MaterialBrowserWindow : EditorWindow private static List m_Materials = new List(); private static List m_Labels = new List(); + private static MaterialBrowserCache m_CachedTiles = null; + private const string PREVIEW_SIZE_PREF_NAME = "chisel_matbrowser_pviewSize"; [MenuItem( "Window/Chisel/Material Browser" )] private static void Init() { + Debug.Log( $"Thumbnail cache: {CachedTiles.name}" ); + MaterialBrowserWindow window = EditorWindow.GetWindow( false, "Material Browser" ); window.maxSize = new Vector2( 1920, 2000 ); window.minSize = new Vector2( 200, 100 ); @@ -98,13 +134,14 @@ private void OnGUI() m_LabelsScrollPosition = lScope.scrollPosition; GUILayout.BeginVertical( GUILayout.ExpandHeight( true ), GUILayout.ExpandWidth( true ) ); { - foreach( string label in m_Labels ) + for( int i = 0; i < m_Labels.Count; i++ ) { - if( GUILayout.Button( label ) ) - { - m_LabelSearchText = label; - GetMaterials( true ); - } + if( m_Labels != null ) + if( GUILayout.Button( m_Labels[i] ) ) + { + m_LabelSearchText = m_Labels[i]; + GetMaterials( true ); + } } } GUILayout.EndVertical(); @@ -151,7 +188,9 @@ private void OnGUI() // bottom toolbar using( GUILayout.HorizontalScope toolbarScope = new GUILayout.HorizontalScope( EditorStyles.toolbar, GUILayout.ExpandWidth( true ) ) ) { + int count = ( m_Labels.Count > 0 ) ? m_Labels.Count : 0; GUILayout.Label( $"Materials: {m_Materials.Count}" ); + GUILayout.Label( $"Labels: {count}" ); GUILayout.FlexibleSpace(); diff --git a/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta b/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta index e1a389703..05524451a 100644 --- a/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta +++ b/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: bd2b8a2b8faebe34288757a66a20525b +guid: 8a16b375fadd3314aaf8e39b9044f8d7 NativeFormatImporter: externalObjects: {} mainObjectFileID: 0 From 566c03aac359eb9a181cde2c4ff9f1fca8181b58 Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Sun, 4 Oct 2020 20:15:34 -0500 Subject: [PATCH 05/44] make names match convention of the rest of the project finish implementing thumbnail caching. --- .gitignore | 6 +- .../MaterialBrowser/ChiselEmbeddedTextures.cs | 51 ++++++ .../ChiselEmbeddedTextures.cs.meta | 3 + .../ChiselMaterialBrowserCache.cs | 130 +++++++++++++++ ...eta => ChiselMaterialBrowserCache.cs.meta} | 0 .../ChiselMaterialBrowserTile.cs | 72 +++++++++ ...meta => ChiselMaterialBrowserTile.cs.meta} | 0 .../ChiselMaterialBrowserUtilities.cs | 60 +++++++ .../ChiselMaterialBrowserUtilities.cs.meta | 3 + ...ndow.cs => ChiselMaterialBrowserWindow.cs} | 133 ++++------------ ...ta => ChiselMaterialBrowserWindow.cs.meta} | 0 .../ChiselMaterialThumbnailRenderer.cs | 75 +++++++++ .../ChiselMaterialThumbnailRenderer.cs.meta | 3 + .../MaterialBrowser/MaterialBrowserCache.cs | 69 -------- .../MaterialBrowser/MaterialBrowserTile.cs | 148 ------------------ .../Chisel/Editor/com.chisel.editor.asmdef | 6 +- .../MaterialBrowserCache.asset | 14 -- .../MaterialBrowserCache.asset.meta | 8 - ProjectSettings/ProjectVersion.txt | 4 +- README.md | 2 +- 20 files changed, 437 insertions(+), 350 deletions(-) create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselEmbeddedTextures.cs create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselEmbeddedTextures.cs.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs rename Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/{MaterialBrowserCache.cs.meta => ChiselMaterialBrowserCache.cs.meta} (100%) create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs rename Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/{MaterialBrowserTile.cs.meta => ChiselMaterialBrowserTile.cs.meta} (100%) create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs.meta rename Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/{MaterialBrowserWindow.cs => ChiselMaterialBrowserWindow.cs} (62%) rename Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/{MaterialBrowserWindow.cs.meta => ChiselMaterialBrowserWindow.cs.meta} (100%) create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialThumbnailRenderer.cs create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialThumbnailRenderer.cs.meta delete mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs delete mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs delete mode 100644 Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset delete mode 100644 Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta diff --git a/.gitignore b/.gitignore index 7ba4f388b..d6d0dd65b 100644 --- a/.gitignore +++ b/.gitignore @@ -60,6 +60,8 @@ UserSettings/EditorUserSettings.asset Packages/packages-lock.json +material_browser_cache.json + # temporary (remove me before merging material browser PR) -/Assets/Real Materials/ -/Assets/Real Materials.meta +/Assets/Surfaces Beta 0.4/ +/Assets/Surfaces Beta 0.4.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselEmbeddedTextures.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselEmbeddedTextures.cs new file mode 100644 index 000000000..70b4a47e7 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselEmbeddedTextures.cs @@ -0,0 +1,51 @@ +using System; +using UnityEditor; +using UnityEngine; + +namespace Chisel.Editors +{ + internal static class ChiselEmbeddedTextures + { + private static string m_TempTexB64 = + @"iVBORw0KGgoAAAANSUhE + UgAAAIAAAACACAYAAADDPmHLAAACQ0lEQVR4Ae3bwW + 3FMAxEQRetLlz0D3QwsEgLbw5BeCXsHZGK87zv+yv/ + nHN+5Z+n/PBv7+WHf3v3AhDAEVBWgAAEIAABwptA+e + EbAm0BtgAChPl3D+AewEWQm0BXwf4WEL4LcBEUfvjW + QGugNdAaaA1MbwJmADOAPwaVjwECEIAABAgPguWHf3 + t3BDgCHAFlBQhAAAIQwBCYvQ10BDgCHAGOAEeAI6D6 + aVg5/bd3M4AZwAxQVoAABCAAAWwBtgBbQPMfRMwAZg + AzgBnADGAGMAOYAZL/JFrm//ZuCDQEGgLLChCAAAQg + gDXQGmgNtAZaA4PzgCEw+NB35vECeAFsAZuIWk0AAh + CglvrtlwAEIMAmolYTgAAEqKV++yUAAQiwiajVBCAA + AWqp334JQAACbCJq9VNr+H+/1Q9hvr69AOHP4e5L4A + XwAjS/hfuOgo/C6m8CEIAA1fSbAc5Jfgm9L7wjwBHg + CNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE + CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA + BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE + CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA + BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE + CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA + BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8B4g + L8ARmZaVKQSMMPAAAAAElFTkSuQmCC"; + + private static Texture2D m_TemporaryTexture = null; + + public static Texture2D TemporaryTexture + { + get + { + if( m_TemporaryTexture == null ) + { + m_TemporaryTexture = new Texture2D( 128, 128, TextureFormat.RGBA32, false, PlayerSettings.colorSpace == ColorSpace.Linear ); + + m_TemporaryTexture.LoadImage( Convert.FromBase64String( m_TempTexB64 ) ); + + m_TemporaryTexture.Apply(); + } + + return m_TemporaryTexture; + } + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselEmbeddedTextures.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselEmbeddedTextures.cs.meta new file mode 100644 index 000000000..eecfb0c8f --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselEmbeddedTextures.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9dac7a578e4a432797e3a3b0cde953ed +timeCreated: 1601856748 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs new file mode 100644 index 000000000..a181b961e --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs @@ -0,0 +1,130 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.ChiselMaterialBrowserCache.cs + +License: +Author: Daniel Cornelius + +* * * * * * * * * * * * * * * * * * * * * */ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using UnityEditor; +using UnityEngine; +using Newtonsoft.Json; + +// $TODO: unreachable code detected, ignoring because of debug toggle. maybe worth doing a different way +#pragma warning disable 162 + +namespace Chisel.Editors +{ + [Serializable] + internal class ChiselMaterialBrowserCache + { + [JsonProperty( "name", Required = Required.Always, Order = 0 )] + public string Name => ConstructPath(); + + [JsonIgnore, NonSerialized] + private const bool debug = false; + + [Serializable] + public struct CachedThumbnail + { + [JsonProperty( "instance_id", Required = Required.Always, Order = 0 )] + public int instanceID; + + [JsonProperty( "name", Required = Required.Always, Order = 1 )] + public string name; + + [JsonProperty( "thumbnail_b64", Required = Required.Always, Order = 2 )] + public string data; + + public CachedThumbnail( string name, int instanceID, Texture2D data ) + { + this.name = name; + this.instanceID = instanceID; + this.data = Convert.ToBase64String( data.EncodeToPNG() ); + } + + public Texture2D GetThumbnail() + { + Texture2D temp = new Texture2D( 1, 1, TextureFormat.RGBA32, false, PlayerSettings.colorSpace == ColorSpace.Linear ); + + if( !string.IsNullOrEmpty( data ) ) + { + temp.LoadImage( Convert.FromBase64String( data ) ); + temp.Apply(); + + if( debug ) + Debug.Log( $"Loaded thumbnail for material: [{this.name}]" ); + + return temp; + } + + return ChiselEmbeddedTextures.TemporaryTexture; + } + } + + + [JsonProperty( "stored_thumbnail_data", Required = Required.Always, Order = 1 )] + public List storedPreviewTextures = new List(); + + [JsonIgnore] + public int NumEntries => storedPreviewTextures.Count; + + + public void AddEntry( CachedThumbnail entry ) + { + if( storedPreviewTextures.All( e => e.name != entry.name ) ) + { + storedPreviewTextures.Add( entry ); + + if( debug ) + Debug.Log( $"Added entry for material: [{entry.name}]" ); + } + } + + public Texture2D GetThumbnail( string materialName ) + { + // if the name exists in the list, return its thumbnail + return storedPreviewTextures.FirstOrDefault( e => e.name == materialName ).GetThumbnail(); + } + + public static string ConstructPath() + { + string path = Application.dataPath.Replace( "/", $"{Path.DirectorySeparatorChar}" ); + + path = Directory.GetParent( path ).FullName; + + if( !path.EndsWith( $"{Path.DirectorySeparatorChar}" ) ) path += Path.DirectorySeparatorChar; + + return $"{path}material_browser_cache.json"; + } + + private static ChiselMaterialBrowserCache CreateDefault() + { + ChiselMaterialBrowserCache cache = new ChiselMaterialBrowserCache(); + + cache.Save(); + + if( debug ) + Debug.Log( $"Couldnt find cache, creating one." ); + + return cache; + } + + public void Save() + { + File.WriteAllText( ConstructPath(), JsonConvert.SerializeObject( this, Formatting.Indented ) ); + } + + public static ChiselMaterialBrowserCache Load() + { + if( !File.Exists( ConstructPath() ) ) { return CreateDefault(); } + + return JsonConvert.DeserializeObject( File.ReadAllText( ConstructPath(), Encoding.UTF8 ) ); + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs.meta similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs.meta rename to Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs new file mode 100644 index 000000000..21091af88 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs @@ -0,0 +1,72 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.ChiselMaterialBrowserTile.cs + +License: MIT (https://tldrlegal.com/license/mit-license) +Author: Daniel Cornelius + +* * * * * * * * * * * * * * * * * * * * * */ + +using UnityEditor; +using UnityEngine; + +namespace Chisel.Editors +{ + internal class ChiselMaterialBrowserTile + { + public string path; + public string shaderName; + public string materialName; + public string[] labels; + + public Texture2D Preview + { + get + { + if( m_Preview == null ) { return ChiselEmbeddedTextures.TemporaryTexture; } + + return m_Preview; + } + } + + public int InstanceID => m_InstanceID; + + private int m_InstanceID = 0; + private Texture2D m_Preview = null; + + public ChiselMaterialBrowserTile( string instID ) + { + path = AssetDatabase.GUIDToAssetPath( instID ); + + Material m = AssetDatabase.LoadAssetAtPath( path ); + + m_InstanceID = m.GetInstanceID(); + labels = AssetDatabase.GetLabels( m ); + shaderName = m.shader.name; + materialName = m.name; + + if( m_Preview == null ) + { + m_Preview = ChiselMaterialBrowserWindow.CachedTiles.GetThumbnail( materialName ); + + if( !materialName.Contains( "Font Material" ) ) // dont even consider font materials + ChiselMaterialThumbnailRenderer.Add( materialName, () => !AssetPreview.IsLoadingAssetPreviews(), () => { m_Preview = AssetPreview.GetAssetPreview( m ); } ); + + if( ChiselMaterialBrowserUtilities.IsValidEntry( this ) ) + ChiselMaterialBrowserWindow.CachedTiles.AddEntry( new ChiselMaterialBrowserCache.CachedThumbnail( materialName, m_InstanceID, m_Preview ) ); + } + } + + // $TODO: Use GUI instead of GUILayout + public void Draw( RectOffset offset ) + { + //if(m_Preview == null) Debug.LogError( $"Preview thumbnail [{materialName}] null" ); + + if( Preview != null ) + { + GUIContent previewContent = new GUIContent( Preview, $"{materialName}\nIn: [{path}]" ); + + GUILayout.Box( previewContent, GUILayout.Height( offset.top ), GUILayout.Width( offset.bottom ) ); + } + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs.meta similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs.meta rename to Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs new file mode 100644 index 000000000..871d2aa5c --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs @@ -0,0 +1,60 @@ +namespace Chisel.Editors +{ + internal static class ChiselMaterialBrowserUtilities + { + // checks a path and returns true/false if a material is ignored or not + public static bool IsValidEntry( ChiselMaterialBrowserTile tile ) + { + // these are here to clean things up a little bit and make it easier to read + + bool PathContains( string path ) + { + return tile.path.ToLower().Contains( path ); + } + + // checks for any shaders we want to exclude + bool HasInvalidShader() + { + string shader = tile.shaderName.ToLower(); + + string[] excludedShaders = new string[] + { + "skybox/" + }; + + return shader.Contains( excludedShaders[0] ); + } + + string chiselPath = "packages/com.chisel.components/package resources/"; + + string[] ignoredEntries = new string[] + { + "packages/com.unity.searcher/", // 0, we ignore this to get rid of the built-in font materials + "packages/com.unity.entities/", // 1, we ignore this to get rid of the entities materials + $"{chiselPath}preview materials/", // 2, these are tool textures, so we are ignoring them + }; + + // if the path contains any of the ignored paths, then this will return false + bool valid = !PathContains( ignoredEntries[0] ) + && !PathContains( ignoredEntries[1] ) + && !PathContains( ignoredEntries[2] ) + && !HasInvalidShader(); // also check the shader + + return valid; + } + + // step val by powers of two + private static int GetPow2( int val ) + { + val--; + val |= val >> 1; + val |= val >> 2; + val |= val >> 4; + val |= val >> 8; + val |= val >> 16; + val++; + + return val; + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs.meta new file mode 100644 index 000000000..4a948a524 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d081047e998f4e5396bd178535186bd1 +timeCreated: 1601856923 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs similarity index 62% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs rename to Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs index c6450a9f5..dac680e7f 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs @@ -1,5 +1,5 @@ /* * * * * * * * * * * * * * * * * * * * * * -Chisel.Editors.MaterialBrowserWindow.cs +Chisel.Editors.ChiselMaterialBrowserWindow.cs License: MIT (https://tldrlegal.com/license/mit-license) Author: Daniel Cornelius @@ -7,50 +7,20 @@ $TODO: Optimize away all the GUILayout logic $TODO: Implement culling for tiles not visible (hide them if they arent within the viewable window area) $TODO: Do we want to filter by label, too? it would allow user-ignored materials. -$TODO: Implement preview caching $TODO: Optimize optimize optimize * * * * * * * * * * * * * * * * * * * * * */ using System.Collections.Generic; +using System.IO; using UnityEditor; using UnityEngine; namespace Chisel.Editors { - internal class MaterialBrowserWindow : EditorWindow + internal class ChiselMaterialBrowserWindow : EditorWindow { - public static MaterialBrowserCache CachedTiles - { - get - { - if( m_CachedTiles == null ) - { - Debug.Log( $"No thumbnail cache loaded, finding one." ); - string[] foundCaches = AssetDatabase.FindAssets( "t:MaterialBrowserCache" ); - - if( foundCaches.Length > 0 ) { m_CachedTiles = AssetDatabase.LoadAssetAtPath( AssetDatabase.GUIDToAssetPath( foundCaches[0] ) ); } - - if( m_CachedTiles != null ) - Debug.Log( $"Loaded thumbnail cache [{m_CachedTiles.name}]" ); - else - { - Debug.Log( $"Could not find thumbnail cache, creating one." ); - - MaterialBrowserCache temp = ScriptableObject.CreateInstance(); - AssetDatabase.CreateAsset( temp, $"Assets/MaterialBrowserCache.asset" ); - EditorUtility.SetDirty( temp ); - - AssetDatabase.SaveAssets(); - AssetDatabase.Refresh(); - - m_CachedTiles = temp; - } - } - - return m_CachedTiles; - } - } + public static ChiselMaterialBrowserCache CachedTiles => m_CachedTiles; private Vector2 m_PreviewsScrollPosition = Vector2.zero; private Vector2 m_LabelsScrollPosition = Vector2.zero; @@ -59,25 +29,33 @@ public static MaterialBrowserCache CachedTiles private static string m_SearchFieldText = string.Empty; private static string m_LabelSearchText = string.Empty; - private static List m_Materials = new List(); + private static List m_Materials = new List(); private static List m_Labels = new List(); - private static MaterialBrowserCache m_CachedTiles = null; + private static ChiselMaterialBrowserCache m_CachedTiles = null; private const string PREVIEW_SIZE_PREF_NAME = "chisel_matbrowser_pviewSize"; [MenuItem( "Window/Chisel/Material Browser" )] private static void Init() { - Debug.Log( $"Thumbnail cache: {CachedTiles.name}" ); + InitCache(); + GetMaterials(); - MaterialBrowserWindow window = EditorWindow.GetWindow( false, "Material Browser" ); + ChiselMaterialBrowserWindow window = EditorWindow.GetWindow( false, "Material Browser" ); window.maxSize = new Vector2( 1920, 2000 ); window.minSize = new Vector2( 200, 100 ); m_PreviewSize = EditorPrefs.GetInt( PREVIEW_SIZE_PREF_NAME, 128 ); - GetMaterials(); + //Debug.Log( $"Thumbnail cache: [{CachedTiles.name}], Number of entries: [{CachedTiles.NumEntries}]" ); + } + + private static void InitCache() + { + m_CachedTiles ??= ChiselMaterialBrowserCache.Load(); + + Debug.Log( $"Thumbnail cache: [{CachedTiles.Name}], Number of entries: [{CachedTiles.NumEntries}]" ); } private void OnEnable() @@ -167,12 +145,7 @@ private void OnGUI() { if( idx == m_Materials.Count ) break; - if( m_Materials[idx].Preview != null ) - { - GUIContent previewContent = new GUIContent( m_Materials[idx].Preview, $"{m_Materials[idx].materialName}\nIn: [{m_Materials[idx].path}]" ); - - if( GUILayout.Button( previewContent, GUILayout.Height( m_PreviewSize ), GUILayout.Width( m_PreviewSize ) ) ) {} - } + m_Materials[idx].Draw( new RectOffset( 0, 0, m_PreviewSize, m_PreviewSize ) ); idx++; } @@ -210,8 +183,14 @@ private void OnGUI() // and then adds them to the list of materials to be used in this window private static void GetMaterials( bool usingLabel = false ) { + if( m_CachedTiles == null ) + InitCache(); + + ChiselMaterialThumbnailRenderer.CancelAll(); + AssetPreview.SetPreviewTextureCacheSize( 2000 ); + m_Materials.Clear(); - m_Labels.Clear(); + //m_Labels.Clear(); // exclude the label search tag if we arent searching for a specific label right now string search = usingLabel ? $"l:{m_LabelSearchText} {m_SearchFieldText}" : $"{m_SearchFieldText}"; @@ -221,7 +200,7 @@ private static void GetMaterials( bool usingLabel = false ) // assemble preview tiles foreach( var id in guids ) { - MaterialBrowserTile browserTile = new MaterialBrowserTile( id ); + ChiselMaterialBrowserTile browserTile = new ChiselMaterialBrowserTile( id ); // add any used labels we arent currently storing foreach( string label in browserTile.labels ) @@ -231,68 +210,14 @@ private static void GetMaterials( bool usingLabel = false ) } // check each entry against a filter to exclude certain entries - if( IsValidEntry( browserTile ) ) + if( ChiselMaterialBrowserUtilities.IsValidEntry( browserTile ) ) { // if we have the material already, skip, else add it - if( m_Materials.Contains( browserTile ) ) break; - else m_Materials.Add( browserTile ); + m_Materials.Add( browserTile ); } } - } - // checks a path and returns true/false if a material is ignored or not - private static bool IsValidEntry( MaterialBrowserTile tile ) - { - // these are here to clean things up a little bit and make it easier to read - - bool PathContains( string path ) - { - return tile.path.ToLower().Contains( path ); - } - - // checks for any shaders we want to exclude - bool HasInvalidShader() - { - string shader = tile.shaderName.ToLower(); - - string[] excludedShaders = new string[] - { - "skybox/" - }; - - return shader.Contains( excludedShaders[0] ); - } - - string chiselPath = "packages/com.chisel.components/package resources/"; - - string[] ignoredEntries = new string[] - { - "packages/com.unity.searcher/", // 0, we ignore this to get rid of the built-in font materials - "packages/com.unity.entities/", // 1, we ignore this to get rid of the entities materials - $"{chiselPath}preview materials/", // 2, these are tool textures, so we are ignoring them - }; - - // if the path contains any of the ignored paths, then this will return false - bool valid = !PathContains( ignoredEntries[0] ) - && !PathContains( ignoredEntries[1] ) - && !PathContains( ignoredEntries[2] ) - && !HasInvalidShader(); // also check the shader - - return valid; - } - - // used by the preview size slider to step the preview size by powers of two - private static int GetPow2( int val ) - { - val--; - val |= val >> 1; - val |= val >> 2; - val |= val >> 4; - val |= val >> 8; - val |= val >> 16; - val++; - - return val; + CachedTiles.Save(); } } } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs.meta similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserWindow.cs.meta rename to Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialThumbnailRenderer.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialThumbnailRenderer.cs new file mode 100644 index 000000000..dba54905c --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialThumbnailRenderer.cs @@ -0,0 +1,75 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.ChiselMaterialThumbnailRenderer.cs + +License: MIT (https://tldrlegal.com/license/mit-license) +Author: Daniel Cornelius + +* * * * * * * * * * * * * * * * * * * * * */ + +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEngine; + +// $TODO: unreachable code detected, ignoring because of debug toggle. maybe worth doing a different way +#pragma warning disable 162 + +namespace Chisel.Editors +{ + // slightly modified from http://answers.unity.com/answers/243291/view.html + internal static class ChiselMaterialThumbnailRenderer + { + private const bool debug = false; + + private class RenderJob + { + // used for debug + public string taskName; + + public Func Completed { get; } + public Action ContinueWith { get; } + + public RenderJob( string name, Func completed, Action continueWith ) + { + Completed = completed; + ContinueWith = continueWith; + taskName = name; + } + } + + private static readonly List jobs = new List(); + + public static void Add( string name, Func completed, Action continueWith ) + { + if( !jobs.Any() ) EditorApplication.update += Update; + jobs.Add( new RenderJob( name, completed, continueWith ) ); + } + + private static void Update() + { + if( jobs.Count > 0 ) + { + int i = 0; + for( i = 0; i >= 0; i-- ) + { + if( jobs[i].Completed() ) + { + if( debug ) + Debug.Log( $"Completed thumbnail render task for [{jobs[i].taskName}]" ); + + jobs[i].ContinueWith(); + jobs.RemoveAt( i ); + } + } + } + + if( !jobs.Any() ) EditorApplication.update -= Update; + } + + public static void CancelAll() + { + jobs.Clear(); + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialThumbnailRenderer.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialThumbnailRenderer.cs.meta new file mode 100644 index 000000000..bb34bc72f --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialThumbnailRenderer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f96e7427018a4141911591d290a1506c +timeCreated: 1601856748 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs deleted file mode 100644 index f54e6dba1..000000000 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserCache.cs +++ /dev/null @@ -1,69 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * -Chisel.Editors.MaterialBrowserCache.cs - -License: -Author: Daniel Cornelius - -* * * * * * * * * * * * * * * * * * * * * */ - -using System; -using System.Collections.Generic; -using UnityEditor; -using UnityEngine; - -namespace Chisel.Editors -{ - [CreateAssetMenu( fileName = "MaterialBrowserCache", menuName = "Material Browser Cache", order = int.MaxValue )] - internal class MaterialBrowserCache : ScriptableObject - { - [Serializable] - public struct CachedThumbnail - { - public int instanceID; - public string name; - public string data; - - public CachedThumbnail( string name, int instanceID, Texture2D data ) - { - this.name = name; - this.instanceID = instanceID; - this.data = Convert.ToBase64String( data.EncodeToPNG() ); - } - - public Texture2D GetThumbnail() - { - Texture2D temp = EmbeddedTextures.TemporaryTexture; - - if( data.Length > 0 ) - { - temp.LoadImage( Convert.FromBase64String( data ) ); - temp.Apply(); - - return temp; - } - - return temp; - } - } - - private List storedPreviewTextures = new List(); - - - public void AddEntry( CachedThumbnail entry ) - { - if( !TryGetEntry( entry.name, out CachedThumbnail e ) ) storedPreviewTextures.Add( entry ); - } - - public bool TryGetEntry( string materialName, out CachedThumbnail entry ) - { - entry = storedPreviewTextures.Find( e => e.name == materialName ); - - return !entry.Equals( null ); - } - - public Texture2D GetThumbnail( string materialName ) - { - return TryGetEntry( materialName, out CachedThumbnail entry ) ? entry.GetThumbnail() : EmbeddedTextures.TemporaryTexture; - } - } -} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs deleted file mode 100644 index c227f76f6..000000000 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/MaterialBrowserTile.cs +++ /dev/null @@ -1,148 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * -Chisel.Editors.MaterialBrowserTile.cs - -License: MIT (https://tldrlegal.com/license/mit-license) -Author: Daniel Cornelius - -* * * * * * * * * * * * * * * * * * * * * */ - -using System; -using System.Collections.Generic; -using System.Linq; -using UnityEditor; -using UnityEngine; - -namespace Chisel.Editors -{ - internal static class EmbeddedTextures - { - private static string m_TempTexB64 = - @"iVBORw0KGgoAAAANSUhE - UgAAAIAAAACACAYAAADDPmHLAAACQ0lEQVR4Ae3bwW - 3FMAxEQRetLlz0D3QwsEgLbw5BeCXsHZGK87zv+yv/ - nHN+5Z+n/PBv7+WHf3v3AhDAEVBWgAAEIAABwptA+e - EbAm0BtgAChPl3D+AewEWQm0BXwf4WEL4LcBEUfvjW - QGugNdAaaA1MbwJmADOAPwaVjwECEIAABAgPguWHf3 - t3BDgCHAFlBQhAAAIQwBCYvQ10BDgCHAGOAEeAI6D6 - aVg5/bd3M4AZwAxQVoAABCAAAWwBtgBbQPMfRMwAZg - AzgBnADGAGMAOYAZL/JFrm//ZuCDQEGgLLChCAAAQg - gDXQGmgNtAZaA4PzgCEw+NB35vECeAFsAZuIWk0AAh - CglvrtlwAEIMAmolYTgAAEqKV++yUAAQiwiajVBCAA - AWqp334JQAACbCJq9VNr+H+/1Q9hvr69AOHP4e5L4A - XwAjS/hfuOgo/C6m8CEIAA1fSbAc5Jfgm9L7wjwBHg - CNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE - CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA - BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE - CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA - BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8BCE - CATUStJgABCFBL/fZLAAIQYBNRqwlAAALUUr/9EoAA - BNhE1GoCEIAAtdRvvwQgAAE2EbWaAAQgQC312y8B4g - L8ARmZaVKQSMMPAAAAAElFTkSuQmCC"; - - private static Texture2D m_TemporaryTexture = null; - - public static Texture2D TemporaryTexture - { - get - { - if( m_TemporaryTexture == null ) - { - m_TemporaryTexture = new Texture2D( 128, 128, TextureFormat.RGBA32, false, PlayerSettings.colorSpace == ColorSpace.Linear ); - - m_TemporaryTexture.LoadImage( Convert.FromBase64String( m_TempTexB64 ) ); - - m_TemporaryTexture.Apply(); - } - - return m_TemporaryTexture; - } - } - } - - internal class MaterialBrowserTile - { - public string path; - public string shaderName; - public string materialName; - public string[] labels; - - public Texture2D Preview - { - get - { - if( m_Preview == null ) { return EmbeddedTextures.TemporaryTexture; } - - return m_Preview; - } - } - - public int InstanceID => m_InstanceID; - - private int m_InstanceID = 0; - private Texture2D m_Preview = null; - - public MaterialBrowserTile( string instID ) - { - path = AssetDatabase.GUIDToAssetPath( instID ); - - Material m = AssetDatabase.LoadAssetAtPath( path ); - - m_InstanceID = m.GetInstanceID(); - labels = AssetDatabase.GetLabels( m ); - shaderName = m.shader.name; - materialName = m.name; - - if( !materialName.Contains( "Font Material" ) ) // dont even consider font materials - DelayedThumbnailRenderHandler.Add( materialName, () => !AssetPreview.IsLoadingAssetPreview( m_InstanceID ), () => { m_Preview = AssetPreview.GetAssetPreview( m ); } ); - } - - public void Draw( RectOffset offset ) - { - } - } - - // slightly modified from http://answers.unity.com/answers/243291/view.html - internal static class DelayedThumbnailRenderHandler - { - private class RenderJob - { - // used for debug - public string taskName = ""; - - public Func Completed { get; } - public Action ContinueWith { get; } - - public RenderJob( string name, Func completed, Action continueWith ) - { - Completed = completed; - ContinueWith = continueWith; - taskName = name; - } - } - - private static readonly List jobs = new List(); - - public static void Add( string name, Func completed, Action continueWith ) - { - if( !jobs.Any() ) EditorApplication.update += Update; - jobs.Add( new RenderJob( name, completed, continueWith ) ); - } - - private static void Update() - { - int i = 0; - for( i = 0; i >= 0; i-- ) - { - if( jobs[i].Completed() ) - { - //Debug.Log( $"Completed thumbnail render task for [{jobs[i].taskName}]" ); - - jobs[i].ContinueWith(); - jobs.RemoveAt( i ); - } - } - - if( !jobs.Any() ) EditorApplication.update -= Update; - } - } -} diff --git a/Packages/com.chisel.editor/Chisel/Editor/com.chisel.editor.asmdef b/Packages/com.chisel.editor/Chisel/Editor/com.chisel.editor.asmdef index 8f84ba6ba..98c1b7b5c 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/com.chisel.editor.asmdef +++ b/Packages/com.chisel.editor/Chisel/Editor/com.chisel.editor.asmdef @@ -15,8 +15,10 @@ ], "excludePlatforms": [], "allowUnsafeCode": false, - "overrideReferences": false, - "precompiledReferences": [], + "overrideReferences": true, + "precompiledReferences": [ + "Newtonsoft.Json.dll" + ], "autoReferenced": true, "defineConstraints": [], "versionDefines": [], diff --git a/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset b/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset deleted file mode 100644 index 59b99e076..000000000 --- a/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset +++ /dev/null @@ -1,14 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 566b787ede4f4f8fa61fe7ca97f6c911, type: 3} - m_Name: MaterialBrowserCache - m_EditorClassIdentifier: diff --git a/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta b/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta deleted file mode 100644 index 05524451a..000000000 --- a/Packages/com.chisel.editor/Editor Resources/MaterialBrowserCache.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 8a16b375fadd3314aaf8e39b9044f8d7 -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 0 - userData: - assetBundleName: - assetBundleVariant: diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 3a454357e..e7b4de36f 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2020.2.0b4 -m_EditorVersionWithRevision: 2020.2.0b4 (7b778e331e0b) +m_EditorVersion: 2020.2.0b5 +m_EditorVersionWithRevision: 2020.2.0b5 (e2067236bd5c) diff --git a/README.md b/README.md index 6b1271041..966b83a85 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Chisel is an extension for **Unity** that allows for _rapid_, iterative, **non d * If you have questions be sure to ask in the discord! **[Click here for to join the official Discord server!](https://discord.gg/zttNkPQ)** Notes: -- Chisel requires Unity 2020.2.b04 or newer +- Chisel requires Unity 2020.2.b05 or newer - UX is still under development - CSG algorithm is still under development - It is *not yet* ready for production, but please feel free to try it out, or better yet, help out! From ab47091b6125e2b7d3ab0fde01c46dea4ac79d69 Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Sun, 4 Oct 2020 20:27:28 -0500 Subject: [PATCH 06/44] use instance ID instead of name for checking if an entry is valid should prevent cases where a thumbnail isnt cached because there is a duplicate file name in the project somewhere, but the material isnt actually a duplicate material. --- .../Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs | 6 +++--- .../Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs index a181b961e..ae516610b 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserCache.cs @@ -77,7 +77,7 @@ public Texture2D GetThumbnail() public void AddEntry( CachedThumbnail entry ) { - if( storedPreviewTextures.All( e => e.name != entry.name ) ) + if( storedPreviewTextures.All( e => e.instanceID != entry.instanceID ) ) { storedPreviewTextures.Add( entry ); @@ -86,10 +86,10 @@ public void AddEntry( CachedThumbnail entry ) } } - public Texture2D GetThumbnail( string materialName ) + public Texture2D GetThumbnail( int instanceID ) { // if the name exists in the list, return its thumbnail - return storedPreviewTextures.FirstOrDefault( e => e.name == materialName ).GetThumbnail(); + return storedPreviewTextures.FirstOrDefault( e => e.instanceID == instanceID ).GetThumbnail(); } public static string ConstructPath() diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs index 21091af88..46f3666d8 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs @@ -46,7 +46,7 @@ public ChiselMaterialBrowserTile( string instID ) if( m_Preview == null ) { - m_Preview = ChiselMaterialBrowserWindow.CachedTiles.GetThumbnail( materialName ); + m_Preview = ChiselMaterialBrowserWindow.CachedTiles.GetThumbnail( m_InstanceID ); if( !materialName.Contains( "Font Material" ) ) // dont even consider font materials ChiselMaterialThumbnailRenderer.Add( materialName, () => !AssetPreview.IsLoadingAssetPreviews(), () => { m_Preview = AssetPreview.GetAssetPreview( m ); } ); From 8af8fb146b194b9556066ac803f540872e2aab5d Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Sun, 4 Oct 2020 20:36:43 -0500 Subject: [PATCH 07/44] fix file headers --- .../MaterialBrowser/ChiselMaterialBrowserUtilities.cs | 9 +++++++++ .../MaterialBrowser/ChiselMaterialBrowserWindow.cs | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs index 871d2aa5c..ea16867d7 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs @@ -1,3 +1,12 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.ChiselMaterialBrowserCache.cs + +License: MIT (https://tldrlegal.com/license/mit-license) +Author: Daniel Cornelius + +$TODO: Do we want to filter by label, too? it would allow user-ignored materials. +* * * * * * * * * * * * * * * * * * * * * */ + namespace Chisel.Editors { internal static class ChiselMaterialBrowserUtilities diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs index dac680e7f..2d319a2d3 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs @@ -6,7 +6,6 @@ $TODO: Optimize away all the GUILayout logic $TODO: Implement culling for tiles not visible (hide them if they arent within the viewable window area) -$TODO: Do we want to filter by label, too? it would allow user-ignored materials. $TODO: Optimize optimize optimize * * * * * * * * * * * * * * * * * * * * * */ From 8dffcf772619271af0739b389fbc502654bc317c Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Mon, 5 Oct 2020 01:24:30 -0500 Subject: [PATCH 08/44] swap label bar to the right side. --- .../ChiselMaterialBrowserWindow.cs | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs index 2d319a2d3..bf950ae6b 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs @@ -29,7 +29,7 @@ internal class ChiselMaterialBrowserWindow : EditorWindow private static string m_LabelSearchText = string.Empty; private static List m_Materials = new List(); - private static List m_Labels = new List(); + private static List m_Labels = new List(); private static ChiselMaterialBrowserCache m_CachedTiles = null; @@ -99,31 +99,12 @@ private void OnGUI() // header bar GUILayout.BeginHorizontal(); - GUILayout.Label( "Asset Labels (used)", EditorStyles.toolbarButton, GUILayout.Width( 120 ) ); GUILayout.Label( "", EditorStyles.toolbarButton, GUILayout.ExpandWidth( true ) ); + GUILayout.Label( "Asset Labels (used)", EditorStyles.toolbarButton, GUILayout.Width( 120 ) ); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); { - // tag bar - using( GUILayout.ScrollViewScope lScope = new GUILayout.ScrollViewScope( m_LabelsScrollPosition, false, false, GUILayout.ExpandHeight( true ), GUILayout.Width( 120 ) ) ) - { - m_LabelsScrollPosition = lScope.scrollPosition; - GUILayout.BeginVertical( GUILayout.ExpandHeight( true ), GUILayout.ExpandWidth( true ) ); - { - for( int i = 0; i < m_Labels.Count; i++ ) - { - if( m_Labels != null ) - if( GUILayout.Button( m_Labels[i] ) ) - { - m_LabelSearchText = m_Labels[i]; - GetMaterials( true ); - } - } - } - GUILayout.EndVertical(); - } - // previews area GUILayout.BeginVertical( "GameViewBackground" ); { @@ -154,6 +135,25 @@ private void OnGUI() } } GUILayout.EndVertical(); // previews area + + // tag bar + using( GUILayout.ScrollViewScope lScope = new GUILayout.ScrollViewScope( m_LabelsScrollPosition, false, false, GUILayout.ExpandHeight( true ), GUILayout.Width( 120 ) ) ) + { + m_LabelsScrollPosition = lScope.scrollPosition; + GUILayout.BeginVertical( GUILayout.ExpandHeight( true ), GUILayout.ExpandWidth( true ) ); + { + for( int i = 0; i < m_Labels.Count; i++ ) + { + if( m_Labels != null ) + if( GUILayout.Button( m_Labels[i] ) ) + { + m_LabelSearchText = m_Labels[i]; + GetMaterials( true ); + } + } + } + GUILayout.EndVertical(); + } } GUILayout.EndHorizontal(); // tag & previews area From 9fa8ca9953c6eb7b23aa64520af8f422da591f56 Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Mon, 5 Oct 2020 02:40:54 -0500 Subject: [PATCH 09/44] testing stuff this will be moved over to the actual window once i figure out how i want it laid out --- .../ChiselMaterialBrowserTile.cs | 4 +- .../ChiselMaterialBrowserWindow.cs | 2 +- .../Editor/MaterialBrowser/Testing.meta | 3 + .../ChiselMaterialBrowserWindowLayoutTest.cs | 131 ++++++++++++++++++ ...selMaterialBrowserWindowLayoutTest.cs.meta | 3 + 5 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs index 46f3666d8..fe881f858 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserTile.cs @@ -57,7 +57,7 @@ public ChiselMaterialBrowserTile( string instID ) } // $TODO: Use GUI instead of GUILayout - public void Draw( RectOffset offset ) + public void Draw( Rect offset ) { //if(m_Preview == null) Debug.LogError( $"Preview thumbnail [{materialName}] null" ); @@ -65,7 +65,7 @@ public void Draw( RectOffset offset ) { GUIContent previewContent = new GUIContent( Preview, $"{materialName}\nIn: [{path}]" ); - GUILayout.Box( previewContent, GUILayout.Height( offset.top ), GUILayout.Width( offset.bottom ) ); + GUILayout.Box( previewContent, GUILayout.Height( offset.height ), GUILayout.Width( offset.width ) ); } } } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs index bf950ae6b..5f1edada0 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserWindow.cs @@ -125,7 +125,7 @@ private void OnGUI() { if( idx == m_Materials.Count ) break; - m_Materials[idx].Draw( new RectOffset( 0, 0, m_PreviewSize, m_PreviewSize ) ); + m_Materials[idx].Draw( new Rect( 0, 0, m_PreviewSize, m_PreviewSize ) ); idx++; } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing.meta new file mode 100644 index 000000000..f63b58977 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d1c9f9cae89f4ceb9d4878255ec42c60 +timeCreated: 1601879362 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs new file mode 100644 index 000000000..70346c716 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs @@ -0,0 +1,131 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.ChiselMaterialBrowserWindowLayoutTest.cs + +License: +Author: Daniel Cornelius + +$TODO: DELETE ME WHEN DONE + +* * * * * * * * * * * * * * * * * * * * * */ + +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +namespace Chisel.Editors +{ + internal sealed class ChiselMaterialBrowserWindowTest : EditorWindow + { + private Vector2 m_ScrollPos = Vector2.zero; + + private List tiles = new List() // 8x8 + { + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box", + "box", "box", "box", "box", "box", "box", "box", "box" + }; + + [MenuItem( "Window/Chisel/Material Browser Test" )] + private static void Init() + { + ChiselMaterialBrowserWindowTest window = EditorWindow.GetWindow( false, "Material Browser" ); + window.maxSize = new Vector2( 1920, 2000 ); + window.minSize = new Vector2( 200, 100 ); + } + + private void OnGUI() + { + Rect rect = this.position; + + DrawToolbar( rect ); + DrawLabelAndTileArea( rect ); + DrawFooter( rect ); + } + + private void DrawFooter( Rect rect ) + { + } + + private float scrollViewHeight; + private float xOffset = 2; + + private void DrawLabelAndTileArea( Rect rect ) + { + GUI.Box( new Rect( 0, 20, rect.width - 130, rect.height - 20 ), "", "GameViewBackground" ); + + int idx = 0; + int numColumns = (int) ( ( rect.width - 150 ) / 64 ); + + m_ScrollPos = GUI.BeginScrollView( new Rect( 0, 20, rect.width - 130, rect.height - 20 ), m_ScrollPos, new Rect( 0, 20, rect.width - 150, scrollViewHeight ) ); + { + float yOffset = 22; + int row = 0; + + foreach( var entry in tiles ) + { + if( idx == tiles.Count ) break; + + // begin horizontal + for( int x = 0; x < numColumns; x++ ) + { + if( x > 0 ) + xOffset = 68 * x; + + if( idx == tiles.Count ) break; + + GUI.Button( new Rect( xOffset, yOffset, 64, 64 ), $"{tiles[idx]}, #{idx + 1}" ); + + idx++; + } + + row += 1; + xOffset = 2; + if( row > 0 ) + { + yOffset = 22 + ( 68 * row ); + + scrollViewHeight = yOffset; + } + + // end horizontal + } + } + GUI.EndScrollView(); + + GUI.Label( new Rect( 0, 0, rect.width, 22 ), + $"ScrollPos: {m_ScrollPos.ToString()} | ScrollHeight: {scrollViewHeight:#000} | idx: {idx:#00} | NumCollums: {numColumns:#00} | NumElements: {tiles.Count:#000}" ); + } + + + private void DrawToolbar( Rect rect ) + { + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs.meta new file mode 100644 index 000000000..2b4c937c8 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5c5ebfe5716b43bb87c58850d17a3dab +timeCreated: 1601879389 \ No newline at end of file From 87136b3bf5f72fec8fdb444eab6499ce100350f2 Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Wed, 7 Oct 2020 04:12:06 -0500 Subject: [PATCH 10/44] begin trying to get tabs to rotate --- .../ChiselMaterialBrowserWindowLayoutTest.cs | 134 ++++++++++++------ 1 file changed, 89 insertions(+), 45 deletions(-) diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs index 70346c716..090184688 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs @@ -8,69 +8,114 @@ * * * * * * * * * * * * * * * * * * * * * */ +using System; using System.Collections.Generic; +using System.Reflection; using UnityEditor; using UnityEngine; +using Random = UnityEngine.Random; namespace Chisel.Editors { internal sealed class ChiselMaterialBrowserWindowTest : EditorWindow { + private const int NUM_TILES = 8 * 200; // 8 collumns, 50 rows + private const int TOOLBAR_WIDTH = 140; + private const int THUMB_SIZE = 64; + private const int TOOLBAR_HEIGHT = 22; + private Vector2 m_ScrollPos = Vector2.zero; - private List tiles = new List() // 8x8 - { - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box", - "box", "box", "box", "box", "box", "box", "box", "box" - }; + private static List tiles = new List(); + + private static bool isRenderingNoise = false; [MenuItem( "Window/Chisel/Material Browser Test" )] private static void Init() { ChiselMaterialBrowserWindowTest window = EditorWindow.GetWindow( false, "Material Browser" ); - window.maxSize = new Vector2( 1920, 2000 ); - window.minSize = new Vector2( 200, 100 ); + window.maxSize = new Vector2( 690, 2000 ); + window.minSize = new Vector2( 420, 310 ); + + + for( int i = 0; i < NUM_TILES; i++ ) + { + if( !isRenderingNoise ) + tiles.Add( GetRandNoiseTex() ); + } + } + + private void OnEnable() + { + if( tiles.Count < 1 ) + { + for( int i = 0; i < NUM_TILES; i++ ) + { + if( !isRenderingNoise ) + tiles.Add( GetRandNoiseTex() ); + } + } + } + + private static Texture2D GetRandNoiseTex() + { + isRenderingNoise = true; + Texture2D noiseTex = new Texture2D( THUMB_SIZE, THUMB_SIZE, TextureFormat.RGB24, false, PlayerSettings.colorSpace == ColorSpace.Linear ); + Color[] pix = new Color[THUMB_SIZE * THUMB_SIZE]; + + for( int y = 0; y < THUMB_SIZE; y++ ) + { + for( int x = 0; x < THUMB_SIZE; x++ ) + { + float xCoord = 0 + x / THUMB_SIZE; + float yCoord = 0 + y / THUMB_SIZE; + float sample = Mathf.Clamp01( 1 - Mathf.PerlinNoise( xCoord, yCoord ) * Random.value ); + pix[y * THUMB_SIZE + x] = new Color( sample, sample, sample ); + } + } + + noiseTex.SetPixels( pix ); + noiseTex.Apply(); + + isRenderingNoise = false; + return noiseTex; } private void OnGUI() { Rect rect = this.position; + DrawTabBar( rect ); + //DrawLabelAndTileArea( rect ); DrawToolbar( rect ); - DrawLabelAndTileArea( rect ); DrawFooter( rect ); } + private Vector2 pivot; + + private void DrawTabBar( Rect rect ) + { + pivot = new Vector2( rect.width, rect.height - TOOLBAR_HEIGHT ); + + Matrix4x4 guiMatrix = GUI.matrix; + + GUI.Box( new Rect( rect.width - TOOLBAR_HEIGHT, 0, TOOLBAR_HEIGHT, rect.height ), "", "DockHeader" ); + GUI.EndClip(); + + GUIUtility.RotateAroundPivot( 90, pivot ); + //GUI.matrix = Matrix4x4.identity; + //GUI.matrix = new Matrix4x4( new Vector4( 0, 1, 0, 0 ), new Vector4( -1, 0, 0, 0 ), new Vector4( 0, 0, 1, 0 ), new Vector4( 0, 0, 0, 1 ) ); + + int offset = TOOLBAR_HEIGHT; + for( int i = 0; i < 4; i++ ) { GUI.Button( new Rect( offset + ( 100 * ( 1 + i ) ), rect.height - TOOLBAR_HEIGHT, 100, TOOLBAR_HEIGHT ), $"BUTTON {i}" ); } + + GUI.matrix = guiMatrix; + GUI.BeginClip( rect ); + } + private void DrawFooter( Rect rect ) { + GUI.Label( new Rect( 0, rect.height - TOOLBAR_HEIGHT, rect.width, TOOLBAR_HEIGHT ), $"xMax: {rect.xMax} | yMax: {rect.yMax} | xMin: {rect.xMin} | yMin: {rect.yMin}" ); } private float scrollViewHeight; @@ -78,12 +123,13 @@ private void DrawFooter( Rect rect ) private void DrawLabelAndTileArea( Rect rect ) { - GUI.Box( new Rect( 0, 20, rect.width - 130, rect.height - 20 ), "", "GameViewBackground" ); + GUI.Box( new Rect( 0, TOOLBAR_HEIGHT, rect.width - TOOLBAR_WIDTH, rect.height - ( TOOLBAR_HEIGHT * 2 ) ), "", "GameViewBackground" ); int idx = 0; - int numColumns = (int) ( ( rect.width - 150 ) / 64 ); + int numColumns = (int) ( ( rect.width - ( TOOLBAR_WIDTH + 10 ) ) / THUMB_SIZE ); - m_ScrollPos = GUI.BeginScrollView( new Rect( 0, 20, rect.width - 130, rect.height - 20 ), m_ScrollPos, new Rect( 0, 20, rect.width - 150, scrollViewHeight ) ); + m_ScrollPos = GUI.BeginScrollView( new Rect( 0, 28, rect.width - ( TOOLBAR_WIDTH + 2 ), rect.height - 52 ), m_ScrollPos, + new Rect( 0, 28, rect.width - ( TOOLBAR_WIDTH + 16 ), scrollViewHeight ) ); { float yOffset = 22; int row = 0; @@ -96,11 +142,11 @@ private void DrawLabelAndTileArea( Rect rect ) for( int x = 0; x < numColumns; x++ ) { if( x > 0 ) - xOffset = 68 * x; + xOffset = ( THUMB_SIZE + 3 ) * x; if( idx == tiles.Count ) break; - GUI.Button( new Rect( xOffset, yOffset, 64, 64 ), $"{tiles[idx]}, #{idx + 1}" ); + GUI.Box( new Rect( xOffset, yOffset, THUMB_SIZE, THUMB_SIZE ), tiles[idx] ); // make a custom guistyle for these, or make a method to do a proper tile idx++; } @@ -109,7 +155,7 @@ private void DrawLabelAndTileArea( Rect rect ) xOffset = 2; if( row > 0 ) { - yOffset = 22 + ( 68 * row ); + yOffset = TOOLBAR_HEIGHT + ( ( THUMB_SIZE + 3 ) * row ); scrollViewHeight = yOffset; } @@ -118,14 +164,12 @@ private void DrawLabelAndTileArea( Rect rect ) } } GUI.EndScrollView(); - - GUI.Label( new Rect( 0, 0, rect.width, 22 ), - $"ScrollPos: {m_ScrollPos.ToString()} | ScrollHeight: {scrollViewHeight:#000} | idx: {idx:#00} | NumCollums: {numColumns:#00} | NumElements: {tiles.Count:#000}" ); } private void DrawToolbar( Rect rect ) { + GUI.Label( new Rect( 0, 0, rect.width, TOOLBAR_HEIGHT ), $"WindowSize: {position}" ); } } } From 492220e60f82ec589ea35f634432fb1991f8f612 Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Thu, 8 Oct 2020 21:04:53 -0500 Subject: [PATCH 11/44] start work on converting test window to UIElements --- .gitignore | 1 + .../ChiselMaterialBrowserUtilities.cs | 20 ++++ .../ChiselMaterialBrowserWindowLayoutTest.cs | 90 +++++++++----- .../MaterialBrowser/Testing/Resources.meta | 3 + .../Testing/Resources/Editor.meta | 3 + .../Testing/Resources/Editor/Chisel.meta | 3 + .../Resources/Editor/Chisel/EditorWindow.uss | 12 ++ .../Editor/Chisel/EditorWindow.uss.meta | 3 + .../Resources/Editor/Chisel/EditorWindow.uxml | 12 ++ .../Editor/Chisel/EditorWindow.uxml.meta | 3 + .../MaterialBrowser/Testing/UIElements.meta | 3 + .../Testing/UIElements/ChiselUIElements.cs | 110 ++++++++++++++++++ .../UIElements/ChiselUIElements.cs.meta | 3 + 13 files changed, 235 insertions(+), 31 deletions(-) create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements.meta create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs create mode 100644 Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs.meta diff --git a/.gitignore b/.gitignore index d6d0dd65b..cf7f68c59 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,7 @@ UserSettings/EditorUserSettings.asset Packages/packages-lock.json material_browser_cache.json +UIElementsSchema/ # temporary (remove me before merging material browser PR) /Assets/Surfaces Beta 0.4/ diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs index ea16867d7..b41dc9ffd 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs @@ -7,6 +7,10 @@ $TODO: Do we want to filter by label, too? it would allow user-ignored materials. * * * * * * * * * * * * * * * * * * * * * */ +using UnityEditor; +using UnityEngine; +using UnityEngine.UIElements; + namespace Chisel.Editors { internal static class ChiselMaterialBrowserUtilities @@ -66,4 +70,20 @@ private static int GetPow2( int val ) return val; } } + + internal static class ChiselUIElementUtility + { + private static VisualElement m_BaseUXML; + private static StyleSheet m_BaseUSS; + + public static VisualElement GetRootElement( this EditorWindow window ) + { + if( m_BaseUXML == null ) m_BaseUXML = Resources.Load( "Editor/Chisel/EditorWindow" ).CloneTree(); + if( m_BaseUSS == null ) m_BaseUSS = Resources.Load( "Editor/Chisel/EditorWindow" ); + + m_BaseUXML.styleSheets.Add( m_BaseUSS ); + + return m_BaseUXML; + } + } } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs index 090184688..974c20178 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs @@ -10,10 +10,11 @@ using System; using System.Collections.Generic; -using System.Reflection; using UnityEditor; using UnityEngine; using Random = UnityEngine.Random; +using UnityEditor.UIElements; +using UnityEngine.UIElements; namespace Chisel.Editors { @@ -30,6 +31,11 @@ internal sealed class ChiselMaterialBrowserWindowTest : EditorWindow private static bool isRenderingNoise = false; + private VisualElement Root => rootVisualElement; + + private Box m_TopToolbar; + private Toolbar m_TabBar; + [MenuItem( "Window/Chisel/Material Browser Test" )] private static void Init() { @@ -47,6 +53,16 @@ private static void Init() private void OnEnable() { + // $TODO: Convert to UI Toolkit + this.GetRootElement(); + + m_TopToolbar = Root.AddBox( new Rect( position.width, 0, TOOLBAR_HEIGHT, position.height ), new Color( 0.15f, 0.15f, 0.15f, 1f ), -1 ); + + m_TabBar = m_TopToolbar.AddToolbar( new Rect( 0, 0, position.width, TOOLBAR_HEIGHT ) ); + m_TopToolbar.SetRotation( 90 ); + + for( int i = 0; i < 4; i++ ) { m_TabBar.AddButton( $"Tab {i}", new Vector2Int( 100, 22 ), 0 ); } + if( tiles.Count < 1 ) { for( int i = 0; i < NUM_TILES; i++ ) @@ -83,39 +99,16 @@ private static Texture2D GetRandNoiseTex() private void OnGUI() { + m_TopToolbar?.SetPosition( new Vector2( position.width, 0 ) ); + m_TopToolbar?.SetSize( new Vector2( position.height, TOOLBAR_HEIGHT ) ); + m_TabBar.SetSize( new Vector2( ( position.height > ( 100 * 4 ) ) ? 100 * 4 : position.height, TOOLBAR_HEIGHT) ); + Rect rect = this.position; - DrawTabBar( rect ); + //DrawTabBar( rect ); //DrawLabelAndTileArea( rect ); - DrawToolbar( rect ); - DrawFooter( rect ); - } - - private Vector2 pivot; - - private void DrawTabBar( Rect rect ) - { - pivot = new Vector2( rect.width, rect.height - TOOLBAR_HEIGHT ); - - Matrix4x4 guiMatrix = GUI.matrix; - - GUI.Box( new Rect( rect.width - TOOLBAR_HEIGHT, 0, TOOLBAR_HEIGHT, rect.height ), "", "DockHeader" ); - GUI.EndClip(); - - GUIUtility.RotateAroundPivot( 90, pivot ); - //GUI.matrix = Matrix4x4.identity; - //GUI.matrix = new Matrix4x4( new Vector4( 0, 1, 0, 0 ), new Vector4( -1, 0, 0, 0 ), new Vector4( 0, 0, 1, 0 ), new Vector4( 0, 0, 0, 1 ) ); - - int offset = TOOLBAR_HEIGHT; - for( int i = 0; i < 4; i++ ) { GUI.Button( new Rect( offset + ( 100 * ( 1 + i ) ), rect.height - TOOLBAR_HEIGHT, 100, TOOLBAR_HEIGHT ), $"BUTTON {i}" ); } - - GUI.matrix = guiMatrix; - GUI.BeginClip( rect ); - } - - private void DrawFooter( Rect rect ) - { - GUI.Label( new Rect( 0, rect.height - TOOLBAR_HEIGHT, rect.width, TOOLBAR_HEIGHT ), $"xMax: {rect.xMax} | yMax: {rect.yMax} | xMin: {rect.xMin} | yMin: {rect.yMin}" ); + //DrawToolbar( rect ); + //DrawFooter( rect ); } private float scrollViewHeight; @@ -167,9 +160,44 @@ private void DrawLabelAndTileArea( Rect rect ) } + private int tabSel = 0; + + private void DrawTabBar( Rect rect ) + { + Matrix4x4 guiMatrix = GUI.matrix; + + GUI.Box( new Rect( rect.width - TOOLBAR_HEIGHT, 0, TOOLBAR_HEIGHT, rect.height ), "", "DockHeader" ); + //GUI.EndClip(); + + RotateAroundPivot( 90, rect.center, rect ); + //GUIUtility.RotateAroundPivot( 90, new Vector2( rect.width, rect.height -TOOLBAR_HEIGHT ) ); + //GUI.matrix = Matrix4x4.identity; + //GUI.matrix = new Matrix4x4( new Vector4( 0, 1, 0, 0 ), new Vector4( -1, 0, 0, 0 ), new Vector4( 0, 0, 1, 0 ), new Vector4( 0, 0, 0, 1 ) ); + + int offset = TOOLBAR_HEIGHT; + for( int i = 0; i < 4; i++ ) { GUI.Button( new Rect( TOOLBAR_HEIGHT, 0, TOOLBAR_HEIGHT, rect.height ), $"BUTTON {i}" ); } + + GUI.matrix = guiMatrix; + //GUI.BeginClip( rect ); + } + + private void DrawFooter( Rect rect ) + { + GUI.Label( new Rect( 0, rect.height - TOOLBAR_HEIGHT, rect.width, TOOLBAR_HEIGHT ), $"xMax: {rect.xMax} | yMax: {rect.yMax} | xMin: {rect.xMin} | yMin: {rect.yMin}" ); + } + private void DrawToolbar( Rect rect ) { GUI.Label( new Rect( 0, 0, rect.width, TOOLBAR_HEIGHT ), $"WindowSize: {position}" ); } + + private void RotateAroundPivot( float angle, Vector2 pivot, Rect windowRect ) + { + Matrix4x4 matrix = GUI.matrix; + GUI.matrix = Matrix4x4.identity; + Vector2 vector = (Vector2) GUI.matrix.MultiplyPoint3x4( pivot ) + windowRect.position; + + GUI.matrix = ( Matrix4x4.TRS( vector, Quaternion.Euler( 0, 0, angle ), Vector3.one ) * Matrix4x4.TRS( -vector, Quaternion.identity, Vector3.one ) ) * matrix; + } } } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources.meta new file mode 100644 index 000000000..b0ca52e20 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 426036b3edfd436cb163757c8a1c85e3 +timeCreated: 1602118974 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor.meta new file mode 100644 index 000000000..3e4e8c89f --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8f70dd43a26f46aba117e38718757a28 +timeCreated: 1602118974 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel.meta new file mode 100644 index 000000000..5f234709f --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ed8f8ef78f15477b85e95d273705edb9 +timeCreated: 1602118974 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss new file mode 100644 index 000000000..93fddb75c --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss @@ -0,0 +1,12 @@ +/* * * * * * * * * * * * * * * * * * * * * * +EditorWindow.uss + +License: MIT (https://tldrlegal.com/license/mit-license) +Author: Daniel Cornelius + +Base stylesheet for editor windows +* * * * * * * * * * * * * * * * * * * * * */ + +Label { + font-size: 16px; +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss.meta new file mode 100644 index 000000000..9166a4b01 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 501b2fa5843a434b88c69c2d87de2b89 +timeCreated: 1602119861 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml new file mode 100644 index 000000000..c83d3f889 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml @@ -0,0 +1,12 @@ + + diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml.meta new file mode 100644 index 000000000..1355b632d --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8688ab5edaf749fdb48378beee341d30 +timeCreated: 1602119537 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements.meta new file mode 100644 index 000000000..f9697f034 --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bab379c5f8914ab29d7c526f44663f15 +timeCreated: 1602201789 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs new file mode 100644 index 000000000..541cd251d --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs @@ -0,0 +1,110 @@ +/* * * * * * * * * * * * * * * * * * * * * * +Chisel.Editors.ChiselUIElements.cs + +License: +Author: Daniel Cornelius + +* * * * * * * * * * * * * * * * * * * * * */ + +using System; +using System.Collections.Generic; +using UnityEditor.UIElements; +using UnityEngine; +using UnityEngine.UIElements; + +namespace Chisel.Editors +{ + public static class ChiselUIElements + { + public static void SetRotation( this VisualElement v, int degrees ) + { + if( v.transform.rotation != Quaternion.Euler( 0, 0, degrees ) ) + v.schedule.Execute( () => { v.transform.rotation = Quaternion.Euler( 0, 0, degrees ); } ); + } + + public static void SetPosition( this VisualElement v, Vector2 position ) + { + if( v.transform.position != new Vector3( position[0], position[1], v.transform.position.z ) ) + v.schedule.Execute( () => { v.transform.position = new Vector3( position[0], position[1], v.transform.position.z ); } ); + } + + public static void SetSize( this VisualElement v, Vector2 size ) + { + if( v.style.width != size[0] || v.style.height != size[1] ) + { + v.schedule.Execute( () => + { + v.style.width = size[0]; + v.style.height = size[1]; + } ); + } + } + + public static Box AddBox( this VisualElement v, Rect sizeAndPosition, Color tint = default, int depth = 0 ) + { + if(tint == default) tint = new Color( 0.5f, 0.5f, 0.5f, 0.5f ); + + Box box = new Box(); + box.style.width = sizeAndPosition.width; + box.style.height = sizeAndPosition.height; + box.style.backgroundColor = tint; + + v.Add( box ); + + return box; + } + +#region BUTTON + + public static void AddButton( this VisualElement v, string label, Rect sizeAndPosition, int depth = 0, Action action = null ) + { + if( action == null ) + { + action = () => {}; // just add an empty action, this button is doing nothing yet + Debug.LogWarning( $"The button with title \"{label}\" was created without any action, it will always do nothing." ); + } + + Button button = new Button( action ); + button.text = label; + button.style.width = sizeAndPosition.width; + button.style.height = sizeAndPosition.height; + + button.transform.position = new Vector3( sizeAndPosition.x, sizeAndPosition.y, depth ); + + v.Add( button ); + } + + public static void AddButton( this Toolbar t, string label, Vector2Int size, int depth = 0, Action action = null ) + { + if( action == null ) + { + action = () => {}; // just add an empty action, this button is doing nothing yet + Debug.LogWarning( $"The toolbar button with title \"{label}\" was created without any action, it will always do nothing." ); + } + + ToolbarButton button = new ToolbarButton( action ); + button.text = label; + button.style.width = size[0]; + button.style.height = size[1]; + + button.transform.position = new Vector3( 0, 0, depth ); + + t.Add( button ); + } + +#endregion BUTTON + + public static Toolbar AddToolbar( this VisualElement v, Rect sizeAndPosition, int depth = 0 ) + { + Toolbar toolbar = new Toolbar(); + toolbar.style.width = sizeAndPosition.width; + toolbar.style.height = sizeAndPosition.height; + + toolbar.transform.position = new Vector3( sizeAndPosition.x, sizeAndPosition.y, depth ); + + v.Add( toolbar ); + + return toolbar; + } + } +} diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs.meta b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs.meta new file mode 100644 index 000000000..df962a76f --- /dev/null +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8f7db1bf99094c4589b3261c9befc21b +timeCreated: 1602201809 \ No newline at end of file From 7d1e7fc399c68a0012dec2297a85ca4da77af9c8 Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Fri, 9 Oct 2020 02:40:08 -0500 Subject: [PATCH 12/44] Update ChiselMaterialBrowserWindowLayoutTest.cs --- .../ChiselMaterialBrowserWindowLayoutTest.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs index 974c20178..c5058484b 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs @@ -56,7 +56,7 @@ private void OnEnable() // $TODO: Convert to UI Toolkit this.GetRootElement(); - m_TopToolbar = Root.AddBox( new Rect( position.width, 0, TOOLBAR_HEIGHT, position.height ), new Color( 0.15f, 0.15f, 0.15f, 1f ), -1 ); + m_TopToolbar = Root.AddBox( new Rect( position.width, TOOLBAR_HEIGHT, TOOLBAR_HEIGHT, position.height ), new Color( 0.15f, 0.15f, 0.15f, 1f ), -1 ); m_TabBar = m_TopToolbar.AddToolbar( new Rect( 0, 0, position.width, TOOLBAR_HEIGHT ) ); m_TopToolbar.SetRotation( 90 ); @@ -85,7 +85,7 @@ private static Texture2D GetRandNoiseTex() { float xCoord = 0 + x / THUMB_SIZE; float yCoord = 0 + y / THUMB_SIZE; - float sample = Mathf.Clamp01( 1 - Mathf.PerlinNoise( xCoord, yCoord ) * Random.value ); + float sample = Mathf.Clamp01( 1 - Mathf.PerlinNoise( xCoord, yCoord ) * Random.value ) * 0.5f; pix[y * THUMB_SIZE + x] = new Color( sample, sample, sample ); } } @@ -99,14 +99,15 @@ private static Texture2D GetRandNoiseTex() private void OnGUI() { - m_TopToolbar?.SetPosition( new Vector2( position.width, 0 ) ); - m_TopToolbar?.SetSize( new Vector2( position.height, TOOLBAR_HEIGHT ) ); - m_TabBar.SetSize( new Vector2( ( position.height > ( 100 * 4 ) ) ? 100 * 4 : position.height, TOOLBAR_HEIGHT) ); + m_TopToolbar?.SetPosition( new Vector2( position.width, TOOLBAR_HEIGHT ) ); + m_TopToolbar?.SetSize( new Vector2( position.height, TOOLBAR_HEIGHT ) ); + + m_TabBar.SetSize( new Vector2( ( position.height > ( 100 * 4 ) ) ? 100 * 4 : position.height, TOOLBAR_HEIGHT ) ); Rect rect = this.position; //DrawTabBar( rect ); - //DrawLabelAndTileArea( rect ); + DrawLabelAndTileArea( rect ); //DrawToolbar( rect ); //DrawFooter( rect ); } From b267cceb2e4e76cc8fb908e9461228b260aeb18d Mon Sep 17 00:00:00 2001 From: Daniel Cornelius Date: Tue, 13 Oct 2020 01:19:34 -0500 Subject: [PATCH 13/44] finish converting window to IU Toolkit need to figure out what to do with the tabs and fix a layout bug, then work out how to port over the IMGUI functionality --- .../ChiselMaterialBrowserUtilities.cs | 18 +- .../ChiselMaterialBrowserWindowLayoutTest.cs | 326 +++++++++++++----- .../Testing/UIElements/ChiselUIElements.cs | 167 +++++++-- .../Resources.meta | 0 .../Resources/Editor.meta | 0 .../Resources/Editor/Chisel.meta | 0 .../Resources/Editor/Chisel/EditorWindow.uss | 4 +- .../Editor/Chisel/EditorWindow.uss.meta | 0 .../Resources/Editor/Chisel/EditorWindow.uxml | 0 .../Editor/Chisel/EditorWindow.uxml.meta | 0 .../Editor/Chisel/MaterialBrowser.uss | 105 ++++++ .../Editor/Chisel/MaterialBrowser.uss.meta | 3 + .../Editor/Chisel/MaterialBrowserWindow.uxml | 3 + .../Chisel/MaterialBrowserWindow.uxml.meta | 10 + 14 files changed, 504 insertions(+), 132 deletions(-) rename Packages/com.chisel.editor/{Chisel/Editor/Editor/MaterialBrowser/Testing => Editor Resources}/Resources.meta (100%) rename Packages/com.chisel.editor/{Chisel/Editor/Editor/MaterialBrowser/Testing => Editor Resources}/Resources/Editor.meta (100%) rename Packages/com.chisel.editor/{Chisel/Editor/Editor/MaterialBrowser/Testing => Editor Resources}/Resources/Editor/Chisel.meta (100%) rename Packages/com.chisel.editor/{Chisel/Editor/Editor/MaterialBrowser/Testing => Editor Resources}/Resources/Editor/Chisel/EditorWindow.uss (72%) rename Packages/com.chisel.editor/{Chisel/Editor/Editor/MaterialBrowser/Testing => Editor Resources}/Resources/Editor/Chisel/EditorWindow.uss.meta (100%) rename Packages/com.chisel.editor/{Chisel/Editor/Editor/MaterialBrowser/Testing => Editor Resources}/Resources/Editor/Chisel/EditorWindow.uxml (100%) rename Packages/com.chisel.editor/{Chisel/Editor/Editor/MaterialBrowser/Testing => Editor Resources}/Resources/Editor/Chisel/EditorWindow.uxml.meta (100%) create mode 100644 Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowser.uss create mode 100644 Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowser.uss.meta create mode 100644 Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowserWindow.uxml create mode 100644 Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowserWindow.uxml.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs index b41dc9ffd..fd47573cf 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/ChiselMaterialBrowserUtilities.cs @@ -57,7 +57,7 @@ bool HasInvalidShader() } // step val by powers of two - private static int GetPow2( int val ) + public static int GetPow2( int val ) { val--; val |= val >> 1; @@ -70,20 +70,4 @@ private static int GetPow2( int val ) return val; } } - - internal static class ChiselUIElementUtility - { - private static VisualElement m_BaseUXML; - private static StyleSheet m_BaseUSS; - - public static VisualElement GetRootElement( this EditorWindow window ) - { - if( m_BaseUXML == null ) m_BaseUXML = Resources.Load( "Editor/Chisel/EditorWindow" ).CloneTree(); - if( m_BaseUSS == null ) m_BaseUSS = Resources.Load( "Editor/Chisel/EditorWindow" ); - - m_BaseUXML.styleSheets.Add( m_BaseUSS ); - - return m_BaseUXML; - } - } } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs index c5058484b..cb6e8486a 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/ChiselMaterialBrowserWindowLayoutTest.cs @@ -4,8 +4,7 @@ License: Author: Daniel Cornelius -$TODO: DELETE ME WHEN DONE - +$TODO: Port over the browser window to this system. * * * * * * * * * * * * * * * * * * * * * */ using System; @@ -18,51 +17,268 @@ namespace Chisel.Editors { - internal sealed class ChiselMaterialBrowserWindowTest : EditorWindow + internal sealed partial class ChiselMaterialBrowserWindowTest : EditorWindow { - private const int NUM_TILES = 8 * 200; // 8 collumns, 50 rows - private const int TOOLBAR_WIDTH = 140; - private const int THUMB_SIZE = 64; - private const int TOOLBAR_HEIGHT = 22; + private const int NUM_TILES = 8 * 200; + private const int THUMB_SIZE = 64; + + private int m_TileSize = 64; private Vector2 m_ScrollPos = Vector2.zero; private static List tiles = new List(); - private static bool isRenderingNoise = false; + //private static bool isRenderingNoise = false; private VisualElement Root => rootVisualElement; - private Box m_TopToolbar; + private float m_ContentSize; + + private Toolbar m_TopToolbar; + private Toolbar m_TopToolBar2; private Toolbar m_TabBar; + private Toolbar m_StatusBar; + + private ToolbarSpacer m_TopToolBar2Spacer0; + + private ToolbarButton m_PropertiesLabel; + + private Box m_PropertiesGroup; + private Box m_StatusLabelBox; + private ScrollView m_TileView; + private Label m_TileSizeLabel; [MenuItem( "Window/Chisel/Material Browser Test" )] private static void Init() { ChiselMaterialBrowserWindowTest window = EditorWindow.GetWindow( false, "Material Browser" ); - window.maxSize = new Vector2( 690, 2000 ); - window.minSize = new Vector2( 420, 310 ); - + window.maxSize = new Vector2( 800, 720 ); + window.minSize = new Vector2( 800, 720 ); + /* // legacy code for( int i = 0; i < NUM_TILES; i++ ) { if( !isRenderingNoise ) tiles.Add( GetRandNoiseTex() ); - } + }*/ + } + + private VisualElement ConstructTilePreview( string baseElementName, int num ) + { + VisualElement tileBase = new VisualElement(); + + // button for tile + Button bg = tileBase.AddButton( $"", $"{baseElementName}BG", () => { Debug.Log( $"Clicked material button 'Label [{num}]'" ); } ); + bg.SetSize( m_TileSize, m_TileSize ); + // tile thumbnail + bg.AddImage( GetRandNoiseTex(), $"{baseElementName}Image" ); + // label bg + Box box = bg.AddBox( $"{baseElementName}LabelBG" ); + box.SetPosition( 0, m_TileSize - 19 ); + box.SetSize( m_TileSize, 19 ); + box.style.backgroundColor = new StyleColor( new Color( 0, 0, 0, 0.3f ) ); + // label + box.AddLabel( $"Label [{num}]", $"{baseElementName}Label" ); + + return tileBase; } private void OnEnable() { - // $TODO: Convert to UI Toolkit - this.GetRootElement(); + Root.Clear(); + /*==================================================== + * Construct data + */ + List elements = new List(); + + void ConstructElements() + { + //Stopwatch watch = Stopwatch.StartNew(); + elements.Clear(); + m_ContentSize = 0; + for( int i = 0; i < NUM_TILES; i++ ) + { + elements.Add( ConstructTilePreview( "tileImage", i + 1 ) ); + + if( i % (int) Mathf.Clamp( ( position.width - 270 ) / m_TileSize, 2, 8 ) == 0 ) { m_ContentSize += m_TileSize + 4; } + } + + if( m_TileView != null ) + { + foreach( var e in elements ) { m_TileView.Add( e ); } + + m_TileView.MarkDirtyRepaint(); + } + + //watch.Stop(); + //Debug.Log( $"Constructed {elements.Count} elements in {watch.ElapsedMilliseconds:###00}ms" ); + } + + ConstructElements(); + + EditorApplication.update -= Update; + EditorApplication.update += Update; + - m_TopToolbar = Root.AddBox( new Rect( position.width, TOOLBAR_HEIGHT, TOOLBAR_HEIGHT, position.height ), new Color( 0.15f, 0.15f, 0.15f, 1f ), -1 ); + /*==================================================== + * Begin UI layout + */ + // load and assign the root VisualElement + this.GetRootElement( "MaterialBrowserWindow", "MaterialBrowser" ); - m_TabBar = m_TopToolbar.AddToolbar( new Rect( 0, 0, position.width, TOOLBAR_HEIGHT ) ); - m_TopToolbar.SetRotation( 90 ); + // top toolbar + m_TopToolbar = Root.AddToolbar( "toolBar" ); + m_TopToolbar.AddButton( "Refresh", "toolBarRefreshButton", () => + { + if( m_TileView != null ) + { + m_TileView.Clear(); + ConstructElements(); + Root.MarkDirtyRepaint(); + } + + //Debug.Log( "Clicked refresh button" ); + } ); + + // label bar + m_TopToolBar2 = Root.AddToolbar( "toolBar2" ); + m_TopToolBar2Spacer0 = m_TopToolBar2.AddSpacer( "topToolBar2Spacer0" ); + m_TopToolBar2Spacer0.SetSize( position.width - 263, 24 ); + + // properties area label on label bar + m_PropertiesLabel = (ToolbarButton) m_TopToolBar2.AddButton( "Properties", "topToolBar2PropertiesLabel", () => {}, false ); + m_PropertiesLabel.SetSize( 264, 24 ); + + // vertical tab bar + m_TabBar = Root.AddToolbar( "tabBar" ); + m_TabBar.SetPosition( position.width, 0 ); + m_TabBar.SetRotation( 90 ); + + // properties area controlled by tab bar + m_PropertiesGroup = Root.AddBox( "propertiesBox" ); + m_PropertiesGroup.SetPosition( position.width - 264, 48 ); + m_PropertiesGroup.SetSize( 240, position.height - 48 ); + + // content scroll view, has all the material previews. + m_TileView = Root.AddScrollView( in elements, ref m_ScrollPos, in m_TileSize, false, "tileView" ); + m_TileView.SetPosition( 0, 48 ); + m_TileView.SetSize( position.width - 252, position.height - 70 ); + + // bottom status bar + m_StatusBar = Root.AddToolbar( "statusBar" ); + m_StatusBar.AddLabel( "status text", "statusBarLabel" ); + + // bottom status bar, preview tile size DOWN + Button downBTN = m_StatusBar.AddButton( "<", "previewSizeButtonLeft", () => + { + m_TileSize = Mathf.Clamp( ChiselMaterialBrowserUtilities.GetPow2( m_TileSize / 2 ), 64, 256 ); + + m_ContentSize = 0; - for( int i = 0; i < 4; i++ ) { m_TabBar.AddButton( $"Tab {i}", new Vector2Int( 100, 22 ), 0 ); } + for( int i = 0; i < elements.Count; i++ ) + { + if( i % (int) Mathf.Clamp( ( position.width - 270 ) / m_TileSize, 2, 8 ) == 0 ) { m_ContentSize += m_TileSize + 4; } + } + + foreach( var ve in m_TileView.contentContainer.Children() ) + { + // button + preview texture + ve.SetSize( m_TileSize, m_TileSize ); + ve[0].SetSize( m_TileSize, m_TileSize ); + + // label BG + ve[0][1].SetSize( m_TileSize, 19 ); + ve[0][1].SetPosition( 0, m_TileSize - 19 ); + } + + m_TileSizeLabel.text = $"{m_TileSize}"; + } ); + downBTN.SetPosition( position.width - 364, 0 ); + downBTN.SetSize( 20, 24 ); + // bottom status bar, preview tile size label + m_StatusLabelBox = m_StatusBar.AddBox( "tileSizeLabelBG" ); + m_StatusLabelBox.SetPosition( position.width - 346, 0 ); + m_StatusLabelBox.SetSize( 60, 22 ); + m_TileSizeLabel = m_StatusLabelBox.AddLabel( $"{m_TileSize}", "tileSizeLabel" ); + + // bottom status bar, preview tile size UP + Button upBTN = m_StatusBar.AddButton( ">", "previewSizeButtonRight", () => + { + m_TileSize = Mathf.Clamp( ChiselMaterialBrowserUtilities.GetPow2( m_TileSize * 2 ), 64, 256 ); + + m_ContentSize = 0; + + for( int i = 0; i < elements.Count; i++ ) + { + if( i % (int) Mathf.Clamp( ( position.width - 270 ) / m_TileSize, 2, 8 ) == 0 ) { m_ContentSize += m_TileSize + 4; } + } + + foreach( var ve in m_TileView.contentContainer.Children() ) + { + // button + preview texture + ve.SetSize( m_TileSize, m_TileSize ); + ve[0].SetSize( m_TileSize, m_TileSize ); + + // label BG + ve[0][1].SetSize( m_TileSize, 19 ); + ve[0][1].SetPosition( 0, m_TileSize - 19 ); + } + + m_TileSizeLabel.text = $"{m_TileSize}"; + } ); + upBTN.SetPosition( position.width - 284, 0 ); + upBTN.SetSize( 20, 24 ); + + + // handle setting tab labels and functionality + for( int i = 0; i < 4; i++ ) + { + int num = i; + string label = $"Button {i}"; + + // set tab labels + switch( num ) + { + case 0: + { + label = "Labels"; + break; + } + case 1: + { + label = "Properties"; + break; + } + case 2: { break; } + case 3: { break; } + default: throw new IndexOutOfRangeException( $"Tab {num} is out of maximum range of 4, add a new tab before trying to access it" ); + } + + // for each button in the vertical tab well, figure out what it is, and set its action accordingly + Button b = m_TabBar.AddButton( label, $"tabBarButton{num}", () => + { + switch( num ) + { + case 0: + { + m_PropertiesLabel.SetText( "Labels" ); + break; + } + case 1: + { + m_PropertiesLabel.SetText( "Properties" ); + break; + } + case 2: { break; } + case 3: { break; } + default: throw new IndexOutOfRangeException( $"Tab {num} is out of maximum range of 4, add a new tab before trying to access it" ); + } + } ); + b.SetSize( 100, 24 ); + } + + /* // legacy code if( tiles.Count < 1 ) { for( int i = 0; i < NUM_TILES; i++ ) @@ -70,12 +286,19 @@ private void OnEnable() if( !isRenderingNoise ) tiles.Add( GetRandNoiseTex() ); } - } + }*/ + } + + private void Update() + { + m_TileView.contentContainer.SetSize( position.width - 270, m_ContentSize ); } + // $TODO: REMOVE ME, Replace with thumbnailing system. + // generates a random perlin noise texture, used for preview purposes and to create load private static Texture2D GetRandNoiseTex() { - isRenderingNoise = true; + //isRenderingNoise = true; Texture2D noiseTex = new Texture2D( THUMB_SIZE, THUMB_SIZE, TextureFormat.RGB24, false, PlayerSettings.colorSpace == ColorSpace.Linear ); Color[] pix = new Color[THUMB_SIZE * THUMB_SIZE]; @@ -93,25 +316,11 @@ private static Texture2D GetRandNoiseTex() noiseTex.SetPixels( pix ); noiseTex.Apply(); - isRenderingNoise = false; + //isRenderingNoise = false; return noiseTex; } - private void OnGUI() - { - m_TopToolbar?.SetPosition( new Vector2( position.width, TOOLBAR_HEIGHT ) ); - m_TopToolbar?.SetSize( new Vector2( position.height, TOOLBAR_HEIGHT ) ); - - m_TabBar.SetSize( new Vector2( ( position.height > ( 100 * 4 ) ) ? 100 * 4 : position.height, TOOLBAR_HEIGHT ) ); - - Rect rect = this.position; - - //DrawTabBar( rect ); - DrawLabelAndTileArea( rect ); - //DrawToolbar( rect ); - //DrawFooter( rect ); - } - + /* // legacy code private float scrollViewHeight; private float xOffset = 2; @@ -158,47 +367,6 @@ private void DrawLabelAndTileArea( Rect rect ) } } GUI.EndScrollView(); - } - - - private int tabSel = 0; - - private void DrawTabBar( Rect rect ) - { - Matrix4x4 guiMatrix = GUI.matrix; - - GUI.Box( new Rect( rect.width - TOOLBAR_HEIGHT, 0, TOOLBAR_HEIGHT, rect.height ), "", "DockHeader" ); - //GUI.EndClip(); - - RotateAroundPivot( 90, rect.center, rect ); - //GUIUtility.RotateAroundPivot( 90, new Vector2( rect.width, rect.height -TOOLBAR_HEIGHT ) ); - //GUI.matrix = Matrix4x4.identity; - //GUI.matrix = new Matrix4x4( new Vector4( 0, 1, 0, 0 ), new Vector4( -1, 0, 0, 0 ), new Vector4( 0, 0, 1, 0 ), new Vector4( 0, 0, 0, 1 ) ); - - int offset = TOOLBAR_HEIGHT; - for( int i = 0; i < 4; i++ ) { GUI.Button( new Rect( TOOLBAR_HEIGHT, 0, TOOLBAR_HEIGHT, rect.height ), $"BUTTON {i}" ); } - - GUI.matrix = guiMatrix; - //GUI.BeginClip( rect ); - } - - private void DrawFooter( Rect rect ) - { - GUI.Label( new Rect( 0, rect.height - TOOLBAR_HEIGHT, rect.width, TOOLBAR_HEIGHT ), $"xMax: {rect.xMax} | yMax: {rect.yMax} | xMin: {rect.xMin} | yMin: {rect.yMin}" ); - } - - private void DrawToolbar( Rect rect ) - { - GUI.Label( new Rect( 0, 0, rect.width, TOOLBAR_HEIGHT ), $"WindowSize: {position}" ); - } - - private void RotateAroundPivot( float angle, Vector2 pivot, Rect windowRect ) - { - Matrix4x4 matrix = GUI.matrix; - GUI.matrix = Matrix4x4.identity; - Vector2 vector = (Vector2) GUI.matrix.MultiplyPoint3x4( pivot ) + windowRect.position; - - GUI.matrix = ( Matrix4x4.TRS( vector, Quaternion.Euler( 0, 0, angle ), Vector3.one ) * Matrix4x4.TRS( -vector, Quaternion.identity, Vector3.one ) ) * matrix; - } + }*/ } } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs index 541cd251d..b1d605bc5 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs +++ b/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/UIElements/ChiselUIElements.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; +using UnityEditor; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; @@ -16,47 +17,63 @@ namespace Chisel.Editors { public static class ChiselUIElements { +#region CONFIG + + public static void GetRootElement( this EditorWindow window, string uxmlPath, string ussPath = "EditorWindow" ) + { + //VisualTreeAsset m_VTree = Resources.Load( $"Editor/Chisel/{uxmlPath}" ); + + //m_VTree.CloneTree( window.rootVisualElement ); + + window.rootVisualElement.styleSheets.Add( Resources.Load( $"Editor/Chisel/{ussPath}" ) ); + } + + public static void LoadStyleSheet( this VisualElement element, string name ) + { + element.styleSheets.Add( Resources.Load( $"Editor/Chisel/{name}" ) ); + } + public static void SetRotation( this VisualElement v, int degrees ) { if( v.transform.rotation != Quaternion.Euler( 0, 0, degrees ) ) v.schedule.Execute( () => { v.transform.rotation = Quaternion.Euler( 0, 0, degrees ); } ); } - public static void SetPosition( this VisualElement v, Vector2 position ) + public static void SetPosition( this VisualElement v, float x, float y ) { - if( v.transform.position != new Vector3( position[0], position[1], v.transform.position.z ) ) - v.schedule.Execute( () => { v.transform.position = new Vector3( position[0], position[1], v.transform.position.z ); } ); + if( v.transform.position != new Vector3( x, y, v.transform.position.z ) ) + v.schedule.Execute( () => { v.transform.position = new Vector3( x, y, v.transform.position.z ); } ); } - public static void SetSize( this VisualElement v, Vector2 size ) + public static void SetSize( this VisualElement v, float width, float height ) { - if( v.style.width != size[0] || v.style.height != size[1] ) + if( v.style.width != width || v.style.height != height ) { v.schedule.Execute( () => { - v.style.width = size[0]; - v.style.height = size[1]; + v.style.width = width; + v.style.height = height; } ); } } - public static Box AddBox( this VisualElement v, Rect sizeAndPosition, Color tint = default, int depth = 0 ) + public static void SetText( this Label label, string text ) { - if(tint == default) tint = new Color( 0.5f, 0.5f, 0.5f, 0.5f ); - - Box box = new Box(); - box.style.width = sizeAndPosition.width; - box.style.height = sizeAndPosition.height; - box.style.backgroundColor = tint; - - v.Add( box ); + if( label.text != text ) + label.schedule.Execute( () => { label.text = text; } ); + } - return box; + public static void SetText( this Button button, string text ) + { + if( button.text != text ) + button.schedule.Execute( () => { button.text = text; } ); } +#endregion CONFIG + #region BUTTON - public static void AddButton( this VisualElement v, string label, Rect sizeAndPosition, int depth = 0, Action action = null ) + public static Button AddButton( this VisualElement v, string label, string elementName = "", Action action = null, bool canClick = true ) { if( action == null ) { @@ -65,16 +82,16 @@ public static void AddButton( this VisualElement v, string label, Rect sizeAndPo } Button button = new Button( action ); - button.text = label; - button.style.width = sizeAndPosition.width; - button.style.height = sizeAndPosition.height; - - button.transform.position = new Vector3( sizeAndPosition.x, sizeAndPosition.y, depth ); + button.text = label; + button.name = elementName; + button.SetEnabled( canClick ); v.Add( button ); + + return button; } - public static void AddButton( this Toolbar t, string label, Vector2Int size, int depth = 0, Action action = null ) + public static Button AddButton( this Toolbar t, string label, string elementName = "", Action action = null, bool canClick = true ) { if( action == null ) { @@ -83,28 +100,110 @@ public static void AddButton( this Toolbar t, string label, Vector2Int size, int } ToolbarButton button = new ToolbarButton( action ); - button.text = label; - button.style.width = size[0]; - button.style.height = size[1]; - - button.transform.position = new Vector3( 0, 0, depth ); + button.text = label; + button.name = elementName; + button.SetEnabled( canClick ); t.Add( button ); + + return button; } #endregion BUTTON - public static Toolbar AddToolbar( this VisualElement v, Rect sizeAndPosition, int depth = 0 ) + public static Toolbar AddToolbar( this VisualElement v, string elementName = "" ) { Toolbar toolbar = new Toolbar(); - toolbar.style.width = sizeAndPosition.width; - toolbar.style.height = sizeAndPosition.height; - - toolbar.transform.position = new Vector3( sizeAndPosition.x, sizeAndPosition.y, depth ); + toolbar.name = elementName; v.Add( toolbar ); return toolbar; } + + public static ToolbarSpacer AddSpacer( this Toolbar t, string elementName ) + { + ToolbarSpacer spacer = new ToolbarSpacer(); + + spacer.name = elementName; + + t.Add( spacer ); + + return spacer; + } + + public static Label AddLabel( this VisualElement v, string text, string elementName = "" ) + { + Label label = new Label( text ); + label.name = elementName; + + v.Add( label ); + + return label; + } + + public static Box AddBox( this VisualElement v, string elementName = "" ) + { + Box box = new Box(); + box.name = elementName; + + v.Add( box ); + + return box; + } + + public static ScrollView AddScrollView( this VisualElement v, in List elements, ref Vector2 scrollPos, in int tileSize, bool horizontalScrollBar = false, string elementName = "", + string contentContainerName = "contentContainer" ) where T : VisualElement + { + ScrollView sv = new ScrollView( ScrollViewMode.Vertical ); + + sv.name = elementName; + sv.scrollOffset = scrollPos; + sv.elasticity = 0; + sv.showHorizontal = horizontalScrollBar; + + sv.contentContainer.name = contentContainerName; + + foreach( var ve in elements ) + { + ve.SetSize( tileSize, tileSize ); + sv.AddContent( ve ); + } + + v.Add( sv ); + + return sv; + } + + public static void AddContent( this ScrollView sv, VisualElement element ) + { + sv.schedule.Execute( () => { sv.Add( element ); } ); + } + + public static Image AddImage( this VisualElement v, Texture2D image, string elementName = "" ) + { + Image i = new Image(); + i.name = elementName; + i.image = image; + i.scaleMode = ScaleMode.ScaleToFit; + i.uv = new Rect( 0, 1, image.width, image.height ); + + v.Add( i ); + + return i; + } + + public static Slider AddSlider( this VisualElement v, out int value, int min, int max, string label, string elementName = "" ) + { + Slider slider = new Slider( min, max, SliderDirection.Horizontal ); + slider.name = elementName; + slider.label = label; + + value = (int) slider.value; + + v.Add( slider ); + + return slider; + } } } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources.meta b/Packages/com.chisel.editor/Editor Resources/Resources.meta similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources.meta rename to Packages/com.chisel.editor/Editor Resources/Resources.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor.meta b/Packages/com.chisel.editor/Editor Resources/Resources/Editor.meta similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor.meta rename to Packages/com.chisel.editor/Editor Resources/Resources/Editor.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel.meta b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel.meta similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel.meta rename to Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uss similarity index 72% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss rename to Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uss index 93fddb75c..1db7c9427 100644 --- a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss +++ b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uss @@ -4,9 +4,9 @@ EditorWindow.uss License: MIT (https://tldrlegal.com/license/mit-license) Author: Daniel Cornelius -Base stylesheet for editor windows +Base stylesheet for editor windows, these are applied to the entire window. * * * * * * * * * * * * * * * * * * * * * */ -Label { +* { font-size: 16px; } diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss.meta b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uss.meta similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uss.meta rename to Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uss.meta diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uxml similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml rename to Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uxml diff --git a/Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml.meta b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uxml.meta similarity index 100% rename from Packages/com.chisel.editor/Chisel/Editor/Editor/MaterialBrowser/Testing/Resources/Editor/Chisel/EditorWindow.uxml.meta rename to Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/EditorWindow.uxml.meta diff --git a/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowser.uss b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowser.uss new file mode 100644 index 000000000..0786b41fe --- /dev/null +++ b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowser.uss @@ -0,0 +1,105 @@ +#statusBar { + font-size: 12px; + -unity-text-align: middle-left; + background-color: rgb(57, 57, 57); + position: absolute; + width: 100%; + max-height: 24px; + min-height: 24px; + max-width: 100%; + min-width: 100%; + top: 704px; + left: auto; + border-top-width: 1px; + border-top-color: #6e6e6e; +} + +#toolBar, #toolBar2 { + height: 24px; +} + +#statusBarLabel { + font-size: 12px; + width: 100%; + height: 24px; + position: relative; + -unity-text-align: middle-left; + padding-left: 4px; +} + +#tabBar { + background-color: rgb(40, 40, 40); + height: 24px; +} + +#toolBarRefreshButton { + -unity-text-align: middle-center; +} + +#propertiesBox { + position: absolute; +} + +#topToolBar2PropertiesLabel { + -unity-text-align: middle-left; + color: white; +} + +#tileView { + position: absolute; + padding: 2px; +} + +#tileImageBG { + margin: 1px; + padding: 1px; +} + +#tileImageBG:hover { + background-color: #c2c2c2; +} + +#tileImageBG:active { + background-color: #67b618; +} + +#contentContainer { + flex-direction: row; + flex-wrap: wrap; + width: 100%; +} + +#tabBarButton0, #tabBarButton1, #tabBarButton2, #tabBarButton3 { + -unity-text-align: middle-left; +} + +#tileImageLabelBG { + position: absolute; + width: 100%; + height: 16px; +} + +#tileImageLabel { + position: absolute; + width: 100%; + height: 16px; + font-size: 11px; + -unity-text-align: middle-left; +} + +#tileSizeLabel { + font-size: 14px; + -unity-text-align: middle-center; + padding-top: 2px; + color: rgb(190,190,190); +} + +#tileSizeLabelBG { + position: absolute; + align-content: center; + background-color: #323232; +} + +#previewSizeButtonLeft, #previewSizeButtonRight { + position: absolute; +} diff --git a/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowser.uss.meta b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowser.uss.meta new file mode 100644 index 000000000..47dd0e4b9 --- /dev/null +++ b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowser.uss.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 76d1be0340e342d0826dd7032a14c537 +timeCreated: 1602301410 \ No newline at end of file diff --git a/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowserWindow.uxml b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowserWindow.uxml new file mode 100644 index 000000000..cb53464d5 --- /dev/null +++ b/Packages/com.chisel.editor/Editor Resources/Resources/Editor/Chisel/MaterialBrowserWindow.uxml @@ -0,0 +1,3 @@ + +