quantum-space-buddies/QSB/PlayerBodySetup/Remote/ShaderReplacer.cs

48 lines
1.3 KiB
C#
Raw Permalink Normal View History

2022-03-26 17:06:54 +00:00
using UnityEngine;
2022-02-18 01:31:38 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.PlayerBodySetup.Remote;
public static class ShaderReplacer
2022-02-18 01:31:38 +00:00
{
2022-03-03 03:46:33 +00:00
/// <summary>
/// the materials on the prefabs have the exact same name as the ones in game.
/// if we just use Shader.Find, we can get the in-game ones instead of the prefab ones,
/// and replace the prefab ones with the in-game ones.
/// i am amazed that this works, and i hope it isn't super brittle.
/// </summary>
public static void ReplaceShaders(GameObject prefab)
2022-02-18 01:31:38 +00:00
{
2022-03-03 03:46:33 +00:00
foreach (var renderer in prefab.GetComponentsInChildren<Renderer>(true))
2022-02-18 01:31:38 +00:00
{
2022-03-03 03:46:33 +00:00
foreach (var material in renderer.sharedMaterials)
{
2022-03-03 03:46:33 +00:00
if (material == null)
{
2022-03-03 03:46:33 +00:00
continue;
}
2022-03-03 03:46:33 +00:00
2022-03-26 14:17:40 +00:00
var replacementShader = Shader.Find(material.shader.name);
if (replacementShader == null)
{
continue;
}
2022-11-11 02:56:58 +00:00
// preserve override tag and render queue (for Standard shader)
// keywords and properties are already preserved
if (material.renderQueue != material.shader.renderQueue)
{
var renderType = material.GetTag("RenderType", false);
var renderQueue = material.renderQueue;
material.shader = replacementShader;
material.SetOverrideTag("RenderType", renderType);
material.renderQueue = renderQueue;
}
2022-12-02 22:04:09 +00:00
else
{
material.shader = replacementShader;
}
}
2022-02-18 01:31:38 +00:00
}
}
2022-03-26 17:06:54 +00:00
}