reflection_probe.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* reflection_probe.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 = p_distance;
  62. RS::get_singleton()->reflection_probe_set_max_distance(probe, p_distance);
  63. }
  64. float ReflectionProbe::get_max_distance() const {
  65. return max_distance;
  66. }
  67. void ReflectionProbe::set_mesh_lod_threshold(float p_pixels) {
  68. mesh_lod_threshold = p_pixels;
  69. RS::get_singleton()->reflection_probe_set_mesh_lod_threshold(probe, p_pixels);
  70. }
  71. float ReflectionProbe::get_mesh_lod_threshold() const {
  72. return mesh_lod_threshold;
  73. }
  74. void ReflectionProbe::set_extents(const Vector3 &p_extents) {
  75. extents = p_extents;
  76. for (int i = 0; i < 3; i++) {
  77. if (extents[i] < 0.01) {
  78. extents[i] = 0.01;
  79. }
  80. if (extents[i] - 0.01 < ABS(origin_offset[i])) {
  81. origin_offset[i] = SIGN(origin_offset[i]) * (extents[i] - 0.01);
  82. }
  83. }
  84. RS::get_singleton()->reflection_probe_set_extents(probe, extents);
  85. RS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
  86. update_gizmos();
  87. }
  88. Vector3 ReflectionProbe::get_extents() const {
  89. return extents;
  90. }
  91. void ReflectionProbe::set_origin_offset(const Vector3 &p_extents) {
  92. origin_offset = p_extents;
  93. for (int i = 0; i < 3; i++) {
  94. if (extents[i] - 0.01 < ABS(origin_offset[i])) {
  95. origin_offset[i] = SIGN(origin_offset[i]) * (extents[i] - 0.01);
  96. }
  97. }
  98. RS::get_singleton()->reflection_probe_set_extents(probe, extents);
  99. RS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
  100. update_gizmos();
  101. }
  102. Vector3 ReflectionProbe::get_origin_offset() const {
  103. return origin_offset;
  104. }
  105. void ReflectionProbe::set_enable_box_projection(bool p_enable) {
  106. box_projection = p_enable;
  107. RS::get_singleton()->reflection_probe_set_enable_box_projection(probe, p_enable);
  108. }
  109. bool ReflectionProbe::is_box_projection_enabled() const {
  110. return box_projection;
  111. }
  112. void ReflectionProbe::set_as_interior(bool p_enable) {
  113. interior = p_enable;
  114. RS::get_singleton()->reflection_probe_set_as_interior(probe, interior);
  115. }
  116. bool ReflectionProbe::is_set_as_interior() const {
  117. return interior;
  118. }
  119. void ReflectionProbe::set_enable_shadows(bool p_enable) {
  120. enable_shadows = p_enable;
  121. RS::get_singleton()->reflection_probe_set_enable_shadows(probe, p_enable);
  122. }
  123. bool ReflectionProbe::are_shadows_enabled() const {
  124. return enable_shadows;
  125. }
  126. void ReflectionProbe::set_cull_mask(uint32_t p_layers) {
  127. cull_mask = p_layers;
  128. RS::get_singleton()->reflection_probe_set_cull_mask(probe, p_layers);
  129. }
  130. uint32_t ReflectionProbe::get_cull_mask() const {
  131. return cull_mask;
  132. }
  133. void ReflectionProbe::set_update_mode(UpdateMode p_mode) {
  134. update_mode = p_mode;
  135. RS::get_singleton()->reflection_probe_set_update_mode(probe, RS::ReflectionProbeUpdateMode(p_mode));
  136. }
  137. ReflectionProbe::UpdateMode ReflectionProbe::get_update_mode() const {
  138. return update_mode;
  139. }
  140. AABB ReflectionProbe::get_aabb() const {
  141. AABB aabb;
  142. aabb.position = -origin_offset;
  143. aabb.size = origin_offset + extents;
  144. return aabb;
  145. }
  146. void ReflectionProbe::_validate_property(PropertyInfo &p_property) const {
  147. if (p_property.name == "ambient_color" || p_property.name == "ambient_color_energy") {
  148. if (ambient_mode != AMBIENT_COLOR) {
  149. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  150. }
  151. }
  152. }
  153. void ReflectionProbe::_bind_methods() {
  154. ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &ReflectionProbe::set_intensity);
  155. ClassDB::bind_method(D_METHOD("get_intensity"), &ReflectionProbe::get_intensity);
  156. ClassDB::bind_method(D_METHOD("set_ambient_mode", "ambient"), &ReflectionProbe::set_ambient_mode);
  157. ClassDB::bind_method(D_METHOD("get_ambient_mode"), &ReflectionProbe::get_ambient_mode);
  158. ClassDB::bind_method(D_METHOD("set_ambient_color", "ambient"), &ReflectionProbe::set_ambient_color);
  159. ClassDB::bind_method(D_METHOD("get_ambient_color"), &ReflectionProbe::get_ambient_color);
  160. ClassDB::bind_method(D_METHOD("set_ambient_color_energy", "ambient_energy"), &ReflectionProbe::set_ambient_color_energy);
  161. ClassDB::bind_method(D_METHOD("get_ambient_color_energy"), &ReflectionProbe::get_ambient_color_energy);
  162. ClassDB::bind_method(D_METHOD("set_max_distance", "max_distance"), &ReflectionProbe::set_max_distance);
  163. ClassDB::bind_method(D_METHOD("get_max_distance"), &ReflectionProbe::get_max_distance);
  164. ClassDB::bind_method(D_METHOD("set_mesh_lod_threshold", "ratio"), &ReflectionProbe::set_mesh_lod_threshold);
  165. ClassDB::bind_method(D_METHOD("get_mesh_lod_threshold"), &ReflectionProbe::get_mesh_lod_threshold);
  166. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &ReflectionProbe::set_extents);
  167. ClassDB::bind_method(D_METHOD("get_extents"), &ReflectionProbe::get_extents);
  168. ClassDB::bind_method(D_METHOD("set_origin_offset", "origin_offset"), &ReflectionProbe::set_origin_offset);
  169. ClassDB::bind_method(D_METHOD("get_origin_offset"), &ReflectionProbe::get_origin_offset);
  170. ClassDB::bind_method(D_METHOD("set_as_interior", "enable"), &ReflectionProbe::set_as_interior);
  171. ClassDB::bind_method(D_METHOD("is_set_as_interior"), &ReflectionProbe::is_set_as_interior);
  172. ClassDB::bind_method(D_METHOD("set_enable_box_projection", "enable"), &ReflectionProbe::set_enable_box_projection);
  173. ClassDB::bind_method(D_METHOD("is_box_projection_enabled"), &ReflectionProbe::is_box_projection_enabled);
  174. ClassDB::bind_method(D_METHOD("set_enable_shadows", "enable"), &ReflectionProbe::set_enable_shadows);
  175. ClassDB::bind_method(D_METHOD("are_shadows_enabled"), &ReflectionProbe::are_shadows_enabled);
  176. ClassDB::bind_method(D_METHOD("set_cull_mask", "layers"), &ReflectionProbe::set_cull_mask);
  177. ClassDB::bind_method(D_METHOD("get_cull_mask"), &ReflectionProbe::get_cull_mask);
  178. ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &ReflectionProbe::set_update_mode);
  179. ClassDB::bind_method(D_METHOD("get_update_mode"), &ReflectionProbe::get_update_mode);
  180. ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Once (Fast),Always (Slow)"), "set_update_mode", "get_update_mode");
  181. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "intensity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_intensity", "get_intensity");
  182. 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");
  183. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents", PROPERTY_HINT_NONE, "suffix:m"), "set_extents", "get_extents");
  184. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "origin_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_origin_offset", "get_origin_offset");
  185. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "box_projection"), "set_enable_box_projection", "is_box_projection_enabled");
  186. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_as_interior", "is_set_as_interior");
  187. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_shadows"), "set_enable_shadows", "are_shadows_enabled");
  188. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  189. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mesh_lod_threshold", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_mesh_lod_threshold", "get_mesh_lod_threshold");
  190. ADD_GROUP("Ambient", "ambient_");
  191. ADD_PROPERTY(PropertyInfo(Variant::INT, "ambient_mode", PROPERTY_HINT_ENUM, "Disabled,Environment,Constant Color"), "set_ambient_mode", "get_ambient_mode");
  192. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ambient_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_ambient_color", "get_ambient_color");
  193. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ambient_color_energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_ambient_color_energy", "get_ambient_color_energy");
  194. BIND_ENUM_CONSTANT(UPDATE_ONCE);
  195. BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
  196. BIND_ENUM_CONSTANT(AMBIENT_DISABLED);
  197. BIND_ENUM_CONSTANT(AMBIENT_ENVIRONMENT);
  198. BIND_ENUM_CONSTANT(AMBIENT_COLOR);
  199. }
  200. ReflectionProbe::ReflectionProbe() {
  201. probe = RenderingServer::get_singleton()->reflection_probe_create();
  202. RS::get_singleton()->instance_set_base(get_instance(), probe);
  203. set_disable_scale(true);
  204. }
  205. ReflectionProbe::~ReflectionProbe() {
  206. RS::get_singleton()->free(probe);
  207. }