Debian Codegraph @

scene/3d/voxelizer.h

raw symbols SCIP
1 /**************************************************************************/
2 /* voxelizer.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 #pragma once
32
33 #include "scene/resources/multimesh.h"
34
35 class Voxelizer {
36 public:
37 enum BakeResult {
38 BAKE_RESULT_OK,
39 BAKE_RESULT_INVALID_PARAMETER,
40 BAKE_RESULT_CANCELLED,
41 };
42
43 typedef bool (*BakeStepFunc)(int, int);
44
45 private:
46 enum : uint32_t {
47 CHILD_EMPTY = 0xFFFFFFFF
48 };
49
50 struct Cell {
51 uint32_t children[8];
52 float albedo[3] = {}; //albedo in RGB24
53 float emission[3] = {}; //accumulated light in 16:16 fixed point (needs to be integer for moving lights fast)
54 float normal[3] = {};
55 float alpha = 0.0; //used for upsampling
56 uint16_t x = 0;
57 uint16_t y = 0;
58 uint16_t z = 0;
59 uint16_t level = 0;
60
61 Cell() {
62 for (int i = 0; i < 8; i++) {
63 children[i] = CHILD_EMPTY;
64 }
65 }
66 };
67
68 Vector<Cell> bake_cells;
69 int cell_subdiv = 0;
70
71 struct CellSort {
72 union {
73 uint64_t key = 0;
74 struct {
75 uint64_t z : 16;
76 uint64_t y : 16;
77 uint64_t x : 16;
78 uint64_t level : 16;
79 };
80 };
81
82 int32_t index = 0;
83
84 _FORCE_INLINE_ bool operator<(const CellSort &p_cell_sort) const {
85 return key < p_cell_sort.key;
86 }
87 };
88
89 struct MaterialCache {
90 //128x128 textures
91 Vector<Color> albedo;
92 Vector<Color> emission;
93 };
94
95 HashMap<Ref<Material>, MaterialCache> material_cache;
96 float exposure_normalization = 1.0;
97 AABB original_bounds;
98 AABB po2_bounds;
99 int axis_cell_size[3] = {};
100
101 Transform3D to_cell_space;
102
103 int color_scan_cell_width = 4;
104 int bake_texture_size = 128;
105 float cell_size = 0.0;
106
107 int max_original_cells = 0;
108 int leaf_voxel_count = 0;
109
110 Vector<Color> _get_bake_texture(Ref<Image> p_image, const Color &p_color_mul, const Color &p_color_add);
111 MaterialCache _get_material_cache(Ref<Material> p_material);
112
113 void _plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, const Vector3 *p_vtx, const Vector3 *p_normal, const Vector2 *p_uv, const MaterialCache &p_material, const AABB &p_aabb);
114 void _fixup_plot(int p_idx, int p_level);
115 void _debug_mesh(int p_idx, int p_level, const AABB &p_aabb, Ref<MultiMesh> &p_multimesh, int &idx);
116
117 bool sorted = false;
118 void _sort();
119
120 public:
121 void begin_bake(int p_subdiv, const AABB &p_bounds, float p_exposure_normalization);
122 int get_bake_steps(Ref<Mesh> &p_mesh) const;
123 BakeResult plot_mesh(const Transform3D &p_xform, Ref<Mesh> &p_mesh, const Vector<Ref<Material>> &p_materials, const Ref<Material> &p_override_material, BakeStepFunc p_bake_step_function);
124 void end_bake();
125
126 int get_voxel_gi_octree_depth() const;
127 Vector3i get_voxel_gi_octree_size() const;
128 int get_voxel_gi_cell_count() const;
129 Vector<uint8_t> get_voxel_gi_octree_cells() const;
130 Vector<uint8_t> get_voxel_gi_data_cells() const;
131 Vector<int> get_voxel_gi_level_cell_count() const;
132 BakeResult get_sdf_3d_image(Vector<uint8_t> &r_image, BakeStepFunc p_bake_step_function) const;
133
134 Ref<MultiMesh> create_debug_multimesh();
135
136 Transform3D get_to_cell_space_xform() const;
137 Voxelizer();
138 };