mirror of
https://git.aaro.dev/VRCBoard/vrcboard-udon.git
synced 2026-03-17 01:09:45 +00:00
Refactor VRCBoard components to replace material override with material index and update related methods
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user