From 5f840f973352c793468ef1f2c54948b5e544a5ce Mon Sep 17 00:00:00 2001 From: takahiro kawanaka Date: Sat, 25 May 2024 22:27:45 +0900 Subject: [PATCH 1/5] add RemoveTexturePropertyIfNull() --- .../Core.MaterialEditor.SceneController.cs | 20 +++++++++++++------ .../Core.MaterialEditor.CharaController.cs | 19 ++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs b/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs index 7bfdb35b..87fba2d8 100644 --- a/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs +++ b/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs @@ -1329,11 +1329,21 @@ public void RemoveMaterialTexture(int id, Material material, string propertyName if (displayMessage) MaterialEditorPlugin.Logger.LogMessage("Save and reload scene to refresh textures."); textureProperty.TexID = null; - if (textureProperty.NullCheck()) - MaterialTexturePropertyList.Remove(textureProperty); + RemoveTexturePropertyIfNull(textureProperty); } } + /// + /// If TextureProperty is null, delete it. + /// + /// + void RemoveTexturePropertyIfNull(MaterialTextureProperty textureProperty) + { + if (!textureProperty.NullCheck()) + return; + MaterialTexturePropertyList.Remove(textureProperty); + } + /// /// Add a texture offset property to be saved and loaded with the scene and optionally also update the materials. /// @@ -1406,8 +1416,7 @@ public void RemoveMaterialTextureOffset(int id, Material material, string proper { textureProperty.Offset = null; textureProperty.OffsetOriginal = null; - if (textureProperty.NullCheck()) - MaterialTexturePropertyList.Remove(textureProperty); + RemoveTexturePropertyIfNull(textureProperty); } } @@ -1484,8 +1493,7 @@ public void RemoveMaterialTextureScale(int id, Material material, string propert { textureProperty.Scale = null; textureProperty.ScaleOriginal = null; - if (textureProperty.NullCheck()) - MaterialTexturePropertyList.Remove(textureProperty); + RemoveTexturePropertyIfNull(textureProperty); } } diff --git a/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs b/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs index 9ae01e60..507a0418 100644 --- a/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs +++ b/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs @@ -1850,10 +1850,19 @@ public void RemoveMaterialTexture(int slot, ObjectType objectType, Material mate if (displayMessage) MaterialEditorPlugin.Logger.LogMessage("Save and reload character or change outfits to refresh textures."); textureProperty.TexID = null; - if (textureProperty.NullCheck()) - MaterialTexturePropertyList.Remove(textureProperty); + RemoveTexturePropertyIfNull(textureProperty); } } + /// + /// If TextureProperty is null, delete it. + /// + /// + void RemoveTexturePropertyIfNull(MaterialTextureProperty textureProperty) + { + if (!textureProperty.NullCheck()) + return; + MaterialTexturePropertyList.Remove(textureProperty); + } /// /// Add a texture offset property to be saved and loaded with the card and optionally also update the materials. @@ -1932,8 +1941,7 @@ public void RemoveMaterialTextureOffset(int slot, ObjectType objectType, Materia { textureProperty.Offset = null; textureProperty.OffsetOriginal = null; - if (textureProperty.NullCheck()) - MaterialTexturePropertyList.Remove(textureProperty); + RemoveTexturePropertyIfNull(textureProperty); } } @@ -2015,8 +2023,7 @@ public void RemoveMaterialTextureScale(int slot, ObjectType objectType, Material { textureProperty.Scale = null; textureProperty.ScaleOriginal = null; - if (textureProperty.NullCheck()) - MaterialTexturePropertyList.Remove(textureProperty); + RemoveTexturePropertyIfNull(textureProperty); } } From 66d90dea79fb8a5913ab8f1147730c94ce4d3c55 Mon Sep 17 00:00:00 2001 From: takahiro kawanaka Date: Sun, 26 May 2024 15:46:55 +0900 Subject: [PATCH 2/5] add SetTextureWithProperty --- .../Core.MaterialEditor.SceneController.cs | 43 ++++++++++------- .../Core.MaterialEditor.CharaController.cs | 46 +++++++++++-------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs b/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs index 87fba2d8..afe2a4eb 100644 --- a/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs +++ b/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs @@ -273,11 +273,7 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio bool setTex = false; if (newTextureProperty.TexID != null) - { - var tex = TextureDictionary[(int)newTextureProperty.TexID].Texture; - MaterialEditorPlugin.Instance.ConvertNormalMap(ref tex, newTextureProperty.Property); - setTex = SetTexture(ociItem.objectItem, newTextureProperty.MaterialName, newTextureProperty.Property, tex); - } + setTex = SetTextureWithProperty(ociItem.objectItem, newTextureProperty); bool setOffset = SetTextureOffset(ociItem.objectItem, newTextureProperty.MaterialName, newTextureProperty.Property, newTextureProperty.Offset); bool setScale = SetTextureScale(ociItem.objectItem, newTextureProperty.MaterialName, newTextureProperty.Property, newTextureProperty.Scale); @@ -378,10 +374,8 @@ protected override void OnObjectsCopied(ReadOnlyDictionary MaterialTextureProperty newTextureProperty = new MaterialTextureProperty(copiedItem.Value.GetSceneId(), loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.TexID, loadedProperty.Offset, loadedProperty.OffsetOriginal, loadedProperty.Scale, loadedProperty.ScaleOriginal); bool setTex = false; - - var tex = TextureDictionary[(int)newTextureProperty.TexID].Texture; - Instance.ConvertNormalMap(ref tex, newTextureProperty.Property); - if (loadedProperty.TexID != null) setTex = SetTexture(ociItem.objectItem, newTextureProperty.MaterialName, newTextureProperty.Property, tex); + if (loadedProperty.TexID != null) + setTex = SetTextureWithProperty(ociItem.objectItem, newTextureProperty); bool setOffset = SetTextureOffset(ociItem.objectItem, newTextureProperty.MaterialName, newTextureProperty.Property, newTextureProperty.Offset); bool setScale = SetTextureScale(ociItem.objectItem, newTextureProperty.MaterialName, newTextureProperty.Property, newTextureProperty.Scale); @@ -1249,10 +1243,6 @@ public void SetMaterialTextureFromFile(int id, Material material, string propert var texBytes = File.ReadAllBytes(filePath); var texID = SetAndGetTextureID(texBytes); - var tex = TextureDictionary[texID].Texture; - Instance.ConvertNormalMap(ref tex, propertyName); - SetTexture(go, material.NameFormatted(), propertyName, tex); - var textureProperty = MaterialTexturePropertyList.FirstOrDefault(x => x.ID == id && x.Property == propertyName && x.MaterialName == material.NameFormatted()); if (textureProperty == null) { @@ -1261,9 +1251,30 @@ public void SetMaterialTextureFromFile(int id, Material material, string propert } else textureProperty.TexID = texID; + + SetTextureWithProperty(go, textureProperty); } } /// + /// Sets the texture indicated by TexID to texture of Material indicated by TextureProperty + /// + /// GameObject to search for the renderer + /// TextureProperty with TexID to set for Material + /// True if the value was set, false if it could not be set + private bool SetTextureWithProperty(GameObject go, MaterialTextureProperty textureProperty) + { + if (!textureProperty.TexID.HasValue) + return false; + + int texID = textureProperty.TexID.Value; + if (!TextureDictionary.TryGetValue(texID, out var container)) + return false; + + var tex = container.Texture; + MaterialEditorPlugin.Instance.ConvertNormalMap(ref tex, textureProperty.Property); + return SetTexture(go, textureProperty.MaterialName, textureProperty.Property, tex); + } + /// /// Add a texture property to be saved and loaded with the card. /// /// Item ID as found in studio's dicObjectCtrl @@ -1277,10 +1288,6 @@ public void SetMaterialTexture(int id, Material material, string propertyName, b var texID = SetAndGetTextureID(data); - var tex = TextureDictionary[texID].Texture; - Instance.ConvertNormalMap(ref tex, propertyName); - SetTexture(go, material.NameFormatted(), propertyName, tex); - var textureProperty = MaterialTexturePropertyList.FirstOrDefault(x => x.ID == id && x.Property == propertyName && x.MaterialName == material.NameFormatted()); if (textureProperty == null) { @@ -1289,6 +1296,8 @@ public void SetMaterialTexture(int id, Material material, string propertyName, b } else textureProperty.TexID = texID; + + SetTextureWithProperty(go, textureProperty); } /// /// Get the saved material property value or null if none is saved diff --git a/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs b/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs index 507a0418..35630747 100644 --- a/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs +++ b/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs @@ -718,12 +718,7 @@ public IEnumerator LoadData(bool clothes, bool accessories, bool hair) var go = FindGameObject(property.ObjectType, property.Slot); if (Instance.CheckBlacklist(property.MaterialName, property.Property)) continue; - if (property.TexID != null && TextureDictionary.TryGetValue((int)property.TexID, out var textureContainer)) - { - var tex = textureContainer.Texture; - MaterialEditorPlugin.Instance.ConvertNormalMap(ref tex, property.Property); - SetTexture(go, property.MaterialName, property.Property, tex); - } + SetTextureWithProperty(go, property); SetTextureOffset(go, property.MaterialName, property.Property, property.Offset); SetTextureScale(go, property.MaterialName, property.Property, property.Scale); } @@ -1182,6 +1177,25 @@ private IEnumerator RefreshClothesMainTexCoroutine() } } /// + /// Sets the texture indicated by TexID to texture of Material indicated by TextureProperty + /// + /// GameObject to search for the renderer + /// TextureProperty with TexID to set for Material + /// True if the value was set, false if it could not be set + private bool SetTextureWithProperty(GameObject go, MaterialTextureProperty textureProperty) + { + if (!textureProperty.TexID.HasValue) + return false; + + int texID = textureProperty.TexID.Value; + if (!TextureDictionary.TryGetValue(texID, out var container)) + return false; + + var tex = container.Texture; + MaterialEditorPlugin.Instance.ConvertNormalMap(ref tex, textureProperty.Property); + return SetTexture(go, textureProperty.MaterialName, textureProperty.Property, tex); + } + /// /// Refresh the body MainTex, typically called after editing colors in the character maker /// public void RefreshBodyMainTex() => StartCoroutine(RefreshBodyMainTexCoroutine()); @@ -1196,12 +1210,7 @@ private IEnumerator RefreshBodyMainTexCoroutine() continue; if (property.ObjectType == ObjectType.Character && property.Property == "MainTex") - if (property.TexID != null) - { - var tex = TextureDictionary[(int)property.TexID].Texture; - Instance.ConvertNormalMap(ref tex, property.Property); - SetTexture(ChaControl.gameObject, property.MaterialName, property.Property, tex); - } + SetTextureWithProperty(ChaControl.gameObject, property); } } /// @@ -1772,18 +1781,17 @@ public void SetMaterialTextureFromFile(int slot, ObjectType objectType, Material var texBytes = File.ReadAllBytes(filePath); var texID = SetAndGetTextureID(texBytes); - var tex = TextureDictionary[texID].Texture; - Instance.ConvertNormalMap(ref tex, propertyName); - SetTexture(go, material.NameFormatted(), propertyName, tex); - var textureProperty = MaterialTexturePropertyList.FirstOrDefault(x => x.ObjectType == objectType && x.CoordinateIndex == GetCoordinateIndex(objectType) && x.Slot == slot && x.Property == propertyName && x.MaterialName == material.NameFormatted()); if (textureProperty == null) MaterialTexturePropertyList.Add(new MaterialTextureProperty(objectType, GetCoordinateIndex(objectType), slot, material.NameFormatted(), propertyName, texID)); else textureProperty.TexID = texID; + + SetTextureWithProperty(go, textureProperty); } } + /// /// Add a texture property to be saved and loaded with the card. /// @@ -1797,15 +1805,13 @@ public void SetMaterialTexture(int slot, ObjectType objectType, Material materia if (data == null) return; var texID = SetAndGetTextureID(data); - var tex = TextureDictionary[texID].Texture; - MaterialEditorPlugin.Instance.ConvertNormalMap(ref tex, propertyName); - SetTexture(go, material.NameFormatted(), propertyName, tex); - var textureProperty = MaterialTexturePropertyList.FirstOrDefault(x => x.ObjectType == objectType && x.CoordinateIndex == GetCoordinateIndex(objectType) && x.Slot == slot && x.Property == propertyName && x.MaterialName == material.NameFormatted()); if (textureProperty == null) MaterialTexturePropertyList.Add(new MaterialTextureProperty(objectType, GetCoordinateIndex(objectType), slot, material.NameFormatted(), propertyName, texID)); else textureProperty.TexID = texID; + + SetTextureWithProperty(go, textureProperty); } /// /// Get the saved material property value or null if none is saved From 923dc58b306b5f33caf895b86126563cff297c45 Mon Sep 17 00:00:00 2001 From: takahiro kawanaka Date: Sun, 26 May 2024 16:09:07 +0900 Subject: [PATCH 3/5] Functionalize some processes --- .../Core.MaterialEditor.CharaController.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs b/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs index 35630747..943c5cf6 100644 --- a/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs +++ b/src/MaterialEditor.Core/Core.MaterialEditor.CharaController.cs @@ -146,6 +146,17 @@ protected override void OnReload(GameMode currentGameMode, bool maintainState) /// Used by SetMaterialTextureFromFile if setTexInUpdate is true, needed for loading files via file dialogue /// internal new void Update() + { + SetMaterialTextureFromFileByUpdate(); + base.Update(); + if (MaterialEditorPlugin.PurgeOrphanedPropertiesHotkey.Value.IsDown()) + PurgeOrphanedProperties(); + } + + /// + /// Used by SetMaterialTextureFromFile if setTexInUpdate is true, needed for loading files via file dialogue + /// + void SetMaterialTextureFromFileByUpdate() { try { @@ -163,9 +174,6 @@ protected override void OnReload(GameMode currentGameMode, bool maintainState) MatToSet = null; GameObjectToSet = null; } - base.Update(); - if (MaterialEditorPlugin.PurgeOrphanedPropertiesHotkey.Value.IsDown()) - PurgeOrphanedProperties(); } /// From 64b3365a2bda45422477e26550498b0831b946d9 Mon Sep 17 00:00:00 2001 From: takahiro kawanaka Date: Sun, 26 May 2024 23:05:20 +0900 Subject: [PATCH 4/5] Functionalized duplicate code --- .../Core.MaterialEditor.SceneController.cs | 83 ++++++++++++------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs b/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs index afe2a4eb..0996c6ab 100644 --- a/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs +++ b/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs @@ -124,6 +124,24 @@ protected void PurgeUnusedTextures() } } + /// + /// Return GameObject from ObjectCtrlInfo ID + /// + /// + /// + /// GameObject with OCI + protected static GameObject ExtractGameObject(ReadOnlyDictionary items, int id, out int objectId ) + { + if (!items.TryGetValue(id, out ObjectCtrlInfo objectCtrlInfo) || objectCtrlInfo == null || !(objectCtrlInfo is OCIItem ociItem)) + { + objectId = -1; + return null; + } + + objectId = MEStudio.GetObjectID(ociItem); + return ociItem.objectItem; + } + /// /// Loads saved data /// @@ -165,13 +183,13 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio for (var i = 0; i < properties.Count; i++) { var loadedProperty = properties[i]; - if (loadedItems.TryGetValue(loadedProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem) + GameObject go = ExtractGameObject(loadedItems, loadedProperty.ID, out var objID); + if (go != null) { - CopyMaterial(ociItem.objectItem, loadedProperty.MaterialName, loadedProperty.MaterialCopyName); - var id = MEStudio.GetObjectID(objectCtrlInfo); - if (MaterialCopyList.Any(x => x.ID == id && x.MaterialName == loadedProperty.MaterialName && x.MaterialCopyName == loadedProperty.MaterialCopyName)) + CopyMaterial(go, loadedProperty.MaterialName, loadedProperty.MaterialCopyName); + if (MaterialCopyList.Any(x => x.ID == objID && x.MaterialName == loadedProperty.MaterialName && x.MaterialCopyName == loadedProperty.MaterialCopyName)) continue; - MaterialCopyList.Add(new MaterialCopy(id, loadedProperty.MaterialName, loadedProperty.MaterialCopyName)); + MaterialCopyList.Add(new MaterialCopy(objID, loadedProperty.MaterialName, loadedProperty.MaterialCopyName)); } } } @@ -182,12 +200,13 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio for (var i = 0; i < properties.Count; i++) { var loadedProperty = properties[i]; - if (loadedItems.TryGetValue(loadedProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem) + GameObject go = ExtractGameObject(loadedItems, loadedProperty.ID, out var objID); + if (go != null) { - bool setShader = SetShader(ociItem.objectItem, loadedProperty.MaterialName, loadedProperty.ShaderName); - bool setRenderQueue = SetRenderQueue(ociItem.objectItem, loadedProperty.MaterialName, loadedProperty.RenderQueue); + bool setShader = SetShader(go, loadedProperty.MaterialName, loadedProperty.ShaderName); + bool setRenderQueue = SetRenderQueue(go, loadedProperty.MaterialName, loadedProperty.RenderQueue); if (setShader || setRenderQueue) - MaterialShaderList.Add(new MaterialShader(MEStudio.GetObjectID(objectCtrlInfo), loadedProperty.MaterialName, loadedProperty.ShaderName, loadedProperty.ShaderNameOriginal, loadedProperty.RenderQueue, loadedProperty.RenderQueueOriginal)); + MaterialShaderList.Add(new MaterialShader(objID, loadedProperty.MaterialName, loadedProperty.ShaderName, loadedProperty.ShaderNameOriginal, loadedProperty.RenderQueue, loadedProperty.RenderQueueOriginal)); } } } @@ -198,9 +217,10 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio for (var i = 0; i < properties.Count; i++) { var loadedProperty = properties[i]; - if (loadedItems.TryGetValue(loadedProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem) - if (MaterialAPI.SetRendererProperty(ociItem.objectItem, loadedProperty.RendererName, loadedProperty.Property, int.Parse(loadedProperty.Value))) - RendererPropertyList.Add(new RendererProperty(MEStudio.GetObjectID(objectCtrlInfo), loadedProperty.RendererName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); + GameObject go = ExtractGameObject(loadedItems, loadedProperty.ID, out var objID); + if (go != null) + if (MaterialAPI.SetRendererProperty(go, loadedProperty.RendererName, loadedProperty.Property, int.Parse(loadedProperty.Value))) + RendererPropertyList.Add(new RendererProperty(objID, loadedProperty.RendererName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); } } @@ -210,9 +230,10 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio for (var i = 0; i < properties.Count; i++) { var loadedProperty = properties[i]; - if (loadedItems.TryGetValue(loadedProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem) - if (MaterialAPI.SetProjectorProperty(ociItem.objectItem, loadedProperty.ProjectorName, loadedProperty.Property, float.Parse(loadedProperty.Value))) - ProjectorPropertyList.Add(new ProjectorProperty(MEStudio.GetObjectID(objectCtrlInfo), loadedProperty.ProjectorName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); + GameObject go = ExtractGameObject(loadedItems, loadedProperty.ID, out var objID); + if (go != null) + if (MaterialAPI.SetProjectorProperty(go, loadedProperty.ProjectorName, loadedProperty.Property, float.Parse(loadedProperty.Value))) + ProjectorPropertyList.Add(new ProjectorProperty(objID, loadedProperty.ProjectorName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); } } @@ -222,9 +243,10 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio for (var i = 0; i < properties.Count; i++) { var loadedProperty = properties[i]; - if (loadedItems.TryGetValue(loadedProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem) - if (SetFloat(ociItem.objectItem, loadedProperty.MaterialName, loadedProperty.Property, float.Parse(loadedProperty.Value))) - MaterialFloatPropertyList.Add(new MaterialFloatProperty(MEStudio.GetObjectID(objectCtrlInfo), loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); + GameObject go = ExtractGameObject(loadedItems, loadedProperty.ID, out var objID); + if (go != null) + if (SetFloat(go, loadedProperty.MaterialName, loadedProperty.Property, float.Parse(loadedProperty.Value))) + MaterialFloatPropertyList.Add(new MaterialFloatProperty(objID, loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); } } @@ -234,9 +256,10 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio for (var i = 0; i < properties.Count; i++) { var loadedProperty = properties[i]; - if (loadedItems.TryGetValue(loadedProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem) - if (SetKeyword(ociItem.objectItem, loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value)) - MaterialKeywordPropertyList.Add(new MaterialKeywordProperty(MEStudio.GetObjectID(objectCtrlInfo), loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); + GameObject go = ExtractGameObject(loadedItems, loadedProperty.ID, out var objID); + if (go != null) + if (SetKeyword(go, loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value)) + MaterialKeywordPropertyList.Add(new MaterialKeywordProperty(objID, loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); } } @@ -246,9 +269,10 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio for (var i = 0; i < properties.Count; i++) { var loadedProperty = properties[i]; - if (loadedItems.TryGetValue(loadedProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem) - if (SetColor(ociItem.objectItem, loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value)) - MaterialColorPropertyList.Add(new MaterialColorProperty(MEStudio.GetObjectID(objectCtrlInfo), loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); + GameObject go = ExtractGameObject(loadedItems, loadedProperty.ID, out var objID); + if (go != null) + if (SetColor(go, loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value)) + MaterialColorPropertyList.Add(new MaterialColorProperty(objID, loadedProperty.MaterialName, loadedProperty.Property, loadedProperty.Value, loadedProperty.ValueOriginal)); } } @@ -258,7 +282,8 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio for (var i = 0; i < properties.Count; i++) { var loadedProperty = properties[i]; - if (loadedItems.TryGetValue(loadedProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem) + GameObject go = ExtractGameObject(loadedItems, loadedProperty.ID, out var objID); + if (go != null) { int? texID = null; if (operation == SceneOperationKind.Import) @@ -269,14 +294,14 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio else texID = loadedProperty.TexID; - MaterialTextureProperty newTextureProperty = new MaterialTextureProperty(MEStudio.GetObjectID(objectCtrlInfo), loadedProperty.MaterialName, loadedProperty.Property, texID, loadedProperty.Offset, loadedProperty.OffsetOriginal, loadedProperty.Scale, loadedProperty.ScaleOriginal); + MaterialTextureProperty newTextureProperty = new MaterialTextureProperty(objID, loadedProperty.MaterialName, loadedProperty.Property, texID, loadedProperty.Offset, loadedProperty.OffsetOriginal, loadedProperty.Scale, loadedProperty.ScaleOriginal); bool setTex = false; if (newTextureProperty.TexID != null) - setTex = SetTextureWithProperty(ociItem.objectItem, newTextureProperty); + setTex = SetTextureWithProperty(go, newTextureProperty); - bool setOffset = SetTextureOffset(ociItem.objectItem, newTextureProperty.MaterialName, newTextureProperty.Property, newTextureProperty.Offset); - bool setScale = SetTextureScale(ociItem.objectItem, newTextureProperty.MaterialName, newTextureProperty.Property, newTextureProperty.Scale); + bool setOffset = SetTextureOffset(go, newTextureProperty.MaterialName, newTextureProperty.Property, newTextureProperty.Offset); + bool setScale = SetTextureScale(go, newTextureProperty.MaterialName, newTextureProperty.Property, newTextureProperty.Scale); if (setTex || setOffset || setScale) MaterialTexturePropertyList.Add(newTextureProperty); From bb6ceb4e7bcb74a912234bd091f1d6105491d0c3 Mon Sep 17 00:00:00 2001 From: takahiro kawanaka Date: Sun, 26 May 2024 23:05:53 +0900 Subject: [PATCH 5/5] CRLF->LF --- .../Core.MaterialEditor.SceneController.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs b/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs index 0996c6ab..2438e56c 100644 --- a/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs +++ b/src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs @@ -124,22 +124,22 @@ protected void PurgeUnusedTextures() } } - /// - /// Return GameObject from ObjectCtrlInfo ID - /// - /// - /// + /// + /// Return GameObject from ObjectCtrlInfo ID + /// + /// + /// /// GameObject with OCI - protected static GameObject ExtractGameObject(ReadOnlyDictionary items, int id, out int objectId ) - { - if (!items.TryGetValue(id, out ObjectCtrlInfo objectCtrlInfo) || objectCtrlInfo == null || !(objectCtrlInfo is OCIItem ociItem)) - { - objectId = -1; - return null; - } - - objectId = MEStudio.GetObjectID(ociItem); - return ociItem.objectItem; + protected static GameObject ExtractGameObject(ReadOnlyDictionary items, int id, out int objectId ) + { + if (!items.TryGetValue(id, out ObjectCtrlInfo objectCtrlInfo) || objectCtrlInfo == null || !(objectCtrlInfo is OCIItem ociItem)) + { + objectId = -1; + return null; + } + + objectId = MEStudio.GetObjectID(ociItem); + return ociItem.objectItem; } ///