Renderable.cs 4.9 KB

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