Debian Codegraph @

scene/3d/lightmap_gi.h

raw symbols SCIP
1 /**************************************************************************/
2 /* lightmap_gi.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 "core/templates/local_vector.h"
34 #include "scene/3d/light_3d.h"
35 #include "scene/3d/lightmapper.h"
36 #include "scene/3d/visual_instance_3d.h"
37
38 class Sky;
39 class CameraAttributes;
40
41 class LightmapGIData : public Resource {
42 GDCLASS(LightmapGIData, Resource);
43 RES_BASE_EXTENSION("lmbake")
44
45 public:
46 enum ShadowmaskMode {
47 SHADOWMASK_MODE_NONE,
48 SHADOWMASK_MODE_REPLACE,
49 SHADOWMASK_MODE_OVERLAY,
50 SHADOWMASK_MODE_ONLY,
51 };
52
53 private:
54 // The 'merged' texture atlases actually used by the renderer.
55 Ref<TextureLayered> combined_light_texture;
56 Ref<TextureLayered> combined_shadowmask_texture;
57
58 // The temporary texture atlas arrays which are used for storage.
59 // If a single atlas is too large, it's split and recombined during loading.
60 TypedArray<TextureLayered> storage_light_textures;
61 TypedArray<TextureLayered> storage_shadowmask_textures;
62
63 bool uses_spherical_harmonics = false;
64 bool interior = false;
65
66 bool _uses_packed_directional = false;
67
68 RID lightmap;
69 AABB bounds;
70 float baked_exposure = 1.0;
71 uint32_t lightprobe_hash = 0;
72
73 struct User {
74 NodePath path;
75 int32_t sub_instance = 0;
76 Rect2 uv_scale;
77 int slice_index = 0;
78 };
79
80 Vector<User> users;
81
82 void _set_user_data(const Array &p_data);
83 Array _get_user_data() const;
84 void _set_probe_data(const Dictionary &p_data);
85 Dictionary _get_probe_data() const;
86
87 void _reset_lightmap_textures();
88 void _reset_shadowmask_textures();
89
90 protected:
91 static void _bind_methods();
92
93 public:
94 void add_user(const NodePath &p_path, const Rect2 &p_uv_scale, int p_slice_index, int32_t p_sub_instance = -1);
95 int get_user_count() const;
96 NodePath get_user_path(int p_user) const;
97 int32_t get_user_sub_instance(int p_user) const;
98 Rect2 get_user_lightmap_uv_scale(int p_user) const;
99 int get_user_lightmap_slice_index(int p_user) const;
100 void clear_users();
101
102 #ifndef DISABLE_DEPRECATED
103 void set_light_texture(const Ref<TextureLayered> &p_light_texture);
104 Ref<TextureLayered> get_light_texture() const;
105
106 void _set_light_textures_data(const Array &p_data);
107 Array _get_light_textures_data() const;
108 #endif
109
110 void set_uses_spherical_harmonics(bool p_enable);
111 bool is_using_spherical_harmonics() const;
112
113 void _set_uses_packed_directional(bool p_enable);
114 bool _is_using_packed_directional() const;
115
116 void update_shadowmask_mode(ShadowmaskMode p_mode);
117 ShadowmaskMode get_shadowmask_mode() const;
118
119 bool is_interior() const;
120 float get_baked_exposure() const;
121
122 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, uint32_t p_lightprobe_hash);
123 PackedVector3Array get_capture_points() const;
124 PackedColorArray get_capture_sh() const;
125 PackedInt32Array get_capture_tetrahedra() const;
126 PackedInt32Array get_capture_bsp_tree() const;
127 uint32_t get_lightprobe_hash() const;
128
129 AABB get_capture_bounds() const;
130
131 void clear();
132
133 void set_lightmap_textures(const TypedArray<TextureLayered> &p_data);
134 TypedArray<TextureLayered> get_lightmap_textures() const;
135
136 void set_shadowmask_textures(const TypedArray<TextureLayered> &p_data);
137 TypedArray<TextureLayered> get_shadowmask_textures() const;
138 void clear_shadowmask_textures();
139 bool has_shadowmask_textures();
140
141 virtual RID get_rid() const override;
142 LightmapGIData();
143 ~LightmapGIData();
144 };
145
146 class LightmapGI : public VisualInstance3D {
147 GDCLASS(LightmapGI, VisualInstance3D);
148
149 public:
150 enum BakeQuality {
151 BAKE_QUALITY_LOW,
152 BAKE_QUALITY_MEDIUM,
153 BAKE_QUALITY_HIGH,
154 BAKE_QUALITY_ULTRA,
155 };
156
157 enum GenerateProbes {
158 GENERATE_PROBES_DISABLED,
159 GENERATE_PROBES_SUBDIV_4,
160 GENERATE_PROBES_SUBDIV_8,
161 GENERATE_PROBES_SUBDIV_16,
162 GENERATE_PROBES_SUBDIV_32,
163 };
164
165 enum BakeError {
166 BAKE_ERROR_OK,
167 BAKE_ERROR_NO_SCENE_ROOT,
168 BAKE_ERROR_FOREIGN_DATA,
169 BAKE_ERROR_NO_LIGHTMAPPER,
170 BAKE_ERROR_NO_SAVE_PATH,
171 BAKE_ERROR_NO_MESHES,
172 BAKE_ERROR_MESHES_INVALID,
173 BAKE_ERROR_CANT_CREATE_IMAGE,
174 BAKE_ERROR_USER_ABORTED,
175 BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL,
176 BAKE_ERROR_LIGHTMAP_TOO_SMALL,
177 BAKE_ERROR_ATLAS_TOO_SMALL,
178 };
179
180 enum EnvironmentMode {
181 ENVIRONMENT_MODE_DISABLED,
182 ENVIRONMENT_MODE_SCENE,
183 ENVIRONMENT_MODE_CUSTOM_SKY,
184 ENVIRONMENT_MODE_CUSTOM_COLOR,
185 };
186
187 private:
188 BakeQuality bake_quality = BAKE_QUALITY_MEDIUM;
189 bool use_denoiser = true;
190 float denoiser_strength = 0.1f;
191 int denoiser_range = 10;
192 int bounces = 3;
193 float bounce_indirect_energy = 1.0;
194 float bias = 0.0005;
195 float texel_scale = 1.0;
196 int max_texture_size = 16384;
197 bool supersampling_enabled = false;
198 float supersampling_factor = 2.0;
199 bool interior = false;
200 EnvironmentMode environment_mode = ENVIRONMENT_MODE_SCENE;
201 Ref<Sky> environment_custom_sky;
202 Color environment_custom_color = Color(1, 1, 1);
203 float environment_custom_energy = 1.0;
204 bool directional = false;
205 bool use_texture_for_bounces = true;
206 LightmapGIData::ShadowmaskMode shadowmask_mode = LightmapGIData::SHADOWMASK_MODE_NONE;
207 GenerateProbes gen_probes = GENERATE_PROBES_SUBDIV_8;
208 Ref<CameraAttributes> camera_attributes;
209
210 Ref<LightmapGIData> light_data;
211 Node *last_owner = nullptr;
212
213 struct LightsFound {
214 Transform3D xform;
215 Light3D *light = nullptr;
216 };
217
218 struct MeshesFound {
219 Transform3D xform;
220 NodePath node_path;
221 int32_t subindex = 0;
222 Ref<Mesh> mesh;
223 float lightmap_scale = 0.0;
224 Vector<Ref<Material>> overrides;
225 };
226
227 void _find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes);
228
229 void _assign_lightmaps();
230 void _clear_lightmaps();
231
232 struct BSPSimplex {
233 int vertices[4] = {};
234 int planes[4] = {};
235 };
236
237 struct BSPNode {
238 static const int32_t EMPTY_LEAF = INT32_MIN;
239 Plane plane;
240 int32_t over = EMPTY_LEAF;
241 int32_t under = EMPTY_LEAF;
242 };
243
244 int _bsp_get_simplex_side(const LocalVector<Vector3> &p_points, const LocalVector<BSPSimplex> &p_simplices, const Plane &p_plane, uint32_t p_simplex) const;
245 int32_t _compute_bsp_tree(const LocalVector<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);
246
247 struct BakeStepUD {
248 Lightmapper::BakeStepFunc func;
249 void *ud = nullptr;
250 float from_percent = 0.0;
251 float to_percent = 0.0;
252 };
253
254 static bool _lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh);
255
256 struct GenProbesOctree {
257 Vector3i offset;
258 uint32_t size = 0;
259 GenProbesOctree *children[8] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
260 ~GenProbesOctree() {
261 for (int i = 0; i < 8; i++) {
262 if (children[i] != nullptr) {
263 memdelete(children[i]);
264 }
265 }
266 }
267 };
268
269 void _plot_triangle_into_octree(GenProbesOctree *p_cell, float p_cell_size, const Vector3 *p_triangle);
270 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);
271
272 BakeError _save_and_reimport_atlas_textures(const Ref<Lightmapper> p_lightmapper, const String &p_base_name, TypedArray<TextureLayered> &r_textures, bool p_is_shadowmask = false) const;
273
274 protected:
275 void _validate_property(PropertyInfo &p_property) const;
276 static void _bind_methods();
277 void _notification(int p_what);
278
279 public:
280 void set_light_data(const Ref<LightmapGIData> &p_data);
281 Ref<LightmapGIData> get_light_data() const;
282
283 void set_bake_quality(BakeQuality p_quality);
284 BakeQuality get_bake_quality() const;
285
286 void set_use_denoiser(bool p_enable);
287 bool is_using_denoiser() const;
288
289 void set_denoiser_strength(float p_denoiser_strength);
290 float get_denoiser_strength() const;
291
292 void set_denoiser_range(int p_denoiser_range);
293 int get_denoiser_range() const;
294
295 void set_directional(bool p_enable);
296 bool is_directional() const;
297
298 void set_shadowmask_mode(LightmapGIData::ShadowmaskMode p_mode);
299 LightmapGIData::ShadowmaskMode get_shadowmask_mode() const;
300
301 void set_use_texture_for_bounces(bool p_enable);
302 bool is_using_texture_for_bounces() const;
303
304 void set_interior(bool p_interior);
305 bool is_interior() const;
306
307 void set_environment_mode(EnvironmentMode p_mode);
308 EnvironmentMode get_environment_mode() const;
309
310 void set_environment_custom_sky(const Ref<Sky> &p_sky);
311 Ref<Sky> get_environment_custom_sky() const;
312
313 void set_environment_custom_color(const Color &p_color);
314 Color get_environment_custom_color() const;
315
316 void set_environment_custom_energy(float p_energy);
317 float get_environment_custom_energy() const;
318
319 void set_bounces(int p_bounces);
320 int get_bounces() const;
321
322 void set_bounce_indirect_energy(float p_indirect_energy);
323 float get_bounce_indirect_energy() const;
324
325 void set_bias(float p_bias);
326 float get_bias() const;
327
328 void set_texel_scale(float p_multiplier);
329 float get_texel_scale() const;
330
331 void set_max_texture_size(int p_size);
332 int get_max_texture_size() const;
333
334 void set_supersampling_enabled(bool p_enable);
335 bool is_supersampling_enabled() const;
336
337 void set_supersampling_factor(float p_factor);
338 float get_supersampling_factor() const;
339
340 void set_generate_probes(GenerateProbes p_generate_probes);
341 GenerateProbes get_generate_probes() const;
342
343 void set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes);
344 Ref<CameraAttributes> get_camera_attributes() const;
345
346 AABB get_aabb() const override;
347
348 BakeError bake(Node *p_from_node, String p_image_data_path = "", Lightmapper::BakeStepFunc p_bake_step = nullptr, void *p_bake_userdata = nullptr);
349
350 virtual PackedStringArray get_configuration_warnings() const override;
351
352 LightmapGI();
353 };
354
355 VARIANT_ENUM_CAST(LightmapGIData::ShadowmaskMode);
356 VARIANT_ENUM_CAST(LightmapGI::BakeQuality);
357 VARIANT_ENUM_CAST(LightmapGI::GenerateProbes);
358 VARIANT_ENUM_CAST(LightmapGI::BakeError);
359 VARIANT_ENUM_CAST(LightmapGI::EnvironmentMode);