Light.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 : ManagedComponent
  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. /// Range at which the light contribution fades out to zero. Use <see cref="UseAutoAttenuation"/> to provide
  42. /// a radius automatically dependant on light intensity. The radius will cut-off light contribution and therefore
  43. /// manually set very small radius can end up being very physically incorrect.
  44. /// </summary>
  45. public float AttenuationRadius
  46. {
  47. get { return _nativeLight.AttenuationRadius; }
  48. set { _nativeLight.AttenuationRadius = value; serializableData.attenuationRadius = value; }
  49. }
  50. /// <summary>
  51. /// Radius of the light source. If non-zero then this light represents an area light, otherwise it is a punctual
  52. /// light. Area lights have different attenuation then punctual lights, and their appearance in specular reflections
  53. /// is realistic. Shape of the area light depends on light type:
  54. /// - For directional light the shape is a disc projected on the hemisphere on the horizon. This parameter
  55. /// represents angular radius (in degrees) of the disk and should be very small (think of how much space the Sun
  56. /// takes on the sky - roughly 0.5 degrees).
  57. /// - For radial light the shape is a sphere and the radius is the radius of the sphere.
  58. /// - For spot lights the shape is a disc oriented in the direction of the spot light and the radius is the radius
  59. /// of the disc.
  60. /// </summary>
  61. public float SourceRadius
  62. {
  63. get { return _nativeLight.SourceRadius; }
  64. set { _nativeLight.SourceRadius = value; serializableData.sourceRadius = value; }
  65. }
  66. /// <summary>
  67. /// Determines the power of the light source.This will be luminous flux for radial & spot lights, luminance for
  68. /// directional lights with no area, and illuminance for directional lights with area(non-zero source radius).
  69. /// </summary>
  70. public float Intensity
  71. {
  72. get { return _nativeLight.Intensity; }
  73. set { _nativeLight.Intensity = value; serializableData.intensity = value; }
  74. }
  75. /// <summary>
  76. /// Total angle covered by a spot light. Ignored by other light types.
  77. /// </summary>
  78. public Degree SpotAngle
  79. {
  80. get { return _nativeLight.SpotAngle; }
  81. set { _nativeLight.SpotAngle = value; serializableData.spotAngle = value; }
  82. }
  83. /// <summary>
  84. /// Falloff angle covered by a spot light. Falloff angle determines at what point does light intensity starts
  85. /// linearly falling off as the angle approaches the total spot angle. Ignored by other light types.
  86. /// </summary>
  87. public Degree SpotFalloffAngle
  88. {
  89. get { return _nativeLight.SpotFalloffAngle; }
  90. set { _nativeLight.SpotFalloffAngle = value; serializableData.spotFalloffAngle = value; }
  91. }
  92. /// <summary>
  93. /// If enabled the <see cref="AttenuationRadius"/> property will automatically be controlled in order to provide
  94. /// reasonable light radius, depending on its intensity.
  95. /// </summary>
  96. public bool UseAutoAttenuation
  97. {
  98. get { return _nativeLight.UseAutoAttenuation; }
  99. set { _nativeLight.UseAutoAttenuation = value; serializableData.useAutoAttenuation = value; }
  100. }
  101. /// <summary>
  102. /// Determines does this light cast a shadow when rendered.
  103. /// </summary>
  104. public bool CastsShadow
  105. {
  106. get { return _nativeLight.CastsShadow; }
  107. set { _nativeLight.CastsShadow = value; serializableData.castShadows = value; }
  108. }
  109. /// <summary>
  110. /// Returns world space bounds that completely encompass the light's area of influence.
  111. /// </summary>
  112. public Sphere Bounds
  113. {
  114. get { Native.UpdateTransform(SceneObject); return Native.Bounds; }
  115. }
  116. private void OnReset()
  117. {
  118. if (_nativeLight != null)
  119. _nativeLight.OnDestroy();
  120. _nativeLight = new NativeLight(SceneObject);
  121. // Restore saved values after reset
  122. _nativeLight.Color = serializableData.color;
  123. _nativeLight.SpotAngle = serializableData.spotAngle;
  124. _nativeLight.SpotFalloffAngle = serializableData.spotFalloffAngle;
  125. _nativeLight.AttenuationRadius = serializableData.attenuationRadius;
  126. _nativeLight.SourceRadius = serializableData.sourceRadius;
  127. _nativeLight.Intensity = serializableData.intensity;
  128. _nativeLight.Type = serializableData.type;
  129. _nativeLight.CastsShadow = serializableData.castShadows;
  130. _nativeLight.UseAutoAttenuation = serializableData.useAutoAttenuation;
  131. }
  132. private void OnUpdate()
  133. {
  134. _nativeLight.UpdateTransform(SceneObject);
  135. }
  136. private void OnDestroy()
  137. {
  138. _nativeLight.OnDestroy();
  139. }
  140. /// <inheritdoc/>
  141. protected internal override bool CalculateBounds(out AABox box, out Sphere sphere)
  142. {
  143. sphere = Bounds;
  144. Vector3 extents = new Vector3(sphere.Radius, sphere.Radius, sphere.Radius);
  145. box = new AABox(sphere.Center - extents, sphere.Center + extents);
  146. return true;
  147. }
  148. /// <summary>
  149. /// Holds all data the light component needs to persist through serialization.
  150. /// </summary>
  151. [SerializeObject]
  152. private class SerializableData
  153. {
  154. public Color color = Color.White;
  155. public Degree spotAngle = new Degree(45);
  156. public Degree spotFalloffAngle = new Degree(40);
  157. public float attenuationRadius = 10.0f;
  158. public float sourceRadius = 0.0f;
  159. public float intensity = 5.0f;
  160. public LightType type = LightType.Radial;
  161. public bool castShadows = false;
  162. public bool useAutoAttenuation = true;
  163. }
  164. }
  165. /** @} */
  166. }