Renderable.cs 5.2 KB

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