mirror of
https://git.aaro.dev/VRCBoard/vrcboard-udon.git
synced 2026-03-17 04:29:46 +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 OnImageDataUpdate();
|
||||||
protected abstract void OnImageLoaded();
|
protected abstract void OnImageLoaded();
|
||||||
|
|
||||||
// ReSharper disable once InconsistentNaming
|
|
||||||
protected internal void _Register(VRCBoardManager _manager)
|
protected internal void _Register(VRCBoardManager _manager)
|
||||||
{
|
{
|
||||||
manager = _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();
|
OnImageLoaded();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -329,19 +329,19 @@ MonoBehaviour:
|
|||||||
Data:
|
Data:
|
||||||
- Name: $k
|
- Name: $k
|
||||||
Entry: 1
|
Entry: 1
|
||||||
Data: materialOverride
|
Data: materialIndex
|
||||||
- Name: $v
|
- Name: $v
|
||||||
Entry: 7
|
Entry: 7
|
||||||
Data: 18|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
|
Data: 18|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
|
||||||
- Name: <Name>k__BackingField
|
- Name: <Name>k__BackingField
|
||||||
Entry: 1
|
Entry: 1
|
||||||
Data: materialOverride
|
Data: materialIndex
|
||||||
- Name: <UserType>k__BackingField
|
- Name: <UserType>k__BackingField
|
||||||
Entry: 7
|
Entry: 7
|
||||||
Data: 19|System.RuntimeType, mscorlib
|
Data: 19|System.RuntimeType, mscorlib
|
||||||
- Name:
|
- Name:
|
||||||
Entry: 1
|
Entry: 1
|
||||||
Data: UnityEngine.Material, UnityEngine.CoreModule
|
Data: System.Int32, mscorlib
|
||||||
- Name:
|
- Name:
|
||||||
Entry: 8
|
Entry: 8
|
||||||
Data:
|
Data:
|
||||||
@@ -368,11 +368,7 @@ MonoBehaviour:
|
|||||||
Data: 1
|
Data: 1
|
||||||
- Name:
|
- Name:
|
||||||
Entry: 7
|
Entry: 7
|
||||||
Data: 21|UnityEngine.HeaderAttribute, UnityEngine.CoreModule
|
Data: 21|UnityEngine.HideInInspector, UnityEngine.CoreModule
|
||||||
- Name: header
|
|
||||||
Entry: 1
|
|
||||||
Data: Material Override (Optional, if set the properties are applied globally
|
|
||||||
to all renderers using the material)
|
|
||||||
- Name:
|
- Name:
|
||||||
Entry: 8
|
Entry: 8
|
||||||
Data:
|
Data:
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ namespace VRCBoard.Components
|
|||||||
[Header("Uploader ID (only applies to images of 'shared' or 'instance' type)")]
|
[Header("Uploader ID (only applies to images of 'shared' or 'instance' type)")]
|
||||||
public string uploaderIdOverride;
|
public string uploaderIdOverride;
|
||||||
|
|
||||||
[Header("Material Override (Optional, if set the properties are applied globally to all renderers using the material)")]
|
[HideInInspector] public int materialIndex;
|
||||||
public Material materialOverride;
|
|
||||||
|
|
||||||
[HideInInspector] public string[] imageIds = new string[0];
|
[HideInInspector] public string[] imageIds = new string[0];
|
||||||
[HideInInspector] public string[] texturePropertyMappings = new string[0];
|
[HideInInspector] public string[] texturePropertyMappings = new string[0];
|
||||||
@@ -31,6 +30,7 @@ namespace VRCBoard.Components
|
|||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
_renderer = GetComponent<Renderer>();
|
_renderer = GetComponent<Renderer>();
|
||||||
|
OnImageLoaded();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnRegister()
|
protected override void OnRegister()
|
||||||
@@ -102,10 +102,11 @@ namespace VRCBoard.Components
|
|||||||
protected override void OnInitialize()
|
protected override void OnInitialize()
|
||||||
{
|
{
|
||||||
TryGetUploader();
|
TryGetUploader();
|
||||||
|
OnImageLoaded();
|
||||||
}
|
}
|
||||||
protected override void OnImageDataUpdate()
|
protected override void OnImageDataUpdate()
|
||||||
{
|
{
|
||||||
TryGetUploader();
|
// OnImageLoaded();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnSupporterDataUpdate()
|
protected override void OnSupporterDataUpdate()
|
||||||
@@ -128,43 +129,50 @@ namespace VRCBoard.Components
|
|||||||
if (i >= defaultTextures.Length) continue;
|
if (i >= defaultTextures.Length) continue;
|
||||||
|
|
||||||
string propertyName = texturePropertyMappings[i];
|
string propertyName = texturePropertyMappings[i];
|
||||||
Texture2D texture = manager.GetTexture2D(_uploaderId, imageIds[i]);
|
Texture2D texture = manager ? manager.GetTexture2D(_uploaderId, imageIds[i]) : defaultTextures[i];
|
||||||
|
|
||||||
if (texture == null)
|
if (texture == null)
|
||||||
{
|
{
|
||||||
|
Debug.Log("texture was null, using default texture");
|
||||||
texture = defaultTextures[i];
|
texture = defaultTextures[i];
|
||||||
}
|
}
|
||||||
if (string.IsNullOrEmpty(propertyName)) continue;
|
if (string.IsNullOrEmpty(propertyName)) continue;
|
||||||
|
|
||||||
if (texture != null) propertyBlock.SetTexture(propertyName, texture);
|
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
|
#if UNITY_EDITOR && !COMPILER_UDONSHARP
|
||||||
[CustomEditor(typeof(VRCBoardImage), true)]
|
[CustomEditor(typeof(VRCBoardImage), true)]
|
||||||
public class VRCBoardImageComponentEditor : Editor
|
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()
|
public override void OnInspectorGUI()
|
||||||
{
|
{
|
||||||
base.OnInspectorGUI();
|
base.OnInspectorGUI();
|
||||||
|
|
||||||
VRCBoardImage script = (VRCBoardImage)target;
|
VRCBoardImage script = (VRCBoardImage)target;
|
||||||
|
|
||||||
Renderer renderer = script.GetComponent<Renderer>();
|
Renderer renderer = script.GetComponent<Renderer>();
|
||||||
Material material = renderer.sharedMaterial;
|
|
||||||
if (script.materialOverride)
|
if (renderer == null)
|
||||||
{
|
|
||||||
EditorGUILayout.HelpBox("Material override enabled, material may behave in odd ways", MessageType.Warning);
|
|
||||||
material = script.materialOverride;
|
|
||||||
} else if (renderer == null)
|
|
||||||
{
|
{
|
||||||
EditorGUILayout.HelpBox("No renderer, did you intend to use material override?", MessageType.Warning);
|
EditorGUILayout.HelpBox("No renderer, did you intend to use material override?", MessageType.Warning);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
string[] materialPropertyNames = material.GetPropertyNames(MaterialPropertyType.Texture);
|
|
||||||
|
|
||||||
bool equals =
|
bool equals =
|
||||||
Equals(script.imageIds.Length, script.defaultTextures.Length) &&
|
Equals(script.imageIds.Length, script.defaultTextures.Length) &&
|
||||||
@@ -172,6 +180,28 @@ namespace VRCBoard.Components
|
|||||||
|
|
||||||
|
|
||||||
EditorGUILayout.Separator();
|
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");
|
GUILayout.BeginVertical("Images", "window");
|
||||||
int newSize = script.imageIds.Length;
|
int newSize = script.imageIds.Length;
|
||||||
GUILayout.BeginHorizontal();
|
GUILayout.BeginHorizontal();
|
||||||
@@ -221,7 +251,6 @@ namespace VRCBoard.Components
|
|||||||
defaultImage = EditorGUILayout.ObjectField(defaultImage, typeof(Texture2D), false) as Texture2D;
|
defaultImage = EditorGUILayout.ObjectField(defaultImage, typeof(Texture2D), false) as Texture2D;
|
||||||
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.EndHorizontal();
|
||||||
if (defaultImage != null) propertyBlock.SetTexture(propertyName, defaultImage);
|
if (defaultImage != null) propertyBlock.SetTexture(propertyName, defaultImage);
|
||||||
if (script.materialOverride != null) script.materialOverride.SetTexture(propertyName, defaultImage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (script.defaultTextures[i] != defaultImage)
|
if (script.defaultTextures[i] != defaultImage)
|
||||||
@@ -243,8 +272,8 @@ namespace VRCBoard.Components
|
|||||||
}
|
}
|
||||||
GUILayout.EndVertical();
|
GUILayout.EndVertical();
|
||||||
}
|
}
|
||||||
if (script.materialOverride== null && renderer) renderer.SetPropertyBlock(propertyBlock);
|
if (renderer) renderer.SetPropertyBlock(propertyBlock,script.materialIndex);
|
||||||
EditorGUILayout.EndHorizontal();
|
EditorGUILayout.EndVertical();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -311,12 +311,13 @@ namespace VRCBoard
|
|||||||
}
|
}
|
||||||
|
|
||||||
atlasIndex = (int)atlasIndexToken.Double;
|
atlasIndex = (int)atlasIndexToken.Double;
|
||||||
texture = _downloadedAtlasTextures[atlasIndex];
|
|
||||||
bool positionSuccess = uploadInfo.TryGetValue("p", out DataToken positionToken);
|
bool positionSuccess = uploadInfo.TryGetValue("p", out DataToken positionToken);
|
||||||
if (!positionSuccess || positionToken.TokenType != TokenType.Double)
|
if (!positionSuccess || positionToken.TokenType != TokenType.Double)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
texture = _downloadedAtlasTextures[atlasIndex];
|
||||||
position = (int)positionToken.Double;
|
position = (int)positionToken.Double;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -517,27 +518,28 @@ namespace VRCBoard
|
|||||||
_atlasDownloadTimes[index] = Time.time;
|
_atlasDownloadTimes[index] = Time.time;
|
||||||
Debug.Log("Downloaded atlas texture " + index);
|
Debug.Log("Downloaded atlas texture " + index);
|
||||||
foreach (var component in vrcBoardComponents)
|
foreach (var component in vrcBoardComponents)
|
||||||
component._OnImageLoaded(index);
|
component._OnAtlasImageLoaded(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[PublicAPI]
|
||||||
public Texture2D GetTexture2D(string uploader, string imageId)
|
public Texture2D GetTexture2D(string uploader, string imageId)
|
||||||
{
|
{
|
||||||
bool success = _GetImageAtlasTexture(imageId, uploader, out int atlasIndex, out Texture2D texture,
|
bool success = _GetImageAtlasTexture(imageId, uploader, out int atlasIndex, out Texture2D texture,
|
||||||
out int position, out int size, out string type);
|
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);
|
Debug.LogWarning("[VRCBoard] [GetTexture2D] Failed to get texture for " + uploader);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!texture)
|
if (texture == null)
|
||||||
{
|
{
|
||||||
Debug.LogWarning("[VRCBoard] [GetTexture2D] Failed to get texture for " + uploader);
|
Debug.LogWarning("[VRCBoard] [GetTexture2D] Failed to get texture for " + uploader);
|
||||||
return null;
|
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;
|
int startX = position % size;
|
||||||
@@ -552,6 +554,9 @@ namespace VRCBoard
|
|||||||
Color[] pixels = texture.GetPixels(startX * blockWidth, startY * blockHeight, blockWidth, blockHeight);
|
Color[] pixels = texture.GetPixels(startX * blockWidth, startY * blockHeight, blockWidth, blockHeight);
|
||||||
newTexture.SetPixels(pixels);
|
newTexture.SetPixels(pixels);
|
||||||
newTexture.wrapMode = TextureWrapMode.Clamp;
|
newTexture.wrapMode = TextureWrapMode.Clamp;
|
||||||
|
newTexture.filterMode = FilterMode.Trilinear;
|
||||||
|
newTexture.anisoLevel = 1;
|
||||||
|
//newTexture.alphaIsTransparency = true;
|
||||||
newTexture.Apply();
|
newTexture.Apply();
|
||||||
return newTexture;
|
return newTexture;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user