Skip to content
Open
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
7 changes: 1 addition & 6 deletions Chroma/EnvironmentEnhancement/GeometryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ internal GameObject Create(CustomData customData)
bool collision = customData.Get<bool?>(_v2 ? V2_COLLISION : COLLISION) ?? false;

object materialData = customData.GetRequired<object>(_v2 ? V2_MATERIAL : MATERIAL);
MaterialsManager.MaterialInfo materialInfo = materialData switch
{
string name => _materialsManager.MaterialInfos[name],
CustomData data => _materialsManager.CreateMaterialInfo(data),
_ => throw new InvalidOperationException($"Could not read [{MATERIAL}].")
};
MaterialsManager.MaterialInfo materialInfo = _materialsManager.GetMaterial(materialData);
ShaderType shaderType = materialInfo.ShaderType;

PrimitiveType primitiveType = geometryType switch
Expand Down
27 changes: 27 additions & 0 deletions Chroma/EnvironmentEnhancement/MaterialsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Heck.Animation;
using IPA.Utilities;
using JetBrains.Annotations;
using Newtonsoft.Json;
using UnityEngine;
using Zenject;
using static Chroma.ChromaController;
Expand Down Expand Up @@ -54,6 +55,8 @@ private MaterialsManager(

internal Dictionary<string, MaterialInfo> MaterialInfos { get; } = new();

internal Dictionary<string, MaterialInfo> ReuseMaterials { get; } = new(); // grrr mappers

public void Dispose()
{
foreach (Material createdMaterial in _createdMaterials)
Expand Down Expand Up @@ -92,6 +95,30 @@ internal MaterialInfo CreateMaterialInfo(CustomData customData)
return materialInfo;
}


internal MaterialInfo GetMaterial(object materialData)
{
if (materialData is string name)
{
return MaterialInfos[name];
}

if (materialData is not CustomData data)
{
throw new InvalidOperationException($"Could not read [{MATERIAL}].");
}

string json = JsonConvert.SerializeObject(data);
if (ReuseMaterials.TryGetValue(json, out MaterialInfo m))
{
return m;
}

MaterialInfo materialInfo = CreateMaterialInfo(data);
ReuseMaterials[json] = materialInfo;
return materialInfo;
}

private static Material InstantiateSharedMaterial(ShaderType shaderType)
{
return new Material(Shader.Find(shaderType switch
Expand Down