From 9777e0225d7570f53c48950d3416fa83daff0bc8 Mon Sep 17 00:00:00 2001 From: Aaro Varis Date: Fri, 18 Oct 2024 13:51:27 +0300 Subject: [PATCH] Refactor VRCBoard components to replace material override with material index and update related methods --- Components/VRCBoardBaseComponent.cs | 3 +- Components/VRCBoardImage.asset | 12 ++---- Components/VRCBoardImage.cs | 63 +++++++++++++++++++++-------- VRCBoardManager.cs | 15 ++++--- 4 files changed, 61 insertions(+), 32 deletions(-) diff --git a/Components/VRCBoardBaseComponent.cs b/Components/VRCBoardBaseComponent.cs index 0aaab20..27c98f8 100644 --- a/Components/VRCBoardBaseComponent.cs +++ b/Components/VRCBoardBaseComponent.cs @@ -19,7 +19,6 @@ namespace VRCBoard.Components protected abstract void OnImageDataUpdate(); protected abstract void OnImageLoaded(); - // ReSharper disable once InconsistentNaming protected internal void _Register(VRCBoardManager _manager) { manager = _manager; @@ -59,7 +58,7 @@ namespace VRCBoard.Components } } - protected internal void _OnImageLoaded(int atlasIndex = -1) + protected internal void _OnAtlasImageLoaded(int atlasIndex = -1) { OnImageLoaded(); } diff --git a/Components/VRCBoardImage.asset b/Components/VRCBoardImage.asset index d169863..2d292a4 100644 --- a/Components/VRCBoardImage.asset +++ b/Components/VRCBoardImage.asset @@ -329,19 +329,19 @@ MonoBehaviour: Data: - Name: $k Entry: 1 - Data: materialOverride + Data: materialIndex - Name: $v Entry: 7 Data: 18|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor - Name: k__BackingField Entry: 1 - Data: materialOverride + Data: materialIndex - Name: k__BackingField Entry: 7 Data: 19|System.RuntimeType, mscorlib - Name: Entry: 1 - Data: UnityEngine.Material, UnityEngine.CoreModule + Data: System.Int32, mscorlib - Name: Entry: 8 Data: @@ -368,11 +368,7 @@ MonoBehaviour: Data: 1 - Name: Entry: 7 - Data: 21|UnityEngine.HeaderAttribute, UnityEngine.CoreModule - - Name: header - Entry: 1 - Data: Material Override (Optional, if set the properties are applied globally - to all renderers using the material) + Data: 21|UnityEngine.HideInInspector, UnityEngine.CoreModule - Name: Entry: 8 Data: diff --git a/Components/VRCBoardImage.cs b/Components/VRCBoardImage.cs index 48c87cf..22e2bb7 100644 --- a/Components/VRCBoardImage.cs +++ b/Components/VRCBoardImage.cs @@ -16,8 +16,7 @@ namespace VRCBoard.Components [Header("Uploader ID (only applies to images of 'shared' or 'instance' type)")] public string uploaderIdOverride; - [Header("Material Override (Optional, if set the properties are applied globally to all renderers using the material)")] - public Material materialOverride; + [HideInInspector] public int materialIndex; [HideInInspector] public string[] imageIds = new string[0]; [HideInInspector] public string[] texturePropertyMappings = new string[0]; @@ -31,6 +30,7 @@ namespace VRCBoard.Components private void Start() { _renderer = GetComponent(); + OnImageLoaded(); } protected override void OnRegister() @@ -102,10 +102,11 @@ namespace VRCBoard.Components protected override void OnInitialize() { TryGetUploader(); + OnImageLoaded(); } protected override void OnImageDataUpdate() { - TryGetUploader(); + // OnImageLoaded(); } protected override void OnSupporterDataUpdate() @@ -128,43 +129,50 @@ namespace VRCBoard.Components if (i >= defaultTextures.Length) continue; string propertyName = texturePropertyMappings[i]; - Texture2D texture = manager.GetTexture2D(_uploaderId, imageIds[i]); + Texture2D texture = manager ? manager.GetTexture2D(_uploaderId, imageIds[i]) : defaultTextures[i]; if (texture == null) { + Debug.Log("texture was null, using default texture"); texture = defaultTextures[i]; } if (string.IsNullOrEmpty(propertyName)) continue; if (texture != null) propertyBlock.SetTexture(propertyName, texture); - if (materialOverride != null) materialOverride.SetTexture(propertyName, texture); } - if (materialOverride== null && _renderer) _renderer.SetPropertyBlock(propertyBlock); + if (_renderer) _renderer.SetPropertyBlock(propertyBlock,materialIndex); } } #if UNITY_EDITOR && !COMPILER_UDONSHARP [CustomEditor(typeof(VRCBoardImage), true)] public class VRCBoardImageComponentEditor : Editor { + public void OnDisable() + { + VRCBoardImage script = (VRCBoardImage)target; + Renderer renderer = script.GetComponent(); + int materialCount = renderer.sharedMaterials.Length; + MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock(); + for (int i = 0; i < materialCount; i++) + { + renderer.SetPropertyBlock(propertyBlock, i); + } + } + public override void OnInspectorGUI() { base.OnInspectorGUI(); VRCBoardImage script = (VRCBoardImage)target; - Renderer renderer = script.GetComponent(); - Material material = renderer.sharedMaterial; - if (script.materialOverride) - { - EditorGUILayout.HelpBox("Material override enabled, material may behave in odd ways", MessageType.Warning); - material = script.materialOverride; - } else if (renderer == null) + + if (renderer == null) { EditorGUILayout.HelpBox("No renderer, did you intend to use material override?", MessageType.Warning); return; } - string[] materialPropertyNames = material.GetPropertyNames(MaterialPropertyType.Texture); + bool equals = Equals(script.imageIds.Length, script.defaultTextures.Length) && @@ -172,6 +180,28 @@ namespace VRCBoard.Components EditorGUILayout.Separator(); + + int materialCount = renderer.sharedMaterials.Length; + int newMaterialIndex = materialCount > 1 + ? EditorGUILayout.IntSlider("Material Slot", script.materialIndex, 0, materialCount - 1) : 0; + newMaterialIndex = Mathf.Clamp(newMaterialIndex, 0, materialCount); + if (newMaterialIndex != script.materialIndex) + { + script.materialIndex = newMaterialIndex; + OnDisable(); + EditorUtility.SetDirty(script); + } + + Material material = renderer.sharedMaterials[newMaterialIndex]; + + if (material == null) + { + EditorGUILayout.HelpBox("No material assigned to material slot ("+newMaterialIndex+")", MessageType.Warning); + return; + } + + string[] materialPropertyNames = material.GetPropertyNames(MaterialPropertyType.Texture); + GUILayout.BeginVertical("Images", "window"); int newSize = script.imageIds.Length; GUILayout.BeginHorizontal(); @@ -221,7 +251,6 @@ namespace VRCBoard.Components defaultImage = EditorGUILayout.ObjectField(defaultImage, typeof(Texture2D), false) as Texture2D; EditorGUILayout.EndHorizontal(); if (defaultImage != null) propertyBlock.SetTexture(propertyName, defaultImage); - if (script.materialOverride != null) script.materialOverride.SetTexture(propertyName, defaultImage); } if (script.defaultTextures[i] != defaultImage) @@ -243,8 +272,8 @@ namespace VRCBoard.Components } GUILayout.EndVertical(); } - if (script.materialOverride== null && renderer) renderer.SetPropertyBlock(propertyBlock); - EditorGUILayout.EndHorizontal(); + if (renderer) renderer.SetPropertyBlock(propertyBlock,script.materialIndex); + EditorGUILayout.EndVertical(); } } diff --git a/VRCBoardManager.cs b/VRCBoardManager.cs index ebe99a7..3032767 100644 --- a/VRCBoardManager.cs +++ b/VRCBoardManager.cs @@ -311,12 +311,13 @@ namespace VRCBoard } atlasIndex = (int)atlasIndexToken.Double; - texture = _downloadedAtlasTextures[atlasIndex]; + bool positionSuccess = uploadInfo.TryGetValue("p", out DataToken positionToken); if (!positionSuccess || positionToken.TokenType != TokenType.Double) { return false; } + texture = _downloadedAtlasTextures[atlasIndex]; position = (int)positionToken.Double; return true; } @@ -517,27 +518,28 @@ namespace VRCBoard _atlasDownloadTimes[index] = Time.time; Debug.Log("Downloaded atlas texture " + index); foreach (var component in vrcBoardComponents) - component._OnImageLoaded(index); + component._OnAtlasImageLoaded(index); } + [PublicAPI] public Texture2D GetTexture2D(string uploader, string imageId) { bool success = _GetImageAtlasTexture(imageId, uploader, out int atlasIndex, out Texture2D texture, out int position, out int size, out string type); - if (!success) + if (!success || atlasIndex == -1) { Debug.LogWarning("[VRCBoard] [GetTexture2D] Failed to get texture for " + uploader); return null; } - if (!texture) + if (texture == null) { Debug.LogWarning("[VRCBoard] [GetTexture2D] Failed to get texture for " + uploader); return null; } - int blockSize = (int)Mathf.Floor((float)texture.width / (float)size); + int blockSize = (int)Mathf.Floor((float)(texture.width+1) / (float)(size)); int startX = position % size; @@ -552,6 +554,9 @@ namespace VRCBoard Color[] pixels = texture.GetPixels(startX * blockWidth, startY * blockHeight, blockWidth, blockHeight); newTexture.SetPixels(pixels); newTexture.wrapMode = TextureWrapMode.Clamp; + newTexture.filterMode = FilterMode.Trilinear; + newTexture.anisoLevel = 1; + //newTexture.alphaIsTransparency = true; newTexture.Apply(); return newTexture; }