Skip to content
0.2.0-dev Download ZIP
148 lines ·6.0 KB Raw
1/**************************************************************************/
2/* fog_volume.cpp */
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#include "fog_volume.h"
32
33#include "core/object/class_db.h"
34#include "core/os/os.h"
35#include "scene/main/viewport.h"
36#include "scene/resources/environment.h"
37#include "servers/rendering/rendering_server.h"
38
39///////////////////////////
40
41void FogVolume::_bind_methods() {
42 ClassDB::bind_method(D_METHOD("set_size", "size"), &FogVolume::set_size);
43 ClassDB::bind_method(D_METHOD("get_size"), &FogVolume::get_size);
44 ClassDB::bind_method(D_METHOD("set_shape", "shape"), &FogVolume::set_shape);
45 ClassDB::bind_method(D_METHOD("get_shape"), &FogVolume::get_shape);
46 ClassDB::bind_method(D_METHOD("set_material", "material"), &FogVolume::set_material);
47 ClassDB::bind_method(D_METHOD("get_material"), &FogVolume::get_material);
48
49 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:m"), "set_size", "get_size");
50 ADD_PROPERTY(PropertyInfo(Variant::INT, "shape", PROPERTY_HINT_ENUM, "Ellipsoid (Local),Cone (Local),Cylinder (Local),Box (Local),World (Global)"), "set_shape", "get_shape");
51 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "FogMaterial,ShaderMaterial", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_material", "get_material");
52}
53
54void FogVolume::_validate_property(PropertyInfo &p_property) const {
55 if (p_property.name == "size" && shape == RSE::FOG_VOLUME_SHAPE_WORLD) {
56 p_property.usage = PROPERTY_USAGE_NONE;
57 }
58}
59
60#ifndef DISABLE_DEPRECATED
61bool FogVolume::_set(const StringName &p_name, const Variant &p_value) {
62 if (p_name == "extents") { // Compatibility with Godot 3.x.
63 set_size((Vector3)p_value * 2);
64 return true;
65 }
66 return false;
67}
68
69bool FogVolume::_get(const StringName &p_name, Variant &r_property) const {
70 if (p_name == "extents") { // Compatibility with Godot 3.x.
71 r_property = size / 2;
72 return true;
73 }
74 return false;
75}
76#endif // DISABLE_DEPRECATED
77
78void FogVolume::set_size(const Vector3 &p_size) {
79 size = p_size;
80 size = size.maxf(0);
81 RS::get_singleton()->fog_volume_set_size(_get_volume(), size);
82 update_gizmos();
83}
84
85Vector3 FogVolume::get_size() const {
86 return size;
87}
88
89void FogVolume::set_shape(RSE::FogVolumeShape p_type) {
90 shape = p_type;
91 RS::get_singleton()->fog_volume_set_shape(_get_volume(), shape);
92 RS::get_singleton()->instance_set_ignore_culling(get_instance(), shape == RSE::FOG_VOLUME_SHAPE_WORLD);
93 update_gizmos();
94 notify_property_list_changed();
95}
96
97RSE::FogVolumeShape FogVolume::get_shape() const {
98 return shape;
99}
100
101void FogVolume::set_material(const Ref<Material> &p_material) {
102 material = p_material;
103 RID material_rid;
104 if (material.is_valid()) {
105 material_rid = material->get_rid();
106 }
107 RS::get_singleton()->fog_volume_set_material(_get_volume(), material_rid);
108 update_gizmos();
109}
110
111Ref<Material> FogVolume::get_material() const {
112 return material;
113}
114
115AABB FogVolume::get_aabb() const {
116 if (shape != RSE::FOG_VOLUME_SHAPE_WORLD) {
117 return AABB(-size / 2, size);
118 }
119 return AABB();
120}
121
122PackedStringArray FogVolume::get_configuration_warnings() const {
123 PackedStringArray warnings = VisualInstance3D::get_configuration_warnings();
124
125 Ref<Environment> environment = get_viewport()->find_world_3d()->get_environment();
126
127 if (OS::get_singleton()->get_current_rendering_method() != "forward_plus") {
128 warnings.push_back(RTR("Fog Volumes are only visible when using the Forward+ renderer."));
129 return warnings;
130 }
131
132 if (environment.is_valid() && !environment->is_volumetric_fog_enabled()) {
133 warnings.push_back(RTR("Fog Volumes need volumetric fog to be enabled in the scene's Environment in order to be visible."));
134 }
135
136 return warnings;
137}
138
139FogVolume::FogVolume() {
140 volume = RS::get_singleton()->fog_volume_create();
141 RS::get_singleton()->fog_volume_set_shape(volume, RSE::FOG_VOLUME_SHAPE_BOX);
142 set_base(volume);
143}
144
145FogVolume::~FogVolume() {
146 ERR_FAIL_NULL(RenderingServer::get_singleton());
147 RS::get_singleton()->free_rid(volume);
148}