Light.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Rendering
  6. * @{
  7. */
  8. /// <summary>
  9. /// Component that illuminates a portion of the scene covered by the light.
  10. /// </summary>
  11. [RunInEditor]
  12. public sealed class Light : Component
  13. {
  14. private NativeLight _nativeLight;
  15. [SerializeField]
  16. private SerializableData serializableData = new SerializableData();
  17. /// <summary>
  18. /// Returns the non-component version of Light that is wrapped by this component.
  19. /// </summary>
  20. internal NativeLight Native
  21. {
  22. get { return _nativeLight; }
  23. }
  24. /// <summary>
  25. /// Light type that determines how are elements near it illuminated.
  26. /// </summary>
  27. public LightType Type
  28. {
  29. get { return _nativeLight.Type; }
  30. set { _nativeLight.Type = value; serializableData.type = value; }
  31. }
  32. /// <summary>
  33. /// Color emitted from the light.
  34. /// </summary>
  35. public Color Color
  36. {
  37. get { return _nativeLight.Color; }
  38. set { _nativeLight.Color = value; serializableData.color = value; }
  39. }
  40. /// <summary>
  41. /// Maximum range of the light. Light will not affect any geometry past that point. Range is automatically
  42. /// calculated from intensity if <see cref="PhysicallyBasedAttenuation"/> is turned on.
  43. /// </summary>
  44. public float Range
  45. {
  46. get { return _nativeLight.Range; }
  47. set { _nativeLight.Range = value; serializableData.range = value; }
  48. }
  49. /// <summary>
  50. /// Power of the light source. This is luminous flux for point & spot lights, and radiance for directional lights.
  51. /// </summary>
  52. public float Intensity
  53. {
  54. get { return _nativeLight.Intensity; }
  55. set { _nativeLight.Intensity = value; serializableData.intensity = value; }
  56. }
  57. /// <summary>
  58. /// Total angle covered by a spot light. Ignored by other light types.
  59. /// </summary>
  60. public Degree SpotAngle
  61. {
  62. get { return _nativeLight.SpotAngle; }
  63. set { _nativeLight.SpotAngle = value; serializableData.spotAngle = value; }
  64. }
  65. /// <summary>
  66. /// Falloff angle covered by a spot light. Falloff angle determines at what point does light intensity starts
  67. /// linearly falling off as the angle approaches the total spot angle. Ignored by other light types.
  68. /// </summary>
  69. public Degree SpotFalloffAngle
  70. {
  71. get { return _nativeLight.SpotFalloffAngle; }
  72. set { _nativeLight.SpotFalloffAngle = value; serializableData.spotFalloffAngle = value; }
  73. }
  74. /// <summary>
  75. /// Determines is the light attenuation handled in a physically correct way, or should the user have more artistic
  76. /// control over it. If true the range and attenuation of the light are controlled by inverse square of distance.
  77. /// If false then the user is allowed to set the range and attenuation is adjusted accordingly.
  78. /// </summary>
  79. public bool PhysicallyBasedAttenuation
  80. {
  81. get { return _nativeLight.PhysicallyBasedAttenuation; }
  82. set { _nativeLight.PhysicallyBasedAttenuation = value; serializableData.physicallyBasedAttenuation = value; }
  83. }
  84. /// <summary>
  85. /// Determines does this light cast a shadow when rendered.
  86. /// </summary>
  87. public bool CastsShadow
  88. {
  89. get { return _nativeLight.CastsShadow; }
  90. set { _nativeLight.CastsShadow = value; serializableData.castShadows = value; }
  91. }
  92. /// <summary>
  93. /// Returns world space bounds that completely encompass the light's area of influence.
  94. /// </summary>
  95. public Sphere Bounds
  96. {
  97. get { Native.UpdateTransform(SceneObject); return Native.Bounds; }
  98. }
  99. private void OnReset()
  100. {
  101. if (_nativeLight != null)
  102. _nativeLight.OnDestroy();
  103. _nativeLight = new NativeLight(SceneObject);
  104. // Restore saved values after reset
  105. _nativeLight.Color = serializableData.color;
  106. _nativeLight.SpotAngle = serializableData.spotAngle;
  107. _nativeLight.SpotFalloffAngle = serializableData.spotFalloffAngle;
  108. _nativeLight.Range = serializableData.range;
  109. _nativeLight.Intensity = serializableData.intensity;
  110. _nativeLight.Type = serializableData.type;
  111. _nativeLight.CastsShadow = serializableData.castShadows;
  112. _nativeLight.PhysicallyBasedAttenuation = serializableData.physicallyBasedAttenuation;
  113. }
  114. private void OnUpdate()
  115. {
  116. _nativeLight.UpdateTransform(SceneObject);
  117. }
  118. private void OnDestroy()
  119. {
  120. _nativeLight.OnDestroy();
  121. }
  122. /// <inheritdoc/>
  123. protected internal override bool CalculateBounds(out AABox box, out Sphere sphere)
  124. {
  125. sphere = Bounds;
  126. Vector3 extents = new Vector3(sphere.Radius, sphere.Radius, sphere.Radius);
  127. box = new AABox(sphere.Center - extents, sphere.Center + extents);
  128. return true;
  129. }
  130. /// <summary>
  131. /// Holds all data the light component needs to persist through serialization.
  132. /// </summary>
  133. [SerializeObject]
  134. private class SerializableData
  135. {
  136. public Color color = Color.White;
  137. public Degree spotAngle = new Degree(45);
  138. public Degree spotFalloffAngle = new Degree(40);
  139. public float range = 10.0f;
  140. public float intensity = 5.0f;
  141. public LightType type = LightType.Point;
  142. public bool castShadows = false;
  143. public bool physicallyBasedAttenuation = true;
  144. }
  145. }
  146. /** @} */
  147. }