summaryrefslogtreecommitdiff
path: root/scene/3d/baked_lightmap.h
blob: 9e15f1bb108d0b9cb0c4a1539ec9cb9c2c1696fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef BAKED_INDIRECT_LIGHT_H
#define BAKED_INDIRECT_LIGHT_H

#include "multimesh_instance.h"
#include "scene/3d/light.h"
#include "scene/3d/visual_instance.h"

class BakedLightmapData : public Resource {
	GDCLASS(BakedLightmapData, Resource);

	RID baked_light;
	AABB bounds;
	float energy;
	int cell_subdiv;
	Transform cell_space_xform;

	struct User {

		NodePath path;
		Ref<Texture> lightmap;
		int instance_index;
	};

	Vector<User> users;

	void _set_user_data(const Array &p_data);
	Array _get_user_data() const;

protected:
	static void _bind_methods();

public:
	void set_bounds(const AABB &p_bounds);
	AABB get_bounds() const;

	void set_octree(const PoolVector<uint8_t> &p_octree);
	PoolVector<uint8_t> get_octree() const;

	void set_cell_space_transform(const Transform &p_xform);
	Transform get_cell_space_transform() const;

	void set_cell_subdiv(int p_cell_subdiv);
	int get_cell_subdiv() const;

	void set_energy(float p_energy);
	float get_energy() const;

	void add_user(const NodePath &p_path, const Ref<Texture> &p_lightmap, int p_instance = -1);
	int get_user_count() const;
	NodePath get_user_path(int p_user) const;
	Ref<Texture> get_user_lightmap(int p_user) const;
	int get_user_instance(int p_user) const;
	void clear_users();

	virtual RID get_rid() const;
	BakedLightmapData();
	~BakedLightmapData();
};

class BakedLightmap : public VisualInstance {
	GDCLASS(BakedLightmap, VisualInstance);

public:
	enum Subdiv {
		SUBDIV_128,
		SUBDIV_256,
		SUBDIV_512,
		SUBDIV_1024,
		SUBDIV_MAX

	};

	enum BakeQuality {
		BAKE_QUALITY_LOW,
		BAKE_QUALITY_MEDIUM,
		BAKE_QUALITY_HIGH
	};

	enum BakeMode {
		BAKE_MODE_CONE_TRACE,
		BAKE_MODE_RAY_TRACE,
	};

	enum BakeError {
		BAKE_ERROR_OK,
		BAKE_ERROR_NO_SAVE_PATH,
		BAKE_ERROR_NO_MESHES,
		BAKE_ERROR_CANT_CREATE_IMAGE,
		BAKE_ERROR_USER_ABORTED

	};

	typedef void (*BakeBeginFunc)(int);
	typedef bool (*BakeStepFunc)(int, const String &);
	typedef void (*BakeEndFunc)();

private:
	Subdiv bake_subdiv;
	Subdiv capture_subdiv;
	Vector3 extents;
	float propagation;
	float energy;
	BakeQuality bake_quality;
	BakeMode bake_mode;
	bool hdr;
	String image_path;

	Ref<BakedLightmapData> light_data;

	struct PlotMesh {
		Ref<Material> override_material;
		Vector<Ref<Material> > instance_materials;
		Ref<Mesh> mesh;
		Transform local_xform;
		NodePath path;
		int instance_idx;
	};

	struct PlotLight {
		Light *light;
		Transform local_xform;
	};

	void _find_meshes_and_lights(Node *p_at_node, List<PlotMesh> &plot_meshes, List<PlotLight> &plot_lights);

	void _debug_bake();

	void _assign_lightmaps();
	void _clear_lightmaps();

	static bool _bake_time(void *ud, float p_secs, float p_progress);

	struct BakeTimeData {
		String text;
		int pass;
		uint64_t last_step;
	};

protected:
	static void _bind_methods();
	void _notification(int p_what);

public:
	static BakeBeginFunc bake_begin_function;
	static BakeStepFunc bake_step_function;
	static BakeEndFunc bake_end_function;

	void set_light_data(const Ref<BakedLightmapData> &p_data);
	Ref<BakedLightmapData> get_light_data() const;

	void set_bake_subdiv(Subdiv p_subdiv);
	Subdiv get_bake_subdiv() const;

	void set_capture_subdiv(Subdiv p_subdiv);
	Subdiv get_capture_subdiv() const;

	void set_extents(const Vector3 &p_extents);
	Vector3 get_extents() const;

	void set_propagation(float p_propagation);
	float get_propagation() const;

	void set_energy(float p_energy);
	float get_energy() const;

	void set_bake_quality(BakeQuality p_quality);
	BakeQuality get_bake_quality() const;

	void set_bake_mode(BakeMode p_mode);
	BakeMode get_bake_mode() const;

	void set_hdr(bool p_enable);
	bool is_hdr() const;

	void set_image_path(const String &p_path);
	String get_image_path() const;

	AABB get_aabb() const;
	PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;

	BakeError bake(Node *p_from_node, bool p_create_visual_debug = false);
	BakedLightmap();
};

VARIANT_ENUM_CAST(BakedLightmap::Subdiv);
VARIANT_ENUM_CAST(BakedLightmap::BakeQuality);
VARIANT_ENUM_CAST(BakedLightmap::BakeMode);
VARIANT_ENUM_CAST(BakedLightmap::BakeError);

#endif // BAKED_INDIRECT_LIGHT_H