2018-01-05 07:50:27 +08:00
|
|
|
/**************************************************************************/
|
2021-06-05 06:47:26 +08:00
|
|
|
/* lightmap_gi.h */
|
2018-01-05 07:50:27 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
2017-12-18 11:34:48 +08:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2018-01-05 07:50:27 +08:00
|
|
|
/* */
|
2017-12-18 11:34:48 +08: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 */
|
2018-01-05 07:50:27 +08:00
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
2017-12-18 11:34:48 +08:00
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
2018-01-05 07:50:27 +08:00
|
|
|
/* */
|
2017-12-18 11:34:48 +08:00
|
|
|
/* 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 07:50:27 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
|
2021-06-05 06:47:26 +08:00
|
|
|
#ifndef LIGHTMAP_GI_H
|
|
|
|
#define LIGHTMAP_GI_H
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/templates/local_vector.h"
|
2020-05-01 20:34:23 +08:00
|
|
|
#include "scene/3d/light_3d.h"
|
|
|
|
#include "scene/3d/lightmapper.h"
|
|
|
|
#include "scene/3d/visual_instance_3d.h"
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2022-08-01 07:20:24 +08:00
|
|
|
class Sky;
|
|
|
|
class CameraAttributes;
|
|
|
|
|
2021-06-05 06:47:26 +08:00
|
|
|
class LightmapGIData : public Resource {
|
|
|
|
GDCLASS(LightmapGIData, Resource);
|
2020-05-01 20:34:23 +08:00
|
|
|
RES_BASE_EXTENSION("lmbake")
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
Ref<TextureLayered> light_texture;
|
2023-06-03 06:09:40 +08:00
|
|
|
TypedArray<TextureLayered> light_textures;
|
2020-05-01 20:34:23 +08:00
|
|
|
|
|
|
|
bool uses_spherical_harmonics = false;
|
|
|
|
bool interior = false;
|
|
|
|
|
|
|
|
RID lightmap;
|
2017-12-14 19:59:46 +08:00
|
|
|
AABB bounds;
|
2022-08-01 07:20:24 +08:00
|
|
|
float baked_exposure = 1.0;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
|
|
|
struct User {
|
|
|
|
NodePath path;
|
2021-02-08 05:29:31 +08:00
|
|
|
int32_t sub_instance = 0;
|
2020-05-01 20:34:23 +08:00
|
|
|
Rect2 uv_scale;
|
2021-02-08 05:29:31 +08:00
|
|
|
int slice_index = 0;
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Vector<User> users;
|
|
|
|
|
|
|
|
void _set_user_data(const Array &p_data);
|
|
|
|
Array _get_user_data() const;
|
2020-05-01 20:34:23 +08:00
|
|
|
void _set_probe_data(const Dictionary &p_data);
|
|
|
|
Dictionary _get_probe_data() const;
|
2023-06-03 06:09:40 +08:00
|
|
|
|
|
|
|
void _reset_lightmap_textures();
|
2017-12-14 19:59:46 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
2020-05-01 20:34:23 +08:00
|
|
|
void add_user(const NodePath &p_path, const Rect2 &p_uv_scale, int p_slice_index, int32_t p_sub_instance = -1);
|
|
|
|
int get_user_count() const;
|
|
|
|
NodePath get_user_path(int p_user) const;
|
|
|
|
int32_t get_user_sub_instance(int p_user) const;
|
|
|
|
Rect2 get_user_lightmap_uv_scale(int p_user) const;
|
|
|
|
int get_user_lightmap_slice_index(int p_user) const;
|
|
|
|
void clear_users();
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2023-06-03 06:09:40 +08:00
|
|
|
#ifndef DISABLE_DEPRECATED
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_light_texture(const Ref<TextureLayered> &p_light_texture);
|
|
|
|
Ref<TextureLayered> get_light_texture() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2023-06-03 06:09:40 +08:00
|
|
|
void _set_light_textures_data(const Array &p_data);
|
|
|
|
Array _get_light_textures_data() const;
|
|
|
|
#endif
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_uses_spherical_harmonics(bool p_enable);
|
|
|
|
bool is_using_spherical_harmonics() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
bool is_interior() const;
|
2022-08-01 07:20:24 +08:00
|
|
|
float get_baked_exposure() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2022-08-01 07:20:24 +08:00
|
|
|
void set_capture_data(const AABB &p_bounds, bool p_interior, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree, float p_baked_exposure);
|
2020-05-01 20:34:23 +08:00
|
|
|
PackedVector3Array get_capture_points() const;
|
|
|
|
PackedColorArray get_capture_sh() const;
|
|
|
|
PackedInt32Array get_capture_tetrahedra() const;
|
|
|
|
PackedInt32Array get_capture_bsp_tree() const;
|
|
|
|
AABB get_capture_bounds() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void clear();
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2023-06-03 06:09:40 +08:00
|
|
|
void set_lightmap_textures(const TypedArray<TextureLayered> &p_data);
|
|
|
|
TypedArray<TextureLayered> get_lightmap_textures() const;
|
|
|
|
|
2020-07-10 18:34:39 +08:00
|
|
|
virtual RID get_rid() const override;
|
2021-06-05 06:47:26 +08:00
|
|
|
LightmapGIData();
|
|
|
|
~LightmapGIData();
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
|
|
|
|
2021-06-05 06:47:26 +08:00
|
|
|
class LightmapGI : public VisualInstance3D {
|
|
|
|
GDCLASS(LightmapGI, VisualInstance3D);
|
2017-12-14 19:59:46 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
enum BakeQuality {
|
|
|
|
BAKE_QUALITY_LOW,
|
|
|
|
BAKE_QUALITY_MEDIUM,
|
2020-05-01 20:34:23 +08:00
|
|
|
BAKE_QUALITY_HIGH,
|
|
|
|
BAKE_QUALITY_ULTRA,
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
2020-05-12 16:10:34 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
enum GenerateProbes {
|
|
|
|
GENERATE_PROBES_DISABLED,
|
|
|
|
GENERATE_PROBES_SUBDIV_4,
|
|
|
|
GENERATE_PROBES_SUBDIV_8,
|
|
|
|
GENERATE_PROBES_SUBDIV_16,
|
|
|
|
GENERATE_PROBES_SUBDIV_32,
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum BakeError {
|
|
|
|
BAKE_ERROR_OK,
|
2022-04-29 14:06:48 +08:00
|
|
|
BAKE_ERROR_NO_SCENE_ROOT,
|
|
|
|
BAKE_ERROR_FOREIGN_DATA,
|
2020-05-01 20:34:23 +08:00
|
|
|
BAKE_ERROR_NO_LIGHTMAPPER,
|
2017-12-14 19:59:46 +08:00
|
|
|
BAKE_ERROR_NO_SAVE_PATH,
|
|
|
|
BAKE_ERROR_NO_MESHES,
|
2020-05-01 20:34:23 +08:00
|
|
|
BAKE_ERROR_MESHES_INVALID,
|
2017-12-14 19:59:46 +08:00
|
|
|
BAKE_ERROR_CANT_CREATE_IMAGE,
|
2020-05-12 16:10:34 +08:00
|
|
|
BAKE_ERROR_USER_ABORTED,
|
2023-09-11 20:14:39 +08:00
|
|
|
BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL,
|
2022-08-26 05:02:55 +08:00
|
|
|
BAKE_ERROR_LIGHTMAP_TOO_SMALL,
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
enum EnvironmentMode {
|
|
|
|
ENVIRONMENT_MODE_DISABLED,
|
|
|
|
ENVIRONMENT_MODE_SCENE,
|
|
|
|
ENVIRONMENT_MODE_CUSTOM_SKY,
|
|
|
|
ENVIRONMENT_MODE_CUSTOM_COLOR,
|
|
|
|
};
|
2017-12-14 19:59:46 +08:00
|
|
|
|
|
|
|
private:
|
2021-02-08 05:29:31 +08:00
|
|
|
BakeQuality bake_quality = BAKE_QUALITY_MEDIUM;
|
|
|
|
bool use_denoiser = true;
|
2023-09-12 01:59:50 +08:00
|
|
|
float denoiser_strength = 0.1f;
|
2021-06-27 22:56:22 +08:00
|
|
|
int bounces = 3;
|
2023-10-14 01:32:22 +08:00
|
|
|
float bounce_indirect_energy = 1.0;
|
2021-02-08 05:29:31 +08:00
|
|
|
float bias = 0.0005;
|
2022-08-26 05:02:55 +08:00
|
|
|
float texel_scale = 1.0;
|
2021-02-08 05:29:31 +08:00
|
|
|
int max_texture_size = 16384;
|
|
|
|
bool interior = false;
|
2021-06-27 22:56:22 +08:00
|
|
|
EnvironmentMode environment_mode = ENVIRONMENT_MODE_SCENE;
|
2020-05-01 20:34:23 +08:00
|
|
|
Ref<Sky> environment_custom_sky;
|
2021-06-27 22:56:22 +08:00
|
|
|
Color environment_custom_color = Color(1, 1, 1);
|
2021-02-08 05:29:31 +08:00
|
|
|
float environment_custom_energy = 1.0;
|
|
|
|
bool directional = false;
|
2023-10-14 01:32:22 +08:00
|
|
|
bool use_texture_for_bounces = true;
|
2021-06-27 22:56:22 +08:00
|
|
|
GenerateProbes gen_probes = GENERATE_PROBES_SUBDIV_8;
|
2022-08-01 07:20:24 +08:00
|
|
|
Ref<CameraAttributes> camera_attributes;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2021-06-05 06:47:26 +08:00
|
|
|
Ref<LightmapGIData> light_data;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
struct LightsFound {
|
2020-10-17 13:08:21 +08:00
|
|
|
Transform3D xform;
|
2021-02-08 05:29:31 +08:00
|
|
|
Light3D *light = nullptr;
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
struct MeshesFound {
|
2020-10-17 13:08:21 +08:00
|
|
|
Transform3D xform;
|
2020-05-01 20:34:23 +08:00
|
|
|
NodePath node_path;
|
2021-02-08 05:29:31 +08:00
|
|
|
int32_t subindex = 0;
|
2020-05-01 20:34:23 +08:00
|
|
|
Ref<Mesh> mesh;
|
2021-02-08 05:29:31 +08:00
|
|
|
int32_t lightmap_scale = 0;
|
2020-05-01 20:34:23 +08:00
|
|
|
Vector<Ref<Material>> overrides;
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void _find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes);
|
2017-12-14 19:59:46 +08:00
|
|
|
|
|
|
|
void _assign_lightmaps();
|
|
|
|
void _clear_lightmaps();
|
|
|
|
|
|
|
|
struct BakeTimeData {
|
|
|
|
String text;
|
2021-02-08 05:29:31 +08:00
|
|
|
int pass = 0;
|
|
|
|
uint64_t last_step = 0;
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
struct BSPSimplex {
|
2021-02-08 05:29:31 +08:00
|
|
|
int vertices[4] = {};
|
|
|
|
int planes[4] = {};
|
2020-05-01 20:34:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BSPNode {
|
|
|
|
static const int32_t EMPTY_LEAF = INT32_MIN;
|
|
|
|
Plane plane;
|
2021-02-08 05:29:31 +08:00
|
|
|
int32_t over = EMPTY_LEAF;
|
|
|
|
int32_t under = EMPTY_LEAF;
|
2020-05-01 20:34:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int _bsp_get_simplex_side(const Vector<Vector3> &p_points, const LocalVector<BSPSimplex> &p_simplices, const Plane &p_plane, uint32_t p_simplex) const;
|
|
|
|
int32_t _compute_bsp_tree(const Vector<Vector3> &p_points, const LocalVector<Plane> &p_planes, LocalVector<int32_t> &planes_tested, const LocalVector<BSPSimplex> &p_simplices, const LocalVector<int32_t> &p_simplex_indices, LocalVector<BSPNode> &bsp_nodes);
|
|
|
|
|
|
|
|
struct BakeStepUD {
|
|
|
|
Lightmapper::BakeStepFunc func;
|
2021-02-08 05:29:31 +08:00
|
|
|
void *ud = nullptr;
|
|
|
|
float from_percent = 0.0;
|
|
|
|
float to_percent = 0.0;
|
2020-05-01 20:34:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool _lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh);
|
|
|
|
|
|
|
|
struct GenProbesOctree {
|
|
|
|
Vector3i offset;
|
2021-02-08 05:29:31 +08:00
|
|
|
uint32_t size = 0;
|
2020-05-01 20:34:23 +08:00
|
|
|
GenProbesOctree *children[8] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
|
|
|
|
~GenProbesOctree() {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
if (children[i] != nullptr) {
|
|
|
|
memdelete(children[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void _plot_triangle_into_octree(GenProbesOctree *p_cell, float p_cell_size, const Vector3 *p_triangle);
|
2022-06-18 22:20:55 +08:00
|
|
|
void _gen_new_positions_from_octree(const GenProbesOctree *p_cell, float p_cell_size, const Vector<Vector3> &probe_positions, LocalVector<Vector3> &new_probe_positions, HashMap<Vector3i, bool> &positions_used, const AABB &p_bounds);
|
2020-05-01 20:34:23 +08:00
|
|
|
|
2017-12-14 19:59:46 +08:00
|
|
|
protected:
|
2022-08-13 04:57:11 +08:00
|
|
|
void _validate_property(PropertyInfo &p_property) const;
|
2017-12-14 19:59:46 +08:00
|
|
|
static void _bind_methods();
|
|
|
|
void _notification(int p_what);
|
|
|
|
|
|
|
|
public:
|
2021-06-05 06:47:26 +08:00
|
|
|
void set_light_data(const Ref<LightmapGIData> &p_data);
|
|
|
|
Ref<LightmapGIData> get_light_data() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_bake_quality(BakeQuality p_quality);
|
|
|
|
BakeQuality get_bake_quality() const;
|
|
|
|
|
|
|
|
void set_use_denoiser(bool p_enable);
|
|
|
|
bool is_using_denoiser() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2023-09-12 01:59:50 +08:00
|
|
|
void set_denoiser_strength(float p_denoiser_strength);
|
|
|
|
float get_denoiser_strength() const;
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_directional(bool p_enable);
|
|
|
|
bool is_directional() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2023-10-14 01:32:22 +08:00
|
|
|
void set_use_texture_for_bounces(bool p_enable);
|
|
|
|
bool is_using_texture_for_bounces() const;
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_interior(bool p_interior);
|
|
|
|
bool is_interior() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_environment_mode(EnvironmentMode p_mode);
|
|
|
|
EnvironmentMode get_environment_mode() const;
|
2019-06-22 23:27:09 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_environment_custom_sky(const Ref<Sky> &p_sky);
|
|
|
|
Ref<Sky> get_environment_custom_sky() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_environment_custom_color(const Color &p_color);
|
|
|
|
Color get_environment_custom_color() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_environment_custom_energy(float p_energy);
|
|
|
|
float get_environment_custom_energy() const;
|
|
|
|
|
|
|
|
void set_bounces(int p_bounces);
|
|
|
|
int get_bounces() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2023-10-14 01:32:22 +08:00
|
|
|
void set_bounce_indirect_energy(float p_indirect_energy);
|
|
|
|
float get_bounce_indirect_energy() const;
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_bias(float p_bias);
|
|
|
|
float get_bias() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2022-08-26 05:02:55 +08:00
|
|
|
void set_texel_scale(float p_multiplier);
|
|
|
|
float get_texel_scale() const;
|
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_max_texture_size(int p_size);
|
|
|
|
int get_max_texture_size() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
void set_generate_probes(GenerateProbes p_generate_probes);
|
|
|
|
GenerateProbes get_generate_probes() const;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2022-08-01 07:20:24 +08:00
|
|
|
void set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes);
|
|
|
|
Ref<CameraAttributes> get_camera_attributes() const;
|
|
|
|
|
2020-07-10 18:34:39 +08:00
|
|
|
AABB get_aabb() const override;
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2020-05-01 20:34:23 +08:00
|
|
|
BakeError bake(Node *p_from_node, String p_image_data_path = "", Lightmapper::BakeStepFunc p_bake_step = nullptr, void *p_bake_userdata = nullptr);
|
2023-02-26 08:24:41 +08:00
|
|
|
|
2023-07-05 21:41:44 +08:00
|
|
|
virtual Array get_configuration_warnings() const override;
|
2023-02-26 08:24:41 +08:00
|
|
|
|
2021-06-05 06:47:26 +08:00
|
|
|
LightmapGI();
|
2017-12-14 19:59:46 +08:00
|
|
|
};
|
|
|
|
|
2021-06-05 06:47:26 +08:00
|
|
|
VARIANT_ENUM_CAST(LightmapGI::BakeQuality);
|
|
|
|
VARIANT_ENUM_CAST(LightmapGI::GenerateProbes);
|
|
|
|
VARIANT_ENUM_CAST(LightmapGI::BakeError);
|
|
|
|
VARIANT_ENUM_CAST(LightmapGI::EnvironmentMode);
|
2017-12-14 19:59:46 +08:00
|
|
|
|
2022-07-24 05:41:51 +08:00
|
|
|
#endif // LIGHTMAP_GI_H
|