Renderable.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. private Animation animation;
  18. [SerializeField]
  19. private SerializableData serializableData = new SerializableData();
  20. /// <summary>
  21. /// Returns the non-component version of Renderable that is wrapped by this component.
  22. /// </summary>
  23. internal NativeRenderable Native
  24. {
  25. get { return _native; }
  26. }
  27. /// <summary>
  28. /// Mesh to render.
  29. /// </summary>
  30. public Mesh Mesh
  31. {
  32. get { return _native.Mesh; }
  33. set
  34. {
  35. _native.Mesh = value;
  36. serializableData.mesh = value;
  37. int subMeshCount = 0;
  38. if (value != null)
  39. subMeshCount = value.SubMeshCount;
  40. Material[] newMaterials = new Material[subMeshCount];
  41. int numToCopy = MathEx.Min(newMaterials.Length, serializableData.materials.Length);
  42. Array.Copy(serializableData.materials, newMaterials, numToCopy);
  43. serializableData.materials = newMaterials;
  44. if (animation != null)
  45. animation.UpdateBounds(false);
  46. }
  47. }
  48. /// <summary>
  49. /// Material to use when rendering the mesh. If the mesh contains multiple sub-meshes then you may set individual
  50. /// materials for each sub-mesh.
  51. /// </summary>
  52. public Material Material
  53. {
  54. get { return _native.GetMaterial(0); }
  55. set
  56. { _native.SetMaterial(value); serializableData.materials[0] = value; }
  57. }
  58. /// <summary>
  59. /// Materials to use when rendering the mesh.
  60. /// </summary>
  61. public Material[] Materials
  62. {
  63. get { return _native.Materials; }
  64. set
  65. {
  66. _native.Materials = value;
  67. serializableData.materials = new Material[value.Length];
  68. Array.Copy(value, serializableData.materials, value.Length);
  69. }
  70. }
  71. /// <summary>
  72. /// Returns a material for a specific sub-mesh.
  73. /// </summary>
  74. /// <param name="index">Index of the sub-mesh.</param>
  75. /// <returns>Material used for rendering the sub-mesh at the specified index.</returns>
  76. public Material GetMaterial(int index = 0)
  77. {
  78. return _native.GetMaterial(index);
  79. }
  80. /// <summary>
  81. /// Sets a material for a specific sub-mesh.
  82. /// </summary>
  83. /// <param name="material">Material to use for rendering the sub-mesh at the specified index.</param>
  84. /// <param name="index">Index of the sub-mesh.</param>
  85. public void SetMaterial(Material material, int index = 0)
  86. {
  87. _native.SetMaterial(material, index);
  88. serializableData.materials[index] = material;
  89. }
  90. /// <summary>
  91. /// Layer bitfield that controls whether a renderable is considered visible in a specific camera. Renderable layer
  92. /// must match camera layer in order for the camera to render the component.
  93. /// </summary>
  94. public UInt64 Layers
  95. {
  96. get { return _native.Layers; }
  97. set { _native.Layers = value; serializableData.layers = value; }
  98. }
  99. /// <summary>
  100. /// Gets world bounds of the mesh rendered by this object.
  101. /// </summary>
  102. public Bounds Bounds
  103. {
  104. get { return _native.GetBounds(SceneObject); }
  105. }
  106. private void OnInitialize()
  107. {
  108. animation = SceneObject.GetComponent<Animation>();
  109. if (animation != null)
  110. {
  111. RegisterAnimation(animation);
  112. animation.RegisterRenderable(this);
  113. }
  114. }
  115. private void OnReset()
  116. {
  117. if (_native != null)
  118. _native.OnDestroy();
  119. _native = new NativeRenderable(SceneObject);
  120. // Restore saved values after reset
  121. _native.Mesh = serializableData.mesh;
  122. if (serializableData.materials != null)
  123. _native.Materials = serializableData.materials;
  124. _native.Layers = serializableData.layers;
  125. }
  126. private void OnUpdate()
  127. {
  128. _native.UpdateTransform(SceneObject, false);
  129. }
  130. private void OnDestroy()
  131. {
  132. if (animation != null)
  133. animation.UnregisterRenderable();
  134. _native.OnDestroy();
  135. }
  136. /// <summary>
  137. /// Registers an <see cref="Animation"/> component with the renderable. Rendering will be affected by the animation.
  138. /// </summary>
  139. /// <param name="animation">Component that was added</param>
  140. internal void RegisterAnimation(Animation animation)
  141. {
  142. bool isMeshAnimated = serializableData.mesh != null &&
  143. (serializableData.mesh.Skeleton != null || serializableData.mesh.MorphShapes != null);
  144. if (!isMeshAnimated)
  145. return;
  146. this.animation = animation;
  147. if (_native != null)
  148. {
  149. _native.Animation = animation.Native;
  150. // Need to update transform because animated renderables handle local transforms through bones, so it
  151. // shouldn't be included in the renderable's transform.
  152. _native.UpdateTransform(SceneObject, true);
  153. }
  154. }
  155. /// <summary>
  156. /// Removes animation from the renderable. Rendering will no longer be affected by animation.
  157. /// </summary>
  158. internal void UnregisterAnimation()
  159. {
  160. animation = null;
  161. if (_native != null)
  162. {
  163. _native.Animation = null;
  164. // Need to update transform because animated renderables handle local transforms through bones, so it
  165. // shouldn't be included in the renderable's transform.
  166. _native.UpdateTransform(SceneObject, true);
  167. }
  168. }
  169. /// <inheritdoc/>
  170. protected internal override bool CalculateBounds(out AABox box, out Sphere sphere)
  171. {
  172. Bounds bounds = Bounds;
  173. box = bounds.Box;
  174. sphere = bounds.Sphere;
  175. return true;
  176. }
  177. /// <summary>
  178. /// Holds all data the renderable component needs to persist through serialization.
  179. /// </summary>
  180. [SerializeObject]
  181. private class SerializableData
  182. {
  183. public Mesh mesh;
  184. public Material[] materials = new Material[1];
  185. public UInt64 layers = 1;
  186. }
  187. }
  188. /** @} */
  189. }