Renderable.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. namespace BansheeEngine
  3. {
  4. public class Renderable : Component
  5. {
  6. private RenderableHandler handler;
  7. [SerializeField]
  8. private SerializableData serializableData = new SerializableData();
  9. internal RenderableHandler Handler
  10. {
  11. get { return handler; }
  12. }
  13. public Mesh Mesh
  14. {
  15. get { return handler.Mesh; }
  16. set
  17. {
  18. handler.Mesh = value;
  19. serializableData.mesh = value;
  20. Material[] newMaterials = new Material[value.SubMeshCount];
  21. int numToCopy = MathEx.Min(newMaterials.Length, serializableData.materials.Length);
  22. Array.Copy(serializableData.materials, newMaterials, numToCopy);
  23. serializableData.materials = newMaterials;
  24. }
  25. }
  26. public Material Material
  27. {
  28. get { return handler.GetMaterial(0); }
  29. set
  30. { handler.SetMaterial(value); serializableData.materials[0] = value; }
  31. }
  32. public Material GetMaterial(int index = 0)
  33. {
  34. return handler.GetMaterial(index);
  35. }
  36. public void SetMaterial(Material material, int index = 0)
  37. {
  38. handler.SetMaterial(material, index);
  39. serializableData.materials[index] = material;
  40. }
  41. public UInt64 Layers
  42. {
  43. get { return handler.Layers; }
  44. set { handler.Layers = value; serializableData.layers = value; }
  45. }
  46. public Bounds Bounds
  47. {
  48. get { return handler.GetBounds(sceneObject); }
  49. }
  50. private void OnInitialize()
  51. {
  52. serializableData.materials = new Material[0];
  53. serializableData.layers = 0xFFFFFFFFFFFFFFFF;
  54. }
  55. private void OnReset()
  56. {
  57. if (handler != null)
  58. handler.OnDestroy();
  59. handler = new RenderableHandler(sceneObject);
  60. // Restore saved values after reset
  61. handler.Mesh = serializableData.mesh;
  62. if (serializableData.materials != null)
  63. {
  64. for (int i = 0; i < serializableData.materials.Length; i++)
  65. handler.SetMaterial(serializableData.materials[i], i);
  66. }
  67. handler.Layers = serializableData.layers;
  68. }
  69. private void Update()
  70. {
  71. handler.UpdateTransform(sceneObject);
  72. }
  73. private void OnDestroy()
  74. {
  75. handler.OnDestroy();
  76. }
  77. [SerializeObject]
  78. private struct SerializableData
  79. {
  80. public Mesh mesh;
  81. public Material[] materials;
  82. public UInt64 layers;
  83. }
  84. }
  85. }