2017-12-20 01:02:03 +01:00
|
|
|
/*************************************************************************/
|
2019-10-03 22:39:08 +02:00
|
|
|
/* voxelizer.h */
|
2017-12-20 01:02:03 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-12-20 01:02:03 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#ifndef VOXELIZER_H
|
|
|
|
#define VOXELIZER_H
|
2017-12-14 12:59:46 +01:00
|
|
|
|
|
|
|
#include "scene/resources/multimesh.h"
|
|
|
|
|
2019-10-03 22:39:08 +02:00
|
|
|
class Voxelizer {
|
2017-12-14 12:59:46 +01:00
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
CHILD_EMPTY = 0xFFFFFFFF
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Cell {
|
2018-01-18 21:37:17 +01:00
|
|
|
uint32_t children[8];
|
2021-02-07 22:29:31 +01:00
|
|
|
float albedo[3] = {}; //albedo in RGB24
|
|
|
|
float emission[3] = {}; //accumulated light in 16:16 fixed point (needs to be integer for moving lights fast)
|
|
|
|
float normal[3] = {};
|
|
|
|
uint32_t used_sides = 0;
|
|
|
|
float alpha = 0.0; //used for upsampling
|
|
|
|
uint16_t x = 0;
|
|
|
|
uint16_t y = 0;
|
|
|
|
uint16_t z = 0;
|
|
|
|
uint16_t level = 0;
|
2017-12-14 12:59:46 +01:00
|
|
|
|
|
|
|
Cell() {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2018-01-18 21:37:17 +01:00
|
|
|
children[i] = CHILD_EMPTY;
|
2017-12-14 12:59:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Cell> bake_cells;
|
2021-02-07 22:29:31 +01:00
|
|
|
int cell_subdiv = 0;
|
2017-12-14 12:59:46 +01:00
|
|
|
|
2019-10-03 22:39:08 +02:00
|
|
|
struct CellSort {
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint64_t z : 16;
|
|
|
|
uint64_t y : 16;
|
|
|
|
uint64_t x : 16;
|
|
|
|
uint64_t level : 16;
|
|
|
|
};
|
2021-02-07 22:29:31 +01:00
|
|
|
uint64_t key = 0;
|
2019-10-03 22:39:08 +02:00
|
|
|
};
|
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
int32_t index = 0;
|
2019-10-03 22:39:08 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator<(const CellSort &p_cell_sort) const {
|
|
|
|
return key < p_cell_sort.key;
|
2018-10-02 14:59:25 +02:00
|
|
|
}
|
2017-12-14 12:59:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MaterialCache {
|
|
|
|
//128x128 textures
|
|
|
|
Vector<Color> albedo;
|
|
|
|
Vector<Color> emission;
|
|
|
|
};
|
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<Ref<Material>, MaterialCache> material_cache;
|
2022-08-01 01:20:24 +02:00
|
|
|
float exposure_normalization = 1.0;
|
2017-12-14 12:59:46 +01:00
|
|
|
AABB original_bounds;
|
|
|
|
AABB po2_bounds;
|
2021-02-07 22:29:31 +01:00
|
|
|
int axis_cell_size[3] = {};
|
2017-12-14 12:59:46 +01:00
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D to_cell_space;
|
2017-12-14 12:59:46 +01:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
int color_scan_cell_width = 4;
|
|
|
|
int bake_texture_size = 128;
|
|
|
|
float cell_size = 0.0;
|
2017-12-14 12:59:46 +01:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
int max_original_cells = 0;
|
|
|
|
int leaf_voxel_count = 0;
|
2017-12-14 12:59:46 +01:00
|
|
|
|
|
|
|
Vector<Color> _get_bake_texture(Ref<Image> p_image, const Color &p_color_mul, const Color &p_color_add);
|
|
|
|
MaterialCache _get_material_cache(Ref<Material> p_material);
|
2017-12-19 19:04:24 +01:00
|
|
|
|
|
|
|
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);
|
2017-12-14 12:59:46 +01:00
|
|
|
void _fixup_plot(int p_idx, int p_level);
|
2019-10-03 22:39:08 +02:00
|
|
|
void _debug_mesh(int p_idx, int p_level, const AABB &p_aabb, Ref<MultiMesh> &p_multimesh, int &idx);
|
2017-12-14 12:59:46 +01:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
bool sorted = false;
|
2019-10-03 22:39:08 +02:00
|
|
|
void _sort();
|
2017-12-14 12:59:46 +01:00
|
|
|
|
|
|
|
public:
|
2022-08-01 01:20:24 +02:00
|
|
|
void begin_bake(int p_subdiv, const AABB &p_bounds, float p_exposure_normalization);
|
2020-10-17 07:08:21 +02:00
|
|
|
void plot_mesh(const Transform3D &p_xform, Ref<Mesh> &p_mesh, const Vector<Ref<Material>> &p_materials, const Ref<Material> &p_override_material);
|
2017-12-14 12:59:46 +01:00
|
|
|
void end_bake();
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
int get_voxel_gi_octree_depth() const;
|
|
|
|
Vector3i get_voxel_gi_octree_size() const;
|
|
|
|
int get_voxel_gi_cell_count() const;
|
|
|
|
Vector<uint8_t> get_voxel_gi_octree_cells() const;
|
|
|
|
Vector<uint8_t> get_voxel_gi_data_cells() const;
|
|
|
|
Vector<int> get_voxel_gi_level_cell_count() const;
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> get_sdf_3d_image() const;
|
2017-12-14 12:59:46 +01:00
|
|
|
|
2019-10-03 22:39:08 +02:00
|
|
|
Ref<MultiMesh> create_debug_multimesh();
|
2017-12-14 12:59:46 +01:00
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D get_to_cell_space_xform() const;
|
2019-10-03 22:39:08 +02:00
|
|
|
Voxelizer();
|
2017-12-14 12:59:46 +01:00
|
|
|
};
|
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#endif // VOXELIZER_H
|