Light.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public class Light : Component
  9. {
  10. private LightInternal internalLight;
  11. [SerializeField]
  12. private SerializableData serializableData = new SerializableData();
  13. internal LightInternal Internal
  14. {
  15. get { return internalLight; }
  16. }
  17. public Vector3 Position
  18. {
  19. get { return internalLight.Position; }
  20. set { internalLight.Position = value; serializableData.position = value; }
  21. }
  22. public Quaternion Rotation
  23. {
  24. get { return internalLight.Rotation; }
  25. set { internalLight.Rotation = value; serializableData.rotation = value; }
  26. }
  27. public LightType Type
  28. {
  29. get { return internalLight.Type; }
  30. set { internalLight.Type = value; serializableData.type = value; }
  31. }
  32. public Color Color
  33. {
  34. get { return internalLight.Color; }
  35. set { internalLight.Color = value; serializableData.color = value; }
  36. }
  37. public float Range
  38. {
  39. get { return internalLight.Range; }
  40. set { internalLight.Range = value; serializableData.range = value; }
  41. }
  42. public float Intensity
  43. {
  44. get { return internalLight.Intensity; }
  45. set { internalLight.Intensity = value; serializableData.intensity = value; }
  46. }
  47. public Degree SpotAngle
  48. {
  49. get { return internalLight.SpotAngle; }
  50. set { internalLight.SpotAngle = value; serializableData.spotAngle = value; }
  51. }
  52. public Degree SpotFalloffAngle
  53. {
  54. get { return internalLight.SpotFalloffAngle; }
  55. set { internalLight.SpotFalloffAngle = value; serializableData.spotFalloffAngle = value; }
  56. }
  57. public bool CastsShadow
  58. {
  59. get { return internalLight.CastsShadow; }
  60. set { internalLight.CastsShadow = value; serializableData.castShadows = value; }
  61. }
  62. public Sphere Bounds
  63. {
  64. get { Internal.UpdateTransform(SceneObject); return Internal.Bounds; }
  65. }
  66. private void OnInitialize()
  67. {
  68. serializableData.position = Vector3.zero;
  69. serializableData.rotation = Quaternion.Identity;
  70. serializableData.color = Color.White;
  71. serializableData.spotAngle = new Degree(45);
  72. serializableData.spotFalloffAngle = new Degree(40);
  73. serializableData.range = 10.0f;
  74. serializableData.intensity = 100.0f;
  75. serializableData.type = LightType.Point;
  76. serializableData.castShadows = false;
  77. }
  78. private void OnReset()
  79. {
  80. if (internalLight != null)
  81. internalLight.OnDestroy();
  82. internalLight = new LightInternal(SceneObject);
  83. // Restore saved values after reset
  84. internalLight.Position = serializableData.position;
  85. internalLight.Rotation = serializableData.rotation;
  86. internalLight.Color = serializableData.color;
  87. internalLight.SpotAngle = serializableData.spotAngle;
  88. internalLight.SpotFalloffAngle = serializableData.spotFalloffAngle;
  89. internalLight.Range = serializableData.range;
  90. internalLight.Intensity = serializableData.intensity;
  91. internalLight.Type = serializableData.type;
  92. internalLight.CastsShadow = serializableData.castShadows;
  93. }
  94. private void Update()
  95. {
  96. }
  97. private void OnDestroy()
  98. {
  99. internalLight.OnDestroy();
  100. }
  101. [SerializeObject]
  102. private struct SerializableData
  103. {
  104. public Vector3 position;
  105. public Quaternion rotation;
  106. public Color color;
  107. public Degree spotAngle;
  108. public Degree spotFalloffAngle;
  109. public float range;
  110. public float intensity;
  111. public LightType type;
  112. public bool castShadows;
  113. }
  114. }
  115. }