Renderable.cs 5.1 KB

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