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