virtualx-engine/scene/3d/light.cpp

404 lines
15 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* light.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* 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. */
/*************************************************************************/
#include "light.h"
2016-03-09 00:00:52 +01:00
#include "project_settings.h"
2014-02-10 02:10:30 +01:00
#include "scene/resources/surface_tool.h"
bool Light::_can_gizmo_scale() const {
return false;
}
void Light::set_param(Param p_param, float p_value) {
2014-02-10 02:10:30 +01:00
ERR_FAIL_INDEX(p_param, PARAM_MAX);
param[p_param] = p_value;
2014-02-10 02:10:30 +01:00
VS::get_singleton()->light_set_param(light, VS::LightParam(p_param), p_value);
2014-02-10 02:10:30 +01:00
if (p_param == PARAM_SPOT_ANGLE || p_param == PARAM_RANGE) {
2017-01-14 18:03:38 +01:00
update_gizmo();
}
2014-02-10 02:10:30 +01:00
}
float Light::get_param(Param p_param) const {
2014-02-10 02:10:30 +01:00
ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
return param[p_param];
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
void Light::set_shadow(bool p_enable) {
2014-02-10 02:10:30 +01:00
shadow = p_enable;
VS::get_singleton()->light_set_shadow(light, p_enable);
2014-02-10 02:10:30 +01:00
}
bool Light::has_shadow() const {
2014-02-10 02:10:30 +01:00
return shadow;
2014-02-10 02:10:30 +01:00
}
void Light::set_negative(bool p_enable) {
2014-02-10 02:10:30 +01:00
negative = p_enable;
VS::get_singleton()->light_set_negative(light, p_enable);
2014-02-10 02:10:30 +01:00
}
bool Light::is_negative() const {
2014-02-10 02:10:30 +01:00
return negative;
2014-02-10 02:10:30 +01:00
}
void Light::set_cull_mask(uint32_t p_cull_mask) {
2014-02-10 02:10:30 +01:00
cull_mask = p_cull_mask;
VS::get_singleton()->light_set_cull_mask(light, p_cull_mask);
}
uint32_t Light::get_cull_mask() const {
2014-02-10 02:10:30 +01:00
return cull_mask;
}
2014-02-10 02:10:30 +01:00
void Light::set_color(const Color &p_color) {
2014-02-10 02:10:30 +01:00
color = p_color;
VS::get_singleton()->light_set_color(light, p_color);
}
Color Light::get_color() const {
2016-03-09 00:00:52 +01:00
return color;
}
2014-02-10 02:10:30 +01:00
void Light::set_shadow_color(const Color &p_shadow_color) {
2014-02-10 02:10:30 +01:00
shadow_color = p_shadow_color;
VS::get_singleton()->light_set_shadow_color(light, p_shadow_color);
}
2014-02-10 02:10:30 +01:00
Color Light::get_shadow_color() const {
2014-02-10 02:10:30 +01:00
return shadow_color;
2014-02-10 02:10:30 +01:00
}
void Light::set_shadow_reverse_cull_face(bool p_enable) {
reverse_cull = p_enable;
VS::get_singleton()->light_set_reverse_cull_face_mode(light, reverse_cull);
}
bool Light::get_shadow_reverse_cull_face() const {
return reverse_cull;
}
Rect3 Light::get_aabb() const {
2014-02-10 02:10:30 +01:00
if (type == VisualServer::LIGHT_DIRECTIONAL) {
return Rect3(Vector3(-1, -1, -1), Vector3(2, 2, 2));
2016-03-09 00:00:52 +01:00
} else if (type == VisualServer::LIGHT_OMNI) {
2016-03-09 00:00:52 +01:00
return Rect3(Vector3(-1, -1, -1) * param[PARAM_RANGE], Vector3(2, 2, 2) * param[PARAM_RANGE]);
2016-03-09 00:00:52 +01:00
} else if (type == VisualServer::LIGHT_SPOT) {
2016-03-09 00:00:52 +01:00
float len = param[PARAM_RANGE];
float size = Math::tan(Math::deg2rad(param[PARAM_SPOT_ANGLE])) * len;
return Rect3(Vector3(-size, -size, -len), Vector3(size * 2, size * 2, len));
2014-02-10 02:10:30 +01:00
}
return Rect3();
2014-02-10 02:10:30 +01:00
}
PoolVector<Face3> Light::get_faces(uint32_t p_usage_flags) const {
2014-02-10 02:10:30 +01:00
return PoolVector<Face3>();
2014-02-10 02:10:30 +01:00
}
void Light::_update_visibility() {
if (!is_inside_tree())
return;
bool editor_ok = true;
#ifdef TOOLS_ENABLED
if (editor_only) {
if (!get_tree()->is_editor_hint()) {
editor_ok = false;
} else {
editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
}
}
#else
if (editor_only) {
editor_ok = false;
}
#endif
//VS::get_singleton()->instance_light_set_enabled(get_instance(),is_visible_in_tree() && editor_ok);
_change_notify("geometry/visible");
}
void Light::_notification(int p_what) {
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
2016-12-20 04:21:07 +01:00
_update_visibility();
}
if (p_what == NOTIFICATION_ENTER_TREE) {
2016-12-20 04:21:07 +01:00
_update_visibility();
}
if (p_what == NOTIFICATION_EXIT_TREE) {
2016-12-20 04:21:07 +01:00
}
}
void Light::set_editor_only(bool p_editor_only) {
editor_only = p_editor_only;
_update_visibility();
}
bool Light::is_editor_only() const {
return editor_only;
}
2014-02-10 02:10:30 +01:00
void Light::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Light::set_editor_only);
ClassDB::bind_method(D_METHOD("is_editor_only"), &Light::is_editor_only);
ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &Light::set_param);
ClassDB::bind_method(D_METHOD("get_param", "param"), &Light::get_param);
ClassDB::bind_method(D_METHOD("set_shadow", "enabled"), &Light::set_shadow);
ClassDB::bind_method(D_METHOD("has_shadow"), &Light::has_shadow);
ClassDB::bind_method(D_METHOD("set_negative", "enabled"), &Light::set_negative);
ClassDB::bind_method(D_METHOD("is_negative"), &Light::is_negative);
ClassDB::bind_method(D_METHOD("set_cull_mask", "cull_mask"), &Light::set_cull_mask);
ClassDB::bind_method(D_METHOD("get_cull_mask"), &Light::get_cull_mask);
ClassDB::bind_method(D_METHOD("set_color", "color"), &Light::set_color);
ClassDB::bind_method(D_METHOD("get_color"), &Light::get_color);
ClassDB::bind_method(D_METHOD("set_shadow_reverse_cull_face", "enable"), &Light::set_shadow_reverse_cull_face);
ClassDB::bind_method(D_METHOD("get_shadow_reverse_cull_face"), &Light::get_shadow_reverse_cull_face);
ClassDB::bind_method(D_METHOD("set_shadow_color", "shadow_color"), &Light::set_shadow_color);
ClassDB::bind_method(D_METHOD("get_shadow_color"), &Light::get_shadow_color);
ADD_GROUP("Light", "light_");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "light_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_color", "get_color");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "light_energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_param", "get_param", PARAM_ENERGY);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "light_negative"), "set_negative", "is_negative");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "light_specular", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_SPECULAR);
ADD_PROPERTY(PropertyInfo(Variant::INT, "light_cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
ADD_GROUP("Shadow", "shadow_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_enabled"), "set_shadow", "has_shadow");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_shadow_color", "get_shadow_color");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "shadow_bias", PROPERTY_HINT_RANGE, "-16,16,0.01"), "set_param", "get_param", PARAM_SHADOW_BIAS);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "shadow_contact", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_param", "get_param", PARAM_CONTACT_SHADOW_SIZE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "shadow_max_distance", PROPERTY_HINT_RANGE, "0,65536,0.1"), "set_param", "get_param", PARAM_SHADOW_MAX_DISTANCE);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_reverse_cull_face"), "set_shadow_reverse_cull_face", "get_shadow_reverse_cull_face");
ADD_GROUP("Editor", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
ADD_GROUP("", "");
BIND_CONSTANT(PARAM_ENERGY);
BIND_CONSTANT(PARAM_SPECULAR);
BIND_CONSTANT(PARAM_RANGE);
BIND_CONSTANT(PARAM_ATTENUATION);
BIND_CONSTANT(PARAM_SPOT_ANGLE);
BIND_CONSTANT(PARAM_SPOT_ATTENUATION);
BIND_CONSTANT(PARAM_CONTACT_SHADOW_SIZE);
BIND_CONSTANT(PARAM_SHADOW_MAX_DISTANCE);
BIND_CONSTANT(PARAM_SHADOW_SPLIT_1_OFFSET);
BIND_CONSTANT(PARAM_SHADOW_SPLIT_2_OFFSET);
BIND_CONSTANT(PARAM_SHADOW_SPLIT_3_OFFSET);
BIND_CONSTANT(PARAM_SHADOW_NORMAL_BIAS);
BIND_CONSTANT(PARAM_SHADOW_BIAS);
BIND_CONSTANT(PARAM_MAX);
2014-02-10 02:10:30 +01:00
}
Light::Light(VisualServer::LightType p_type) {
type = p_type;
light = VisualServer::get_singleton()->light_create(p_type);
VS::get_singleton()->instance_set_base(get_instance(), light);
reverse_cull = false;
editor_only = false;
set_color(Color(1, 1, 1, 1));
set_shadow(false);
set_negative(false);
set_cull_mask(0xFFFFFFFF);
set_param(PARAM_ENERGY, 1);
set_param(PARAM_SPECULAR, 0.5);
set_param(PARAM_RANGE, 5);
set_param(PARAM_ATTENUATION, 1);
set_param(PARAM_SPOT_ANGLE, 45);
set_param(PARAM_SPOT_ATTENUATION, 1);
set_param(PARAM_CONTACT_SHADOW_SIZE, 0);
set_param(PARAM_SHADOW_MAX_DISTANCE, 0);
set_param(PARAM_SHADOW_SPLIT_1_OFFSET, 0.1);
set_param(PARAM_SHADOW_SPLIT_2_OFFSET, 0.2);
set_param(PARAM_SHADOW_SPLIT_3_OFFSET, 0.5);
set_param(PARAM_SHADOW_NORMAL_BIAS, 0.0);
set_param(PARAM_SHADOW_BIAS, 0.15);
2014-02-10 02:10:30 +01:00
}
Light::Light() {
type = VisualServer::LIGHT_DIRECTIONAL;
2014-02-10 02:10:30 +01:00
ERR_PRINT("Light shouldn't be instanced dircetly, use the subtypes.");
}
Light::~Light() {
VS::get_singleton()->instance_set_base(get_instance(), RID());
2014-02-10 02:10:30 +01:00
if (light.is_valid())
VisualServer::get_singleton()->free(light);
}
/////////////////////////////////////////
void DirectionalLight::set_shadow_mode(ShadowMode p_mode) {
shadow_mode = p_mode;
VS::get_singleton()->light_directional_set_shadow_mode(light, VS::LightDirectionalShadowMode(p_mode));
2014-02-10 02:10:30 +01:00
}
DirectionalLight::ShadowMode DirectionalLight::get_shadow_mode() const {
2014-02-10 02:10:30 +01:00
return shadow_mode;
}
void DirectionalLight::set_blend_splits(bool p_enable) {
2014-02-10 02:10:30 +01:00
blend_splits = p_enable;
VS::get_singleton()->light_directional_set_blend_splits(light, p_enable);
2014-02-10 02:10:30 +01:00
}
bool DirectionalLight::is_blend_splits_enabled() const {
return blend_splits;
2014-02-10 02:10:30 +01:00
}
void DirectionalLight::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_shadow_mode", "mode"), &DirectionalLight::set_shadow_mode);
ClassDB::bind_method(D_METHOD("get_shadow_mode"), &DirectionalLight::get_shadow_mode);
2014-02-10 02:10:30 +01:00
ClassDB::bind_method(D_METHOD("set_blend_splits", "enabled"), &DirectionalLight::set_blend_splits);
ClassDB::bind_method(D_METHOD("is_blend_splits_enabled"), &DirectionalLight::is_blend_splits_enabled);
ADD_GROUP("Directional Shadow", "directional_shadow_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "directional_shadow_mode", PROPERTY_HINT_ENUM, "Orthogonal,PSSM 2 Splits,PSSM 4 Splits"), "set_shadow_mode", "get_shadow_mode");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_split_1", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_param", "get_param", PARAM_SHADOW_SPLIT_1_OFFSET);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_split_2", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_param", "get_param", PARAM_SHADOW_SPLIT_2_OFFSET);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_split_3", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_param", "get_param", PARAM_SHADOW_SPLIT_3_OFFSET);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "directional_shadow_blend_splits"), "set_blend_splits", "is_blend_splits_enabled");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "directional_shadow_normal_bias", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_param", "get_param", PARAM_SHADOW_NORMAL_BIAS);
2014-02-10 02:10:30 +01:00
BIND_CONSTANT(SHADOW_ORTHOGONAL);
BIND_CONSTANT(SHADOW_PARALLEL_2_SPLITS);
BIND_CONSTANT(SHADOW_PARALLEL_4_SPLITS);
2014-02-10 02:10:30 +01:00
}
DirectionalLight::DirectionalLight()
: Light(VisualServer::LIGHT_DIRECTIONAL) {
2014-02-10 02:10:30 +01:00
set_param(PARAM_SHADOW_NORMAL_BIAS, 0.2);
set_param(PARAM_SHADOW_BIAS, 1.0);
set_param(PARAM_SHADOW_MAX_DISTANCE, 200);
set_shadow_mode(SHADOW_PARALLEL_4_SPLITS);
blend_splits = false;
}
2014-02-10 02:10:30 +01:00
void OmniLight::set_shadow_mode(ShadowMode p_mode) {
shadow_mode = p_mode;
VS::get_singleton()->light_omni_set_shadow_mode(light, VS::LightOmniShadowMode(p_mode));
2014-02-10 02:10:30 +01:00
}
OmniLight::ShadowMode OmniLight::get_shadow_mode() const {
return shadow_mode;
}
void OmniLight::set_shadow_detail(ShadowDetail p_detail) {
shadow_detail = p_detail;
VS::get_singleton()->light_omni_set_shadow_detail(light, VS::LightOmniShadowDetail(p_detail));
}
OmniLight::ShadowDetail OmniLight::get_shadow_detail() const {
return shadow_detail;
}
2014-02-10 02:10:30 +01:00
void OmniLight::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_shadow_mode", "mode"), &OmniLight::set_shadow_mode);
ClassDB::bind_method(D_METHOD("get_shadow_mode"), &OmniLight::get_shadow_mode);
ClassDB::bind_method(D_METHOD("set_shadow_detail", "detail"), &OmniLight::set_shadow_detail);
ClassDB::bind_method(D_METHOD("get_shadow_detail"), &OmniLight::get_shadow_detail);
2014-02-10 02:10:30 +01:00
ADD_GROUP("Omni", "omni_");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "omni_range", PROPERTY_HINT_RANGE, "0,65536,0.1"), "set_param", "get_param", PARAM_RANGE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "omni_attenuation", PROPERTY_HINT_EXP_EASING), "set_param", "get_param", PARAM_ATTENUATION);
ADD_PROPERTY(PropertyInfo(Variant::INT, "omni_shadow_mode", PROPERTY_HINT_ENUM, "Dual Paraboloid,Cube"), "set_shadow_mode", "get_shadow_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "omni_shadow_detail", PROPERTY_HINT_ENUM, "Vertical,Horizontal"), "set_shadow_detail", "get_shadow_detail");
2014-02-10 02:10:30 +01:00
}
OmniLight::OmniLight()
: Light(VisualServer::LIGHT_OMNI) {
set_shadow_mode(SHADOW_CUBE);
set_shadow_detail(SHADOW_DETAIL_HORIZONTAL);
2014-02-10 02:10:30 +01:00
}
void SpotLight::_bind_methods() {
ADD_GROUP("Spot", "spot_");
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "spot_range", PROPERTY_HINT_RANGE, "0,65536,0.1"), "set_param", "get_param", PARAM_RANGE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "spot_attenuation", PROPERTY_HINT_EXP_EASING), "set_param", "get_param", PARAM_ATTENUATION);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "spot_angle", PROPERTY_HINT_RANGE, "0,180,0.1"), "set_param", "get_param", PARAM_SPOT_ANGLE);
ADD_PROPERTYI(PropertyInfo(Variant::REAL, "spot_angle_attenuation", PROPERTY_HINT_EXP_EASING), "set_param", "get_param", PARAM_SPOT_ATTENUATION);
2014-02-10 02:10:30 +01:00
}