PhysicalCamera.hlsl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef UNITY_PHYSICAL_CAMERA_INCLUDED
  2. #define UNITY_PHYSICAL_CAMERA_INCLUDED
  3. // Has to be kept in sync with ColorUtils.cs
  4. // References:
  5. // "Moving Frostbite to PBR" (Sebastien Lagarde & Charles de Rousiers)
  6. // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
  7. // "Implementing a Physically Based Camera" (Padraic Hennessy)
  8. // https://placeholderart.wordpress.com/2014/11/16/implementing-a-physically-based-camera-understanding-exposure/
  9. float ComputeEV100(float aperture, float shutterSpeed, float ISO)
  10. {
  11. // EV number is defined as:
  12. // 2^ EV_s = N^2 / t and EV_s = EV_100 + log2 (S /100)
  13. // This gives
  14. // EV_s = log2 (N^2 / t)
  15. // EV_100 + log2 (S /100) = log2 (N^2 / t)
  16. // EV_100 = log2 (N^2 / t) - log2 (S /100)
  17. // EV_100 = log2 (N^2 / t . 100 / S)
  18. return log2((aperture * aperture) / shutterSpeed * 100.0 / ISO);
  19. }
  20. float ComputeEV100FromAvgLuminance(float avgLuminance)
  21. {
  22. // We later use the middle gray at 12.7% in order to have
  23. // a middle gray at 18% with a sqrt(2) room for specular highlights
  24. // But here we deal with the spot meter measuring the middle gray
  25. // which is fixed at 12.5 for matching standard camera
  26. // constructor settings (i.e. calibration constant K = 12.5)
  27. // Reference: http://en.wikipedia.org/wiki/Film_speed
  28. const float K = 12.5; // Reflected-light meter calibration constant
  29. return log2(avgLuminance * 100.0 / K);
  30. }
  31. float ConvertEV100ToExposure(float EV100)
  32. {
  33. // Compute the maximum luminance possible with H_sbs sensitivity
  34. // maxLum = 78 / ( S * q ) * N^2 / t
  35. // = 78 / ( S * q ) * 2^ EV_100
  36. // = 78 / (100 * 0.65) * 2^ EV_100
  37. // = 1.2 * 2^ EV
  38. // Reference: http://en.wikipedia.org/wiki/Film_speed
  39. float maxLuminance = 1.2 * pow(2.0, EV100);
  40. return 1.0 / maxLuminance;
  41. }
  42. float ComputeISO(float aperture, float shutterSpeed, float targetEV100)
  43. {
  44. // Compute the required ISO to reach the target EV100
  45. return ((aperture * aperture) * 100.0) / (shutterSpeed * pow(2.0, targetEV100));
  46. }
  47. float ComputeLuminanceAdaptation(float previousLuminance, float currentLuminance, float speedDarkToLight, float speedLightToDark, float deltaTime)
  48. {
  49. float delta = currentLuminance - previousLuminance;
  50. float speed = delta > 0.0 ? speedDarkToLight : speedLightToDark;
  51. // Exponential decay
  52. return previousLuminance + delta * (1.0 - exp2(-deltaTime * speed));
  53. }
  54. #endif // UNITY_PHYSICAL_CAMERA_INCLUDED