Light.cs 6.1 KB

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