Refactor VRCBoard components to replace material override with material index and update related methods

This commit is contained in:
2024-10-18 13:51:27 +03:00
parent 5d0d1b8f37
commit 9777e0225d
4 changed files with 61 additions and 32 deletions

View File

@@ -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();
}

View File

@@ -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: <Name>k__BackingField
Entry: 1
Data: materialOverride
Data: materialIndex
- Name: <UserType>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:

View File

@@ -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<Renderer>();
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<Renderer>();
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<Renderer>();
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();
}
}