Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/lightmapper.h
9896 views
1
/**************************************************************************/
2
/* lightmapper.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/object/ref_counted.h"
34
35
class Image;
36
37
class LightmapDenoiser : public RefCounted {
38
GDCLASS(LightmapDenoiser, RefCounted)
39
protected:
40
static LightmapDenoiser *(*create_function)();
41
42
public:
43
virtual Ref<Image> denoise_image(const Ref<Image> &p_image) = 0;
44
static Ref<LightmapDenoiser> create();
45
};
46
47
class LightmapRaycaster : public RefCounted {
48
GDCLASS(LightmapRaycaster, RefCounted)
49
protected:
50
static LightmapRaycaster *(*create_function)();
51
52
public:
53
// Compatible with embree4 rays.
54
struct alignas(16) Ray {
55
const static unsigned int INVALID_GEOMETRY_ID = ((unsigned int)-1); // from rtcore_common.h
56
57
/*! Default construction does nothing. */
58
_FORCE_INLINE_ Ray() :
59
geomID(INVALID_GEOMETRY_ID) {}
60
61
/*! Constructs a ray from origin, direction, and ray segment. Near
62
* has to be smaller than far. */
63
_FORCE_INLINE_ Ray(const Vector3 &p_org,
64
const Vector3 &p_dir,
65
float p_tnear = 0.0f,
66
float p_tfar = Math::INF) :
67
org(p_org),
68
tnear(p_tnear),
69
dir(p_dir),
70
time(0.0f),
71
tfar(p_tfar),
72
mask(-1),
73
u(0.0),
74
v(0.0),
75
primID(INVALID_GEOMETRY_ID),
76
geomID(INVALID_GEOMETRY_ID),
77
instID(INVALID_GEOMETRY_ID) {}
78
79
/*! Tests if we hit something. */
80
_FORCE_INLINE_ explicit operator bool() const {
81
return geomID != INVALID_GEOMETRY_ID;
82
}
83
84
public:
85
Vector3 org; //!< Ray origin + tnear
86
float tnear; //!< Start of ray segment
87
Vector3 dir; //!< Ray direction + tfar
88
float time; //!< Time of this ray for motion blur.
89
float tfar; //!< End of ray segment
90
unsigned int mask; //!< used to mask out objects during traversal
91
unsigned int id; //!< ray ID
92
unsigned int flags; //!< ray flags
93
94
Vector3 normal; //!< Not normalized geometry normal
95
float u; //!< Barycentric u coordinate of hit
96
float v; //!< Barycentric v coordinate of hit
97
unsigned int primID; //!< primitive ID
98
unsigned int geomID; //!< geometry ID
99
unsigned int instID; //!< instance ID
100
};
101
102
virtual bool intersect(Ray &p_ray) = 0;
103
104
virtual void intersect(Vector<Ray> &r_rays) = 0;
105
106
virtual void add_mesh(const Vector<Vector3> &p_vertices, const Vector<Vector3> &p_normals, const Vector<Vector2> &p_uv2s, unsigned int p_id) = 0;
107
virtual void set_mesh_alpha_texture(Ref<Image> p_alpha_texture, unsigned int p_id) = 0;
108
virtual void commit() = 0;
109
110
virtual void set_mesh_filter(const HashSet<int> &p_mesh_ids) = 0;
111
virtual void clear_mesh_filter() = 0;
112
113
static Ref<LightmapRaycaster> create();
114
};
115
116
class Lightmapper : public RefCounted {
117
GDCLASS(Lightmapper, RefCounted)
118
public:
119
enum GenerateProbes {
120
GENERATE_PROBES_DISABLED,
121
GENERATE_PROBES_SUBDIV_4,
122
GENERATE_PROBES_SUBDIV_8,
123
GENERATE_PROBES_SUBDIV_16,
124
GENERATE_PROBES_SUBDIV_32,
125
};
126
127
enum LightType {
128
LIGHT_TYPE_DIRECTIONAL,
129
LIGHT_TYPE_OMNI,
130
LIGHT_TYPE_SPOT
131
};
132
133
enum BakeError {
134
BAKE_OK,
135
BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE,
136
BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES,
137
BAKE_ERROR_ATLAS_TOO_SMALL,
138
BAKE_ERROR_USER_ABORTED,
139
};
140
141
enum BakeQuality {
142
BAKE_QUALITY_LOW,
143
BAKE_QUALITY_MEDIUM,
144
BAKE_QUALITY_HIGH,
145
BAKE_QUALITY_ULTRA,
146
};
147
148
typedef Lightmapper *(*CreateFunc)();
149
150
static CreateFunc create_custom;
151
static CreateFunc create_gpu;
152
static CreateFunc create_cpu;
153
154
protected:
155
public:
156
typedef bool (*BakeStepFunc)(float, const String &, void *, bool); //step index, step total, step description, userdata
157
158
struct MeshData {
159
//triangle data
160
Vector<Vector3> points;
161
Vector<Vector2> uv2;
162
Vector<Vector3> normal;
163
Vector<RID> material;
164
Ref<Image> albedo_on_uv2;
165
Ref<Image> emission_on_uv2;
166
Variant userdata;
167
};
168
169
virtual void add_mesh(const MeshData &p_mesh) = 0;
170
virtual void add_directional_light(const String &p_name, bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_angular_distance, float p_shadow_blur) = 0;
171
virtual void add_omni_light(const String &p_name, bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_size, float p_shadow_blur) = 0;
172
virtual void add_spot_light(const String &p_name, bool p_static, const Vector3 &p_position, const Vector3 p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size, float p_shadow_blur) = 0;
173
virtual void add_probe(const Vector3 &p_position) = 0;
174
virtual BakeError bake(BakeQuality p_quality, bool p_use_denoiser, float p_denoiser_strength, int p_denoiser_range, int p_bounces, float p_bounce_indirect_energy, float p_bias, int p_max_texture_size, bool p_bake_sh, bool p_bake_shadowmask, bool p_texture_for_bounces, GenerateProbes p_generate_probes, const Ref<Image> &p_environment_panorama, const Basis &p_environment_transform, BakeStepFunc p_step_function = nullptr, void *p_step_userdata = nullptr, float p_exposure_normalization = 1.0, float p_supersampling_factor = 1.0) = 0;
175
176
virtual int get_bake_texture_count() const = 0;
177
virtual Ref<Image> get_bake_texture(int p_index) const = 0;
178
virtual int get_shadowmask_texture_count() const = 0;
179
virtual Ref<Image> get_shadowmask_texture(int p_index) const = 0;
180
virtual int get_bake_mesh_count() const = 0;
181
virtual Variant get_bake_mesh_userdata(int p_index) const = 0;
182
virtual Rect2 get_bake_mesh_uv_scale(int p_index) const = 0;
183
virtual int get_bake_mesh_texture_slice(int p_index) const = 0;
184
virtual int get_bake_probe_count() const = 0;
185
virtual Vector3 get_bake_probe_point(int p_probe) const = 0;
186
virtual Vector<Color> get_bake_probe_sh(int p_probe) const = 0;
187
188
static Ref<Lightmapper> create();
189
190
Lightmapper();
191
};
192
193