Debian Codegraph @

scene/3d/occluder_instance_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* occluder_instance_3d.cpp */
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 #include "occluder_instance_3d.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/marshalls.h"
35 #include "core/math/geometry_2d.h"
36 #include "core/math/triangulate.h"
37 #include "scene/3d/importer_mesh_instance_3d.h"
38 #include "scene/3d/mesh_instance_3d.h"
39 #include "scene/resources/3d/importer_mesh.h"
40 #include "scene/resources/surface_tool.h"
41
42 #ifdef TOOLS_ENABLED
43 #include "editor/editor_node.h"
44 #endif
45
46 RID Occluder3D::get_rid() const {
47 return occluder;
48 }
49
50 void Occluder3D::_update() {
51 _update_arrays(vertices, indices);
52
53 aabb = AABB();
54
55 const Vector3 *ptr = vertices.ptr();
56 for (int i = 0; i < vertices.size(); i++) {
57 aabb.expand_to(ptr[i]);
58 }
59
60 debug_lines.clear();
61 debug_mesh.unref();
62
63 RS::get_singleton()->occluder_set_mesh(occluder, vertices, indices);
64 emit_changed();
65 }
66
67 PackedVector3Array Occluder3D::get_vertices() const {
68 return vertices;
69 }
70
71 PackedInt32Array Occluder3D::get_indices() const {
72 return indices;
73 }
74
75 Vector<Vector3> Occluder3D::get_debug_lines() const {
76 if (!debug_lines.is_empty()) {
77 return debug_lines;
78 }
79
80 if (indices.size() % 3 != 0) {
81 return Vector<Vector3>();
82 }
83
84 const Vector3 *vertices_ptr = vertices.ptr();
85 debug_lines.resize(indices.size() / 3 * 6);
86 Vector3 *line_ptr = debug_lines.ptrw();
87 int line_i = 0;
88 for (int i = 0; i < indices.size() / 3; i++) {
89 for (int j = 0; j < 3; j++) {
90 int a = indices[i * 3 + j];
91 int b = indices[i * 3 + (j + 1) % 3];
92 ERR_FAIL_INDEX_V_MSG(a, vertices.size(), Vector<Vector3>(), "Occluder indices are out of range.");
93 ERR_FAIL_INDEX_V_MSG(b, vertices.size(), Vector<Vector3>(), "Occluder indices are out of range.");
94 line_ptr[line_i++] = vertices_ptr[a];
95 line_ptr[line_i++] = vertices_ptr[b];
96 }
97 }
98 return debug_lines;
99 }
100
101 Ref<ArrayMesh> Occluder3D::get_debug_mesh() const {
102 if (debug_mesh.is_valid()) {
103 return debug_mesh;
104 }
105
106 if (vertices.is_empty() || indices.is_empty() || indices.size() % 3 != 0) {
107 return debug_mesh;
108 }
109
110 Array arrays;
111 arrays.resize(Mesh::ARRAY_MAX);
112 arrays[Mesh::ARRAY_VERTEX] = vertices;
113 arrays[Mesh::ARRAY_INDEX] = indices;
114
115 debug_mesh.instantiate();
116 debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
117 return debug_mesh;
118 }
119
120 AABB Occluder3D::get_aabb() const {
121 return aabb;
122 }
123
124 void Occluder3D::_notification(int p_what) {
125 switch (p_what) {
126 case NOTIFICATION_POSTINITIALIZE: {
127 _update();
128 } break;
129 }
130 }
131
132 Occluder3D::Occluder3D() {
133 occluder = RS::get_singleton()->occluder_create();
134 }
135
136 Occluder3D::~Occluder3D() {
137 if (occluder.is_valid()) {
138 ERR_FAIL_NULL(RenderingServer::get_singleton());
139 RS::get_singleton()->free_rid(occluder);
140 }
141 }
142
143 /////////////////////////////////////////////////
144
145 void ArrayOccluder3D::set_arrays(PackedVector3Array p_vertices, PackedInt32Array p_indices) {
146 vertices = p_vertices;
147 indices = p_indices;
148 _update();
149 }
150
151 void ArrayOccluder3D::set_vertices(PackedVector3Array p_vertices) {
152 vertices = p_vertices;
153 _update();
154 }
155
156 void ArrayOccluder3D::set_indices(PackedInt32Array p_indices) {
157 indices = p_indices;
158 _update();
159 }
160
161 void ArrayOccluder3D::_update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
162 r_vertices = vertices;
163 r_indices = indices;
164 }
165
166 void ArrayOccluder3D::_bind_methods() {
167 ClassDB::bind_method(D_METHOD("set_arrays", "vertices", "indices"), &ArrayOccluder3D::set_arrays);
168
169 ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &ArrayOccluder3D::set_vertices);
170 ClassDB::bind_method(D_METHOD("get_vertices"), &ArrayOccluder3D::get_vertices);
171
172 ClassDB::bind_method(D_METHOD("set_indices", "indices"), &ArrayOccluder3D::set_indices);
173 ClassDB::bind_method(D_METHOD("get_indices"), &ArrayOccluder3D::get_indices);
174
175 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_vertices", "get_vertices");
176 ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "indices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_indices", "get_indices");
177 }
178
179 ArrayOccluder3D::ArrayOccluder3D() {
180 }
181
182 ArrayOccluder3D::~ArrayOccluder3D() {
183 }
184
185 /////////////////////////////////////////////////
186
187 void QuadOccluder3D::set_size(const Size2 &p_size) {
188 if (size == p_size) {
189 return;
190 }
191
192 size = p_size.maxf(0);
193 _update();
194 }
195
196 Size2 QuadOccluder3D::get_size() const {
197 return size;
198 }
199
200 void QuadOccluder3D::_update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
201 Size2 _size = Size2(size.x / 2.0f, size.y / 2.0f);
202
203 r_vertices = {
204 Vector3(-_size.x, -_size.y, 0),
205 Vector3(-_size.x, _size.y, 0),
206 Vector3(_size.x, _size.y, 0),
207 Vector3(_size.x, -_size.y, 0),
208 };
209
210 r_indices = {
211 0, 1, 2,
212 0, 2, 3
213 };
214 }
215
216 void QuadOccluder3D::_bind_methods() {
217 ClassDB::bind_method(D_METHOD("set_size", "size"), &QuadOccluder3D::set_size);
218 ClassDB::bind_method(D_METHOD("get_size"), &QuadOccluder3D::get_size);
219
220 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
221 }
222
223 QuadOccluder3D::QuadOccluder3D() {
224 }
225
226 QuadOccluder3D::~QuadOccluder3D() {
227 }
228
229 /////////////////////////////////////////////////
230
231 void BoxOccluder3D::set_size(const Vector3 &p_size) {
232 if (size == p_size) {
233 return;
234 }
235
236 size = p_size.maxf(0);
237 _update();
238 }
239
240 Vector3 BoxOccluder3D::get_size() const {
241 return size;
242 }
243
244 void BoxOccluder3D::_update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
245 Vector3 _size = Vector3(size.x / 2.0f, size.y / 2.0f, size.z / 2.0f);
246
247 r_vertices = {
248 // front
249 Vector3(-_size.x, -_size.y, _size.z),
250 Vector3(_size.x, -_size.y, _size.z),
251 Vector3(_size.x, _size.y, _size.z),
252 Vector3(-_size.x, _size.y, _size.z),
253 // back
254 Vector3(-_size.x, -_size.y, -_size.z),
255 Vector3(_size.x, -_size.y, -_size.z),
256 Vector3(_size.x, _size.y, -_size.z),
257 Vector3(-_size.x, _size.y, -_size.z),
258 };
259
260 r_indices = {
261 // front
262 0, 1, 2,
263 2, 3, 0,
264 // right
265 1, 5, 6,
266 6, 2, 1,
267 // back
268 7, 6, 5,
269 5, 4, 7,
270 // left
271 4, 0, 3,
272 3, 7, 4,
273 // bottom
274 4, 5, 1,
275 1, 0, 4,
276 // top
277 3, 2, 6,
278 6, 7, 3
279 };
280 }
281
282 void BoxOccluder3D::_bind_methods() {
283 ClassDB::bind_method(D_METHOD("set_size", "size"), &BoxOccluder3D::set_size);
284 ClassDB::bind_method(D_METHOD("get_size"), &BoxOccluder3D::get_size);
285
286 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
287 }
288
289 BoxOccluder3D::BoxOccluder3D() {
290 }
291
292 BoxOccluder3D::~BoxOccluder3D() {
293 }
294
295 /////////////////////////////////////////////////
296
297 void SphereOccluder3D::set_radius(float p_radius) {
298 if (radius == p_radius) {
299 return;
300 }
301
302 radius = MAX(p_radius, 0.0f);
303 _update();
304 }
305
306 float SphereOccluder3D::get_radius() const {
307 return radius;
308 }
309
310 void SphereOccluder3D::_update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
311 r_vertices.resize((RINGS + 2) * (RADIAL_SEGMENTS + 1));
312 int vertex_i = 0;
313 Vector3 *vertex_ptr = r_vertices.ptrw();
314
315 r_indices.resize((RINGS + 1) * RADIAL_SEGMENTS * 6);
316 int idx_i = 0;
317 int *idx_ptr = r_indices.ptrw();
318
319 int current_row = 0;
320 int previous_row = 0;
321 int point = 0;
322 for (int j = 0; j <= (RINGS + 1); j++) {
323 float v = j / float(RINGS + 1);
324 float w = Math::sin(Math::PI * v);
325 float y = Math::cos(Math::PI * v);
326 for (int i = 0; i <= RADIAL_SEGMENTS; i++) {
327 float u = i / float(RADIAL_SEGMENTS);
328
329 float x = Math::cos(u * Math::TAU);
330 float z = Math::sin(u * Math::TAU);
331 vertex_ptr[vertex_i++] = Vector3(x * w, y, z * w) * radius;
332
333 if (i > 0 && j > 0) {
334 idx_ptr[idx_i++] = previous_row + i - 1;
335 idx_ptr[idx_i++] = previous_row + i;
336 idx_ptr[idx_i++] = current_row + i - 1;
337
338 idx_ptr[idx_i++] = previous_row + i;
339 idx_ptr[idx_i++] = current_row + i;
340 idx_ptr[idx_i++] = current_row + i - 1;
341 }
342
343 point++;
344 }
345
346 previous_row = current_row;
347 current_row = point;
348 }
349 }
350
351 void SphereOccluder3D::_bind_methods() {
352 ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SphereOccluder3D::set_radius);
353 ClassDB::bind_method(D_METHOD("get_radius"), &SphereOccluder3D::get_radius);
354
355 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_NONE, "suffix:m"), "set_radius", "get_radius");
356 }
357
358 SphereOccluder3D::SphereOccluder3D() {
359 }
360
361 SphereOccluder3D::~SphereOccluder3D() {
362 }
363
364 /////////////////////////////////////////////////
365
366 void PolygonOccluder3D::set_polygon(const Vector<Vector2> &p_polygon) {
367 polygon = p_polygon;
368 _update();
369 }
370
371 Vector<Vector2> PolygonOccluder3D::get_polygon() const {
372 return polygon;
373 }
374
375 void PolygonOccluder3D::_update_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
376 if (polygon.size() < 3) {
377 r_vertices.clear();
378 r_indices.clear();
379 return;
380 }
381
382 Vector<Point2> occluder_polygon = polygon;
383 if (Triangulate::get_area(occluder_polygon) > 0) {
384 occluder_polygon.reverse();
385 }
386
387 Vector<int> occluder_indices = Geometry2D::triangulate_polygon(occluder_polygon);
388
389 if (occluder_indices.size() < 3) {
390 r_vertices.clear();
391 r_indices.clear();
392 ERR_FAIL_MSG("Failed to triangulate PolygonOccluder3D. Make sure the polygon doesn't have any intersecting edges.");
393 }
394
395 r_vertices.resize(occluder_polygon.size());
396 Vector3 *vertex_ptr = r_vertices.ptrw();
397 const Vector2 *polygon_ptr = occluder_polygon.ptr();
398 for (int i = 0; i < occluder_polygon.size(); i++) {
399 vertex_ptr[i] = Vector3(polygon_ptr[i].x, polygon_ptr[i].y, 0.0);
400 }
401
402 r_indices.resize(occluder_indices.size());
403 memcpy(r_indices.ptrw(), occluder_indices.ptr(), occluder_indices.size() * sizeof(int));
404 }
405
406 bool PolygonOccluder3D::_has_editable_3d_polygon_no_depth() const {
407 return false;
408 }
409
410 void PolygonOccluder3D::_bind_methods() {
411 ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &PolygonOccluder3D::set_polygon);
412 ClassDB::bind_method(D_METHOD("get_polygon"), &PolygonOccluder3D::get_polygon);
413
414 ClassDB::bind_method(D_METHOD("_has_editable_3d_polygon_no_depth"), &PolygonOccluder3D::_has_editable_3d_polygon_no_depth);
415
416 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
417 }
418
419 PolygonOccluder3D::PolygonOccluder3D() {
420 }
421
422 PolygonOccluder3D::~PolygonOccluder3D() {
423 }
424
425 /////////////////////////////////////////////////
426
427 AABB OccluderInstance3D::get_aabb() const {
428 if (occluder.is_valid()) {
429 return occluder->get_aabb();
430 }
431 return AABB();
432 }
433
434 void OccluderInstance3D::set_occluder(const Ref<Occluder3D> &p_occluder) {
435 if (occluder == p_occluder) {
436 return;
437 }
438
439 if (occluder.is_valid()) {
440 occluder->disconnect_changed(callable_mp(this, &OccluderInstance3D::_occluder_changed));
441 }
442
443 occluder = p_occluder;
444
445 if (occluder.is_valid()) {
446 set_base(occluder->get_rid());
447 occluder->connect_changed(callable_mp(this, &OccluderInstance3D::_occluder_changed));
448 } else {
449 set_base(RID());
450 }
451
452 update_gizmos();
453 update_configuration_warnings();
454
455 #ifdef TOOLS_ENABLED
456 // PolygonOccluder3D is edited via an editor plugin, this ensures the plugin is shown/hidden when necessary
457 if (Engine::get_singleton()->is_editor_hint()) {
458 callable_mp(EditorNode::get_singleton(), &EditorNode::edit_current).call_deferred();
459 }
460 #endif
461 }
462
463 void OccluderInstance3D::_occluder_changed() {
464 update_gizmos();
465 update_configuration_warnings();
466 }
467
468 Ref<Occluder3D> OccluderInstance3D::get_occluder() const {
469 return occluder;
470 }
471
472 void OccluderInstance3D::set_bake_mask(uint32_t p_mask) {
473 bake_mask = p_mask;
474 update_configuration_warnings();
475 }
476
477 uint32_t OccluderInstance3D::get_bake_mask() const {
478 return bake_mask;
479 }
480
481 void OccluderInstance3D::set_bake_simplification_distance(float p_dist) {
482 bake_simplification_dist = MAX(p_dist, 0.0f);
483 }
484
485 float OccluderInstance3D::get_bake_simplification_distance() const {
486 return bake_simplification_dist;
487 }
488
489 void OccluderInstance3D::set_bake_mask_value(int p_layer_number, bool p_value) {
490 ERR_FAIL_COND_MSG(p_layer_number < 1, "Render layer number must be between 1 and 20 inclusive.");
491 ERR_FAIL_COND_MSG(p_layer_number > 20, "Render layer number must be between 1 and 20 inclusive.");
492 uint32_t mask = get_bake_mask();
493 if (p_value) {
494 mask |= 1 << (p_layer_number - 1);
495 } else {
496 mask &= ~(1 << (p_layer_number - 1));
497 }
498 set_bake_mask(mask);
499 }
500
501 bool OccluderInstance3D::get_bake_mask_value(int p_layer_number) const {
502 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Render layer number must be between 1 and 20 inclusive.");
503 ERR_FAIL_COND_V_MSG(p_layer_number > 20, false, "Render layer number must be between 1 and 20 inclusive.");
504 return bake_mask & (1 << (p_layer_number - 1));
505 }
506
507 bool OccluderInstance3D::_bake_material_check(Ref<Material> p_material) {
508 StandardMaterial3D *standard_mat = Object::cast_to<StandardMaterial3D>(p_material.ptr());
509 if (standard_mat && standard_mat->get_transparency() != StandardMaterial3D::TRANSPARENCY_DISABLED) {
510 return false;
511 }
512 return true;
513 }
514
515 void OccluderInstance3D::_bake_surface(const Transform3D &p_transform, Array p_surface_arrays, Ref<Material> p_material, float p_simplification_dist, PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
516 if (!_bake_material_check(p_material)) {
517 return;
518 }
519 ERR_FAIL_COND_MSG(p_surface_arrays.size() != Mesh::ARRAY_MAX, "Invalid surface array.");
520
521 PackedVector3Array vertices = p_surface_arrays[Mesh::ARRAY_VERTEX];
522 PackedInt32Array indices = p_surface_arrays[Mesh::ARRAY_INDEX];
523
524 if (vertices.is_empty() || indices.is_empty()) {
525 return;
526 }
527
528 Vector3 *vertices_ptr = vertices.ptrw();
529 for (int j = 0; j < vertices.size(); j++) {
530 vertices_ptr[j] = p_transform.xform(vertices_ptr[j]);
531 }
532
533 if (!Math::is_zero_approx(p_simplification_dist) && SurfaceTool::simplify_func) {
534 Vector<float> vertices_f32 = vector3_to_float32_array(vertices.ptr(), vertices.size());
535
536 float error_scale = SurfaceTool::simplify_scale_func(vertices_f32.ptr(), vertices.size(), sizeof(float) * 3);
537 float target_error = p_simplification_dist / error_scale;
538 float error = -1.0f;
539 int target_index_count = MIN(indices.size(), 36);
540
541 const int simplify_options = SurfaceTool::SIMPLIFY_LOCK_BORDER;
542
543 uint32_t index_count = SurfaceTool::simplify_func(
544 (unsigned int *)indices.ptrw(),
545 (unsigned int *)indices.ptr(),
546 indices.size(),
547 vertices_f32.ptr(), vertices.size(), sizeof(float) * 3,
548 target_index_count, target_error, simplify_options, &error);
549 indices.resize(index_count);
550 }
551
552 SurfaceTool::strip_mesh_arrays(vertices, indices);
553
554 int vertex_offset = r_vertices.size();
555 r_vertices.resize(vertex_offset + vertices.size());
556 memcpy(r_vertices.ptrw() + vertex_offset, vertices.ptr(), vertices.size() * sizeof(Vector3));
557
558 int index_offset = r_indices.size();
559 r_indices.resize(index_offset + indices.size());
560 int *idx_ptr = r_indices.ptrw();
561 for (int j = 0; j < indices.size(); j++) {
562 idx_ptr[index_offset + j] = vertex_offset + indices[j];
563 }
564 }
565
566 void OccluderInstance3D::_bake_node(Node *p_node, PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
567 MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node);
568 if (mi && mi->is_visible_in_tree()) {
569 Ref<Mesh> mesh = mi->get_mesh();
570 bool valid = true;
571
572 if (mesh.is_null()) {
573 valid = false;
574 }
575
576 if (valid && !_bake_material_check(mi->get_material_override())) {
577 valid = false;
578 }
579
580 if ((mi->get_layer_mask() & bake_mask) == 0) {
581 valid = false;
582 }
583
584 if (valid) {
585 Transform3D global_to_local = get_global_transform().affine_inverse() * mi->get_global_transform();
586
587 for (int i = 0; i < mesh->get_surface_count(); i++) {
588 _bake_surface(global_to_local, mesh->surface_get_arrays(i), mi->get_active_material(i), bake_simplification_dist, r_vertices, r_indices);
589 }
590 }
591 }
592
593 for (int i = 0; i < p_node->get_child_count(); i++) {
594 Node *child = p_node->get_child(i);
595 if (!child->get_owner()) {
596 continue; // may be a helper
597 }
598
599 _bake_node(child, r_vertices, r_indices);
600 }
601 }
602
603 void OccluderInstance3D::bake_single_node(const Node3D *p_node, float p_simplification_distance, PackedVector3Array &r_vertices, PackedInt32Array &r_indices) {
604 ERR_FAIL_NULL(p_node);
605
606 Transform3D xform = p_node->is_inside_tree() ? p_node->get_global_transform() : p_node->get_transform();
607
608 const MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node);
609 if (mi) {
610 Ref<Mesh> mesh = mi->get_mesh();
611 bool valid = true;
612
613 if (mesh.is_null()) {
614 valid = false;
615 }
616
617 if (valid && !_bake_material_check(mi->get_material_override())) {
618 valid = false;
619 }
620
621 if (valid) {
622 for (int i = 0; i < mesh->get_surface_count(); i++) {
623 _bake_surface(xform, mesh->surface_get_arrays(i), mi->get_active_material(i), p_simplification_distance, r_vertices, r_indices);
624 }
625 }
626 }
627
628 const ImporterMeshInstance3D *imi = Object::cast_to<ImporterMeshInstance3D>(p_node);
629 if (imi) {
630 Ref<ImporterMesh> mesh = imi->get_mesh();
631 bool valid = true;
632
633 if (mesh.is_null()) {
634 valid = false;
635 }
636
637 if (valid) {
638 for (int i = 0; i < mesh->get_surface_count(); i++) {
639 Ref<Material> material = imi->get_surface_material(i);
640 if (material.is_null()) {
641 material = mesh->get_surface_material(i);
642 }
643 _bake_surface(xform, mesh->get_surface_arrays(i), material, p_simplification_distance, r_vertices, r_indices);
644 }
645 }
646 }
647 }
648
649 OccluderInstance3D::BakeError OccluderInstance3D::bake_scene(Node *p_from_node, String p_occluder_path) {
650 if (p_occluder_path.is_empty()) {
651 if (get_occluder().is_null()) {
652 return BAKE_ERROR_NO_SAVE_PATH;
653 }
654 p_occluder_path = get_occluder()->get_path();
655 if (!p_occluder_path.is_resource_file()) {
656 return BAKE_ERROR_NO_SAVE_PATH;
657 }
658 }
659
660 PackedVector3Array vertices;
661 PackedInt32Array indices;
662
663 _bake_node(p_from_node, vertices, indices);
664
665 if (vertices.is_empty() || indices.is_empty()) {
666 return BAKE_ERROR_NO_MESHES;
667 }
668
669 Ref<ArrayOccluder3D> occ;
670 if (get_occluder().is_valid()) {
671 occ = get_occluder();
672 set_occluder(Ref<Occluder3D>()); // clear
673 }
674
675 if (occ.is_null()) {
676 occ.instantiate();
677 }
678
679 occ->set_arrays(vertices, indices);
680
681 Error err = ResourceSaver::save(occ, p_occluder_path);
682
683 if (err != OK) {
684 return BAKE_ERROR_CANT_SAVE;
685 }
686
687 occ->set_path(p_occluder_path);
688 set_occluder(occ);
689
690 return BAKE_ERROR_OK;
691 }
692
693 PackedStringArray OccluderInstance3D::get_configuration_warnings() const {
694 PackedStringArray warnings = VisualInstance3D::get_configuration_warnings();
695
696 if (!bool(GLOBAL_GET_CACHED(bool, "rendering/occlusion_culling/use_occlusion_culling"))) {
697 warnings.push_back(RTR("Occlusion culling is disabled in the Project Settings, which means occlusion culling won't be performed in the root viewport.\nTo resolve this, open the Project Settings and enable Rendering > Occlusion Culling > Use Occlusion Culling."));
698 }
699
700 if (bake_mask == 0) {
701 warnings.push_back(RTR("The Bake Mask has no bits enabled, which means baking will not produce any occluder meshes for this OccluderInstance3D.\nTo resolve this, enable at least one bit in the Bake Mask property."));
702 }
703
704 if (occluder.is_null()) {
705 warnings.push_back(RTR("No occluder mesh is defined in the Occluder property, so no occlusion culling will be performed using this OccluderInstance3D.\nTo resolve this, set the Occluder property to one of the primitive occluder types or bake the scene meshes by selecting the OccluderInstance3D and pressing the Bake Occluders button at the top of the 3D editor viewport."));
706 } else {
707 Ref<ArrayOccluder3D> arr_occluder = occluder;
708 if (arr_occluder.is_valid() && arr_occluder->get_indices().size() < 3) {
709 // Setting a new ArrayOccluder3D from the inspector will create an empty occluder,
710 // so warn the user about this.
711 warnings.push_back(RTR("The occluder mesh has less than 3 vertices, so no occlusion culling will be performed using this OccluderInstance3D.\nTo generate a proper occluder mesh, select the OccluderInstance3D then use the Bake Occluders button at the top of the 3D editor viewport."));
712 }
713 Ref<PolygonOccluder3D> poly_occluder = occluder;
714 if (poly_occluder.is_valid() && poly_occluder->get_polygon().size() < 3) {
715 warnings.push_back(RTR("The polygon occluder has less than 3 vertices, so no occlusion culling will be performed using this OccluderInstance3D.\nVertices can be added in the inspector or using the polygon editing tools at the top of the 3D editor viewport."));
716 }
717 }
718
719 return warnings;
720 }
721
722 bool OccluderInstance3D::_is_editable_3d_polygon() const {
723 return Ref<PolygonOccluder3D>(occluder).is_valid();
724 }
725
726 Ref<Resource> OccluderInstance3D::_get_editable_3d_polygon_resource() const {
727 return occluder;
728 }
729
730 void OccluderInstance3D::_bind_methods() {
731 ClassDB::bind_method(D_METHOD("set_bake_mask", "mask"), &OccluderInstance3D::set_bake_mask);
732 ClassDB::bind_method(D_METHOD("get_bake_mask"), &OccluderInstance3D::get_bake_mask);
733 ClassDB::bind_method(D_METHOD("set_bake_mask_value", "layer_number", "value"), &OccluderInstance3D::set_bake_mask_value);
734 ClassDB::bind_method(D_METHOD("get_bake_mask_value", "layer_number"), &OccluderInstance3D::get_bake_mask_value);
735 ClassDB::bind_method(D_METHOD("set_bake_simplification_distance", "simplification_distance"), &OccluderInstance3D::set_bake_simplification_distance);
736 ClassDB::bind_method(D_METHOD("get_bake_simplification_distance"), &OccluderInstance3D::get_bake_simplification_distance);
737
738 ClassDB::bind_method(D_METHOD("set_occluder", "occluder"), &OccluderInstance3D::set_occluder);
739 ClassDB::bind_method(D_METHOD("get_occluder"), &OccluderInstance3D::get_occluder);
740
741 ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &OccluderInstance3D::_is_editable_3d_polygon);
742 ClassDB::bind_method(D_METHOD("_get_editable_3d_polygon_resource"), &OccluderInstance3D::_get_editable_3d_polygon_resource);
743
744 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "Occluder3D"), "set_occluder", "get_occluder");
745 ADD_GROUP("Bake", "bake_");
746 ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_bake_mask", "get_bake_mask");
747 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_simplification_distance", PROPERTY_HINT_RANGE, "0.0,2.0,0.01,suffix:m"), "set_bake_simplification_distance", "get_bake_simplification_distance");
748 }
749
750 OccluderInstance3D::OccluderInstance3D() {
751 }
752
753 OccluderInstance3D::~OccluderInstance3D() {
754 }