Skip to content

Commit c134cb2

Browse files
committed
Add Convert Color Space Menu to Ramp Atlas
1 parent f42fd32 commit c134cb2

File tree

8 files changed

+122
-11
lines changed

8 files changed

+122
-11
lines changed

.obsidian/hotkeys.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"markdown:toggle-preview": [],
3+
"editor:toggle-source": [
4+
{
5+
"modifiers": [
6+
"Mod"
7+
],
8+
"key": "E"
9+
}
10+
]
11+
}

.obsidian/workspace.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@
108108
"state": {
109109
"type": "outline",
110110
"state": {
111-
"file": "README.md",
111+
"file": "README_CN.md",
112112
"followCursor": false,
113113
"showSearch": false,
114114
"searchQuery": ""
115115
},
116116
"icon": "lucide-list",
117-
"title": "Outline of README"
117+
"title": "Outline of README_CN"
118118
}
119119
}
120120
]
@@ -208,8 +208,10 @@
208208
"obsidian-git:Open Git source control": false
209209
}
210210
},
211-
"active": "d93ffc2d4e7b14bd",
211+
"active": "1ab61776fdab9c7c",
212212
"lastOpenFiles": [
213+
"package.json~",
214+
"README.md",
213215
"README_CN.md",
214216
"Editor/ScriptableObject.meta",
215217
"Editor/ScriptableObject/LwguiShaderPropertyPreset.cs.meta",
@@ -221,7 +223,6 @@
221223
"Editor/ScriptableObject",
222224
"Editor/ScriptableObject/LWGUIShaderPropertyPreset.cs",
223225
"Editor/ScriptableObject/LWGUIRampAtlas.cs.meta",
224-
"README.md",
225226
"assets~/Pasted image 20250523120309.png",
226227
"assets~/Pasted image 20250522183200.png",
227228
"assets~/Pasted image 20250321174432.png",

Editor/ScriptableObject/LwguiRampAtlas.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,38 @@ public void DiscardChanges()
211211
EditorUtility.ClearDirty(this);
212212
}
213213

214+
public void ConvertColorSpace(ColorSpace targetColorSpace)
215+
{
216+
foreach (var ramp in ramps)
217+
{
218+
if (ramp.colorSpace != targetColorSpace)
219+
{
220+
ramp.colorSpace = targetColorSpace;
221+
ramp.gradient.ConvertColorSpaceWithoutCopy(
222+
targetColorSpace != ColorSpace.Gamma
223+
? ColorSpace.Linear
224+
: ColorSpace.Gamma);
225+
}
226+
}
227+
228+
rampAtlasSRGB = targetColorSpace == ColorSpace.Gamma;
229+
RampHelper.SetRampTextureImporter(_rampAtlasTexturePath, true, !rampAtlasSRGB, EditorJsonUtility.ToJson(this));
230+
UpdateTexturePixels();
231+
SaveTexture();
232+
}
233+
234+
[ContextMenu("Convert Gamma To Linear")]
235+
public void ConvertGammaToLinear()
236+
{
237+
ConvertColorSpace(ColorSpace.Linear);
238+
}
239+
240+
[ContextMenu("Convert Linear To Gamma")]
241+
public void ConvertLinearToGamma()
242+
{
243+
ConvertColorSpace(ColorSpace.Gamma);
244+
}
245+
214246
private void OnEnable()
215247
{
216248
InitData();
@@ -382,6 +414,5 @@ public static LwguiRampAtlas SaveRampAtlasSOToAsset(LwguiRampAtlas rampAtlasSO,
382414

383415
return null;
384416
}
385-
386417
}
387418
}

