From 45c56bbd850202365751ea019baf5131b2eb9fbe Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Wed, 8 Nov 2023 20:24:20 +0900 Subject: [PATCH] feat: support 'Active Apply Color Space' for linear color space --- Scripts/Editor/UIParticleEditor.cs | 12 ------------ Scripts/UIParticleRenderer.cs | 19 +++++++++++++++++++ Scripts/Utils.cs | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/Scripts/Editor/UIParticleEditor.cs b/Scripts/Editor/UIParticleEditor.cs index 9037d05..1695bb1 100644 --- a/Scripts/Editor/UIParticleEditor.cs +++ b/Scripts/Editor/UIParticleEditor.cs @@ -332,8 +332,6 @@ namespace Coffee.UIExtensions DestroyUIParticle(current); } - // #203: When using linear color space, the particle colors are not output correctly. - // To fix, set 'Apply Active Color Space' in renderer module to false. var allPsRenderers = targets.OfType() .SelectMany(x => x.particles) .Where(x => x) @@ -341,16 +339,6 @@ namespace Coffee.UIExtensions .ToArray(); if (0 < allPsRenderers.Length) { - var so = new SerializedObject(allPsRenderers); - var sp = so.FindProperty("m_ApplyActiveColorSpace"); - label = "When using linear color space, the particle colors are not output correctly.\n" + - "To fix, set 'Apply Active Color Space' in renderer module to false."; - if (FixButton(sp.boolValue || sp.hasMultipleDifferentValues, label)) - { - sp.boolValue = false; - so.ApplyModifiedProperties(); - } - // Check to use 'TEXCOORD*.zw' components as custom vertex stream. foreach (var psr in allPsRenderers) { diff --git a/Scripts/UIParticleRenderer.cs b/Scripts/UIParticleRenderer.cs index c5406d7..778d3de 100644 --- a/Scripts/UIParticleRenderer.cs +++ b/Scripts/UIParticleRenderer.cs @@ -18,6 +18,7 @@ namespace Coffee.UIExtensions private static readonly List s_Materials = new List(2); private static MaterialPropertyBlock s_Mpb; private static readonly List s_Renderers = new List(); + private static readonly List s_Colors = new List(); private static readonly Vector3[] s_Corners = new Vector3[4]; private Material _currentMaterialForRendering; private bool _delay; @@ -380,6 +381,24 @@ namespace Coffee.UIExtensions bounds.extents = extents; workerMesh.bounds = bounds; _lastBounds = bounds; + + // Convert linear color to gamma color. + if (QualitySettings.activeColorSpace == ColorSpace.Linear) + { + Profiler.BeginSample("[UIParticleRenderer] Convert Linear to Gamma"); + workerMesh.GetColors(s_Colors); + var count_c = s_Colors.Count; + for (var i = 0; i < count_c; i++) + { + var c = s_Colors[i]; + c.r = c.r.LinearToGamma(); + c.g = c.g.LinearToGamma(); + c.b = c.b.LinearToGamma(); + s_Colors[i] = c; + } + workerMesh.SetColors(s_Colors); + Profiler.EndSample(); + } } Profiler.EndSample(); diff --git a/Scripts/Utils.cs b/Scripts/Utils.cs index 59cf2b1..6700333 100644 --- a/Scripts/Utils.cs +++ b/Scripts/Utils.cs @@ -6,6 +6,25 @@ using Object = UnityEngine.Object; namespace Coffee.UIParticleExtensions { + public static class Color32Extensions + { + private static byte[] s_LinearToGammaLut; + + public static byte LinearToGamma(this byte self) + { + if (s_LinearToGammaLut == null) + { + s_LinearToGammaLut = new byte[256]; + for (var i = 0; i < 256; i++) + { + s_LinearToGammaLut[i] = (byte)(Mathf.LinearToGammaSpace(i / 255f) * 255f); + } + } + + return s_LinearToGammaLut[self]; + } + } + public static class Vector3Extensions { public static Vector3 Inverse(this Vector3 self)