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

@@ -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
}