occluder_instance_3d.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**************************************************************************/
  2. /* occluder_instance_3d.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 OCCLUDER_INSTANCE_3D_H
  31. #define OCCLUDER_INSTANCE_3D_H
  32. #include "scene/3d/visual_instance_3d.h"
  33. class Occluder3D : public Resource {
  34. GDCLASS(Occluder3D, Resource);
  35. RES_BASE_EXTENSION("occ");
  36. RID occluder;
  37. PackedVector3Array vertices;
  38. PackedInt32Array indices;
  39. AABB aabb;
  40. mutable Ref<ArrayMesh> debug_mesh;
  41. mutable Vector<Vector3> debug_lines;
  42. protected:
  43. void _update();
  44. virtual void _update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) = 0;
  45. void _notification(int p_what);
  46. public:
  47. PackedVector3Array get_vertices() const;
  48. PackedInt32Array get_indices() const;
  49. Vector<Vector3> get_debug_lines() const;
  50. Ref<ArrayMesh> get_debug_mesh() const;
  51. AABB get_aabb() const;
  52. virtual RID get_rid() const override;
  53. Occluder3D();
  54. virtual ~Occluder3D();
  55. };
  56. class ArrayOccluder3D : public Occluder3D {
  57. GDCLASS(ArrayOccluder3D, Occluder3D);
  58. PackedVector3Array vertices;
  59. PackedInt32Array indices;
  60. protected:
  61. virtual void _update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) override;
  62. static void _bind_methods();
  63. public:
  64. void set_arrays(PackedVector3Array p_vertices, PackedInt32Array p_indices);
  65. void set_vertices(PackedVector3Array p_vertices);
  66. void set_indices(PackedInt32Array p_indices);
  67. ArrayOccluder3D();
  68. ~ArrayOccluder3D();
  69. };
  70. class QuadOccluder3D : public Occluder3D {
  71. GDCLASS(QuadOccluder3D, Occluder3D);
  72. private:
  73. Size2 size = Vector2(1.0f, 1.0f);
  74. protected:
  75. virtual void _update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) override;
  76. static void _bind_methods();
  77. public:
  78. Size2 get_size() const;
  79. void set_size(const Size2 &p_size);
  80. QuadOccluder3D();
  81. ~QuadOccluder3D();
  82. };
  83. class BoxOccluder3D : public Occluder3D {
  84. GDCLASS(BoxOccluder3D, Occluder3D);
  85. private:
  86. Vector3 size = Vector3(1.0f, 1.0f, 1.0f);
  87. protected:
  88. virtual void _update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) override;
  89. static void _bind_methods();
  90. public:
  91. Vector3 get_size() const;
  92. void set_size(const Vector3 &p_size);
  93. BoxOccluder3D();
  94. ~BoxOccluder3D();
  95. };
  96. class SphereOccluder3D : public Occluder3D {
  97. GDCLASS(SphereOccluder3D, Occluder3D);
  98. private:
  99. static constexpr int RINGS = 7;
  100. static constexpr int RADIAL_SEGMENTS = 7;
  101. float radius = 1.0f;
  102. protected:
  103. virtual void _update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) override;
  104. static void _bind_methods();
  105. public:
  106. float get_radius() const;
  107. void set_radius(float p_radius);
  108. SphereOccluder3D();
  109. ~SphereOccluder3D();
  110. };
  111. class PolygonOccluder3D : public Occluder3D {
  112. GDCLASS(PolygonOccluder3D, Occluder3D);
  113. private:
  114. Vector<Vector2> polygon;
  115. bool _has_editable_3d_polygon_no_depth() const;
  116. protected:
  117. virtual void _update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) override;
  118. static void _bind_methods();
  119. public:
  120. void set_polygon(const Vector<Vector2> &p_polygon);
  121. Vector<Vector2> get_polygon() const;
  122. PolygonOccluder3D();
  123. ~PolygonOccluder3D();
  124. };
  125. class OccluderInstance3D : public VisualInstance3D {
  126. GDCLASS(OccluderInstance3D, VisualInstance3D);
  127. private:
  128. Ref<Occluder3D> occluder;
  129. uint32_t bake_mask = 0xFFFFFFFF;
  130. float bake_simplification_dist = 0.1f;
  131. void _occluder_changed();
  132. static bool _bake_material_check(Ref<Material> p_material);
  133. static void _bake_surface(const Transform3D &p_transform, Array p_surface_arrays, Ref<Material> p_material, float p_simplification_dist, PackedVector3Array &r_vertices, PackedInt32Array &r_indices);
  134. void _bake_node(Node *p_node, PackedVector3Array &r_vertices, PackedInt32Array &r_indices);
  135. bool _is_editable_3d_polygon() const;
  136. Ref<Resource> _get_editable_3d_polygon_resource() const;
  137. protected:
  138. static void _bind_methods();
  139. public:
  140. virtual PackedStringArray get_configuration_warnings() const override;
  141. enum BakeError {
  142. BAKE_ERROR_OK,
  143. BAKE_ERROR_NO_SAVE_PATH,
  144. BAKE_ERROR_NO_MESHES,
  145. BAKE_ERROR_CANT_SAVE,
  146. };
  147. void set_occluder(const Ref<Occluder3D> &p_occluder);
  148. Ref<Occluder3D> get_occluder() const;
  149. virtual AABB get_aabb() const override;
  150. void set_bake_mask(uint32_t p_mask);
  151. uint32_t get_bake_mask() const;
  152. void set_bake_simplification_distance(float p_dist);
  153. float get_bake_simplification_distance() const;
  154. void set_bake_mask_value(int p_layer_number, bool p_enable);
  155. bool get_bake_mask_value(int p_layer_number) const;
  156. BakeError bake_scene(Node *p_from_node, String p_occluder_path = "");
  157. static void bake_single_node(const Node3D *p_node, float p_simplification_distance, PackedVector3Array &r_vertices, PackedInt32Array &r_indices);
  158. OccluderInstance3D();
  159. ~OccluderInstance3D();
  160. };
  161. #endif // OCCLUDER_INSTANCE_3D_H