reflection_probe.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**************************************************************************/
  2. /* reflection_probe.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. #include "reflection_probe.h"
  31. void ReflectionProbe::set_intensity(float p_intensity) {
  32. intensity = p_intensity;
  33. RS::get_singleton()->reflection_probe_set_intensity(probe, p_intensity);
  34. }
  35. float ReflectionProbe::get_intensity() const {
  36. return intensity;
  37. }
  38. void ReflectionProbe::set_ambient_mode(AmbientMode p_mode) {
  39. ambient_mode = p_mode;
  40. RS::get_singleton()->reflection_probe_set_ambient_mode(probe, RS::ReflectionProbeAmbientMode(p_mode));
  41. notify_property_list_changed();
  42. }
  43. ReflectionProbe::AmbientMode ReflectionProbe::get_ambient_mode() const {
  44. return ambient_mode;
  45. }
  46. void ReflectionProbe::set_ambient_color(Color p_ambient) {
  47. ambient_color = p_ambient;
  48. RS::get_singleton()->reflection_probe_set_ambient_color(probe, p_ambient);
  49. }
  50. void ReflectionProbe::set_ambient_color_energy(float p_energy) {
  51. ambient_color_energy = p_energy;
  52. RS::get_singleton()->reflection_probe_set_ambient_energy(probe, p_energy);
  53. }
  54. float ReflectionProbe::get_ambient_color_energy() const {
  55. return ambient_color_energy;
  56. }
  57. Color ReflectionProbe::get_ambient_color() const {
  58. return ambient_color;
  59. }
  60. void ReflectionProbe::set_max_distance(float p_distance) {
  61. max_distance = CLAMP(p_distance, 0.0, 262'144.0);
  62. // Reflection rendering breaks if distance exceeds 262,144 units (due to floating-point precision with the near plane being 0.01).
  63. RS::get_singleton()->reflection_probe_set_max_distance(probe, max_distance);
  64. }
  65. float ReflectionProbe::get_max_distance() const {
  66. return max_distance;
  67. }
  68. void ReflectionProbe::set_mesh_lod_threshold(float p_pixels) {
  69. mesh_lod_threshold = p_pixels;
  70. RS::get_singleton()->reflection_probe_set_mesh_lod_threshold(probe, p_pixels);
  71. }
  72. float ReflectionProbe::get_mesh_lod_threshold() const {
  73. return mesh_lod_threshold;
  74. }
  75. void ReflectionProbe::set_size(const Vector3 &p_size) {
  76. size = p_size;
  77. for (int i = 0; i < 3; i++) {
  78. float half_size = size[i] / 2;
  79. if (half_size < 0.01) {
  80. half_size = 0.01;
  81. }
  82. if (half_size - 0.01 < ABS(origin_offset[i])) {
  83. origin_offset[i] = SIGN(origin_offset[i]) * (half_size - 0.01);
  84. }
  85. }
  86. RS::get_singleton()->reflection_probe_set_size(probe, size);
  87. RS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
  88. update_gizmos();
  89. }
  90. Vector3 ReflectionProbe::get_size() const {
  91. return size;
  92. }
  93. void ReflectionProbe::set_origin_offset(const Vector3 &p_offset) {
  94. origin_offset = p_offset;
  95. for (int i = 0; i < 3; i++) {
  96. float half_size = size[i] / 2;
  97. if (half_size - 0.01 < ABS(origin_offset[i])) {
  98. origin_offset[i] = SIGN(origin_offset[i]) * (half_size - 0.01);
  99. }
  100. }
  101. RS::get_singleton()->reflection_probe_set_size(probe, size);
  102. RS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
  103. update_gizmos();
  104. }
  105. Vector3 ReflectionProbe::get_origin_offset() const {
  106. return origin_offset;
  107. }
  108. void ReflectionProbe::set_enable_box_projection(bool p_enable) {
  109. box_projection = p_enable;
  110. RS::get_singleton()->reflection_probe_set_enable_box_projection(probe, p_enable);
  111. }
  112. bool ReflectionProbe::is_box_projection_enabled() const {
  113. return box_projection;
  114. }
  115. void ReflectionProbe::set_as_interior(bool p_enable) {
  116. interior = p_enable;
  117. RS::get_singleton()->reflection_probe_set_as_interior(probe, interior);
  118. }
  119. bool ReflectionProbe::is_set_as_interior() const {
  120. return interior;
  121. }
  122. void ReflectionProbe::set_enable_shadows(bool p_enable) {
  123. enable_shadows = p_enable;
  124. RS::get_singleton()->reflection_probe_set_enable_shadows(probe, p_enable);
  125. }
  126. bool ReflectionProbe::are_shadows_enabled() const {
  127. return enable_shadows;
  128. }
  129. void ReflectionProbe::set_cull_mask(uint32_t p_layers) {
  130. cull_mask = p_layers;
  131. RS::get_singleton()->reflection_probe_set_cull_mask(probe, p_layers);
  132. }
  133. uint32_t ReflectionProbe::get_cull_mask() const {
  134. return cull_mask;
  135. }
  136. void ReflectionProbe::set_reflection_mask(uint32_t p_layers) {
  137. reflection_mask = p_layers;
  138. RS::get_singleton()->reflection_probe_set_reflection_mask(probe, p_layers);
  139. }
  140. uint32_t ReflectionProbe::get_reflection_mask() const {
  141. return reflection_mask;
  142. }
  143. void ReflectionProbe::set_update_mode(UpdateMode p_mode) {
  144. update_mode = p_mode;
  145. RS::get_singleton()->reflection_probe_set_update_mode(probe, RS::ReflectionProbeUpdateMode(p_mode));
  146. }
  147. ReflectionProbe::UpdateMode ReflectionProbe::get_update_mode() const {
  148. return update_mode;
  149. }
  150. AABB ReflectionProbe::get_aabb() const {
  151. AABB aabb;
  152. aabb.position = -origin_offset;
  153. aabb.size = origin_offset + size / 2;
  154. return aabb;
  155. }
  156. void ReflectionProbe::_validate_property(PropertyInfo &p_property) const {
  157. if (p_property.name == "ambient_color" || p_property.name == "ambient_color_energy") {
  158. if (ambient_mode != AMBIENT_COLOR) {
  159. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  160. }
  161. }
  162. }
  163. void ReflectionProbe::_bind_methods() {
  164. ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &ReflectionProbe::set_intensity);
  165. ClassDB::bind_method(D_METHOD("get_intensity"), &ReflectionProbe::get_intensity);
  166. ClassDB::bind_method(D_METHOD("set_ambient_mode", "ambient"), &ReflectionProbe::set_ambient_mode);
  167. ClassDB::bind_method(D_METHOD("get_ambient_mode"), &ReflectionProbe::get_ambient_mode);
  168. ClassDB::bind_method(D_METHOD("set_ambient_color", "ambient"), &ReflectionProbe::set_ambient_color);
  169. ClassDB::bind_method(D_METHOD("get_ambient_color"), &ReflectionProbe::get_ambient_color);
  170. ClassDB::bind_method(D_METHOD("set_ambient_color_energy", "ambient_energy"), &ReflectionProbe::set_ambient_color_energy);
  171. ClassDB::bind_method(D_METHOD("get_ambient_color_energy"), &ReflectionProbe::get_ambient_color_energy);
  172. ClassDB::bind_method(D_METHOD("set_max_distance", "max_distance"), &ReflectionProbe::set_max_distance);
  173. ClassDB::bind_method(D_METHOD("get_max_distance"), &ReflectionProbe::get_max_distance);
  174. ClassDB::bind_method(D_METHOD("set_mesh_lod_threshold", "ratio"), &ReflectionProbe::set_mesh_lod_threshold);
  175. ClassDB::bind_method(D_METHOD("get_mesh_lod_threshold"), &ReflectionProbe::get_mesh_lod_threshold);
  176. ClassDB::bind_method(D_METHOD("set_size", "size"), &ReflectionProbe::set_size);
  177. ClassDB::bind_method(D_METHOD("get_size"), &ReflectionProbe::get_size);
  178. ClassDB::bind_method(D_METHOD("set_origin_offset", "origin_offset"), &ReflectionProbe::set_origin_offset);
  179. ClassDB::bind_method(D_METHOD("get_origin_offset"), &ReflectionProbe::get_origin_offset);
  180. ClassDB::bind_method(D_METHOD("set_as_interior", "enable"), &ReflectionProbe::set_as_interior);
  181. ClassDB::bind_method(D_METHOD("is_set_as_interior"), &ReflectionProbe::is_set_as_interior);
  182. ClassDB::bind_method(D_METHOD("set_enable_box_projection", "enable"), &ReflectionProbe::set_enable_box_projection);
  183. ClassDB::bind_method(D_METHOD("is_box_projection_enabled"), &ReflectionProbe::is_box_projection_enabled);
  184. ClassDB::bind_method(D_METHOD("set_enable_shadows", "enable"), &ReflectionProbe::set_enable_shadows);
  185. ClassDB::bind_method(D_METHOD("are_shadows_enabled"), &ReflectionProbe::are_shadows_enabled);
  186. ClassDB::bind_method(D_METHOD("set_cull_mask", "layers"), &ReflectionProbe::set_cull_mask);
  187. ClassDB::bind_method(D_METHOD("get_cull_mask"), &ReflectionProbe::get_cull_mask);
  188. ClassDB::bind_method(D_METHOD("set_reflection_mask", "layers"), &ReflectionProbe::set_reflection_mask);
  189. ClassDB::bind_method(D_METHOD("get_reflection_mask"), &ReflectionProbe::get_reflection_mask);
  190. ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &ReflectionProbe::set_update_mode);
  191. ClassDB::bind_method(D_METHOD("get_update_mode"), &ReflectionProbe::get_update_mode);
  192. ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Once (Fast),Always (Slow)"), "set_update_mode", "get_update_mode");
  193. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "intensity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_intensity", "get_intensity");
  194. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_distance", PROPERTY_HINT_RANGE, "0,16384,0.1,or_greater,exp,suffix:m"), "set_max_distance", "get_max_distance");
  195. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  196. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "origin_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_origin_offset", "get_origin_offset");
  197. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "box_projection"), "set_enable_box_projection", "is_box_projection_enabled");
  198. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_as_interior", "is_set_as_interior");
  199. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_shadows"), "set_enable_shadows", "are_shadows_enabled");
  200. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  201. ADD_PROPERTY(PropertyInfo(Variant::INT, "reflection_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_reflection_mask", "get_reflection_mask");
  202. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mesh_lod_threshold", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_mesh_lod_threshold", "get_mesh_lod_threshold");
  203. ADD_GROUP("Ambient", "ambient_");
  204. ADD_PROPERTY(PropertyInfo(Variant::INT, "ambient_mode", PROPERTY_HINT_ENUM, "Disabled,Environment,Constant Color"), "set_ambient_mode", "get_ambient_mode");
  205. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ambient_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_ambient_color", "get_ambient_color");
  206. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ambient_color_energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_ambient_color_energy", "get_ambient_color_energy");
  207. BIND_ENUM_CONSTANT(UPDATE_ONCE);
  208. BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
  209. BIND_ENUM_CONSTANT(AMBIENT_DISABLED);
  210. BIND_ENUM_CONSTANT(AMBIENT_ENVIRONMENT);
  211. BIND_ENUM_CONSTANT(AMBIENT_COLOR);
  212. }
  213. #ifndef DISABLE_DEPRECATED
  214. bool ReflectionProbe::_set(const StringName &p_name, const Variant &p_value) {
  215. if (p_name == "extents") { // Compatibility with Godot 3.x.
  216. set_size((Vector3)p_value * 2);
  217. return true;
  218. }
  219. return false;
  220. }
  221. bool ReflectionProbe::_get(const StringName &p_name, Variant &r_property) const {
  222. if (p_name == "extents") { // Compatibility with Godot 3.x.
  223. r_property = size / 2;
  224. return true;
  225. }
  226. return false;
  227. }
  228. #endif // DISABLE_DEPRECATED
  229. ReflectionProbe::ReflectionProbe() {
  230. probe = RenderingServer::get_singleton()->reflection_probe_create();
  231. RS::get_singleton()->instance_set_base(get_instance(), probe);
  232. set_disable_scale(true);
  233. }
  234. ReflectionProbe::~ReflectionProbe() {
  235. ERR_FAIL_NULL(RenderingServer::get_singleton());
  236. RS::get_singleton()->free(probe);
  237. }