Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ME] Simple refactoring #254

Merged
merged 5 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 93 additions & 51 deletions src/MaterialEditor.Core.Studio/Core.MaterialEditor.SceneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ protected void PurgeUnusedTextures()
}
}

/// <summary>
/// Return GameObject from ObjectCtrlInfo ID
/// </summary>
/// <param name="items"></param>
/// <param name="id"></param>
/// <returns>GameObject with OCI</returns>
protected static GameObject ExtractGameObject(ReadOnlyDictionary<int, ObjectCtrlInfo> 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;
}

/// <summary>
/// Loads saved data
/// </summary>
Expand Down Expand Up @@ -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));
}
}
}
Expand All @@ -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));
}
}
}
Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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)
Expand All @@ -269,18 +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)
{
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(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);
Expand Down Expand Up @@ -378,10 +399,8 @@ protected override void OnObjectsCopied(ReadOnlyDictionary<int, ObjectCtrlInfo>
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);
Expand Down Expand Up @@ -1249,10 +1268,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)
{
Expand All @@ -1261,9 +1276,30 @@ public void SetMaterialTextureFromFile(int id, Material material, string propert
}
else
textureProperty.TexID = texID;

SetTextureWithProperty(go, textureProperty);
}
}
/// <summary>
/// Sets the texture indicated by TexID to texture of Material indicated by TextureProperty
/// </summary>
/// <param name="go">GameObject to search for the renderer</param>
/// <param name="textureProperty">TextureProperty with TexID to set for Material</param>
/// <returns>True if the value was set, false if it could not be set</returns>
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);
}
/// <summary>
/// Add a texture property to be saved and loaded with the card.
/// </summary>
/// <param name="id">Item ID as found in studio's dicObjectCtrl</param>
Expand All @@ -1277,10 +1313,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)
{
Expand All @@ -1289,6 +1321,8 @@ public void SetMaterialTexture(int id, Material material, string propertyName, b
}
else
textureProperty.TexID = texID;

SetTextureWithProperty(go, textureProperty);
}
/// <summary>
/// Get the saved material property value or null if none is saved
Expand Down Expand Up @@ -1329,11 +1363,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);
}
}

/// <summary>
/// If TextureProperty is null, delete it.
/// </summary>
/// <param name="textureProperty"></param>
void RemoveTexturePropertyIfNull(MaterialTextureProperty textureProperty)
{
if (!textureProperty.NullCheck())
return;
MaterialTexturePropertyList.Remove(textureProperty);
}

/// <summary>
/// Add a texture offset property to be saved and loaded with the scene and optionally also update the materials.
/// </summary>
Expand Down Expand Up @@ -1406,8 +1450,7 @@ public void RemoveMaterialTextureOffset(int id, Material material, string proper
{
textureProperty.Offset = null;
textureProperty.OffsetOriginal = null;
if (textureProperty.NullCheck())
MaterialTexturePropertyList.Remove(textureProperty);
RemoveTexturePropertyIfNull(textureProperty);
}
}

Expand Down Expand Up @@ -1484,8 +1527,7 @@ public void RemoveMaterialTextureScale(int id, Material material, string propert
{
textureProperty.Scale = null;
textureProperty.ScaleOriginal = null;
if (textureProperty.NullCheck())
MaterialTexturePropertyList.Remove(textureProperty);
RemoveTexturePropertyIfNull(textureProperty);
}
}

Expand Down
Loading