2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
2021-06-05 00:47:26 +02:00
|
|
|
/* voxel_gi.cpp */
|
2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2017-03-05 15:47:28 +01:00
|
|
|
/*************************************************************************/
|
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-03-05 15:47:28 +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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
#include "voxel_gi.h"
|
2017-03-05 15:47:28 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "mesh_instance_3d.h"
|
2021-08-13 01:05:59 +02:00
|
|
|
#include "multimesh_instance_3d.h"
|
2019-10-03 22:39:08 +02:00
|
|
|
#include "voxelizer.h"
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::_set_data(const Dictionary &p_data) {
|
2019-10-03 22:39:08 +02:00
|
|
|
ERR_FAIL_COND(!p_data.has("bounds"));
|
|
|
|
ERR_FAIL_COND(!p_data.has("octree_size"));
|
|
|
|
ERR_FAIL_COND(!p_data.has("octree_cells"));
|
|
|
|
ERR_FAIL_COND(!p_data.has("octree_data"));
|
2019-10-31 23:54:21 +01:00
|
|
|
ERR_FAIL_COND(!p_data.has("octree_df") && !p_data.has("octree_df_png"));
|
2019-10-03 22:39:08 +02:00
|
|
|
ERR_FAIL_COND(!p_data.has("level_counts"));
|
|
|
|
ERR_FAIL_COND(!p_data.has("to_cell_xform"));
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2019-10-03 22:39:08 +02:00
|
|
|
AABB bounds = p_data["bounds"];
|
|
|
|
Vector3 octree_size = p_data["octree_size"];
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> octree_cells = p_data["octree_cells"];
|
|
|
|
Vector<uint8_t> octree_data = p_data["octree_data"];
|
2019-10-31 23:54:21 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> octree_df;
|
2019-10-31 23:54:21 +01:00
|
|
|
if (p_data.has("octree_df")) {
|
|
|
|
octree_df = p_data["octree_df"];
|
|
|
|
} else if (p_data.has("octree_df_png")) {
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> octree_df_png = p_data["octree_df_png"];
|
2019-10-31 23:54:21 +01:00
|
|
|
Ref<Image> img;
|
2021-06-18 00:03:09 +02:00
|
|
|
img.instantiate();
|
2019-10-31 23:54:21 +01:00
|
|
|
Error err = img->load_png_from_buffer(octree_df_png);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
ERR_FAIL_COND(img->get_format() != Image::FORMAT_L8);
|
|
|
|
octree_df = img->get_data();
|
|
|
|
}
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<int> octree_levels = p_data["level_counts"];
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D to_cell_xform = p_data["to_cell_xform"];
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2019-10-13 02:24:03 +02:00
|
|
|
allocate(to_cell_xform, bounds, octree_size, octree_cells, octree_data, octree_df, octree_levels);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Dictionary VoxelGIData::_get_data() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
Dictionary d;
|
|
|
|
d["bounds"] = get_bounds();
|
2019-10-31 23:54:21 +01:00
|
|
|
Vector3i otsize = get_octree_size();
|
|
|
|
d["octree_size"] = Vector3(otsize);
|
2019-10-03 22:39:08 +02:00
|
|
|
d["octree_cells"] = get_octree_cells();
|
|
|
|
d["octree_data"] = get_data_cells();
|
2019-10-31 23:54:21 +01:00
|
|
|
if (otsize != Vector3i()) {
|
|
|
|
Ref<Image> img;
|
2021-06-18 00:03:09 +02:00
|
|
|
img.instantiate();
|
2019-10-31 23:54:21 +01:00
|
|
|
img->create(otsize.x * otsize.y, otsize.z, false, Image::FORMAT_L8, get_distance_field());
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> df_png = img->save_png_to_buffer();
|
2019-10-31 23:54:21 +01:00
|
|
|
ERR_FAIL_COND_V(df_png.size() == 0, Dictionary());
|
|
|
|
d["octree_df_png"] = df_png;
|
|
|
|
} else {
|
2020-02-17 22:06:54 +01:00
|
|
|
d["octree_df"] = Vector<uint8_t>();
|
2019-10-31 23:54:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:39:08 +02:00
|
|
|
d["level_counts"] = get_level_counts();
|
|
|
|
d["to_cell_xform"] = get_to_cell_xform();
|
|
|
|
return d;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::allocate(const Transform3D &p_to_cell_xform, const AABB &p_aabb, const Vector3 &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) {
|
|
|
|
RS::get_singleton()->voxel_gi_allocate_data(probe, p_to_cell_xform, p_aabb, p_octree_size, p_octree_cells, p_data_cells, p_distance_field, p_level_counts);
|
2019-10-03 22:39:08 +02:00
|
|
|
bounds = p_aabb;
|
|
|
|
to_cell_xform = p_to_cell_xform;
|
|
|
|
octree_size = p_octree_size;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
AABB VoxelGIData::get_bounds() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return bounds;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Vector3 VoxelGIData::get_octree_size() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return octree_size;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Vector<uint8_t> VoxelGIData::get_octree_cells() const {
|
|
|
|
return RS::get_singleton()->voxel_gi_get_octree_cells(probe);
|
2019-10-03 22:39:08 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Vector<uint8_t> VoxelGIData::get_data_cells() const {
|
|
|
|
return RS::get_singleton()->voxel_gi_get_data_cells(probe);
|
2019-10-03 22:39:08 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Vector<uint8_t> VoxelGIData::get_distance_field() const {
|
|
|
|
return RS::get_singleton()->voxel_gi_get_distance_field(probe);
|
2019-10-13 02:24:03 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Vector<int> VoxelGIData::get_level_counts() const {
|
|
|
|
return RS::get_singleton()->voxel_gi_get_level_counts(probe);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Transform3D VoxelGIData::get_to_cell_xform() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return to_cell_xform;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::set_dynamic_range(float p_range) {
|
|
|
|
RS::get_singleton()->voxel_gi_set_dynamic_range(probe, p_range);
|
2019-10-03 22:39:08 +02:00
|
|
|
dynamic_range = p_range;
|
|
|
|
}
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
float VoxelGIData::get_dynamic_range() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return dynamic_range;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::set_propagation(float p_propagation) {
|
|
|
|
RS::get_singleton()->voxel_gi_set_propagation(probe, p_propagation);
|
2019-10-03 22:39:08 +02:00
|
|
|
propagation = p_propagation;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
float VoxelGIData::get_propagation() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return propagation;
|
|
|
|
}
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::set_energy(float p_energy) {
|
|
|
|
RS::get_singleton()->voxel_gi_set_energy(probe, p_energy);
|
2019-10-03 22:39:08 +02:00
|
|
|
energy = p_energy;
|
2016-12-22 14:00:15 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
float VoxelGIData::get_energy() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return energy;
|
2016-12-22 14:00:15 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::set_bias(float p_bias) {
|
|
|
|
RS::get_singleton()->voxel_gi_set_bias(probe, p_bias);
|
2019-10-03 22:39:08 +02:00
|
|
|
bias = p_bias;
|
2017-02-16 12:55:11 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
float VoxelGIData::get_bias() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return bias;
|
2017-02-16 12:55:11 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::set_normal_bias(float p_normal_bias) {
|
|
|
|
RS::get_singleton()->voxel_gi_set_normal_bias(probe, p_normal_bias);
|
2019-10-03 22:39:08 +02:00
|
|
|
normal_bias = p_normal_bias;
|
2017-07-16 04:24:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
float VoxelGIData::get_normal_bias() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return normal_bias;
|
2017-02-06 09:12:15 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::set_interior(bool p_enable) {
|
|
|
|
RS::get_singleton()->voxel_gi_set_interior(probe, p_enable);
|
2019-10-03 22:39:08 +02:00
|
|
|
interior = p_enable;
|
2016-12-22 14:00:15 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
bool VoxelGIData::is_interior() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return interior;
|
2016-12-31 14:53:29 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::set_use_two_bounces(bool p_enable) {
|
|
|
|
RS::get_singleton()->voxel_gi_set_use_two_bounces(probe, p_enable);
|
2019-10-03 22:39:08 +02:00
|
|
|
use_two_bounces = p_enable;
|
2016-12-31 14:53:29 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
bool VoxelGIData::is_using_two_bounces() const {
|
2019-10-03 22:39:08 +02:00
|
|
|
return use_two_bounces;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
RID VoxelGIData::get_rid() const {
|
2016-12-23 04:37:38 +01:00
|
|
|
return probe;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGIData::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("allocate", "to_cell_xform", "aabb", "octree_size", "octree_cells", "data_cells", "distance_field", "level_counts"), &VoxelGIData::allocate);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bounds"), &VoxelGIData::get_bounds);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_octree_size"), &VoxelGIData::get_octree_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_to_cell_xform"), &VoxelGIData::get_to_cell_xform);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_octree_cells"), &VoxelGIData::get_octree_cells);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_data_cells"), &VoxelGIData::get_data_cells);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_level_counts"), &VoxelGIData::get_level_counts);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_dynamic_range", "dynamic_range"), &VoxelGIData::set_dynamic_range);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_dynamic_range"), &VoxelGIData::get_dynamic_range);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_energy", "energy"), &VoxelGIData::set_energy);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_energy"), &VoxelGIData::get_energy);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bias", "bias"), &VoxelGIData::set_bias);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bias"), &VoxelGIData::get_bias);
|
2017-02-16 12:55:11 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_normal_bias", "bias"), &VoxelGIData::set_normal_bias);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_normal_bias"), &VoxelGIData::get_normal_bias);
|
2017-07-16 04:24:37 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_propagation", "propagation"), &VoxelGIData::set_propagation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_propagation"), &VoxelGIData::get_propagation);
|
2017-02-06 09:12:15 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_interior", "interior"), &VoxelGIData::set_interior);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_interior"), &VoxelGIData::is_interior);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_two_bounces", "enable"), &VoxelGIData::set_use_two_bounces);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_using_two_bounces"), &VoxelGIData::is_using_two_bounces);
|
2019-10-03 22:39:08 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_data", "data"), &VoxelGIData::_set_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_data"), &VoxelGIData::_get_data);
|
2016-12-31 14:53:29 +01:00
|
|
|
|
2021-11-03 23:06:17 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-11-26 00:46:49 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dynamic_range", PROPERTY_HINT_RANGE, "1,8,0.01"), "set_dynamic_range", "get_dynamic_range");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_energy", "get_energy");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,8,0.01"), "set_bias", "get_bias");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "normal_bias", PROPERTY_HINT_RANGE, "0,8,0.01"), "set_normal_bias", "get_normal_bias");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "propagation", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_propagation", "get_propagation");
|
2019-10-03 22:39:08 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_two_bounces"), "set_use_two_bounces", "is_using_two_bounces");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior");
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
VoxelGIData::VoxelGIData() {
|
|
|
|
probe = RS::get_singleton()->voxel_gi_create();
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
VoxelGIData::~VoxelGIData() {
|
2020-03-27 19:21:27 +01:00
|
|
|
RS::get_singleton()->free(probe);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
//////////////////////
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGI::set_probe_data(const Ref<VoxelGIData> &p_data) {
|
2016-12-20 04:21:07 +01:00
|
|
|
if (p_data.is_valid()) {
|
2020-03-27 19:21:27 +01:00
|
|
|
RS::get_singleton()->instance_set_base(get_instance(), p_data->get_rid());
|
2016-12-20 04:21:07 +01:00
|
|
|
} else {
|
2020-03-27 19:21:27 +01:00
|
|
|
RS::get_singleton()->instance_set_base(get_instance(), RID());
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
probe_data = p_data;
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Ref<VoxelGIData> VoxelGI::get_probe_data() const {
|
2016-12-20 04:21:07 +01:00
|
|
|
return probe_data;
|
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGI::set_subdiv(Subdiv p_subdiv) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_subdiv, SUBDIV_MAX);
|
|
|
|
subdiv = p_subdiv;
|
2021-06-23 16:49:50 +02:00
|
|
|
update_gizmos();
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
VoxelGI::Subdiv VoxelGI::get_subdiv() const {
|
2016-12-20 04:21:07 +01:00
|
|
|
return subdiv;
|
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGI::set_extents(const Vector3 &p_extents) {
|
2017-03-05 16:44:50 +01:00
|
|
|
extents = p_extents;
|
2021-06-23 16:49:50 +02:00
|
|
|
update_gizmos();
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Vector3 VoxelGI::get_extents() const {
|
2016-12-20 04:21:07 +01:00
|
|
|
return extents;
|
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGI::_find_meshes(Node *p_at_node, List<PlotMesh> &plot_meshes) {
|
2020-03-26 22:49:16 +01:00
|
|
|
MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_at_node);
|
2021-11-26 17:47:37 +01:00
|
|
|
if (mi && mi->get_gi_mode() == GeometryInstance3D::GI_MODE_STATIC && mi->is_visible_in_tree()) {
|
2017-07-29 05:03:54 +02:00
|
|
|
Ref<Mesh> mesh = mi->get_mesh();
|
2016-12-20 04:21:07 +01:00
|
|
|
if (mesh.is_valid()) {
|
2017-11-17 03:09:00 +01:00
|
|
|
AABB aabb = mesh->get_aabb();
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D xf = get_global_transform().affine_inverse() * mi->get_global_transform();
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
if (AABB(-extents, extents * 2).intersects(xf.xform(aabb))) {
|
2017-12-14 12:59:46 +01:00
|
|
|
PlotMesh pm;
|
2017-03-05 16:44:50 +01:00
|
|
|
pm.local_xform = xf;
|
|
|
|
pm.mesh = mesh;
|
|
|
|
for (int i = 0; i < mesh->get_surface_count(); i++) {
|
2021-04-14 05:45:16 +02:00
|
|
|
pm.instance_materials.push_back(mi->get_surface_override_material(i));
|
2017-02-06 04:38:39 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
pm.override_material = mi->get_material_override();
|
2017-12-14 12:59:46 +01:00
|
|
|
plot_meshes.push_back(pm);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Node3D *s = Object::cast_to<Node3D>(p_at_node);
|
2017-09-06 23:50:18 +02:00
|
|
|
if (s) {
|
2017-09-06 23:24:29 +02:00
|
|
|
if (s->is_visible_in_tree()) {
|
|
|
|
Array meshes = p_at_node->call("get_meshes");
|
|
|
|
for (int i = 0; i < meshes.size(); i += 2) {
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D mxf = meshes[i];
|
2017-09-06 23:24:29 +02:00
|
|
|
Ref<Mesh> mesh = meshes[i + 1];
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!mesh.is_valid()) {
|
2017-09-06 23:24:29 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-02-15 12:29:46 +01:00
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
AABB aabb = mesh->get_aabb();
|
2017-02-15 12:29:46 +01:00
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D xf = get_global_transform().affine_inverse() * (s->get_global_transform() * mxf);
|
2017-09-06 23:24:29 +02:00
|
|
|
|
2017-11-17 03:09:00 +01:00
|
|
|
if (AABB(-extents, extents * 2).intersects(xf.xform(aabb))) {
|
2017-12-14 12:59:46 +01:00
|
|
|
PlotMesh pm;
|
2017-09-06 23:24:29 +02:00
|
|
|
pm.local_xform = xf;
|
|
|
|
pm.mesh = mesh;
|
2017-12-14 12:59:46 +01:00
|
|
|
plot_meshes.push_back(pm);
|
2017-09-06 23:24:29 +02:00
|
|
|
}
|
2017-02-15 12:29:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_at_node->get_child_count(); i++) {
|
2016-12-20 04:21:07 +01:00
|
|
|
Node *child = p_at_node->get_child(i);
|
2017-12-14 12:59:46 +01:00
|
|
|
_find_meshes(child, plot_meshes);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
VoxelGI::BakeBeginFunc VoxelGI::bake_begin_function = nullptr;
|
|
|
|
VoxelGI::BakeStepFunc VoxelGI::bake_step_function = nullptr;
|
|
|
|
VoxelGI::BakeEndFunc VoxelGI::bake_end_function = nullptr;
|
2017-10-30 20:33:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Vector3i VoxelGI::get_estimated_cell_size() const {
|
2019-10-31 23:54:21 +01:00
|
|
|
static const int subdiv_value[SUBDIV_MAX] = { 6, 7, 8, 9 };
|
|
|
|
int cell_subdiv = subdiv_value[subdiv];
|
|
|
|
int axis_cell_size[3];
|
|
|
|
AABB bounds = AABB(-extents, extents * 2.0);
|
|
|
|
int longest_axis = bounds.get_longest_axis_index();
|
|
|
|
axis_cell_size[longest_axis] = 1 << cell_subdiv;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i == longest_axis) {
|
2019-10-31 23:54:21 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-10-31 23:54:21 +01:00
|
|
|
|
|
|
|
axis_cell_size[i] = axis_cell_size[longest_axis];
|
|
|
|
float axis_size = bounds.size[longest_axis];
|
|
|
|
|
|
|
|
//shrink until fit subdiv
|
|
|
|
while (axis_size / 2.0 >= bounds.size[i]) {
|
|
|
|
axis_size /= 2.0;
|
|
|
|
axis_cell_size[i] >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:01:00 +01:00
|
|
|
return Vector3i(axis_cell_size[0], axis_cell_size[1], axis_cell_size[2]);
|
2019-10-31 23:54:21 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGI::bake(Node *p_from_node, bool p_create_visual_debug) {
|
2019-10-03 22:39:08 +02:00
|
|
|
static const int subdiv_value[SUBDIV_MAX] = { 6, 7, 8, 9 };
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-02-28 21:09:55 +01:00
|
|
|
p_from_node = p_from_node ? p_from_node : get_parent();
|
|
|
|
ERR_FAIL_NULL(p_from_node);
|
|
|
|
|
2019-10-03 22:39:08 +02:00
|
|
|
Voxelizer baker;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
baker.begin_bake(subdiv_value[subdiv], AABB(-extents, extents * 2.0));
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
List<PlotMesh> mesh_list;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-02-28 21:09:55 +01:00
|
|
|
_find_meshes(p_from_node, mesh_list);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-10-30 20:33:07 +01:00
|
|
|
if (bake_begin_function) {
|
2017-12-14 12:59:46 +01:00
|
|
|
bake_begin_function(mesh_list.size() + 1);
|
2017-10-30 20:33:07 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int pmc = 0;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
for (PlotMesh &E : mesh_list) {
|
2017-10-30 20:33:07 +01:00
|
|
|
if (bake_step_function) {
|
2017-12-14 12:59:46 +01:00
|
|
|
bake_step_function(pmc, RTR("Plotting Meshes") + " " + itos(pmc) + "/" + itos(mesh_list.size()));
|
2017-10-30 20:33:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pmc++;
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
baker.plot_mesh(E.local_xform, E.mesh, E.instance_materials, E.override_material);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
2017-10-30 20:33:07 +01:00
|
|
|
if (bake_step_function) {
|
|
|
|
bake_step_function(pmc++, RTR("Finishing Plot"));
|
|
|
|
}
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
baker.end_bake();
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-10-07 15:46:55 +02:00
|
|
|
//create the data for rendering server
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-12-14 12:59:46 +01:00
|
|
|
if (p_create_visual_debug) {
|
2020-03-26 22:49:16 +01:00
|
|
|
MultiMeshInstance3D *mmi = memnew(MultiMeshInstance3D);
|
2017-12-14 12:59:46 +01:00
|
|
|
mmi->set_multimesh(baker.create_debug_multimesh());
|
2021-10-21 16:46:07 +02:00
|
|
|
add_child(mmi, true);
|
2017-12-14 12:59:46 +01:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-05-16 14:01:01 +02:00
|
|
|
if (is_inside_tree() && get_tree()->get_edited_scene_root() == this) {
|
2017-12-14 12:59:46 +01:00
|
|
|
mmi->set_owner(this);
|
|
|
|
} else {
|
|
|
|
mmi->set_owner(get_owner());
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
2017-12-14 12:59:46 +01:00
|
|
|
#else
|
|
|
|
mmi->set_owner(get_owner());
|
|
|
|
#endif
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-02-16 12:55:11 +01:00
|
|
|
} else {
|
2021-06-05 00:47:26 +02:00
|
|
|
Ref<VoxelGIData> probe_data = get_probe_data();
|
2017-12-09 18:48:02 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (probe_data.is_null()) {
|
2021-06-18 00:03:09 +02:00
|
|
|
probe_data.instantiate();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-12-09 18:48:02 +01:00
|
|
|
|
2019-10-13 02:24:03 +02:00
|
|
|
if (bake_step_function) {
|
|
|
|
bake_step_function(pmc++, RTR("Generating Distance Field"));
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> df = baker.get_sdf_3d_image();
|
2019-10-13 02:24:03 +02:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
probe_data->allocate(baker.get_to_cell_space_xform(), AABB(-extents, extents * 2.0), baker.get_voxel_gi_octree_size(), baker.get_voxel_gi_octree_cells(), baker.get_voxel_gi_data_cells(), df, baker.get_voxel_gi_level_cell_count());
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-02-16 12:55:11 +01:00
|
|
|
set_probe_data(probe_data);
|
2019-10-19 01:23:17 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2019-10-05 15:27:43 +02:00
|
|
|
probe_data->set_edited(true); //so it gets saved
|
2019-10-19 01:23:17 +02:00
|
|
|
#endif
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
2017-10-30 20:33:07 +01:00
|
|
|
|
|
|
|
if (bake_end_function) {
|
|
|
|
bake_end_function();
|
|
|
|
}
|
2019-10-31 23:54:21 +01:00
|
|
|
|
2021-02-10 21:18:45 +01:00
|
|
|
notify_property_list_changed(); //bake property may have changed
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGI::_debug_bake() {
|
2020-04-02 01:20:12 +02:00
|
|
|
bake(nullptr, true);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
AABB VoxelGI::get_aabb() const {
|
2017-11-17 03:09:00 +01:00
|
|
|
return AABB(-extents, extents * 2);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
Vector<Face3> VoxelGI::get_faces(uint32_t p_usage_flags) const {
|
2020-02-17 22:06:54 +01:00
|
|
|
return Vector<Face3>();
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
TypedArray<String> VoxelGI::get_configuration_warnings() const {
|
2020-10-29 11:01:28 +01:00
|
|
|
TypedArray<String> warnings = Node::get_configuration_warnings();
|
2020-05-14 22:59:27 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
if (RenderingServer::get_singleton()->is_low_end()) {
|
2021-09-27 01:07:10 +02:00
|
|
|
warnings.push_back(TTR("VoxelGIs are not supported by the OpenGL video driver.\nUse a LightmapGI instead."));
|
2021-01-23 00:50:24 +01:00
|
|
|
} else if (probe_data.is_null()) {
|
2021-06-05 00:47:26 +02:00
|
|
|
warnings.push_back(TTR("No VoxelGI data set, so this node is disabled. Bake static objects to enable GI."));
|
2019-03-03 00:45:14 +01:00
|
|
|
}
|
2020-10-29 11:01:28 +01:00
|
|
|
return warnings;
|
2019-03-03 00:45:14 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
void VoxelGI::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_probe_data", "data"), &VoxelGI::set_probe_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_probe_data"), &VoxelGI::get_probe_data);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_subdiv", "subdiv"), &VoxelGI::set_subdiv);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_subdiv"), &VoxelGI::get_subdiv);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_extents", "extents"), &VoxelGI::set_extents);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_extents"), &VoxelGI::get_extents);
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("bake", "from_node", "create_visual_debug"), &VoxelGI::bake, DEFVAL(Variant()), DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_bake"), &VoxelGI::_debug_bake);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::set_method_flags(get_class_static(), _scs_create("debug_bake"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
2017-02-12 01:11:37 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "subdiv", PROPERTY_HINT_ENUM, "64,128,256,512"), "set_subdiv", "get_subdiv");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents"), "set_extents", "get_extents");
|
2021-06-05 00:47:26 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "VoxelGIData", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE), "set_probe_data", "get_probe_data");
|
2016-12-20 04:21:07 +01:00
|
|
|
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(SUBDIV_64);
|
|
|
|
BIND_ENUM_CONSTANT(SUBDIV_128);
|
|
|
|
BIND_ENUM_CONSTANT(SUBDIV_256);
|
2017-10-21 20:58:02 +02:00
|
|
|
BIND_ENUM_CONSTANT(SUBDIV_512);
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(SUBDIV_MAX);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
VoxelGI::VoxelGI() {
|
|
|
|
voxel_gi = RS::get_singleton()->voxel_gi_create();
|
2018-07-18 18:47:42 +02:00
|
|
|
set_disable_scale(true);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 00:47:26 +02:00
|
|
|
VoxelGI::~VoxelGI() {
|
|
|
|
RS::get_singleton()->free(voxel_gi);
|
2016-12-20 04:21:07 +01:00
|
|
|
}
|