Renderable.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. namespace BansheeEngine
  3. {
  4. /// <summary>
  5. /// Renderable represents any visible object in the scene. It has a mesh, bounds and a set of materials. Renderer will
  6. /// render any Renderable objects visible by a camera.
  7. /// </summary>
  8. public class Renderable : Component
  9. {
  10. private NativeRenderable _native;
  11. [SerializeField]
  12. private SerializableData serializableData = new SerializableData();
  13. /// <summary>
  14. /// Returns the non-component version of Renderable that is wrapped by this component.
  15. /// </summary>
  16. internal NativeRenderable Native
  17. {
  18. get { return _native; }
  19. }
  20. /// <summary>
  21. /// Mesh to render.
  22. /// </summary>
  23. public Mesh Mesh
  24. {
  25. get { return _native.Mesh; }
  26. set
  27. {
  28. _native.Mesh = value;
  29. serializableData.mesh = value;
  30. int subMeshCount = 0;
  31. if (value != null)
  32. subMeshCount = value.SubMeshCount;
  33. Material[] newMaterials = new Material[subMeshCount];
  34. int numToCopy = MathEx.Min(newMaterials.Length, serializableData.materials.Length);
  35. Array.Copy(serializableData.materials, newMaterials, numToCopy);
  36. serializableData.materials = newMaterials;
  37. }
  38. }
  39. /// <summary>
  40. /// Material to use when rendering the mesh. If the mesh contains multiple sub-meshes then you may set individual
  41. /// materials for each sub-mesh.
  42. /// </summary>
  43. public Material Material
  44. {
  45. get { return _native.GetMaterial(0); }
  46. set
  47. { _native.SetMaterial(value); serializableData.materials[0] = value; }
  48. }
  49. /// <summary>
  50. /// Returns a material for a specific sub-mesh.
  51. /// </summary>
  52. /// <param name="index">Index of the sub-mesh.</param>
  53. /// <returns>Material used for rendering the sub-mesh at the specified index.</returns>
  54. public Material GetMaterial(int index = 0)
  55. {
  56. return _native.GetMaterial(index);
  57. }
  58. /// <summary>
  59. /// Sets a material for a specific sub-mesh.
  60. /// </summary>
  61. /// <param name="material">Material to use for rendering the sub-mesh at the specified index.</param>
  62. /// <param name="index">Index of the sub-mesh.</param>
  63. public void SetMaterial(Material material, int index = 0)
  64. {
  65. _native.SetMaterial(material, index);
  66. serializableData.materials[index] = material;
  67. }
  68. /// <summary>
  69. /// Layer bitfield that controls whether a renderable is considered visible in a specific camera. Renderable layer
  70. /// must match camera layer in order for the camera to render the component.
  71. /// </summary>
  72. public UInt64 Layers
  73. {
  74. get { return _native.Layers; }
  75. set { _native.Layers = value; serializableData.layers = value; }
  76. }
  77. /// <summary>
  78. /// Gets world bounds of the mesh rendered by this object.
  79. /// </summary>
  80. public Bounds Bounds
  81. {
  82. get { return _native.GetBounds(SceneObject); }
  83. }
  84. private void OnInitialize()
  85. {
  86. serializableData.materials = new Material[0];
  87. serializableData.layers = 1;
  88. }
  89. private void OnReset()
  90. {
  91. if (_native != null)
  92. _native.OnDestroy();
  93. _native = new NativeRenderable(SceneObject);
  94. // Restore saved values after reset
  95. _native.Mesh = serializableData.mesh;
  96. if (serializableData.materials != null)
  97. {
  98. for (int i = 0; i < serializableData.materials.Length; i++)
  99. _native.SetMaterial(serializableData.materials[i], i);
  100. }
  101. _native.Layers = serializableData.layers;
  102. }
  103. private void Update()
  104. {
  105. _native.UpdateTransform(SceneObject);
  106. }
  107. private void OnDestroy()
  108. {
  109. _native.OnDestroy();
  110. }
  111. /// <summary>
  112. /// Holds all data the renderable component needs to persist through serialization.
  113. /// </summary>
  114. [SerializeObject]
  115. private struct SerializableData
  116. {
  117. public Mesh mesh;
  118. public Material[] materials;
  119. public UInt64 layers;
  120. }
  121. }
  122. }