occluder_instance_3d.h 6.8 KB

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