1 /*************************************************************************/
2 /*  light.h                                                              */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 #ifndef LIGHT_H
32 #define LIGHT_H
33 
34 #include "scene/3d/visual_instance.h"
35 #include "scene/resources/texture.h"
36 #include "servers/visual_server.h"
37 
38 class Light : public VisualInstance {
39 
40 	GDCLASS(Light, VisualInstance);
41 	OBJ_CATEGORY("3D Light Nodes");
42 
43 public:
44 	enum Param {
45 		PARAM_ENERGY = VS::LIGHT_PARAM_ENERGY,
46 		PARAM_INDIRECT_ENERGY = VS::LIGHT_PARAM_INDIRECT_ENERGY,
47 		PARAM_SPECULAR = VS::LIGHT_PARAM_SPECULAR,
48 		PARAM_RANGE = VS::LIGHT_PARAM_RANGE,
49 		PARAM_ATTENUATION = VS::LIGHT_PARAM_ATTENUATION,
50 		PARAM_SPOT_ANGLE = VS::LIGHT_PARAM_SPOT_ANGLE,
51 		PARAM_SPOT_ATTENUATION = VS::LIGHT_PARAM_SPOT_ATTENUATION,
52 		PARAM_CONTACT_SHADOW_SIZE = VS::LIGHT_PARAM_CONTACT_SHADOW_SIZE,
53 		PARAM_SHADOW_MAX_DISTANCE = VS::LIGHT_PARAM_SHADOW_MAX_DISTANCE,
54 		PARAM_SHADOW_SPLIT_1_OFFSET = VS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET,
55 		PARAM_SHADOW_SPLIT_2_OFFSET = VS::LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET,
56 		PARAM_SHADOW_SPLIT_3_OFFSET = VS::LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET,
57 		PARAM_SHADOW_NORMAL_BIAS = VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS,
58 		PARAM_SHADOW_BIAS = VS::LIGHT_PARAM_SHADOW_BIAS,
59 		PARAM_SHADOW_BIAS_SPLIT_SCALE = VS::LIGHT_PARAM_SHADOW_BIAS_SPLIT_SCALE,
60 		PARAM_MAX = VS::LIGHT_PARAM_MAX
61 	};
62 
63 	enum BakeMode {
64 		BAKE_DISABLED,
65 		BAKE_INDIRECT,
66 		BAKE_ALL
67 	};
68 
69 private:
70 	Color color;
71 	float param[PARAM_MAX];
72 	Color shadow_color;
73 	bool shadow;
74 	bool negative;
75 	bool reverse_cull;
76 	uint32_t cull_mask;
77 	VS::LightType type;
78 	bool editor_only;
79 	void _update_visibility();
80 	BakeMode bake_mode;
81 
82 	// bind helpers
83 
84 protected:
85 	RID light;
86 
87 	virtual bool _can_gizmo_scale() const;
88 
89 	static void _bind_methods();
90 	void _notification(int p_what);
91 	virtual void _validate_property(PropertyInfo &property) const;
92 
93 	Light(VisualServer::LightType p_type);
94 
95 public:
get_light_type()96 	VS::LightType get_light_type() const { return type; }
97 
98 	void set_editor_only(bool p_editor_only);
99 	bool is_editor_only() const;
100 
101 	void set_param(Param p_param, float p_value);
102 	float get_param(Param p_param) const;
103 
104 	void set_shadow(bool p_enable);
105 	bool has_shadow() const;
106 
107 	void set_negative(bool p_enable);
108 	bool is_negative() const;
109 
110 	void set_cull_mask(uint32_t p_cull_mask);
111 	uint32_t get_cull_mask() const;
112 
113 	void set_color(const Color &p_color);
114 	Color get_color() const;
115 
116 	void set_shadow_color(const Color &p_shadow_color);
117 	Color get_shadow_color() const;
118 
119 	void set_shadow_reverse_cull_face(bool p_enable);
120 	bool get_shadow_reverse_cull_face() const;
121 
122 	void set_bake_mode(BakeMode p_mode);
123 	BakeMode get_bake_mode() const;
124 
125 	virtual AABB get_aabb() const;
126 	virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
127 
128 	Light();
129 	~Light();
130 };
131 
132 VARIANT_ENUM_CAST(Light::Param);
133 VARIANT_ENUM_CAST(Light::BakeMode);
134 
135 class DirectionalLight : public Light {
136 
137 	GDCLASS(DirectionalLight, Light);
138 
139 public:
140 	enum ShadowMode {
141 		SHADOW_ORTHOGONAL,
142 		SHADOW_PARALLEL_2_SPLITS,
143 		SHADOW_PARALLEL_4_SPLITS
144 	};
145 
146 	enum ShadowDepthRange {
147 		SHADOW_DEPTH_RANGE_STABLE = VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE,
148 		SHADOW_DEPTH_RANGE_OPTIMIZED = VS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_OPTIMIZED,
149 	};
150 
151 private:
152 	bool blend_splits;
153 	ShadowMode shadow_mode;
154 	ShadowDepthRange shadow_depth_range;
155 
156 protected:
157 	static void _bind_methods();
158 
159 public:
160 	void set_shadow_mode(ShadowMode p_mode);
161 	ShadowMode get_shadow_mode() const;
162 
163 	void set_shadow_depth_range(ShadowDepthRange p_range);
164 	ShadowDepthRange get_shadow_depth_range() const;
165 
166 	void set_blend_splits(bool p_enable);
167 	bool is_blend_splits_enabled() const;
168 
169 	DirectionalLight();
170 };
171 
172 VARIANT_ENUM_CAST(DirectionalLight::ShadowMode)
VARIANT_ENUM_CAST(DirectionalLight::ShadowDepthRange)173 VARIANT_ENUM_CAST(DirectionalLight::ShadowDepthRange)
174 
175 class OmniLight : public Light {
176 
177 	GDCLASS(OmniLight, Light);
178 
179 public:
180 	// omni light
181 	enum ShadowMode {
182 		SHADOW_DUAL_PARABOLOID,
183 		SHADOW_CUBE,
184 	};
185 
186 	// omni light
187 	enum ShadowDetail {
188 		SHADOW_DETAIL_VERTICAL,
189 		SHADOW_DETAIL_HORIZONTAL
190 	};
191 
192 private:
193 	ShadowMode shadow_mode;
194 	ShadowDetail shadow_detail;
195 
196 protected:
197 	static void _bind_methods();
198 
199 public:
200 	void set_shadow_mode(ShadowMode p_mode);
201 	ShadowMode get_shadow_mode() const;
202 
203 	void set_shadow_detail(ShadowDetail p_detail);
204 	ShadowDetail get_shadow_detail() const;
205 
206 	OmniLight();
207 };
208 
209 VARIANT_ENUM_CAST(OmniLight::ShadowMode)
VARIANT_ENUM_CAST(OmniLight::ShadowDetail)210 VARIANT_ENUM_CAST(OmniLight::ShadowDetail)
211 
212 class SpotLight : public Light {
213 
214 	GDCLASS(SpotLight, Light);
215 
216 protected:
217 	static void _bind_methods();
218 
219 public:
220 	virtual String get_configuration_warning() const;
221 
222 	SpotLight() :
223 			Light(VisualServer::LIGHT_SPOT) {}
224 };
225 
226 #endif
227