Renderable.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /// Materials to use when rendering the mesh.
  51. /// </summary>
  52. public Material[] Materials
  53. {
  54. get { return _native.Materials; }
  55. set { _native.Materials = value; }
  56. }
  57. /// <summary>
  58. /// Returns a material for a specific sub-mesh.
  59. /// </summary>
  60. /// <param name="index">Index of the sub-mesh.</param>
  61. /// <returns>Material used for rendering the sub-mesh at the specified index.</returns>
  62. public Material GetMaterial(int index = 0)
  63. {
  64. return _native.GetMaterial(index);
  65. }
  66. /// <summary>
  67. /// Sets a material for a specific sub-mesh.
  68. /// </summary>
  69. /// <param name="material">Material to use for rendering the sub-mesh at the specified index.</param>
  70. /// <param name="index">Index of the sub-mesh.</param>
  71. public void SetMaterial(Material material, int index = 0)
  72. {
  73. _native.SetMaterial(material, index);
  74. serializableData.materials[index] = material;
  75. }
  76. /// <summary>
  77. /// Layer bitfield that controls whether a renderable is considered visible in a specific camera. Renderable layer
  78. /// must match camera layer in order for the camera to render the component.
  79. /// </summary>
  80. public UInt64 Layers
  81. {
  82. get { return _native.Layers; }
  83. set { _native.Layers = value; serializableData.layers = value; }
  84. }
  85. /// <summary>
  86. /// Gets world bounds of the mesh rendered by this object.
  87. /// </summary>
  88. public Bounds Bounds
  89. {
  90. get { return _native.GetBounds(SceneObject); }
  91. }
  92. private void OnInitialize()
  93. {
  94. serializableData.materials = new Material[0];
  95. serializableData.layers = 1;
  96. }
  97. private void OnReset()
  98. {
  99. if (_native != null)
  100. _native.OnDestroy();
  101. _native = new NativeRenderable(SceneObject);
  102. // Restore saved values after reset
  103. _native.Mesh = serializableData.mesh;
  104. if (serializableData.materials != null)
  105. {
  106. for (int i = 0; i < serializableData.materials.Length; i++)
  107. _native.SetMaterial(serializableData.materials[i], i);
  108. }
  109. _native.Layers = serializableData.layers;
  110. }
  111. private void Update()
  112. {
  113. _native.UpdateTransform(SceneObject);
  114. }
  115. private void OnDestroy()
  116. {
  117. _native.OnDestroy();
  118. }
  119. /// <summary>
  120. /// Holds all data the renderable component needs to persist through serialization.
  121. /// </summary>
  122. [SerializeObject]
  123. private struct SerializableData
  124. {
  125. public Mesh mesh;
  126. public Material[] materials;
  127. public UInt64 layers;
  128. }
  129. }
  130. }