Editor/ShaderDrawer.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,18 +805,24 @@ public override void Apply(MaterialProperty prop)
805805
/// Note:
806806
/// - Currently only 8 bits are supported.
807807
///
808-
/// Warning: If used to set Stencil, it will conflict with SRP Batcher!
808+
/// Warning 1: If used to set Stencil, it will conflict with SRP Batcher!
809809
/// (Reproduced in Unity 2022)
810810
/// SRP Batcher does not correctly handle multiple materials with different Stencil Ref values,
811811
/// mistakenly merging them into a single Batch and randomly selecting one material's Stencil Ref value for the entire Batch.
812812
/// In theory, if different materials have different Stencil Ref values, they should not be merged into a single Batch due to differing Render States.
813813
/// Solution:
814814
/// - Force disable SRP Batcher by setting the Material Property Block
815815
/// - Place materials with the same Stencil Ref value in a separate Render Queue to ensure the Batch's Render State is correct
816-
///
816+
///
817+
/// Warning 2: Once in use, do not change the Target Property Type!
818+
/// The underlying type of Int Property is Float Property, and in Materials, Int and Integer are stored separately.
819+
/// Once a Material is saved, the Property Type is determined.
820+
/// If you change the Property Type at this point (such as switching between Int/Integer), some strange bugs may occur.
821+
/// If you must change the Property Type, it is recommended to modify the Property Name as well or delete the saved Property in the material.
822+
///
817823
/// group: parent group name (Default: none)
818824
/// bitDescription 7-0: Description of each Bit. (Default: none)
819-
/// Target Property Type: Int
825+
/// Target Property Type: Int/Integer
820826
/// </summary>
821827
public class BitMaskDrawer : SubDrawer
822828
{

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,20 @@ The Property Value in the selected Preset will be the default value:
374374
/// Note:
375375
/// - Currently only 8 bits are supported.
376376
///
377+
/// Warning 1: If used to set Stencil, it will conflict with SRP Batcher!
378+
/// (Reproduced in Unity 2022)
379+
/// SRP Batcher does not correctly handle multiple materials with different Stencil Ref values,
380+
/// mistakenly merging them into a single Batch and randomly selecting one material's Stencil Ref value for the entire Batch.
381+
/// In theory, if different materials have different Stencil Ref values, they should not be merged into a single Batch due to differing Render States.
382+
/// Solution:
383+
/// - Force disable SRP Batcher by setting the Material Property Block
384+
/// - Place materials with the same Stencil Ref value in a separate Render Queue to ensure the Batch's Render State is correct
385+
///
386+
/// Warning 2: Once in use, do not change the Target Property Type!
387+
/// The underlying type of Int Property is Float Property, and in Materials, Int and Integer are stored separately.
388+
/// Once a Material is saved, the Property Type is determined.
389+
/// If you change the Property Type at this point (such as switching between Int/Integer), some strange bugs may occur.
390+
/// If you must change the Property Type, it is recommended to modify the Property Name as well or delete the saved Property in the material.
377391
/// group: parent group name (Default: none)
378392
/// bitDescription 7-0: Description of each Bit. (Default: none)
379393
/// Target Property Type: Int
@@ -606,7 +620,9 @@ You can create an SO in the following ways:
606620
- Right-click on a material property using RampAtlas(): `Create Ramp Atlas` or `Clone Ramp Atlas`
607621
- An SO created this way will include default values for all Ramps in the current material.
608622

609-
You can click the add button of RampAtlasIndexer() to add a new Ramp to the SO.
623+
You can click the add button of RampAtlasIndexer() to add a new Ramp to the SO.
624+
625+
The context menu in the upper right corner has a one-click color space conversion feature.
610626

611627
> [!CAUTION]
612628
>Currently, the material only saves Texture references and Int values. If you manually modify the number and order of Ramps in the Ramp Atlas SO, the selected Ramps in the material may be disrupted!

README_CN.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,20 @@ Result:
371371
/// Note:
372372
/// - Currently only 8 bits are supported.
373373
///
374+
/// Warning 1: If used to set Stencil, it will conflict with SRP Batcher!
375+
/// (Reproduced in Unity 2022)
376+
/// SRP Batcher does not correctly handle multiple materials with different Stencil Ref values,
377+
/// mistakenly merging them into a single Batch and randomly selecting one material's Stencil Ref value for the entire Batch.
378+
/// In theory, if different materials have different Stencil Ref values, they should not be merged into a single Batch due to differing Render States.
379+
/// Solution:
380+
/// - Force disable SRP Batcher by setting the Material Property Block
381+
/// - Place materials with the same Stencil Ref value in a separate Render Queue to ensure the Batch's Render State is correct
382+
///
383+
/// Warning 2: Once in use, do not change the Target Property Type!
384+
/// The underlying type of Int Property is Float Property, and in Materials, Int and Integer are stored separately.
385+
/// Once a Material is saved, the Property Type is determined.
386+
/// If you change the Property Type at this point (such as switching between Int/Integer), some strange bugs may occur.
387+
/// If you must change the Property Type, it is recommended to modify the Property Name as well or delete the saved Property in the material.
374388
/// group: parent group name (Default: none)
375389
/// bitDescription 7-0: Description of each Bit. (Default: none)
376390
/// Target Property Type: Int
@@ -601,7 +615,9 @@ Ramp Atlas SO负责存储并生成Ramp Atlas Texture:
601615
- 在使用RampAtlas()的材质属性上右键: `Create Ramp Atlas``Clone Ramp Atlas`
602616
- 用这种方式创建的SO会包含当前材质中所有Ramp的默认值
603617

604-
你可以点击RampAtlasIndexer()的添加按钮向SO添加新的Ramp.
618+
你可以点击RampAtlasIndexer()的添加按钮向SO添加新的Ramp.
619+
620+
右上角的上下文菜单中有一键转换颜色空间功能.
605621

606622
> [!CAUTION]
607623
> 目前材质仅保存Texture引用和Int值, 如果你手动修改了Ramp Atlas SO中的Ramp数量和顺序, 那么材质中已选择的Ramp可能被打乱!

Runtime/LwguiGradient/LwguiGradient.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,5 +524,35 @@ public Gradient ToGradient(int maxGradientKeyCount = 8)
524524
}
525525

526526
#endregion
527+
528+
#region Gamma <=> Linear
529+
530+
public LwguiGradient ConvertColorSpaceWithoutCopy(ColorSpace targetColorSpace)
531+
{
532+
for (int c = 0; c < (int)Channel.Num; c++)
533+
{
534+
if (c != (int)Channel.Alpha)
535+
{
536+
var keys = rawCurves[c].keys;
537+
for (int i = 0; i < keys.Length; i++)
538+
{
539+
if (targetColorSpace == ColorSpace.Gamma)
540+
keys[i].value = Mathf.LinearToGammaSpace(keys[i].value);
541+
else
542+
keys[i].value = Mathf.GammaToLinearSpace(keys[i].value);
543+
}
544+
545+
rawCurves[c].keys = keys;
546+
}
547+
}
548+
549+
return this;
550+
}
551+
552+
public LwguiGradient gamma => new LwguiGradient(this).ConvertColorSpaceWithoutCopy(ColorSpace.Gamma);
553+
554+
public LwguiGradient linear => new LwguiGradient(this).ConvertColorSpaceWithoutCopy(ColorSpace.Linear);
555+
556+
#endregion
527557
}
528558
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.jasonma.lwgui",
3-
"version": "1.26.2",
3+
"version": "1.27.0",
44
"displayName": "LWGUI",
55
"description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.",
66
"keywords": [

0 commit comments

Comments
 (0)