Light.cs 5.1 KB

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