Skip to content

Commit

Permalink
Add Ramp Map Color Space Support
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMa0012 committed Mar 13, 2024
1 parent 19d783c commit d3f274d
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 330 deletions.
15 changes: 10 additions & 5 deletions Editor/Helper/RampHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static string lastSavePath
Rect buttonRect,
MaterialProperty prop,
SerializedProperty serializedProperty,
bool isLinear,
bool isDirty,
string defaultFileName,
string rootPath,
Expand Down Expand Up @@ -97,7 +98,7 @@ public static string lastSavePath
{
//Create texture and save PNG
var saveUnityPath = absPath.Replace(projectPath, String.Empty);
CreateAndSaveNewGradientTexture(defaultWidth, defaultHeight, saveUnityPath);
CreateAndSaveNewGradientTexture(defaultWidth, defaultHeight, saveUnityPath, isLinear);
// VersionControlHelper.Add(saveUnityPath);
//Load created texture
newTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(saveUnityPath);
Expand Down Expand Up @@ -129,7 +130,7 @@ public static string lastSavePath

public static bool HasGradient(AssetImporter assetImporter) { return assetImporter.userData.Contains("#");}

public static Gradient GetGradientFromTexture(Texture texture, out bool isDirty, bool doReimport = false)
public static Gradient GetGradientFromTexture(Texture texture, bool isLinear, out bool isDirty, bool doReimport = false)
{
isDirty = false;
if (texture == null) return null;
Expand All @@ -139,7 +140,9 @@ public static Gradient GetGradientFromTexture(Texture texture, out bool isDirty,
{
GradientObject savedGradientObject, editingGradientObject;
isDirty = DecodeGradientFromJSON(assetImporter.userData, out savedGradientObject, out editingGradientObject);
return doReimport ? savedGradientObject.gradient : editingGradientObject.gradient;
var outGradient = doReimport ? savedGradientObject.gradient : editingGradientObject.gradient;
outGradient.colorSpace = isLinear ? ColorSpace.Linear : ColorSpace.Gamma;
return outGradient;
}
else
{
Expand Down Expand Up @@ -201,12 +204,13 @@ private static bool DecodeGradientFromJSON(string json, out GradientObject saved
return subJSONs[0] != subJSONs[1];
}

public static bool CreateAndSaveNewGradientTexture(int width, int height, string unityPath)
public static bool CreateAndSaveNewGradientTexture(int width, int height, string unityPath, bool isLinear)
{
var gradientObject = ScriptableObject.CreateInstance<GradientObject>();
gradientObject.gradient = new Gradient();
gradientObject.gradient.colorKeys = new[] { new GradientColorKey(Color.black, 0.0f), new GradientColorKey(Color.white, 1.0f) };
gradientObject.gradient.alphaKeys = new[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f) };
gradientObject.gradient.colorSpace = isLinear ? ColorSpace.Linear : ColorSpace.Gamma;

var ramp = CreateGradientTexture(gradientObject.gradient, width, height);
var png = ramp.EncodeToPNG();
Expand All @@ -222,7 +226,8 @@ public static bool CreateAndSaveNewGradientTexture(int width, int height, string
textureImporter.textureCompression = TextureImporterCompression.Uncompressed;
textureImporter.alphaSource = TextureImporterAlphaSource.FromInput;
textureImporter.mipmapEnabled = false;

textureImporter.sRGBTexture = !isLinear;

var platformTextureSettings = textureImporter.GetDefaultPlatformTextureSettings();
platformTextureSettings.format = TextureImporterFormat.RGBA32;
platformTextureSettings.textureCompression = TextureImporterCompression.Uncompressed;
Expand Down
14 changes: 10 additions & 4 deletions Editor/ShaderDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
/// group:father group name, support suffix keyword for conditional display (Default: none)
/// defaultFileName: default Ramp Map file name when create a new one (Default: RampMap)
/// rootPath: the path where ramp is stored, replace '/' with '.' (for example: Assets.Art.Ramps). when selecting ramp, it will also be filtered according to the path (Default: Assets)
/// colorSpace: sRGB / Linear (Default: sRGB)
/// defaultWidth: default Ramp Width (Default: 512)
/// Target Property Type: Texture2D
/// </summary>
Expand All @@ -875,6 +876,7 @@ public class RampDrawer : SubDrawer
protected string _defaultFileName;
protected float _defaultWidth;
protected float _defaultHeight = 2;
protected bool _isLinear = false;

private static readonly GUIContent _iconMixImage = EditorGUIUtility.IconContent("darkviewbackground");

Expand All @@ -888,7 +890,9 @@ public class RampDrawer : SubDrawer

public RampDrawer(string group, string defaultFileName, float defaultWidth) : this(group, defaultFileName, DefaultRootPath, defaultWidth) { }

public RampDrawer(string group, string defaultFileName, string rootPath, float defaultWidth)
public RampDrawer(string group, string defaultFileName, string rootPath, float defaultWidth) : this(group, defaultFileName, rootPath, "sRGB", defaultWidth) { }

public RampDrawer(string group, string defaultFileName, string rootPath, string colorSpace, float defaultWidth)
{
if (!rootPath.StartsWith(DefaultRootPath))
{
Expand All @@ -898,6 +902,7 @@ public RampDrawer(string group, string defaultFileName, string rootPath, float d
this.group = group;
this._defaultFileName = defaultFileName;
this._rootPath = rootPath.Replace('.', '/');
this._isLinear = colorSpace.ToLower() == "linear";
this._defaultWidth = Mathf.Max(2.0f, defaultWidth);
}

Expand Down Expand Up @@ -932,12 +937,13 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
bool isDirty;
// used to read/write Gradient value in code
GradientObject gradientObject = ScriptableObject.CreateInstance<GradientObject>();
gradientObject.gradient.colorSpace = _isLinear ? ColorSpace.Linear : ColorSpace.Gamma;
// used to modify Gradient value for users
SerializedObject serializedObject = new SerializedObject(gradientObject);
SerializedProperty serializedProperty = serializedObject.FindProperty("gradient");

// Tex > GradientObject
gradientObject.gradient = RampHelper.GetGradientFromTexture(prop.textureValue, out isDirty);
gradientObject.gradient = RampHelper.GetGradientFromTexture(prop.textureValue, _isLinear, out isDirty);
// GradientObject > SerializedObject
serializedObject.Update();

Expand All @@ -963,7 +969,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
// Draw Ramp Editor
bool hasGradientChanges, doSaveGradient, doDiscardGradient;
Texture2D newCreatedTexture;
hasGradientChanges = RampHelper.RampEditor(buttonRect, prop, serializedProperty, isDirty,
hasGradientChanges = RampHelper.RampEditor(buttonRect, prop, serializedProperty, _isLinear, isDirty,
_defaultFileName, _rootPath, (int)_defaultWidth, (int)_defaultHeight,
out newCreatedTexture, out doSaveGradient, out doDiscardGradient);
if (newCreatedTexture != null)
Expand All @@ -983,7 +989,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
if (doDiscardGradient)
{
// Tex > GradientObject
gradientObject.gradient = RampHelper.GetGradientFromTexture(prop.textureValue, out isDirty, true);
gradientObject.gradient = RampHelper.GetGradientFromTexture(prop.textureValue, _isLinear, out isDirty, true);
// GradientObject > SerializedObject
serializedObject.Update();
RampHelper.SetGradientToTexture(prop.textureValue, gradientObject, true);
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,15 @@ float selectedChannelValue = dot(tex2D(_Tex, uv), _textureChannelMask);
/// group:father group name, support suffix keyword for conditional display (Default: none)
/// defaultFileName: default Ramp Map file name when create a new one (Default: RampMap)
/// rootPath: the path where ramp is stored, replace '/' with '.' (for example: Assets.Art.Ramps). when selecting ramp, it will also be filtered according to the path (Default: Assets)
/// colorSpace: sRGB / Linear (Default: sRGB)
/// defaultWidth: default Ramp Width (Default: 512)
/// Target Property Type: Texture2D
public RampDrawer() : this(String.Empty) { }
public RampDrawer(string group) : this(group, "RampMap") { }
public RampDrawer(string group, string defaultFileName) : this(group, defaultFileName, DefaultRootPath, 512) { }
public RampDrawer(string group, string defaultFileName, float defaultWidth) : this(group, defaultFileName, DefaultRootPath, defaultWidth) { }
public RampDrawer(string group, string defaultFileName, string rootPath, float defaultWidth)
public RampDrawer(string group, string defaultFileName, string rootPath, float defaultWidth) : this(group, defaultFileName, rootPath, "sRGB", defaultWidth) { }
public RampDrawer(string group, string defaultFileName, string rootPath, string colorSpace, float defaultWidth)

```

Expand Down
4 changes: 3 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,15 @@ float selectedChannelValue = dot(tex2D(_Tex, uv), _textureChannelMask);
/// group:father group name, support suffix keyword for conditional display (Default: none)
/// defaultFileName: default Ramp Map file name when create a new one (Default: RampMap)
/// rootPath: the path where ramp is stored, replace '/' with '.' (for example: Assets.Art.Ramps). when selecting ramp, it will also be filtered according to the path (Default: Assets)
/// colorSpace: sRGB / Linear (Default: sRGB)
/// defaultWidth: default Ramp Width (Default: 512)
/// Target Property Type: Texture2D
public RampDrawer() : this(String.Empty) { }
public RampDrawer(string group) : this(group, "RampMap") { }
public RampDrawer(string group, string defaultFileName) : this(group, defaultFileName, DefaultRootPath, 512) { }
public RampDrawer(string group, string defaultFileName, float defaultWidth) : this(group, defaultFileName, DefaultRootPath, defaultWidth) { }
public RampDrawer(string group, string defaultFileName, string rootPath, float defaultWidth)
public RampDrawer(string group, string defaultFileName, string rootPath, float defaultWidth) : this(group, defaultFileName, rootPath, "sRGB", defaultWidth) { }
public RampDrawer(string group, string defaultFileName, string rootPath, string colorSpace, float defaultWidth)

```

Expand Down
8 changes: 8 additions & 0 deletions Test/LWGUI_SampleDrawer 1.mat
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Material:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _LinearRamp:
m_Texture: {fileID: 2800000, guid: 6293a417776889843b0c6d20f25677be, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _Ramp:
m_Texture: {fileID: 2800000, guid: 87addb69bcbcc9b4ea7336eff8d6a52b, type: 3}
m_Scale: {x: 1, y: 1}
Expand All @@ -45,6 +49,10 @@ Material:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _sRGBRamp:
m_Texture: {fileID: 2800000, guid: fad186cbfd0faf2488e2edb774c6d08f, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _tex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
Expand Down
Binary file removed Test/RampMap 1.png
Binary file not shown.
116 changes: 0 additions & 116 deletions Test/RampMap 1.png.meta

This file was deleted.

Binary file removed Test/RampMap 2.png
Binary file not shown.
76 changes: 0 additions & 76 deletions Test/RampMap 2.png.meta

This file was deleted.

Binary file removed Test/RampMap.png
Binary file not shown.

0 comments on commit d3f274d

Please sign in to comment.