Renderable.cs 5.4 KB

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