LightMeterMonitor.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. namespace UnityEngine.Rendering.PostProcessing
  3. {
  4. /// <summary>
  5. /// This class holds settings for the Light Meter monitor.
  6. /// </summary>
  7. [Serializable]
  8. public sealed class LightMeterMonitor : Monitor
  9. {
  10. /// <summary>
  11. /// The width of the rendered light meter.
  12. /// </summary>
  13. public int width = 512;
  14. /// <summary>
  15. /// The height of the rendered light meter.
  16. /// </summary>
  17. public int height = 256;
  18. /// <summary>
  19. /// Should we display grading and tonemapping curves on top?
  20. /// </summary>
  21. /// <remarks>
  22. /// This only works when <see cref="GradingMode.HighDefinitionRange"/> is active.
  23. /// </remarks>
  24. public bool showCurves = true;
  25. internal override bool ShaderResourcesAvailable(PostProcessRenderContext context)
  26. {
  27. return context.resources.shaders.lightMeter && context.resources.shaders.lightMeter.isSupported;
  28. }
  29. internal override void Render(PostProcessRenderContext context)
  30. {
  31. CheckOutput(width, height);
  32. var histogram = context.logHistogram;
  33. var sheet = context.propertySheets.Get(context.resources.shaders.lightMeter);
  34. sheet.ClearKeywords();
  35. sheet.properties.SetBuffer(ShaderIDs.HistogramBuffer, histogram.data);
  36. var scaleOffsetRes = histogram.GetHistogramScaleOffsetRes(context);
  37. scaleOffsetRes.z = 1f / width;
  38. scaleOffsetRes.w = 1f / height;
  39. sheet.properties.SetVector(ShaderIDs.ScaleOffsetRes, scaleOffsetRes);
  40. if (context.logLut != null && showCurves)
  41. {
  42. sheet.EnableKeyword("COLOR_GRADING_HDR");
  43. sheet.properties.SetTexture(ShaderIDs.Lut3D, context.logLut);
  44. }
  45. var autoExpo = context.autoExposure;
  46. if (autoExpo != null)
  47. {
  48. // Make sure filtering values are correct to avoid apocalyptic consequences
  49. float lowPercent = autoExpo.filtering.value.x;
  50. float highPercent = autoExpo.filtering.value.y;
  51. const float kMinDelta = 1e-2f;
  52. highPercent = Mathf.Clamp(highPercent, 1f + kMinDelta, 99f);
  53. lowPercent = Mathf.Clamp(lowPercent, 1f, highPercent - kMinDelta);
  54. var parameters = new Vector4(
  55. lowPercent * 0.01f,
  56. highPercent * 0.01f,
  57. RuntimeUtilities.Exp2(autoExpo.minLuminance.value),
  58. RuntimeUtilities.Exp2(autoExpo.maxLuminance.value)
  59. );
  60. sheet.EnableKeyword("AUTO_EXPOSURE");
  61. sheet.properties.SetVector(ShaderIDs.Params, parameters);
  62. }
  63. var cmd = context.command;
  64. cmd.BeginSample("LightMeter");
  65. cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, output, sheet, 0);
  66. cmd.EndSample("LightMeter");
  67. }
  68. }
  69. }