Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Material color conversion inspector options for Unity - RealToon version
// Material color conversion inspector options for Unity - RealToon version
// by Andy Miira (Andrei Müller), January 2020
//
//
// [INSTRUCTIONS]
// 0) Add this script inside an "Editor" folder in your project.
// You must create this folder if it doesn't already exist.
//
// 1) Select ONE OR MORE Materials in your project,
// then click on the small gear icon at the top-right corner of the material(s)'s Inspector.
// Or simply right-click the top area of the material(s)'s Inspector
// (where is located the material's name, the shader selection dropdown, etc.).
//
// 2) In the dropdown menu, there should be two new options, "Convert Colors to Linear" and "Convert Colors to Gamma".
// Select one of the options to make the respective color conversion. That's it!
//
// Usage example:
// https://twitter.com/andymiira/status/1220811118236643328
//
//
// TIP: You can add more shader color properties to convert, other than those already defined in the code below.
// Example: To add a property called _MainColor, just add the following lines in their respective places in the code,
// after the existing properties.
//
// private static readonly int MainColor = Shader.PropertyToID("_MainColor");
// ConvertGammaToLinear(mat, MainColor);
// ConvertLinearToGamma(mat, MainColor);
//
//
// TIP: To remove a property that you don't need, just comment out or delete its respective lines in the code.
//
//
// Check out more Unity scripts and utilities at:
// https://gist.github.com/andreiagmu
using UnityEditor;
using UnityEngine;
namespace MirrorMirai
{
public class MaterialColorConverterInspector_RealToon : MonoBehaviour
{
//private static readonly int Color = Shader.PropertyToID("_Color");
private static readonly int MainColor = Shader.PropertyToID("_MainColor");
private static readonly int HighlightColor = Shader.PropertyToID("_HighlightColor");
private static readonly int OutlineColor = Shader.PropertyToID("_OutlineColor");
private static readonly int SelfLitColor = Shader.PropertyToID("_SelfLitColor");
private static readonly int GlossColor = Shader.PropertyToID("_GlossColor");
private static readonly int OverallShadowColor = Shader.PropertyToID("_OverallShadowColor");
private static readonly int SelfShadowRealTimeShadowColor =
Shader.PropertyToID("_SelfShadowRealTimeShadowColor");
private static readonly int ShadowTColor = Shader.PropertyToID("_ShadowTColor");
private static readonly int RimLightColor = Shader.PropertyToID("_RimLightColor");
[MenuItem("CONTEXT/Material/Convert Colors to Linear (RealToon)", false, 2050)]
private static void ConvertColorsToLinear(MenuCommand menuCommand)
{
var mat = menuCommand.context as Material;
if (mat == null)
return;
//ConvertGammaToLinear(mat, Color);
ConvertGammaToLinear(mat, MainColor);
ConvertGammaToLinear(mat, HighlightColor);
ConvertGammaToLinear(mat, OutlineColor);
ConvertGammaToLinear(mat, SelfLitColor);
ConvertGammaToLinear(mat, GlossColor);
ConvertGammaToLinear(mat, OverallShadowColor);
ConvertGammaToLinear(mat, SelfShadowRealTimeShadowColor);
ConvertGammaToLinear(mat, ShadowTColor);
ConvertGammaToLinear(mat, RimLightColor);
}
[MenuItem("CONTEXT/Material/Convert Colors to Gamma (RealToon)", false, 2050)]
private static void ConvertColorsToGamma(MenuCommand menuCommand)
{
var mat = menuCommand.context as Material;
if (mat == null)
return;
//ConvertLinearToGamma(mat, Color);
ConvertLinearToGamma(mat, MainColor);
ConvertLinearToGamma(mat, HighlightColor);
ConvertLinearToGamma(mat, OutlineColor);
ConvertLinearToGamma(mat, SelfLitColor);
ConvertLinearToGamma(mat, GlossColor);
ConvertLinearToGamma(mat, OverallShadowColor);
ConvertLinearToGamma(mat, SelfShadowRealTimeShadowColor);
ConvertLinearToGamma(mat, ShadowTColor);
ConvertLinearToGamma(mat, RimLightColor);
}
private static void ConvertGammaToLinear(Material mat, int propertyId)
{
var color = mat.GetColor(propertyId);
mat.SetColor(propertyId, new Color(
Mathf.Pow(color.r, 2.2f),
Mathf.Pow(color.g, 2.2f),
Mathf.Pow(color.b, 2.2f)));
}
private static void ConvertLinearToGamma(Material mat, int propertyId)
{
var color = mat.GetColor(propertyId);
mat.SetColor(propertyId, new Color(
Mathf.Pow(color.r, 1/2.2f),
Mathf.Pow(color.g, 1/2.2f),
Mathf.Pow(color.b, 1/2.2f)));
}
}
}
@andreiagmu

This comment has been minimized.

Copy link
Owner Author

@andreiagmu andreiagmu commented Jan 24, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment