Refactor VRCBoard components and update material properties

This commit is contained in:
2024-10-15 14:58:58 +03:00
parent ea409c627a
commit e25230326b
8 changed files with 477 additions and 686 deletions

View File

@@ -105,7 +105,7 @@ namespace VRCBoard.Components
if (Application.isPlaying)
{
EditorGUILayout.LabelField("Cannot edit a VRCBoard component while in play mode");
return;
//return;
}
base.OnInspectorGUI();

View File

@@ -44,7 +44,7 @@ MonoBehaviour:
Data:
- Name:
Entry: 12
Data: 16
Data: 10
- Name:
Entry: 7
Data:
@@ -329,19 +329,25 @@ MonoBehaviour:
Data:
- Name: $k
Entry: 1
Data: albedoImageId
Data: imageIds
- Name: $v
Entry: 7
Data: 18|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: albedoImageId
Data: imageIds
- Name: <UserType>k__BackingField
Entry: 9
Data: 15
Entry: 7
Data: 19|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.String[], mscorlib
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 15
Data: 19
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -356,16 +362,22 @@ MonoBehaviour:
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 19|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 20|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
Data: 2
- Name:
Entry: 7
Data: 20|UnityEngine.HeaderAttribute, UnityEngine.CoreModule
Data: 21|UnityEngine.HeaderAttribute, UnityEngine.CoreModule
- Name: header
Entry: 1
Data: Image IDs
Data: Uploader Image Mappings
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data: 22|UnityEngine.HideInInspector, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
@@ -386,67 +398,19 @@ MonoBehaviour:
Data:
- Name: $k
Entry: 1
Data: emissionImageId
- Name: $v
Entry: 7
Data: 21|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: emissionImageId
- Name: <UserType>k__BackingField
Entry: 9
Data: 15
- Name: <SystemType>k__BackingField
Entry: 9
Data: 15
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 22|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: normalImageId
Data: texturePropertyMappings
- Name: $v
Entry: 7
Data: 23|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: normalImageId
Data: texturePropertyMappings
- Name: <UserType>k__BackingField
Entry: 9
Data: 15
Data: 19
- Name: <SystemType>k__BackingField
Entry: 9
Data: 15
Data: 19
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -464,7 +428,13 @@ MonoBehaviour:
Data: 24|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
Data: 1
- Name:
Entry: 7
Data: 25|UnityEngine.HideInInspector, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:
@@ -482,25 +452,25 @@ MonoBehaviour:
Data:
- Name: $k
Entry: 1
Data: defaultAlbedo
Data: defaultTextures
- Name: $v
Entry: 7
Data: 25|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 26|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: defaultAlbedo
Data: defaultTextures
- Name: <UserType>k__BackingField
Entry: 7
Data: 26|System.RuntimeType, mscorlib
Data: 27|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: UnityEngine.Texture2D, UnityEngine.CoreModule
Data: UnityEngine.Texture2D[], UnityEngine.CoreModule
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 26
Data: 27
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -515,16 +485,13 @@ MonoBehaviour:
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 27|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 28|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data: 28|UnityEngine.HeaderAttribute, UnityEngine.CoreModule
- Name: header
Entry: 1
Data: Defaults
Data: 29|UnityEngine.HideInInspector, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
@@ -545,25 +512,19 @@ MonoBehaviour:
Data:
- Name: $k
Entry: 1
Data: albedoColor
Data: _mainType
- Name: $v
Entry: 7
Data: 29|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 30|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: albedoColor
Data: _mainType
- Name: <UserType>k__BackingField
Entry: 7
Data: 30|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: UnityEngine.Color, UnityEngine.CoreModule
- Name:
Entry: 8
Data:
Entry: 9
Data: 15
- Name: <SystemType>k__BackingField
Entry: 9
Data: 30
Data: 15
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
@@ -575,304 +536,10 @@ MonoBehaviour:
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: true
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 31|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data: 32|UnityEngine.ColorUsageAttribute, UnityEngine.CoreModule
- Name: showAlpha
Entry: 5
Data: true
- Name: hdr
Entry: 5
Data: true
- Name: minBrightness
Entry: 4
Data: 0
- Name: maxBrightness
Entry: 4
Data: 8
- Name: minExposureValue
Entry: 4
Data: 0.125
- Name: maxExposureValue
Entry: 4
Data: 3
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: defaultEmission
- Name: $v
Entry: 7
Data: 33|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: defaultEmission
- Name: <UserType>k__BackingField
Entry: 9
Data: 26
- Name: <SystemType>k__BackingField
Entry: 9
Data: 26
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 34|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: emissionColor
- Name: $v
Entry: 7
Data: 35|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: emissionColor
- Name: <UserType>k__BackingField
Entry: 9
Data: 30
- Name: <SystemType>k__BackingField
Entry: 9
Data: 30
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 36|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 1
- Name:
Entry: 7
Data: 37|UnityEngine.ColorUsageAttribute, UnityEngine.CoreModule
- Name: showAlpha
Entry: 5
Data: true
- Name: hdr
Entry: 5
Data: true
- Name: minBrightness
Entry: 4
Data: 0
- Name: maxBrightness
Entry: 4
Data: 8
- Name: minExposureValue
Entry: 4
Data: 0.125
- Name: maxExposureValue
Entry: 4
Data: 3
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: defaultNormal
- Name: $v
Entry: 7
Data: 38|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: defaultNormal
- Name: <UserType>k__BackingField
Entry: 9
Data: 26
- Name: <SystemType>k__BackingField
Entry: 9
Data: 26
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 39|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: normalIntensity
- Name: $v
Entry: 7
Data: 40|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: normalIntensity
- Name: <UserType>k__BackingField
Entry: 7
Data: 41|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.Single, mscorlib
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 41
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: true
- Name: _fieldAttributes
Entry: 7
Data: 42|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: _mainType
- Name: $v
Entry: 7
Data: 43|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: _mainType
- Name: <UserType>k__BackingField
Entry: 9
Data: 15
- Name: <SystemType>k__BackingField
Entry: 9
Data: 15
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 44|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0
@@ -896,7 +563,7 @@ MonoBehaviour:
Data: _uploaderId
- Name: $v
Entry: 7
Data: 45|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
Data: 32|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: _uploaderId
@@ -920,7 +587,7 @@ MonoBehaviour:
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 46|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
Data: 33|System.Collections.Generic.List`1[[System.Attribute, mscorlib]], mscorlib
- Name:
Entry: 12
Data: 0

View File

@@ -1,58 +1,31 @@
using UdonSharp;
using System;
using UdonSharp;
using UnityEngine;
using UnityEngine.Serialization;
using VRC.SDK3.Data;
using UnityEditor;
namespace VRCBoard.Components
{
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class VRCBoardImage : VRCBoardBaseComponent
{
[Header("Uploader ID (only applies to images of 'shared' or 'instance' type)")]
public string uploaderIdOverride;
[Header("Image IDs")]
public string albedoImageId;
public string emissionImageId;
public string normalImageId;
[Header("Defaults")]
public Texture2D defaultAlbedo;
[ColorUsageAttribute(true,true)] public Color albedoColor = Color.white;
public Texture2D defaultEmission;
[ColorUsageAttribute(true,true)] public Color emissionColor = Color.white;
public Texture2D defaultNormal;
public float normalIntensity = 1.0f;
[Header("Uploader Image Mappings")]
[HideInInspector] public string[] imageIds = new string[0];
[HideInInspector] public string[] texturePropertyMappings = new string[0];
[HideInInspector] public Texture2D[] defaultTextures = new Texture2D[0];
private string _mainType = "";
private string _uploaderId = "";
protected override void OnRegister()
{
MaterialPropertyBlock _propertyBlock = new MaterialPropertyBlock();
Debug.Log("Registering image component");
if (defaultAlbedo)
_propertyBlock.SetTexture("_MainTexAtlas", defaultAlbedo);
if (defaultEmission)
_propertyBlock.SetTexture("_EmissionMapAtlas", defaultEmission);
if (defaultNormal)
_propertyBlock.SetTexture("_NormalMapAtlas", defaultNormal);
_propertyBlock.SetInteger("_MainTexAtlasIndex", 0);
_propertyBlock.SetInteger("_MainTexAtlasSize", 1);
_propertyBlock.SetInteger("_EmissionMapAtlasIndex", 0);
_propertyBlock.SetInteger("_EmissionMapAtlasSize", 1);
_propertyBlock.SetInteger("_NormalMapAtlasIndex", 0);
_propertyBlock.SetInteger("_NormalMapAtlasSize", 1);
_propertyBlock.SetColor("_EmissionColor", emissionColor);
_propertyBlock.SetFloat("_NormalMapScale", normalIntensity);
_propertyBlock.SetColor("_MainTexColor", albedoColor);
GetComponent<Renderer>().SetPropertyBlock(_propertyBlock);
}
@@ -61,7 +34,8 @@ namespace VRCBoard.Components
string uploaderId = "";
if (string.IsNullOrEmpty(uploaderIdOverride))
{
GetImageAtlasTexture(albedoImageId, null, out Texture2D albedoTexture, out int albedoAtlasPosition, out int albedoAtlasSize, out string albedoType);
string firstImageID = imageIds[0];
GetImageAtlasTexture(firstImageID, null, out Texture2D albedoTexture, out int albedoAtlasPosition, out int albedoAtlasSize, out string albedoType);
if (albedoType == null)
{
Debug.LogError("Failed to get image type for albedo image");
@@ -70,7 +44,7 @@ namespace VRCBoard.Components
_mainType = albedoType;
if (_mainType == "shared")
{
uploaderId = GetRandomUploader(albedoImageId);
uploaderId = GetRandomUploader(firstImageID);
}
else
{
@@ -106,9 +80,11 @@ namespace VRCBoard.Components
Debug.LogError("Failed to get uploader ID");
return;
}
RequestImageLoad(albedoImageId, _uploaderId);
RequestImageLoad(emissionImageId, _uploaderId);
RequestImageLoad(normalImageId, _uploaderId);
for (int i = 0; i < imageIds.Length; i++)
{
string imageId = imageIds[i];
RequestImageLoad(imageId, _uploaderId);
}
}
protected override void OnInitialize()
{
@@ -127,53 +103,111 @@ namespace VRCBoard.Components
protected override void OnImageLoaded()
{
Debug.Log("Image loaded VRCBoardImage");
Texture2D albedoTexture;
int albedoAtlasPosition;
int albedoAtlasSize;
Texture2D emissionTexture;
int emissionAtlasPosition;
int emissionAtlasSize;
Texture2D normalTexture;
int normalAtlasPosition;
int normalAtlasSize;
bool albedoSuccess = GetImageAtlasTexture(albedoImageId, _uploaderId, out albedoTexture, out albedoAtlasPosition, out albedoAtlasSize, out string type1);
bool emissionSuccess = GetImageAtlasTexture(emissionImageId, _uploaderId, out emissionTexture, out emissionAtlasPosition, out emissionAtlasSize, out string type2);
bool normalSuccess = GetImageAtlasTexture(normalImageId, _uploaderId, out normalTexture, out normalAtlasPosition, out normalAtlasSize, out string type3);
MaterialPropertyBlock _propertyBlock = new MaterialPropertyBlock();
if (albedoSuccess && albedoTexture) {
_propertyBlock.SetTexture("_MainTexAtlas", albedoTexture);
_propertyBlock.SetInteger("_MainTexAtlasIndex", albedoAtlasPosition);
_propertyBlock.SetInteger("_MainTexAtlasSize", albedoAtlasSize);
}
else
//bool textureSuccess = GetImageAtlasTexture(albedoImageId, _uploaderId, out Texture2D texture, out int atlasPosition, out int atlasSize, out string type1);
MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
for (int i = 0; i < imageIds.Length; i++)
{
return;
if (i >= texturePropertyMappings.Length) continue;
string propertyName = texturePropertyMappings[i];
Texture2D texture = manager.GetTexture2D(_uploaderId, imageIds[i]);
if (texture == null) continue;
if (string.IsNullOrEmpty(propertyName)) continue;
propertyBlock.SetTexture(propertyName, texture);
}
if (emissionSuccess && emissionTexture) {
_propertyBlock.SetTexture("_EmissionMapAtlas", emissionTexture);
_propertyBlock.SetInteger("_EmissionMapAtlasIndex", emissionAtlasPosition);
_propertyBlock.SetInteger("_EmissionMapAtlasSize", emissionAtlasSize);
}
if (normalSuccess && normalTexture) {
_propertyBlock.SetTexture("_NormalMapAtlas", normalTexture);
_propertyBlock.SetInteger("_NormalMapAtlasIndex", normalAtlasPosition);
_propertyBlock.SetInteger("_NormalMapAtlasSize", normalAtlasSize);
}
_propertyBlock.SetColor("_EmissionColor", emissionColor);
_propertyBlock.SetFloat("_NormalMapScale", normalIntensity);
_propertyBlock.SetColor("_MainTexColor", albedoColor);
GetComponent<Renderer>().SetPropertyBlock(_propertyBlock);
GetComponent<Renderer>().SetPropertyBlock(propertyBlock);
}
}
#if UNITY_EDITOR && !COMPILER_UDONSHARP
[CustomEditor(typeof(VRCBoardImage), true)]
public class VRCBoardImageComponentEditor : Editor
{
public override void OnInspectorGUI()
{
VRCBoardImage script = (VRCBoardImage)target;
Renderer renderer = script.GetComponent<Renderer>();
Material material = renderer.sharedMaterial;
string[] materialPropertyNames = material.GetPropertyNames(MaterialPropertyType.Texture);
base.OnInspectorGUI();
bool equals =
Equals(script.imageIds.Length, script.defaultTextures.Length) &&
Equals(script.imageIds.Length, script.defaultTextures.Length);
EditorGUILayout.Separator();
GUILayout.BeginVertical("Images", "window");
int newSize = script.imageIds.Length;
GUILayout.BeginHorizontal();
if (GUILayout.Button("+"))
{
newSize++;
}
if (GUILayout.Button("-"))
{
newSize--;
}
GUILayout.EndHorizontal();
if (newSize != script.imageIds.Length || !equals)
{
Debug.Log("Resized arrays to"+newSize);
Array.Resize(ref script.imageIds, newSize);
Array.Resize(ref script.texturePropertyMappings, newSize);
Array.Resize(ref script.defaultTextures, newSize);
}
MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
for (int i = 0; i < script.imageIds.Length; i++)
{
EditorGUILayout.Space();
GUILayout.BeginVertical("Image "+i, "window");
string imageId = script.imageIds[i];
string propertyName = script.texturePropertyMappings[i];
Texture2D defaultImage = script.defaultTextures[i];
imageId = EditorGUILayout.TextField("Image Id:", imageId);
int selectedIndex = Array.IndexOf(materialPropertyNames, propertyName);
selectedIndex = EditorGUILayout.Popup("Material Property:", selectedIndex, materialPropertyNames);
bool propertyExists = Array.Exists(materialPropertyNames, materialProperty => materialProperty == propertyName);
if (propertyExists)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Default Texture:");
defaultImage = EditorGUILayout.ObjectField(defaultImage, typeof(Texture2D), false) as Texture2D;
EditorGUILayout.EndHorizontal();
if (defaultImage != null) propertyBlock.SetTexture(propertyName, defaultImage);
script.defaultTextures[i] = defaultImage;
}
script.imageIds[i] = imageId;
if (selectedIndex >= 0 && selectedIndex < materialPropertyNames.Length) script.texturePropertyMappings[i] = materialPropertyNames[selectedIndex];
GUILayout.EndVertical();
}
renderer.SetPropertyBlock(propertyBlock);
EditorGUILayout.EndHorizontal();
}
}
#endif
}