using UdonSharp; using UnityEngine; using UnityEngine.Serialization; using VRC.SDK3.Data; namespace VRCBoard.Components { [UdonBehaviourSyncMode(BehaviourSyncMode.None)] public class VRCBoardImageBasic : 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; 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().SetPropertyBlock(_propertyBlock); } private string GetUploader() { string uploaderId = ""; if (string.IsNullOrEmpty(uploaderIdOverride)) { GetImageAtlasTexture(albedoImageId, 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"); return ""; } _mainType = albedoType; if (_mainType == "shared") { uploaderId = GetRandomUploader(albedoImageId); } else { DataDictionary ownerInfo = manager.GetSupporterDataFromPlayer(manager.InstanceOwner); if (ownerInfo == null) { Debug.LogWarning("Failed to get owner info, user has likely not linked their account."); return ""; } bool ownerSuccess = ownerInfo.TryGetValue("id", out DataToken ownerToken); if (!ownerSuccess || ownerToken.TokenType != TokenType.String) { Debug.LogWarning("Failed to get owner ID from owner info."); return ""; } uploaderId = ownerToken.String; } Debug.Log("image uploader: " + uploaderId); } else { uploaderId = uploaderIdOverride; } return uploaderId; } private void TryGetUploader() { if (!string.IsNullOrEmpty(_uploaderId)) return; _uploaderId = GetUploader(); if (string.IsNullOrEmpty(_uploaderId)) { Debug.LogError("Failed to get uploader ID"); return; } RequestImageLoad(albedoImageId, _uploaderId); RequestImageLoad(emissionImageId, _uploaderId); RequestImageLoad(normalImageId, _uploaderId); } protected override void OnInitialize() { TryGetUploader(); } protected override void OnImageDataUpdate() { TryGetUploader(); } protected override void OnSupporterDataUpdate() { TryGetUploader(); } 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 { return; } 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().SetPropertyBlock(_propertyBlock); } } }