reflection_probe.cpp 13 KB

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