| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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] = {}; |
| 53 |
float emission[3] = {}; |
| 54 |
float normal[3] = {}; |
| 55 |
float alpha = 0.0; |
| 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 |
|
| 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 |
}; |