1 | /**************************************************************************/ |
2 | /* voxel_gi.h */ |
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 | #ifndef VOXEL_GI_H |
32 | #define VOXEL_GI_H |
33 | |
34 | #include "scene/3d/visual_instance_3d.h" |
35 | |
36 | class CameraAttributes; |
37 | |
38 | class VoxelGIData : public Resource { |
39 | GDCLASS(VoxelGIData, Resource); |
40 | |
41 | RID probe; |
42 | |
43 | void _set_data(const Dictionary &p_data); |
44 | Dictionary _get_data() const; |
45 | |
46 | Transform3D to_cell_xform; |
47 | AABB bounds; |
48 | Vector3 octree_size; |
49 | |
50 | float dynamic_range = 2.0; |
51 | float energy = 1.0; |
52 | float bias = 1.5; |
53 | float normal_bias = 0.0; |
54 | float propagation = 0.5; |
55 | bool interior = false; |
56 | bool use_two_bounces = true; |
57 | |
58 | protected: |
59 | static void _bind_methods(); |
60 | |
61 | public: |
62 | void allocate(const Transform3D &p_to_cell_xform, const AABB &p_aabb, const Vector3 &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts); |
63 | AABB get_bounds() const; |
64 | Vector3 get_octree_size() const; |
65 | Vector<uint8_t> get_octree_cells() const; |
66 | Vector<uint8_t> get_data_cells() const; |
67 | Vector<uint8_t> get_distance_field() const; |
68 | Vector<int> get_level_counts() const; |
69 | Transform3D get_to_cell_xform() const; |
70 | |
71 | void set_dynamic_range(float p_range); |
72 | float get_dynamic_range() const; |
73 | |
74 | void set_propagation(float p_propagation); |
75 | float get_propagation() const; |
76 | |
77 | void set_energy(float p_energy); |
78 | float get_energy() const; |
79 | |
80 | void set_bias(float p_bias); |
81 | float get_bias() const; |
82 | |
83 | void set_normal_bias(float p_normal_bias); |
84 | float get_normal_bias() const; |
85 | |
86 | void set_interior(bool p_enable); |
87 | bool is_interior() const; |
88 | |
89 | void set_use_two_bounces(bool p_enable); |
90 | bool is_using_two_bounces() const; |
91 | |
92 | virtual RID get_rid() const override; |
93 | |
94 | VoxelGIData(); |
95 | ~VoxelGIData(); |
96 | }; |
97 | |
98 | class VoxelGI : public VisualInstance3D { |
99 | GDCLASS(VoxelGI, VisualInstance3D); |
100 | |
101 | public: |
102 | enum Subdiv { |
103 | SUBDIV_64, |
104 | SUBDIV_128, |
105 | SUBDIV_256, |
106 | SUBDIV_512, |
107 | SUBDIV_MAX |
108 | |
109 | }; |
110 | |
111 | typedef void (*BakeBeginFunc)(int); |
112 | typedef void (*BakeStepFunc)(int, const String &); |
113 | typedef void (*BakeEndFunc)(); |
114 | |
115 | private: |
116 | Ref<VoxelGIData> probe_data; |
117 | |
118 | RID voxel_gi; |
119 | |
120 | Subdiv subdiv = SUBDIV_128; |
121 | Vector3 size = Vector3(20, 20, 20); |
122 | Ref<CameraAttributes> camera_attributes; |
123 | |
124 | struct PlotMesh { |
125 | Ref<Material> override_material; |
126 | Vector<Ref<Material>> instance_materials; |
127 | Ref<Mesh> mesh; |
128 | Transform3D local_xform; |
129 | }; |
130 | |
131 | void _find_meshes(Node *p_at_node, List<PlotMesh> &plot_meshes); |
132 | void _debug_bake(); |
133 | |
134 | float _get_camera_exposure_normalization(); |
135 | |
136 | protected: |
137 | static void _bind_methods(); |
138 | #ifndef DISABLE_DEPRECATED |
139 | bool _set(const StringName &p_name, const Variant &p_value); |
140 | bool _get(const StringName &p_name, Variant &r_property) const; |
141 | #endif // DISABLE_DEPRECATED |
142 | |
143 | public: |
144 | static BakeBeginFunc bake_begin_function; |
145 | static BakeStepFunc bake_step_function; |
146 | static BakeEndFunc bake_end_function; |
147 | |
148 | void set_probe_data(const Ref<VoxelGIData> &p_data); |
149 | Ref<VoxelGIData> get_probe_data() const; |
150 | |
151 | void set_subdiv(Subdiv p_subdiv); |
152 | Subdiv get_subdiv() const; |
153 | |
154 | void set_size(const Vector3 &p_size); |
155 | Vector3 get_size() const; |
156 | |
157 | void set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes); |
158 | Ref<CameraAttributes> get_camera_attributes() const; |
159 | |
160 | Vector3i get_estimated_cell_size() const; |
161 | |
162 | void bake(Node *p_from_node = nullptr, bool p_create_visual_debug = false); |
163 | |
164 | virtual AABB get_aabb() const override; |
165 | |
166 | PackedStringArray get_configuration_warnings() const override; |
167 | |
168 | VoxelGI(); |
169 | ~VoxelGI(); |
170 | }; |
171 | |
172 | VARIANT_ENUM_CAST(VoxelGI::Subdiv) |
173 | |
174 | #endif // VOXEL_GI_H |
175 | |