暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LightUnitUtils.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using System;
  2. namespace UnityEngine.Rendering
  3. {
  4. /// <summary>
  5. /// Light Unit Utils contains functions and definitions to facilitate conversion between different light intensity units.
  6. /// </summary>
  7. public static class LightUnitUtils
  8. {
  9. static float k_LuminanceToEvFactor => Mathf.Log(100f / ColorUtils.s_LightMeterCalibrationConstant, 2);
  10. static float k_EvToLuminanceFactor => -k_LuminanceToEvFactor;
  11. /// <summary>
  12. /// The solid angle of a full sphere in steradians.
  13. /// </summary>
  14. public const float SphereSolidAngle = 4.0f * Mathf.PI;
  15. /// <summary>
  16. /// Get the unit that light intensity is measured in, for a specific light type.
  17. /// </summary>
  18. /// <param name="lightType">The type of light to get the native light unit for.</param>
  19. /// <returns>The native unit of that light types intensity.</returns>
  20. public static LightUnit GetNativeLightUnit(LightType lightType)
  21. {
  22. switch (lightType)
  23. {
  24. // Punctual lights
  25. case LightType.Spot:
  26. case LightType.Point:
  27. case LightType.Pyramid:
  28. return LightUnit.Candela;
  29. // Directional lights
  30. case LightType.Directional:
  31. case LightType.Box:
  32. return LightUnit.Lux;
  33. // Area lights
  34. case LightType.Rectangle:
  35. case LightType.Disc:
  36. case LightType.Tube:
  37. return LightUnit.Nits;
  38. default:
  39. throw new ArgumentOutOfRangeException();
  40. }
  41. }
  42. /// <summary>
  43. /// Check if a light types intensity can be converted to/from a light unit.
  44. /// </summary>
  45. /// <param name="lightType">Light type to check.</param>
  46. /// <param name="lightUnit">Unit to check.</param>
  47. /// <returns>True if light unit is supported.</returns>
  48. public static bool IsLightUnitSupported(LightType lightType, LightUnit lightUnit)
  49. {
  50. const int punctualUnits = 1 << (int)LightUnit.Lumen |
  51. 1 << (int)LightUnit.Candela |
  52. 1 << (int)LightUnit.Lux |
  53. 1 << (int)LightUnit.Ev100;
  54. const int directionalUnits = 1 << (int)LightUnit.Lux;
  55. const int areaUnits = 1 << (int)LightUnit.Lumen |
  56. 1 << (int)LightUnit.Nits |
  57. 1 << (int)LightUnit.Ev100;
  58. int lightUnitFlag = 1 << (int)lightUnit;
  59. switch (lightType)
  60. {
  61. // Punctual lights
  62. case LightType.Point:
  63. case LightType.Spot:
  64. case LightType.Pyramid:
  65. return (lightUnitFlag & punctualUnits) > 0;
  66. // Directional lights
  67. case LightType.Directional:
  68. case LightType.Box:
  69. return (lightUnitFlag & directionalUnits) > 0;
  70. // Area lights
  71. case LightType.Rectangle:
  72. case LightType.Disc:
  73. case LightType.Tube:
  74. return (lightUnitFlag & areaUnits) > 0;
  75. default:
  76. return false;
  77. }
  78. }
  79. /// <summary>
  80. /// Get the solid angle of a Point light.
  81. /// </summary>
  82. /// <returns>4 * Pi steradians.</returns>
  83. public static float GetSolidAngleFromPointLight()
  84. {
  85. return SphereSolidAngle;
  86. }
  87. /// <summary>
  88. /// Get the solid angle of a Spot light.
  89. /// </summary>
  90. /// <param name="spotAngle">The spot angle in degrees.</param>
  91. /// <returns>Solid angle in steradians.</returns>
  92. public static float GetSolidAngleFromSpotLight(float spotAngle)
  93. {
  94. double angle = Math.PI * spotAngle / 180.0;
  95. double solidAngle = 2.0 * Math.PI * (1.0 - Math.Cos(angle * 0.5));
  96. return (float)solidAngle;
  97. }
  98. /// <summary>
  99. /// Get the solid angle of a Pyramid light.
  100. /// </summary>
  101. /// <param name="spotAngle">The spot angle in degrees.</param>
  102. /// <param name="aspectRatio">The aspect ratio of the pyramid.</param>
  103. /// <returns>Solid angle in steradians.</returns>
  104. public static float GetSolidAngleFromPyramidLight(float spotAngle, float aspectRatio)
  105. {
  106. if (aspectRatio < 1.0f)
  107. {
  108. aspectRatio = (float)(1.0 / aspectRatio);
  109. }
  110. double angleA = Math.PI * spotAngle / 180.0;
  111. double length = Math.Tan(0.5 * angleA) * aspectRatio;
  112. double angleB = Math.Atan(length) * 2.0;
  113. double solidAngle = 4.0 * Math.Asin(Math.Sin(angleA * 0.5) * Math.Sin(angleB * 0.5));
  114. return (float)solidAngle;
  115. }
  116. internal static float GetSolidAngle(LightType lightType, bool spotReflector, float spotAngle, float aspectRatio)
  117. {
  118. return lightType switch
  119. {
  120. LightType.Spot => spotReflector ? GetSolidAngleFromSpotLight(spotAngle) : SphereSolidAngle,
  121. LightType.Pyramid => spotReflector ? GetSolidAngleFromPyramidLight(spotAngle, aspectRatio) : SphereSolidAngle,
  122. LightType.Point => GetSolidAngleFromPointLight(),
  123. _ => throw new ArgumentException("Solid angle is undefined for lights of type " + lightType)
  124. };
  125. }
  126. /// <summary>
  127. /// Get the projected surface area of a Rectangle light.
  128. /// </summary>
  129. /// <param name="rectSizeX">The width of the rectangle.</param>
  130. /// <param name="rectSizeY">The height of the rectangle.</param>
  131. /// <returns>Surface area.</returns>
  132. public static float GetAreaFromRectangleLight(float rectSizeX, float rectSizeY)
  133. {
  134. return Mathf.Abs(rectSizeX * rectSizeY) * Mathf.PI;
  135. }
  136. /// <summary>
  137. /// Get the projected surface area of a Rectangle light.
  138. /// </summary>
  139. /// <param name="rectSize">The size of the rectangle.</param>
  140. /// <returns>Projected surface area.</returns>
  141. public static float GetAreaFromRectangleLight(Vector2 rectSize)
  142. {
  143. return GetAreaFromRectangleLight(rectSize.x, rectSize.y);
  144. }
  145. /// <summary>
  146. /// Get the projected surface area of a Disc light.
  147. /// </summary>
  148. /// <param name="discRadius">The radius of the disc.</param>
  149. /// <returns>Projected surface area.</returns>
  150. public static float GetAreaFromDiscLight(float discRadius)
  151. {
  152. return discRadius * discRadius * Mathf.PI;
  153. }
  154. /// <summary>
  155. /// Get the projected surface area of a Tube light.
  156. /// </summary>
  157. /// <remarks>Note that Tube lights have no physical surface area.
  158. /// Instead this method returns a value suitable for Nits&lt;=&gt;Lumen unit conversion.</remarks>
  159. /// <param name="tubeLength">The length of the tube.</param>
  160. /// <returns>4 * Pi * (tube length).</returns>
  161. public static float GetAreaFromTubeLight(float tubeLength)
  162. {
  163. // Line lights expect radiance (W / (sr * m^2)) in the shader.
  164. // In the UI, we specify luminous flux (power) in lumens.
  165. // First, it needs to be converted to radiometric units (radian flux, W).
  166. //
  167. // Then we must recall how to compute power from radiance:
  168. //
  169. // radiance = differential_power / (differential_projected_area * differential_solid_angle),
  170. // radiance = differential_power / (differential_area * differential_solid_angle * <N, L>),
  171. // power = Integral{area, Integral{hemisphere, radiance * <N, L>}}.
  172. //
  173. // Unlike line lights, our line lights have no surface area, so the integral becomes:
  174. //
  175. // power = Integral{length, Integral{sphere, radiance}}.
  176. //
  177. // For an isotropic line light, radiance is constant, therefore:
  178. //
  179. // power = length * (4 * Pi) * radiance,
  180. // radiance = power / (length * (4 * Pi)).
  181. return Mathf.Abs(tubeLength) * 4.0f * Mathf.PI;
  182. }
  183. /// <summary>
  184. /// Convert intensity in Lumen to Candela.
  185. /// </summary>
  186. /// <param name="lumen">Intensity in Lumen.</param>
  187. /// <param name="solidAngle">Light solid angle in steradians.</param>
  188. /// <returns>Intensity in Candela.</returns>
  189. public static float LumenToCandela(float lumen, float solidAngle)
  190. {
  191. return lumen / solidAngle;
  192. }
  193. /// <summary>
  194. /// Convert intensity in Candela to Lumen.
  195. /// </summary>
  196. /// <param name="candela">Intensity in Candela.</param>
  197. /// <param name="solidAngle">Light solid angle in steradians.</param>
  198. /// <returns>Intensity in Lumen.</returns>
  199. public static float CandelaToLumen(float candela, float solidAngle)
  200. {
  201. return candela * solidAngle;
  202. }
  203. /// <summary>
  204. /// Convert intensity in Lumen to Nits.
  205. /// </summary>
  206. /// <param name="lumen">Intensity in Lumen.</param>
  207. /// <param name="area">Projected surface area of the light source.</param>
  208. /// <returns>Intensity in Nits.</returns>
  209. public static float LumenToNits(float lumen, float area)
  210. {
  211. return lumen / area;
  212. }
  213. /// <summary>
  214. /// Convert intensity in Nits to Lumen.
  215. /// </summary>
  216. /// <param name="nits">Intensity in Nits.</param>
  217. /// <param name="area">Projected surface area of the light source.</param>
  218. /// <returns>Intensity in Lumen.</returns>
  219. public static float NitsToLumen(float nits, float area)
  220. {
  221. return nits * area;
  222. }
  223. /// <summary>
  224. /// Convert intensity in Lux to Candela.
  225. /// </summary>
  226. /// <param name="lux">Intensity in Lux.</param>
  227. /// <param name="distance">Distance between light and surface.</param>
  228. /// <returns>Intensity in Candela.</returns>
  229. public static float LuxToCandela(float lux, float distance)
  230. {
  231. return lux / (distance * distance);
  232. }
  233. /// <summary>
  234. /// Convert intensity in Candela to Lux.
  235. /// </summary>
  236. /// <param name="candela">Intensity in Lux.</param>
  237. /// <param name="distance">Distance between light and surface.</param>
  238. /// <returns>Intensity in Lux.</returns>
  239. public static float CandelaToLux(float candela, float distance)
  240. {
  241. return candela * distance * distance;
  242. }
  243. /// <summary>
  244. /// Convert intensity in Ev100 to Nits.
  245. /// </summary>
  246. /// <param name="ev100">Intensity in Ev100.</param>
  247. /// <returns>Intensity in Nits.</returns>
  248. public static float Ev100ToNits(float ev100)
  249. {
  250. return Mathf.Pow(2.0f, ev100 + k_EvToLuminanceFactor);
  251. }
  252. /// <summary>
  253. /// Convert intensity in Nits to Ev100.
  254. /// </summary>
  255. /// <param name="nits">Intensity in Nits.</param>
  256. /// <returns>Intensity in Ev100.</returns>
  257. public static float NitsToEv100(float nits)
  258. {
  259. return Mathf.Log(nits, 2) + k_LuminanceToEvFactor;
  260. }
  261. /// <summary>
  262. /// Convert intensity in Ev100 to Candela.
  263. /// </summary>
  264. /// <param name="ev100">Intensity in Ev100.</param>
  265. /// <returns>Intensity in Candela.</returns>
  266. public static float Ev100ToCandela(float ev100)
  267. {
  268. return Ev100ToNits(ev100);
  269. }
  270. /// <summary>
  271. /// Convert intensity in Candela to Ev100.
  272. /// </summary>
  273. /// <param name="candela">Intensity in Candela.</param>
  274. /// <returns>Intensity in Ev100.</returns>
  275. public static float CandelaToEv100(float candela)
  276. {
  277. return NitsToEv100(candela);
  278. }
  279. internal static float ConvertIntensityInternal(float intensity, LightUnit fromUnit, LightUnit toUnit,
  280. LightType lightType, float area, float luxAtDistance, float solidAngle)
  281. {
  282. if (!IsLightUnitSupported(lightType, fromUnit) || !IsLightUnitSupported(lightType, toUnit))
  283. {
  284. throw new ArgumentException("Converting " + fromUnit + " to " + toUnit
  285. + " is undefined for lights of type " + lightType);
  286. }
  287. if (fromUnit == toUnit)
  288. {
  289. return intensity;
  290. }
  291. switch (fromUnit)
  292. {
  293. case LightUnit.Lumen:
  294. {
  295. switch (toUnit)
  296. {
  297. case LightUnit.Candela:
  298. {
  299. // Lumen => Candela:
  300. return LumenToCandela(intensity, solidAngle);
  301. }
  302. case LightUnit.Lux:
  303. {
  304. // Lumen => Candela => Lux
  305. float candela = LumenToCandela(intensity, solidAngle);
  306. return CandelaToLux(candela, luxAtDistance);
  307. }
  308. case LightUnit.Nits:
  309. {
  310. // Lumen => Nits
  311. return LumenToNits(intensity, area);
  312. }
  313. case LightUnit.Ev100:
  314. {
  315. // Lumen => Candela/Nits => Ev100
  316. float candelaNits = lightType switch
  317. {
  318. LightType.Point or LightType.Spot or LightType.Pyramid =>
  319. LumenToCandela(intensity, solidAngle),
  320. LightType.Rectangle or LightType.Disc or LightType.Tube =>
  321. LumenToNits(intensity, area),
  322. _ =>
  323. throw new ArgumentException("Converting from Lumen to Ev100 is undefined for light type "
  324. + lightType)
  325. };
  326. return NitsToEv100(candelaNits);
  327. }
  328. default:
  329. throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
  330. }
  331. }
  332. case LightUnit.Candela:
  333. {
  334. switch (toUnit)
  335. {
  336. case LightUnit.Lumen:
  337. {
  338. // Candela => Lumen
  339. return CandelaToLumen(intensity, solidAngle);
  340. }
  341. case LightUnit.Lux:
  342. {
  343. // Candela => Lux
  344. return CandelaToLux(intensity, luxAtDistance);
  345. }
  346. case LightUnit.Ev100:
  347. {
  348. // Candela => Ev100
  349. return NitsToEv100(intensity);
  350. }
  351. default:
  352. throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
  353. }
  354. }
  355. case LightUnit.Lux:
  356. {
  357. switch (toUnit)
  358. {
  359. case LightUnit.Lumen:
  360. {
  361. // Lux => Candela => Lumen
  362. float candela = LuxToCandela(intensity, luxAtDistance);
  363. return CandelaToLumen(candela, solidAngle);
  364. }
  365. case LightUnit.Candela:
  366. {
  367. // Lux => Candela
  368. return LuxToCandela(intensity, luxAtDistance);
  369. }
  370. case LightUnit.Ev100:
  371. {
  372. // Lux => Candela => Ev100
  373. float candela = LuxToCandela(intensity, luxAtDistance);
  374. return NitsToEv100(candela);
  375. }
  376. default:
  377. throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
  378. }
  379. }
  380. case LightUnit.Nits:
  381. {
  382. switch (toUnit)
  383. {
  384. case LightUnit.Lumen:
  385. {
  386. return NitsToLumen(intensity, area);
  387. }
  388. case LightUnit.Ev100:
  389. {
  390. return NitsToEv100(intensity);
  391. }
  392. default:
  393. throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
  394. }
  395. }
  396. case LightUnit.Ev100:
  397. {
  398. switch (toUnit)
  399. {
  400. case LightUnit.Lumen:
  401. {
  402. // Ev100 => Candela/Nits => Lumen
  403. float candelaOrNits = Ev100ToNits(intensity);
  404. return lightType switch
  405. {
  406. LightType.Point or LightType.Spot or LightType.Pyramid =>
  407. CandelaToLumen(candelaOrNits, solidAngle),
  408. LightType.Rectangle or LightType.Disc or LightType.Tube =>
  409. NitsToLumen(candelaOrNits, area),
  410. _ =>
  411. throw new ArgumentException("Converting from Lumen to Ev100 is undefined for light type "
  412. + lightType)
  413. };
  414. }
  415. case LightUnit.Nits:
  416. case LightUnit.Candela:
  417. {
  418. // Ev100 => Candela/Nits
  419. return Ev100ToNits(intensity);
  420. }
  421. case LightUnit.Lux:
  422. {
  423. // Ev100 => Candela => Lux
  424. float candela = Ev100ToNits(intensity);
  425. return CandelaToLux(candela, luxAtDistance);
  426. }
  427. default:
  428. throw new ArgumentOutOfRangeException(nameof(toUnit), toUnit, null);
  429. }
  430. }
  431. default:
  432. throw new ArgumentOutOfRangeException(nameof(fromUnit), fromUnit, null);
  433. }
  434. }
  435. /// <summary>
  436. /// Convert intensity from one unit to another using the parameters of a given Light.
  437. /// </summary>
  438. /// <param name="light">Light to use parameters from.</param>
  439. /// <param name="intensity">Intensity to be converted.</param>
  440. /// <param name="fromUnit">Unit to convert from.</param>
  441. /// <param name="toUnit">Unit to convert to.</param>
  442. /// <returns>Converted intensity.</returns>
  443. public static float ConvertIntensity(Light light, float intensity, LightUnit fromUnit, LightUnit toUnit)
  444. {
  445. LightType lightType = light.type;
  446. float area = lightType switch
  447. {
  448. LightType.Rectangle => GetAreaFromRectangleLight(light.areaSize),
  449. LightType.Disc => GetAreaFromDiscLight(light.areaSize.x), // Disc radius is stored in areaSize.x
  450. LightType.Tube => GetAreaFromTubeLight(light.areaSize.x), // Tube length is stored in areaSize.x
  451. _ => 0.0f
  452. };
  453. float luxAtDistance = light.luxAtDistance;
  454. float solidAngle = lightType switch
  455. {
  456. LightType.Spot or LightType.Pyramid or LightType.Point => GetSolidAngle(lightType, light.enableSpotReflector,
  457. light.spotAngle, light.areaSize.x), // Pyramid aspect ratio is store in areaSize.x
  458. _ => 0.0f
  459. };
  460. return ConvertIntensityInternal(intensity, fromUnit, toUnit, lightType, area, luxAtDistance, solidAngle);
  461. }
  462. }
  463. }