2022-02-03 09:20:14 +00:00
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using QSB.WorldSync;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2022-02-02 20:33:53 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace QSB.PlayerBodySetup.Remote
|
|
|
|
|
{
|
|
|
|
|
public static class FixMaterialsInAllChildren
|
|
|
|
|
{
|
2022-02-03 03:21:31 +00:00
|
|
|
|
private static readonly List<(string MaterialName, Material ReplacementMaterial)> _materialDefinitions = new();
|
2022-02-02 20:33:53 +00:00
|
|
|
|
|
2022-02-03 03:21:31 +00:00
|
|
|
|
private static void ReplaceMaterial(Renderer renderer, int index, Material mat)
|
2022-02-02 20:33:53 +00:00
|
|
|
|
{
|
|
|
|
|
var mats = renderer.materials;
|
|
|
|
|
mats[index] = mat;
|
|
|
|
|
renderer.materials = mats;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 03:21:31 +00:00
|
|
|
|
private static void ReplaceMaterials(Renderer renderer, string materialName, Material replacementMaterial)
|
2022-02-02 20:33:53 +00:00
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < renderer.materials.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (renderer.materials[i].name.Trim() == $"{materialName} (Instance)")
|
|
|
|
|
{
|
|
|
|
|
ReplaceMaterial(renderer, i, replacementMaterial);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void GenerateMaterialDefinitions()
|
|
|
|
|
{
|
2022-02-03 09:20:14 +00:00
|
|
|
|
var matNameList = new List<string>()
|
|
|
|
|
{
|
|
|
|
|
"Traveller_HEA_Player_Skin_mat",
|
|
|
|
|
"Traveller_HEA_Player_Clothes_mat",
|
|
|
|
|
"Traveller_HEA_PlayerSuit_mat",
|
|
|
|
|
"Props_HEA_Jetpack_mat"
|
|
|
|
|
};
|
2022-02-02 20:33:53 +00:00
|
|
|
|
|
2022-02-03 09:20:14 +00:00
|
|
|
|
var allMaterials = (Material[])Resources.FindObjectsOfTypeAll(typeof(Material));
|
2022-02-02 20:33:53 +00:00
|
|
|
|
|
2022-02-03 09:20:14 +00:00
|
|
|
|
foreach (var name in matNameList)
|
|
|
|
|
{
|
2022-02-03 09:30:18 +00:00
|
|
|
|
DebugLog.DebugWrite(name);
|
2022-02-02 20:33:53 +00:00
|
|
|
|
|
2022-02-03 09:30:18 +00:00
|
|
|
|
var matchingMaterial = allMaterials.Where(x => x.name == name).ToArray();
|
2022-02-02 20:33:53 +00:00
|
|
|
|
|
2022-02-03 09:30:18 +00:00
|
|
|
|
foreach (var item in matchingMaterial)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"- {item.name}");
|
|
|
|
|
}
|
2022-02-02 20:33:53 +00:00
|
|
|
|
|
2022-02-03 09:20:14 +00:00
|
|
|
|
if (matchingMaterial == default)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - could not find material with the name {name}!", OWML.Common.MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-02 20:33:53 +00:00
|
|
|
|
|
2022-02-03 09:30:18 +00:00
|
|
|
|
_materialDefinitions.Add(new(name, matchingMaterial[0]));
|
|
|
|
|
}
|
2022-02-03 03:21:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ReplaceMaterials(Transform rootObject)
|
|
|
|
|
{
|
|
|
|
|
if (_materialDefinitions.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
GenerateMaterialDefinitions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var renderer in rootObject.GetComponentsInChildren<Renderer>(true))
|
|
|
|
|
{
|
|
|
|
|
foreach (var (materialName, replacementMaterial) in _materialDefinitions)
|
|
|
|
|
{
|
|
|
|
|
ReplaceMaterials(renderer, materialName, replacementMaterial);
|
|
|
|
|
}
|
2022-02-03 09:20:14 +00:00
|
|
|
|
}
|
2022-02-02 20:33:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|