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