Light.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. /// <summary>
  9. /// Component that illuminates a portion of the scene covered by the light.
  10. /// </summary>
  11. public class Light : Component
  12. {
  13. private NativeLight _nativeLight;
  14. [SerializeField]
  15. private SerializableData serializableData = new SerializableData();
  16. /// <summary>
  17. /// Returns the non-component version of Light that is wrapped by this component.
  18. /// </summary>
  19. internal NativeLight Native
  20. {
  21. get { return _nativeLight; }
  22. }
  23. /// <summary>
  24. /// Light type that determines how are elements near it illuminated.
  25. /// </summary>
  26. public LightType Type
  27. {
  28. get { return _nativeLight.Type; }
  29. set { _nativeLight.Type = value; serializableData.type = value; }
  30. }
  31. /// <summary>
  32. /// Color emitted from the light.
  33. /// </summary>
  34. public Color Color
  35. {
  36. get { return _nativeLight.Color; }
  37. set { _nativeLight.Color = value; serializableData.color = value; }
  38. }
  39. /// <summary>
  40. /// Maximum range of the light. Light will not affect any geometry past that point.
  41. /// </summary>
  42. public float Range
  43. {
  44. get { return _nativeLight.Range; }
  45. set { _nativeLight.Range = value; serializableData.range = value; }
  46. }
  47. /// <summary>
  48. /// Power of the light source. This is luminous flux for point & spot lights, and radiance for directional lights.
  49. /// </summary>
  50. public float Intensity
  51. {
  52. get { return _nativeLight.Intensity; }
  53. set { _nativeLight.Intensity = value; serializableData.intensity = value; }
  54. }
  55. /// <summary>
  56. /// Total angle covered by a spot light. Ignored by other light types.
  57. /// </summary>
  58. public Degree SpotAngle
  59. {
  60. get { return _nativeLight.SpotAngle; }
  61. set { _nativeLight.SpotAngle = value; serializableData.spotAngle = value; }
  62. }
  63. /// <summary>
  64. /// Falloff angle covered by a spot light. Falloff angle determines at what point does light intensity starts
  65. /// linearly falling off as the angle approaches the total spot angle. Ignored by other light types.
  66. /// </summary>
  67. public Degree SpotFalloffAngle
  68. {
  69. get { return _nativeLight.SpotFalloffAngle; }
  70. set { _nativeLight.SpotFalloffAngle = value; serializableData.spotFalloffAngle = value; }
  71. }
  72. /// <summary>
  73. /// Determines does this light cast a shadow when rendered.
  74. /// </summary>
  75. public bool CastsShadow
  76. {
  77. get { return _nativeLight.CastsShadow; }
  78. set { _nativeLight.CastsShadow = value; serializableData.castShadows = value; }
  79. }
  80. /// <summary>
  81. /// Returns world space bounds that completely encompass the light's area of influence.
  82. /// </summary>
  83. public Sphere Bounds
  84. {
  85. get { Native.UpdateTransform(SceneObject); return Native.Bounds; }
  86. }
  87. private void OnInitialize()
  88. {
  89. serializableData.color = Color.White;
  90. serializableData.spotAngle = new Degree(45);
  91. serializableData.spotFalloffAngle = new Degree(40);
  92. serializableData.range = 10.0f;
  93. serializableData.intensity = 100.0f;
  94. serializableData.type = LightType.Point;
  95. serializableData.castShadows = false;
  96. }
  97. private void OnReset()
  98. {
  99. if (_nativeLight != null)
  100. _nativeLight.OnDestroy();
  101. _nativeLight = new NativeLight(SceneObject);
  102. // Restore saved values after reset
  103. _nativeLight.Color = serializableData.color;
  104. _nativeLight.SpotAngle = serializableData.spotAngle;
  105. _nativeLight.SpotFalloffAngle = serializableData.spotFalloffAngle;
  106. _nativeLight.Range = serializableData.range;
  107. _nativeLight.Intensity = serializableData.intensity;
  108. _nativeLight.Type = serializableData.type;
  109. _nativeLight.CastsShadow = serializableData.castShadows;
  110. }
  111. private void Update()
  112. {
  113. }
  114. private void OnDestroy()
  115. {
  116. _nativeLight.OnDestroy();
  117. }
  118. /// <summary>
  119. /// Holds all data the light component needs to persist through serialization.
  120. /// </summary>
  121. [SerializeObject]
  122. private struct SerializableData
  123. {
  124. public Color color;
  125. public Degree spotAngle;
  126. public Degree spotFalloffAngle;
  127. public float range;
  128. public float intensity;
  129. public LightType type;
  130. public bool castShadows;
  131. }
  132. }
  133. }