mesh_storage.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* mesh_storage.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef MESH_STORAGE_H
  31. #define MESH_STORAGE_H
  32. #include "servers/rendering_server.h"
  33. #include "utilities.h"
  34. class RendererMeshStorage {
  35. public:
  36. virtual ~RendererMeshStorage() {}
  37. /* MESH API */
  38. virtual RID mesh_allocate() = 0;
  39. virtual void mesh_initialize(RID p_rid) = 0;
  40. virtual void mesh_free(RID p_rid) = 0;
  41. virtual void mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) = 0;
  42. /// Returns stride
  43. virtual void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) = 0;
  44. virtual int mesh_get_blend_shape_count(RID p_mesh) const = 0;
  45. virtual void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) = 0;
  46. virtual RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const = 0;
  47. virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
  48. virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
  49. virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
  50. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) = 0;
  51. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const = 0;
  52. virtual RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const = 0;
  53. virtual int mesh_get_surface_count(RID p_mesh) const = 0;
  54. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) = 0;
  55. virtual AABB mesh_get_custom_aabb(RID p_mesh) const = 0;
  56. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) = 0;
  57. virtual void mesh_set_path(RID p_mesh, const String &p_path) = 0;
  58. virtual String mesh_get_path(RID p_mesh) const = 0;
  59. virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) = 0;
  60. virtual void mesh_clear(RID p_mesh) = 0;
  61. virtual bool mesh_needs_instance(RID p_mesh, bool p_has_skeleton) = 0;
  62. /* MESH INSTANCE */
  63. virtual RID mesh_instance_create(RID p_base) = 0;
  64. virtual void mesh_instance_free(RID p_rid) = 0;
  65. virtual void mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) = 0;
  66. virtual void mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) = 0;
  67. virtual void mesh_instance_check_for_update(RID p_mesh_instance) = 0;
  68. virtual void mesh_instance_set_canvas_item_transform(RID p_mesh_instance, const Transform2D &p_transform) = 0;
  69. virtual void update_mesh_instances() = 0;
  70. /* MULTIMESH API */
  71. struct MultiMeshInterpolator {
  72. RS::MultimeshTransformFormat _transform_format = RS::MULTIMESH_TRANSFORM_3D;
  73. bool _use_colors = false;
  74. bool _use_custom_data = false;
  75. // The stride of the buffer in floats.
  76. int _stride = 0;
  77. // Vertex format sizes in floats.
  78. int _vf_size_xform = 0;
  79. int _vf_size_color = 0;
  80. int _vf_size_data = 0;
  81. // Set by allocate, can be used to prevent indexing out of range.
  82. int _num_instances = 0;
  83. // Quality determines whether to use lerp or slerp etc.
  84. int quality = 0;
  85. bool interpolated = false;
  86. bool on_interpolate_update_list = false;
  87. bool on_transform_update_list = false;
  88. Vector<float> _data_prev;
  89. Vector<float> _data_curr;
  90. Vector<float> _data_interpolated;
  91. };
  92. virtual RID multimesh_allocate();
  93. virtual void multimesh_initialize(RID p_rid);
  94. virtual void multimesh_free(RID p_rid);
  95. virtual void multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false);
  96. virtual int multimesh_get_instance_count(RID p_multimesh) const;
  97. virtual void multimesh_set_mesh(RID p_multimesh, RID p_mesh);
  98. virtual void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform);
  99. virtual void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform);
  100. virtual void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color);
  101. virtual void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color);
  102. virtual void multimesh_set_custom_aabb(RID p_multimesh, const AABB &p_aabb);
  103. virtual AABB multimesh_get_custom_aabb(RID p_multimesh) const;
  104. virtual RID multimesh_get_mesh(RID p_multimesh) const;
  105. virtual Transform3D multimesh_instance_get_transform(RID p_multimesh, int p_index) const;
  106. virtual Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const;
  107. virtual Color multimesh_instance_get_color(RID p_multimesh, int p_index) const;
  108. virtual Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const;
  109. virtual void multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer);
  110. virtual RID multimesh_get_buffer_rd_rid(RID p_multimesh) const;
  111. virtual Vector<float> multimesh_get_buffer(RID p_multimesh) const;
  112. virtual void multimesh_set_buffer_interpolated(RID p_multimesh, const Vector<float> &p_buffer, const Vector<float> &p_buffer_prev);
  113. virtual void multimesh_set_physics_interpolated(RID p_multimesh, bool p_interpolated);
  114. virtual void multimesh_set_physics_interpolation_quality(RID p_multimesh, RS::MultimeshPhysicsInterpolationQuality p_quality);
  115. virtual void multimesh_instance_reset_physics_interpolation(RID p_multimesh, int p_index);
  116. virtual void multimesh_set_visible_instances(RID p_multimesh, int p_visible);
  117. virtual int multimesh_get_visible_instances(RID p_multimesh) const;
  118. virtual AABB multimesh_get_aabb(RID p_multimesh);
  119. virtual RID _multimesh_allocate() = 0;
  120. virtual void _multimesh_initialize(RID p_rid) = 0;
  121. virtual void _multimesh_free(RID p_rid) = 0;
  122. virtual void _multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false) = 0;
  123. virtual int _multimesh_get_instance_count(RID p_multimesh) const = 0;
  124. virtual void _multimesh_set_mesh(RID p_multimesh, RID p_mesh) = 0;
  125. virtual void _multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) = 0;
  126. virtual void _multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) = 0;
  127. virtual void _multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) = 0;
  128. virtual void _multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) = 0;
  129. virtual void _multimesh_set_custom_aabb(RID p_multimesh, const AABB &p_aabb) = 0;
  130. virtual AABB _multimesh_get_custom_aabb(RID p_multimesh) const = 0;
  131. virtual RID _multimesh_get_mesh(RID p_multimesh) const = 0;
  132. virtual Transform3D _multimesh_instance_get_transform(RID p_multimesh, int p_index) const = 0;
  133. virtual Transform2D _multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const = 0;
  134. virtual Color _multimesh_instance_get_color(RID p_multimesh, int p_index) const = 0;
  135. virtual Color _multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const = 0;
  136. virtual void _multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) = 0;
  137. virtual RID _multimesh_get_buffer_rd_rid(RID p_multimesh) const = 0;
  138. virtual Vector<float> _multimesh_get_buffer(RID p_multimesh) const = 0;
  139. virtual void _multimesh_set_visible_instances(RID p_multimesh, int p_visible) = 0;
  140. virtual int _multimesh_get_visible_instances(RID p_multimesh) const = 0;
  141. virtual AABB _multimesh_get_aabb(RID p_multimesh) = 0;
  142. // Multimesh is responsible for allocating / destroying a MultiMeshInterpolator object.
  143. // This allows shared functionality for interpolation across backends.
  144. virtual MultiMeshInterpolator *_multimesh_get_interpolator(RID p_multimesh) const = 0;
  145. private:
  146. void _multimesh_add_to_interpolation_lists(RID p_multimesh, MultiMeshInterpolator &r_mmi);
  147. public:
  148. /* SKELETON API */
  149. virtual RID skeleton_allocate() = 0;
  150. virtual void skeleton_initialize(RID p_rid) = 0;
  151. virtual void skeleton_free(RID p_rid) = 0;
  152. virtual void skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) = 0;
  153. virtual int skeleton_get_bone_count(RID p_skeleton) const = 0;
  154. virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform) = 0;
  155. virtual Transform3D skeleton_bone_get_transform(RID p_skeleton, int p_bone) const = 0;
  156. virtual void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) = 0;
  157. virtual Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const = 0;
  158. virtual void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) = 0;
  159. virtual void skeleton_update_dependency(RID p_base, DependencyTracker *p_instance) = 0;
  160. /* INTERPOLATION */
  161. struct InterpolationData {
  162. void notify_free_multimesh(RID p_rid);
  163. LocalVector<RID> multimesh_interpolate_update_list;
  164. LocalVector<RID> multimesh_transform_update_lists[2];
  165. LocalVector<RID> *multimesh_transform_update_list_curr = &multimesh_transform_update_lists[0];
  166. LocalVector<RID> *multimesh_transform_update_list_prev = &multimesh_transform_update_lists[1];
  167. } _interpolation_data;
  168. void update_interpolation_tick(bool p_process = true);
  169. void update_interpolation_frame(bool p_process = true);
  170. };
  171. #endif // MESH_STORAGE_H