Light.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. [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.
  42. /// </summary>
  43. public float Range
  44. {
  45. get { return _nativeLight.Range; }
  46. set { _nativeLight.Range = value; serializableData.range = value; }
  47. }
  48. /// <summary>
  49. /// Power of the light source. This is luminous flux for point & spot lights, and radiance for directional lights.
  50. /// </summary>
  51. public float Intensity
  52. {
  53. get { return _nativeLight.Intensity; }
  54. set { _nativeLight.Intensity = value; serializableData.intensity = value; }
  55. }
  56. /// <summary>
  57. /// Total angle covered by a spot light. Ignored by other light types.
  58. /// </summary>
  59. public Degree SpotAngle
  60. {
  61. get { return _nativeLight.SpotAngle; }
  62. set { _nativeLight.SpotAngle = value; serializableData.spotAngle = value; }
  63. }
  64. /// <summary>
  65. /// Falloff angle covered by a spot light. Falloff angle determines at what point does light intensity starts
  66. /// linearly falling off as the angle approaches the total spot angle. Ignored by other light types.
  67. /// </summary>
  68. public Degree SpotFalloffAngle
  69. {
  70. get { return _nativeLight.SpotFalloffAngle; }
  71. set { _nativeLight.SpotFalloffAngle = value; serializableData.spotFalloffAngle = value; }
  72. }
  73. /// <summary>
  74. /// Determines does this light cast a shadow when rendered.
  75. /// </summary>
  76. public bool CastsShadow
  77. {
  78. get { return _nativeLight.CastsShadow; }
  79. set { _nativeLight.CastsShadow = value; serializableData.castShadows = value; }
  80. }
  81. /// <summary>
  82. /// Returns world space bounds that completely encompass the light's area of influence.
  83. /// </summary>
  84. public Sphere Bounds
  85. {
  86. get { Native.UpdateTransform(SceneObject); return Native.Bounds; }
  87. }
  88. private void OnInitialize()
  89. {
  90. serializableData.color = Color.White;
  91. serializableData.spotAngle = new Degree(45);
  92. serializableData.spotFalloffAngle = new Degree(40);
  93. serializableData.range = 10.0f;
  94. serializableData.intensity = 100.0f;
  95. serializableData.type = LightType.Point;
  96. serializableData.castShadows = false;
  97. }
  98. private void OnReset()
  99. {
  100. if (_nativeLight != null)
  101. _nativeLight.OnDestroy();
  102. _nativeLight = new NativeLight(SceneObject);
  103. // Restore saved values after reset
  104. _nativeLight.Color = serializableData.color;
  105. _nativeLight.SpotAngle = serializableData.spotAngle;
  106. _nativeLight.SpotFalloffAngle = serializableData.spotFalloffAngle;
  107. _nativeLight.Range = serializableData.range;
  108. _nativeLight.Intensity = serializableData.intensity;
  109. _nativeLight.Type = serializableData.type;
  110. _nativeLight.CastsShadow = serializableData.castShadows;
  111. }
  112. private void Update()
  113. {
  114. }
  115. private void OnDestroy()
  116. {
  117. _nativeLight.OnDestroy();
  118. }
  119. /// <summary>
  120. /// Holds all data the light component needs to persist through serialization.
  121. /// </summary>
  122. [SerializeObject]
  123. private struct SerializableData
  124. {
  125. public Color color;
  126. public Degree spotAngle;
  127. public Degree spotFalloffAngle;
  128. public float range;
  129. public float intensity;
  130. public LightType type;
  131. public bool castShadows;
  132. }
  133. }
  134. }