Skip to content

Instantly share code, notes, and snippets.

@float3
Last active August 2, 2022 15:54
Show Gist options
  • Select an option

  • Save float3/023237cda48995b7eeb8df758df93b1a to your computer and use it in GitHub Desktop.

Select an option

Save float3/023237cda48995b7eeb8df758df93b1a to your computer and use it in GitHub Desktop.
Set bakery settings on all materials in scene
#if UNITY_EDITOR
#region
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
#endregion
namespace ThreeBakeryMaterialUtility
{
public class BakeryMaterialUtility : EditorWindow
{
public enum BakeryMode
{
// ReSharper disable once UnusedMember.Global
None,
SH,
RNM,
MonoSH,
}
private const string LMSpecProp = "_BAKERY_LMSPEC";
private const string NonLinearSHProp = "_BAKERY_SHNONLINEAR";
private const string ModeProp = "_BakeryMode";
private const string LMSpecKeyword = "BAKERY_LMSPEC";
private const string NonLinearSHKeyword = "BAKERY_SHNONLINEAR";
private const string RNMKeyword = "BAKERY_RNM";
private const string SHKeyword = "BAKERY_SH";
private const string MonoSHKeyword = "BAKERY_MONOSH";
private static readonly int LMSpecPropID = Shader.PropertyToID(LMSpecProp);
private static readonly int NonLinearSHPropID = Shader.PropertyToID(NonLinearSHProp);
private static readonly int ModePropID = Shader.PropertyToID(ModeProp);
public BakeryMode mode;
public bool nonLinearSH;
public bool lightmapSpecular;
public bool bicubicLightmapping;
public bool inactive = true;
private void OnGUI()
{
EditorGUILayout.Space(10);
mode = (BakeryMode) EditorGUILayout.EnumPopup("Non-Linear SH", mode);
nonLinearSH = EditorGUILayout.Toggle("Non-Linear SH", nonLinearSH);
lightmapSpecular = EditorGUILayout.Toggle("Lightmap Specular", lightmapSpecular);
bicubicLightmapping = EditorGUILayout.Toggle("Bicubic Lightmapping", bicubicLightmapping);
inactive = EditorGUILayout.Toggle("Inactive GameObjects", inactive);
EditorGUILayout.Space(10);
if (GUILayout.Button("Apply Settings", GUILayout.Width(200), GUILayout.Height(30)))
{
Material[] mats = SceneManager.GetActiveScene().GetRootGameObjects()
.SelectMany(g => g.GetComponentsInChildren<Renderer>(inactive)).ToArray()
.SelectMany(r => r.sharedMaterials).ToArray();
foreach (Material mat in mats)
{
if (mat.HasProperty(LMSpecProp))
{
mat.SetFloat(LMSpecPropID, lightmapSpecular ? 1 : 0);
mat.SetKeyword(LMSpecKeyword, lightmapSpecular);
}
if (mat.HasProperty(NonLinearSHProp))
{
mat.SetFloat(NonLinearSHPropID, nonLinearSH ? 1 : 0);
mat.SetKeyword(NonLinearSHKeyword, nonLinearSH);
}
if (mat.HasProperty(ModeProp))
{
mat.SetInt(ModePropID, (int) mode);
mat.SetKeyword(SHKeyword, mode == BakeryMode.SH);
mat.SetKeyword(RNMKeyword, mode == BakeryMode.RNM);
mat.SetKeyword(MonoSHKeyword, mode == BakeryMode.MonoSH);
}
}
}
}
[MenuItem("Tools/3/Bakery Material Utility")]
private static void ShowWindow()
{
BakeryMaterialUtility window = GetWindow<BakeryMaterialUtility>("Bakery Material Utility");
window.Show();
}
}
public static class Helpers
{
public static void SetKeyword(this Material mat, string keyword, bool enable)
{
if (enable)
mat.EnableKeyword(keyword);
else
mat.DisableKeyword(keyword);
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment