Skip to content

Instantly share code, notes, and snippets.

@andreiagmu
Last active January 24, 2020 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiagmu/5c9f627561e2b23b75dc749ff0d40ab7 to your computer and use it in GitHub Desktop.
Save andreiagmu/5c9f627561e2b23b75dc749ff0d40ab7 to your computer and use it in GitHub Desktop.
Material color conversion inspector options for Unity - Base version
// Material color conversion inspector options for Unity - Base 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!
//
//
// 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 : MonoBehaviour
{
private static readonly int Color = Shader.PropertyToID("_Color");
//private static readonly int MainColor = Shader.PropertyToID("_MainColor");
[MenuItem("CONTEXT/Material/Convert Colors to Linear", false, 2050)]
private static void ConvertColorsToLinear(MenuCommand menuCommand)
{
var mat = menuCommand.context as Material;
if (mat == null)
return;
ConvertGammaToLinear(mat, Color);
//ConvertGammaToLinear(mat, MainColor);
}
[MenuItem("CONTEXT/Material/Convert Colors to Gamma", false, 2050)]
private static void ConvertColorsToGamma(MenuCommand menuCommand)
{
var mat = menuCommand.context as Material;
if (mat == null)
return;
ConvertLinearToGamma(mat, Color);
//ConvertLinearToGamma(mat, MainColor);
}
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)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment