occluder_instance_3d.h 6.7 KB

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