Renderable.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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. /// <inheritdoc/>
  120. internal protected override bool CalculateBounds(out AABox box, out Sphere sphere)
  121. {
  122. Bounds bounds = Bounds;
  123. box = bounds.Box;
  124. sphere = bounds.Sphere;
  125. return true;
  126. }
  127. /// <summary>
  128. /// Holds all data the renderable component needs to persist through serialization.
  129. /// </summary>
  130. [SerializeObject]
  131. private class SerializableData
  132. {
  133. public Mesh mesh;
  134. public Material[] materials = new Material[0];
  135. public UInt64 layers = 1;
  136. }
  137. }
  138. }