summaryrefslogtreecommitdiff
path: root/scene/3d/baked_light_instance.h
blob: 2fda26eceaeafeeb9b27eb9fcd62446769716057 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*************************************************************************/
/*  baked_light_instance.h                                               */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* 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 */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* 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.                */
/*************************************************************************/
#ifndef BAKED_LIGHT_INSTANCE_H
#define BAKED_LIGHT_INSTANCE_H

#include "scene/3d/visual_instance.h"
#include "scene/resources/baked_light.h"
#include "scene/3d/multimesh_instance.h"


class BakedLightBaker;
class Light;

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

public:
	enum DebugMode {
		DEBUG_ALBEDO,
		DEBUG_LIGHT
	};

private:
	RID baked_light;
	int cell_subdiv;
	Rect3 bounds;
	int cells_per_axis;

	enum {
		CHILD_EMPTY=0xFFFFFFFF,
	};


	/* BAKE DATA */

	struct BakeCell {

		uint32_t childs[8];
		float albedo[3]; //albedo in RGB24
		float light[3]; //accumulated light in 16:16 fixed point (needs to be integer for moving lights fast)
		float radiance[3]; //accumulated light in 16:16 fixed point (needs to be integer for moving lights fast)
		uint32_t used_sides;
		float alpha; //used for upsampling
		uint32_t light_pass; //used for baking light

		BakeCell() {
			for(int i=0;i<8;i++) {
				childs[i]=0xFFFFFFFF;
			}

			for(int i=0;i<3;i++) {
				light[i]=0;
				albedo[i]=0;
				radiance[i]=0;
			}
			alpha=0;
			light_pass=0;
			used_sides=0;
		}
	};


	int bake_texture_size;
	int color_scan_cell_width;

	struct MaterialCache {
		//128x128 textures
		Vector<Color> albedo;
		Vector<Color> emission;
	};


	Vector<Color> _get_bake_texture(Image &p_image, const Color &p_color);



	Map<Ref<Material>,MaterialCache> material_cache;
	MaterialCache _get_material_cache(Ref<Material> p_material);

	int bake_cells_alloc;
	int bake_cells_used;
	int zero_alphas;
	Vector<int> bake_cells_level_used;
	PoolVector<BakeCell> bake_cells;
	PoolVector<BakeCell>::Write bake_cells_write;



	void _plot_face(int p_idx,int p_level,const Vector3 *p_vtx,const Vector2* p_uv, const MaterialCache& p_material,const Rect3& p_aabb);
	void _fixup_plot(int p_idx, int p_level, int p_x, int p_y, int p_z);
	void _bake_add_mesh(const Transform& p_xform,Ref<Mesh>& p_mesh);
	void _bake_add_to_aabb(const Transform& p_xform,Ref<Mesh>& p_mesh,bool &first);

	void _debug_mesh(int p_idx, int p_level, const Rect3 &p_aabb,DebugMode p_mode,Ref<MultiMesh> &p_multimesh,int &idx);
	void _debug_mesh_albedo();
	void _debug_mesh_light();


	_FORCE_INLINE_ int _find_cell(int x,int y, int z);
	int _plot_ray(const Vector3& p_from, const Vector3& p_to);

	uint32_t light_pass;


	void _bake_directional(int p_idx, int p_level, int p_x,int p_y,int p_z,const Vector3& p_dir,const Color& p_color,int p_sign);
	void _upscale_light(int p_idx,int p_level);
	void _bake_light(Light* p_light);

	Color _cone_trace(const Vector3& p_from, const Vector3& p_dir, float p_half_angle);
	void _bake_radiance(int p_idx, int p_level, int p_x,int p_y,int p_z);

friend class GeometryInstance;

	Set<GeometryInstance*> geometries;
friend class Light;

	Set<Light*> lights;
protected:

	static void _bind_methods();
public:

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

	void bake();
	void bake_lights();
	void bake_radiance();


	void create_debug_mesh(DebugMode p_mode);

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

	String get_configuration_warning() const;

	BakedLight();
	~BakedLight();
};


#if 0
class BakedLightSampler : public VisualInstance {
	GDCLASS(BakedLightSampler,VisualInstance);


public:

	enum Param {
		PARAM_RADIUS=VS::BAKED_LIGHT_SAMPLER_RADIUS,
		PARAM_STRENGTH=VS::BAKED_LIGHT_SAMPLER_STRENGTH,
		PARAM_ATTENUATION=VS::BAKED_LIGHT_SAMPLER_ATTENUATION,
		PARAM_DETAIL_RATIO=VS::BAKED_LIGHT_SAMPLER_DETAIL_RATIO,
		PARAM_MAX=VS::BAKED_LIGHT_SAMPLER_MAX
	};



protected:

	RID base;
	float params[PARAM_MAX];
	int resolution;
	static void _bind_methods();
public:

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

	void set_param(Param p_param,float p_value);
	float get_param(Param p_param) const;

	void set_resolution(int p_resolution);
	int get_resolution() const;

	BakedLightSampler();
	~BakedLightSampler();
};

VARIANT_ENUM_CAST( BakedLightSampler::Param );

#endif
#endif // BAKED_LIGHT_H