Light.cs 4.1 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 NativeLight _nativeLight;
  11. [SerializeField]
  12. private SerializableData serializableData = new SerializableData();
  13. internal NativeLight Native
  14. {
  15. get { return _nativeLight; }
  16. }
  17. public Vector3 Position
  18. {
  19. get { return _nativeLight.Position; }
  20. set { _nativeLight.Position = value; serializableData.position = value; }
  21. }
  22. public Quaternion Rotation
  23. {
  24. get { return _nativeLight.Rotation; }
  25. set { _nativeLight.Rotation = value; serializableData.rotation = value; }
  26. }
  27. public LightType Type
  28. {
  29. get { return _nativeLight.Type; }
  30. set { _nativeLight.Type = value; serializableData.type = value; }
  31. }
  32. public Color Color
  33. {
  34. get { return _nativeLight.Color; }
  35. set { _nativeLight.Color = value; serializableData.color = value; }
  36. }
  37. public float Range
  38. {
  39. get { return _nativeLight.Range; }
  40. set { _nativeLight.Range = value; serializableData.range = value; }
  41. }
  42. public float Intensity
  43. {
  44. get { return _nativeLight.Intensity; }
  45. set { _nativeLight.Intensity = value; serializableData.intensity = value; }
  46. }
  47. public Degree SpotAngle
  48. {
  49. get { return _nativeLight.SpotAngle; }
  50. set { _nativeLight.SpotAngle = value; serializableData.spotAngle = value; }
  51. }
  52. public Degree SpotFalloffAngle
  53. {
  54. get { return _nativeLight.SpotFalloffAngle; }
  55. set { _nativeLight.SpotFalloffAngle = value; serializableData.spotFalloffAngle = value; }
  56. }
  57. public bool CastsShadow
  58. {
  59. get { return _nativeLight.CastsShadow; }
  60. set { _nativeLight.CastsShadow = value; serializableData.castShadows = value; }
  61. }
  62. public Sphere Bounds
  63. {
  64. get { Native.UpdateTransform(SceneObject); return Native.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 (_nativeLight != null)
  81. _nativeLight.OnDestroy();
  82. _nativeLight = new NativeLight(SceneObject);
  83. // Restore saved values after reset
  84. _nativeLight.Position = serializableData.position;
  85. _nativeLight.Rotation = serializableData.rotation;
  86. _nativeLight.Color = serializableData.color;
  87. _nativeLight.SpotAngle = serializableData.spotAngle;
  88. _nativeLight.SpotFalloffAngle = serializableData.spotFalloffAngle;
  89. _nativeLight.Range = serializableData.range;
  90. _nativeLight.Intensity = serializableData.intensity;
  91. _nativeLight.Type = serializableData.type;
  92. _nativeLight.CastsShadow = serializableData.castShadows;
  93. }
  94. private void Update()
  95. {
  96. }
  97. private void OnDestroy()
  98. {
  99. _nativeLight.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. }