Skip to content

Commit ce69258

Browse files
committed
feat: [improve performance] merged apply_color function and reduced shader compilation size
1 parent 6c808aa commit ce69258

File tree

1 file changed

+15
-200
lines changed

1 file changed

+15
-200
lines changed

Packages/src/Shaders/UIEffect.cginc

Lines changed: 15 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -223,134 +223,34 @@ half4 apply_tone_filter(half4 color)
223223
return color;
224224
}
225225

226-
half4 apply_color_filter(const half4 inColor, const half4 factor, const float intensity)
226+
half4 apply_color_filter(int mode, half4 inColor, half4 factor, float intensity, float glow)
227227
{
228-
const float glow = _ColorGlow;
229-
const int mode = _ColorFilter;
230228
half4 color = inColor;
231229
if (mode == 1) // Color.Multiply
232230
{
233231
color.rgb = color.rgb * factor.rgb;
232+
color *= factor.a;
234233
}
235234
else if (mode == 2) // Color.Additive
236235
{
237-
color.rgb = color.rgb + factor.rgb * color.a;
238-
}
239-
else if (mode == 3) // Color.Subtractive
240-
{
241-
color.rgb = color.rgb - factor.rgb * color.a;
242-
}
243-
else if (mode == 4) // Color.Replace
244-
{
245-
color.rgb = factor.rgb * color.a;
246-
}
247-
else if (mode == 5) // Color.MultiplyLuminance
248-
{
249-
color.rgb = (1 + Luminance(color.rgb)) * factor.rgb / 2 * color.a;
250-
}
251-
else if (mode == 6) // Color.MultiplyAdditive
252-
{
253-
color.rgb = color.rgb * (1 + factor.rgb);
254-
}
255-
else if (mode == 7) // Color.HsvModifier
256-
{
257-
const float3 hsv = rgb_to_hsv(color.rgb);
258-
color.rgb = hsv_to_rgb(hsv + factor.rgb) * color.a * factor.a;
259-
color.a = inColor.a * factor.a;
260-
}
261-
else if (mode == 8) // Color.Contrast
262-
{
263-
color.rgb = ((color.rgb - 0.5) * (factor.r + 1) + 0.5 + factor.g * 1.5) * color.a * factor.a;
264-
color.a = color.a * factor.a;
265-
}
266-
267-
if (0 < mode)
268-
{
269-
color = lerp(inColor, color, intensity);
270-
color.a *= 1 - glow * intensity;
271-
}
272-
273-
return color;
274-
}
275-
276-
half4 apply_transition_color_filter(const half4 inColor, const half4 factor, const float intensity)
277-
{
278-
const float glow = _TransitionColorGlow;
279-
const int mode = _TransitionColorFilter;
280-
half4 color = inColor;
281-
if (mode == 1) // Color.Multiply
282-
{
283-
color.rgb = color.rgb * factor.rgb;
284-
}
285-
else if (mode == 2) // Color.Additive
286-
{
287-
color.rgb = color.rgb + factor.rgb * color.a;
288-
}
289-
else if (mode == 3) // Color.Subtractive
290-
{
291-
color.rgb = color.rgb - factor.rgb * color.a;
292-
}
293-
else if (mode == 4) // Color.Replace
294-
{
295-
color.rgb = factor.rgb * color.a;
296-
}
297-
else if (mode == 5) // Color.MultiplyLuminance
298-
{
299-
color.rgb = (1 + Luminance(color.rgb)) * factor.rgb / 2 * color.a;
300-
}
301-
else if (mode == 6) // Color.MultiplyAdditive
302-
{
303-
color.rgb = color.rgb * (1 + factor.rgb);
304-
}
305-
else if (mode == 7) // Color.HsvModifier
306-
{
307-
const float3 hsv = rgb_to_hsv(color.rgb);
308-
color.rgb = hsv_to_rgb(hsv + factor.rgb) * color.a * factor.a;
309-
color.a = inColor.a * factor.a;
310-
}
311-
else if (mode == 8) // Color.Contrast
312-
{
313-
color.rgb = ((color.rgb - 0.5) * (factor.r + 1) + 0.5 + factor.g * 1.5) * color.a * factor.a;
314-
color.a = color.a * factor.a;
315-
}
316-
317-
if (0 < mode)
318-
{
319-
color = lerp(inColor, color, intensity);
320-
color.a *= 1 - glow * intensity;
321-
}
322-
323-
return color;
324-
}
325-
326-
half4 apply_shadow_color_filter(const half4 inColor, const half4 factor, const float intensity)
327-
{
328-
const float glow = _ShadowColorGlow;
329-
const int mode = _ShadowColorFilter;
330-
half4 color = inColor;
331-
if (mode == 1) // Color.Multiply
332-
{
333-
color.rgb = color.rgb * factor.rgb;
334-
}
335-
else if (mode == 2) // Color.Additive
336-
{
337-
color.rgb = color.rgb + factor.rgb * color.a;
236+
color.rgb = color.rgb + factor.rgb * color.a * factor.a;
338237
}
339238
else if (mode == 3) // Color.Subtractive
340239
{
341-
color.rgb = color.rgb - factor.rgb * color.a;
240+
color.rgb = color.rgb - factor.rgb * color.a * factor.a;
342241
}
343242
else if (mode == 4) // Color.Replace
344243
{
345244
color.rgb = factor.rgb * color.a;
245+
color *= factor.a;
346246
}
347247
else if (mode == 5) // Color.MultiplyLuminance
348248
{
349-
color.rgb = (1 + Luminance(color.rgb)) * factor.rgb / 2 * color.a;
249+
color.rgb = (1 + Luminance(color.rgb)) * factor.rgb * factor.a / 2 * color.a;
350250
}
351251
else if (mode == 6) // Color.MultiplyAdditive
352252
{
353-
color.rgb = color.rgb * (1 + factor.rgb);
253+
color.rgb = color.rgb * (1 + factor.rgb * factor.a);
354254
}
355255
else if (mode == 7) // Color.HsvModifier
356256
{
@@ -369,57 +269,7 @@ half4 apply_shadow_color_filter(const half4 inColor, const half4 factor, const f
369269
color = lerp(inColor, color, intensity);
370270
color.a *= 1 - glow * intensity;
371271
}
372-
373-
return color;
374-
}
375-
376-
half4 apply_edge_color_filter(const half4 inColor, const half4 factor, const float intensity)
377-
{
378-
const float glow = _EdgeColorGlow;
379-
const int mode = _EdgeColorFilter;
380-
half4 color = inColor;
381-
if (mode == 1) // Color.Multiply
382-
{
383-
color.rgb = color.rgb * factor.rgb;
384-
}
385-
else if (mode == 2) // Color.Additive
386-
{
387-
color.rgb = color.rgb + factor.rgb * color.a;
388-
}
389-
else if (mode == 3) // Color.Subtractive
390-
{
391-
color.rgb = color.rgb - factor.rgb * color.a;
392-
}
393-
else if (mode == 4) // Color.Replace
394-
{
395-
color.rgb = factor.rgb * color.a;
396-
}
397-
else if (mode == 5) // Color.MultiplyLuminance
398-
{
399-
color.rgb = (1 + Luminance(color.rgb)) * factor.rgb / 2 * color.a;
400-
}
401-
else if (mode == 6) // Color.MultiplyAdditive
402-
{
403-
color.rgb = color.rgb * (1 + factor.rgb);
404-
}
405-
else if (mode == 7) // Color.HsvModifier
406-
{
407-
const float3 hsv = rgb_to_hsv(color.rgb);
408-
color.rgb = hsv_to_rgb(hsv + factor.rgb) * color.a * factor.a;
409-
color.a = inColor.a * factor.a;
410-
}
411-
else if (mode == 8) // Color.Contrast
412-
{
413-
color.rgb = ((color.rgb - 0.5) * (factor.r + 1) + 0.5 + factor.g * 1.5) * color.a * factor.a;
414-
color.a = color.a * factor.a;
415-
}
416272

417-
if (0 < mode)
418-
{
419-
color = lerp(inColor, color, intensity);
420-
color.a *= 1 - glow * intensity;
421-
}
422-
423273
return color;
424274
}
425275

@@ -505,7 +355,7 @@ half4 apply_transition_filter(half4 color, const float alpha, const float2 uvLoc
505355
}
506356
#elif TRANSITION_PATTERN // Transition.Pattern
507357
{
508-
const half4 patternColor = apply_transition_color_filter(half4(color.rgb, 1), half4(_TransitionColor.rgb * color.a, 1), _TransitionColor.a);
358+
const half4 patternColor = apply_color_filter(_TransitionColorFilter, half4(color.rgb, 1), half4(_TransitionColor.rgb * color.a, 1), _TransitionColor.a, _TransitionColorGlow);
509359
float isPattern = min(inv_lerp(_TransitionRange.x, _TransitionRange.y, uvLocal.x), 0.995) < (_TransitionPatternReverse ? alpha : 1 - alpha);
510360
isPattern = _TransitionPatternReverse ? isPattern : 1 - isPattern;
511361
const float patternFactor = _PatternArea == 0 ? 1 : _PatternArea == 1 ? 1 - edgeFactor : edgeFactor;
@@ -518,8 +368,8 @@ half4 apply_transition_filter(half4 color, const float alpha, const float2 uvLoc
518368
const float softness = max(0.0001, _TransitionWidth * _TransitionSoftness);
519369
const half bandLerp = saturate((_TransitionWidth - factor) * 2 / softness);
520370
const half softLerp = saturate(factor * 2 / softness);
521-
half4 bandColor = apply_transition_color_filter(half4(color.rgb, 1), half4(_TransitionColor.rgb, 1),
522-
_TransitionColor.a);
371+
half4 bandColor = apply_color_filter(_TransitionColorFilter, half4(color.rgb, 1),
372+
half4(_TransitionColor.rgb, 1), _TransitionColor.a, _TransitionColorGlow);
523373
bandColor *= color.a;
524374

525375
#if TRANSITION_MELT
@@ -605,42 +455,7 @@ half4 apply_gradation_filter(const half4 inColor, const float2 uvGrad)
605455
}
606456
#endif
607457

608-
const int mode = _GradationColorFilter;
609-
const float intensity = _GradationIntensity;
610-
half4 color = inColor;
611-
if (mode == 1) // Color.Multiply
612-
{
613-
color.rgb = color.rgb * factor.rgb;
614-
color *= factor.a;
615-
}
616-
else if (mode == 2) // Color.Additive
617-
{
618-
color.rgb = color.rgb + factor.rgb * color.a * factor.a;
619-
}
620-
else if (mode == 3) // Color.Subtractive
621-
{
622-
color.rgb = color.rgb - factor.rgb * color.a * factor.a;
623-
}
624-
else if (mode == 4) // Color.Replace
625-
{
626-
color.rgb = factor.rgb * color.a;
627-
color *= factor.a;
628-
}
629-
else if (mode == 5) // Color.MultiplyLuminance
630-
{
631-
color.rgb = (1 + Luminance(color.rgb)) * factor.rgb * factor.a / 2 * color.a;
632-
}
633-
else if (mode == 6) // Color.MultiplyAdditive
634-
{
635-
color.rgb = color.rgb * (1 + factor.rgb * factor.a);
636-
}
637-
638-
if (0 < mode)
639-
{
640-
color = lerp(inColor, color, intensity);
641-
}
642-
643-
return color;
458+
return apply_color_filter(_GradationColorFilter, inColor, factor, _GradationIntensity, 0);
644459
}
645460

646461
half4 apply_detail_filter(half4 color, float2 uvLocal)
@@ -750,20 +565,20 @@ half4 uieffect_internal(float2 uv, float4 uvMask, const float2 uvLocal, const fl
750565

751566
if (0 < isShadow)
752567
{
753-
return apply_shadow_color_filter(color, _ShadowColor, 1);
568+
return apply_color_filter(_ShadowColorFilter, color, _ShadowColor, 1, _ShadowColorGlow);
754569
}
755570
else
756571
{
757572
#if EDGE_PLAIN || EDGE_SHINY
758573
{
759-
color = apply_color_filter(color, _ColorValue, _ColorIntensity);
760-
const half4 edgeColor = apply_edge_color_filter(color, _EdgeColor, 1);
574+
color = apply_color_filter(_ColorFilter, color, _ColorValue, _ColorIntensity, _ColorGlow);
575+
const half4 edgeColor = apply_color_filter(_EdgeColorFilter, color, _EdgeColor, 1, _EdgeColorGlow);
761576
const float isEdgeShiny = is_edge_shiny(uvLocal);
762577
return lerp(color, edgeColor, edgeFactor * isEdgeShiny);
763578
}
764579
#else
765580
{
766-
return apply_color_filter(color, _ColorValue, _ColorIntensity);
581+
return apply_color_filter(_ColorFilter, color, _ColorValue, _ColorIntensity, _ColorGlow);
767582
}
768583
#endif
769584
}

0 commit comments

Comments
 (0)