Light.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. Range is automatically
  37. /// calculated from intensity if <see cref="PhysicallyBasedAttenuation"/> is turned on.
  38. /// </summary>
  39. public float Range
  40. {
  41. get { return _nativeLight.Range; }
  42. set { _nativeLight.Range = value; serializableData.range = value; }
  43. }
  44. /// <summary>
  45. /// Power of the light source. This is luminous flux for point & spot lights, and radiance for directional lights.
  46. /// </summary>
  47. public float Intensity
  48. {
  49. get { return _nativeLight.Intensity; }
  50. set { _nativeLight.Intensity = value; serializableData.intensity = value; }
  51. }
  52. /// <summary>
  53. /// Total angle covered by a spot light. Ignored by other light types.
  54. /// </summary>
  55. public Degree SpotAngle
  56. {
  57. get { return _nativeLight.SpotAngle; }
  58. set { _nativeLight.SpotAngle = value; serializableData.spotAngle = value; }
  59. }
  60. /// <summary>
  61. /// Falloff angle covered by a spot light. Falloff angle determines at what point does light intensity starts
  62. /// linearly falling off as the angle approaches the total spot angle. Ignored by other light types.
  63. /// </summary>
  64. public Degree SpotFalloffAngle
  65. {
  66. get { return _nativeLight.SpotFalloffAngle; }
  67. set { _nativeLight.SpotFalloffAngle = value; serializableData.spotFalloffAngle = value; }
  68. }
  69. /// <summary>
  70. /// Determines is the light attenuation handled in a physically correct way, or should the user have more artistic
  71. /// control over it. If true the range and attenuation of the light are controlled by inverse square of distance.
  72. /// If false then the user is allowed to set the range and attenuation is adjusted accordingly.
  73. /// </summary>
  74. public bool PhysicallyBasedAttenuation
  75. {
  76. get { return _nativeLight.PhysicallyBasedAttenuation; }
  77. set { _nativeLight.PhysicallyBasedAttenuation = value; serializableData.physicallyBasedAttenuation = value; }
  78. }
  79. /// <summary>
  80. /// Determines does this light cast a shadow when rendered.
  81. /// </summary>
  82. public bool CastsShadow
  83. {
  84. get { return _nativeLight.CastsShadow; }
  85. set { _nativeLight.CastsShadow = value; serializableData.castShadows = value; }
  86. }
  87. /// <summary>
  88. /// Returns world space bounds that completely encompass the light's area of influence.
  89. /// </summary>
  90. public Sphere Bounds
  91. {
  92. get { Native.UpdateTransform(SceneObject); return Native.Bounds; }
  93. }
  94. private void OnReset()
  95. {
  96. if (_nativeLight != null)
  97. _nativeLight.OnDestroy();
  98. _nativeLight = new NativeLight(SceneObject);
  99. // Restore saved values after reset
  100. _nativeLight.Color = serializableData.color;
  101. _nativeLight.SpotAngle = serializableData.spotAngle;
  102. _nativeLight.SpotFalloffAngle = serializableData.spotFalloffAngle;
  103. _nativeLight.Range = serializableData.range;
  104. _nativeLight.Intensity = serializableData.intensity;
  105. _nativeLight.Type = serializableData.type;
  106. _nativeLight.CastsShadow = serializableData.castShadows;
  107. _nativeLight.PhysicallyBasedAttenuation = serializableData.physicallyBasedAttenuation;
  108. }
  109. private void OnUpdate()
  110. {
  111. _nativeLight.UpdateTransform(SceneObject);
  112. }
  113. private void OnDestroy()
  114. {
  115. _nativeLight.OnDestroy();
  116. }
  117. /// <inheritdoc/>
  118. protected internal override bool CalculateBounds(out AABox box, out Sphere sphere)
  119. {
  120. sphere = Bounds;
  121. Vector3 extents = new Vector3(sphere.Radius, sphere.Radius, sphere.Radius);
  122. box = new AABox(sphere.Center - extents, sphere.Center + extents);
  123. return true;
  124. }
  125. /// <summary>
  126. /// Holds all data the light component needs to persist through serialization.
  127. /// </summary>
  128. [SerializeObject]
  129. private class SerializableData
  130. {
  131. public Color color = Color.White;
  132. public Degree spotAngle = new Degree(45);
  133. public Degree spotFalloffAngle = new Degree(40);
  134. public float range = 10.0f;
  135. public float intensity = 5.0f;
  136. public LightType type = LightType.Point;
  137. public bool castShadows = false;
  138. public bool physicallyBasedAttenuation = true;
  139. }
  140. }
  141. }