reflection_probe.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. VS::get_singleton()->reflection_probe_set_intensity(probe, p_intensity);
  34. }
  35. float ReflectionProbe::get_intensity() const {
  36. return intensity;
  37. }
  38. void ReflectionProbe::set_interior_ambient(Color p_ambient) {
  39. interior_ambient = p_ambient;
  40. VS::get_singleton()->reflection_probe_set_interior_ambient(probe, p_ambient);
  41. }
  42. void ReflectionProbe::set_interior_ambient_energy(float p_energy) {
  43. interior_ambient_energy = p_energy;
  44. VS::get_singleton()->reflection_probe_set_interior_ambient_energy(probe, p_energy);
  45. }
  46. float ReflectionProbe::get_interior_ambient_energy() const {
  47. return interior_ambient_energy;
  48. }
  49. Color ReflectionProbe::get_interior_ambient() const {
  50. return interior_ambient;
  51. }
  52. void ReflectionProbe::set_interior_ambient_probe_contribution(float p_contribution) {
  53. interior_ambient_probe_contribution = p_contribution;
  54. VS::get_singleton()->reflection_probe_set_interior_ambient_probe_contribution(probe, p_contribution);
  55. }
  56. float ReflectionProbe::get_interior_ambient_probe_contribution() const {
  57. return interior_ambient_probe_contribution;
  58. }
  59. void ReflectionProbe::set_max_distance(float p_distance) {
  60. max_distance = p_distance;
  61. VS::get_singleton()->reflection_probe_set_max_distance(probe, p_distance);
  62. }
  63. float ReflectionProbe::get_max_distance() const {
  64. return max_distance;
  65. }
  66. void ReflectionProbe::set_extents(const Vector3 &p_extents) {
  67. extents = p_extents;
  68. for (int i = 0; i < 3; i++) {
  69. if (extents[i] < 0.01) {
  70. extents[i] = 0.01;
  71. }
  72. if (extents[i] - 0.01 < ABS(origin_offset[i])) {
  73. origin_offset[i] = SGN(origin_offset[i]) * (extents[i] - 0.01);
  74. _change_notify("origin_offset");
  75. }
  76. }
  77. VS::get_singleton()->reflection_probe_set_extents(probe, extents);
  78. VS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
  79. _change_notify("extents");
  80. update_gizmo();
  81. }
  82. Vector3 ReflectionProbe::get_extents() const {
  83. return extents;
  84. }
  85. void ReflectionProbe::set_origin_offset(const Vector3 &p_extents) {
  86. origin_offset = p_extents;
  87. for (int i = 0; i < 3; i++) {
  88. if (extents[i] - 0.01 < ABS(origin_offset[i])) {
  89. origin_offset[i] = SGN(origin_offset[i]) * (extents[i] - 0.01);
  90. }
  91. }
  92. VS::get_singleton()->reflection_probe_set_extents(probe, extents);
  93. VS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
  94. _change_notify("origin_offset");
  95. update_gizmo();
  96. }
  97. Vector3 ReflectionProbe::get_origin_offset() const {
  98. return origin_offset;
  99. }
  100. void ReflectionProbe::set_enable_box_projection(bool p_enable) {
  101. box_projection = p_enable;
  102. VS::get_singleton()->reflection_probe_set_enable_box_projection(probe, p_enable);
  103. }
  104. bool ReflectionProbe::is_box_projection_enabled() const {
  105. return box_projection;
  106. }
  107. void ReflectionProbe::set_as_interior(bool p_enable) {
  108. interior = p_enable;
  109. VS::get_singleton()->reflection_probe_set_as_interior(probe, interior);
  110. _change_notify();
  111. }
  112. bool ReflectionProbe::is_set_as_interior() const {
  113. return interior;
  114. }
  115. void ReflectionProbe::set_enable_shadows(bool p_enable) {
  116. enable_shadows = p_enable;
  117. VS::get_singleton()->reflection_probe_set_enable_shadows(probe, p_enable);
  118. }
  119. bool ReflectionProbe::are_shadows_enabled() const {
  120. return enable_shadows;
  121. }
  122. void ReflectionProbe::set_cull_mask(uint32_t p_layers) {
  123. cull_mask = p_layers;
  124. VS::get_singleton()->reflection_probe_set_cull_mask(probe, p_layers);
  125. }
  126. uint32_t ReflectionProbe::get_cull_mask() const {
  127. return cull_mask;
  128. }
  129. void ReflectionProbe::set_update_mode(UpdateMode p_mode) {
  130. update_mode = p_mode;
  131. VS::get_singleton()->reflection_probe_set_update_mode(probe, VS::ReflectionProbeUpdateMode(p_mode));
  132. }
  133. ReflectionProbe::UpdateMode ReflectionProbe::get_update_mode() const {
  134. return update_mode;
  135. }
  136. AABB ReflectionProbe::get_aabb() const {
  137. AABB aabb;
  138. aabb.position = -origin_offset;
  139. aabb.size = origin_offset + extents;
  140. return aabb;
  141. }
  142. PoolVector<Face3> ReflectionProbe::get_faces(uint32_t p_usage_flags) const {
  143. return PoolVector<Face3>();
  144. }
  145. void ReflectionProbe::_validate_property(PropertyInfo &property) const {
  146. if (property.name == "interior/ambient_color" || property.name == "interior/ambient_energy" || property.name == "interior/ambient_contrib") {
  147. if (!interior) {
  148. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  149. }
  150. }
  151. }
  152. void ReflectionProbe::_bind_methods() {
  153. ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &ReflectionProbe::set_intensity);
  154. ClassDB::bind_method(D_METHOD("get_intensity"), &ReflectionProbe::get_intensity);
  155. ClassDB::bind_method(D_METHOD("set_interior_ambient", "ambient"), &ReflectionProbe::set_interior_ambient);
  156. ClassDB::bind_method(D_METHOD("get_interior_ambient"), &ReflectionProbe::get_interior_ambient);
  157. ClassDB::bind_method(D_METHOD("set_interior_ambient_energy", "ambient_energy"), &ReflectionProbe::set_interior_ambient_energy);
  158. ClassDB::bind_method(D_METHOD("get_interior_ambient_energy"), &ReflectionProbe::get_interior_ambient_energy);
  159. ClassDB::bind_method(D_METHOD("set_interior_ambient_probe_contribution", "ambient_probe_contribution"), &ReflectionProbe::set_interior_ambient_probe_contribution);
  160. ClassDB::bind_method(D_METHOD("get_interior_ambient_probe_contribution"), &ReflectionProbe::get_interior_ambient_probe_contribution);
  161. ClassDB::bind_method(D_METHOD("set_max_distance", "max_distance"), &ReflectionProbe::set_max_distance);
  162. ClassDB::bind_method(D_METHOD("get_max_distance"), &ReflectionProbe::get_max_distance);
  163. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &ReflectionProbe::set_extents);
  164. ClassDB::bind_method(D_METHOD("get_extents"), &ReflectionProbe::get_extents);
  165. ClassDB::bind_method(D_METHOD("set_origin_offset", "origin_offset"), &ReflectionProbe::set_origin_offset);
  166. ClassDB::bind_method(D_METHOD("get_origin_offset"), &ReflectionProbe::get_origin_offset);
  167. ClassDB::bind_method(D_METHOD("set_as_interior", "enable"), &ReflectionProbe::set_as_interior);
  168. ClassDB::bind_method(D_METHOD("is_set_as_interior"), &ReflectionProbe::is_set_as_interior);
  169. ClassDB::bind_method(D_METHOD("set_enable_box_projection", "enable"), &ReflectionProbe::set_enable_box_projection);
  170. ClassDB::bind_method(D_METHOD("is_box_projection_enabled"), &ReflectionProbe::is_box_projection_enabled);
  171. ClassDB::bind_method(D_METHOD("set_enable_shadows", "enable"), &ReflectionProbe::set_enable_shadows);
  172. ClassDB::bind_method(D_METHOD("are_shadows_enabled"), &ReflectionProbe::are_shadows_enabled);
  173. ClassDB::bind_method(D_METHOD("set_cull_mask", "layers"), &ReflectionProbe::set_cull_mask);
  174. ClassDB::bind_method(D_METHOD("get_cull_mask"), &ReflectionProbe::get_cull_mask);
  175. ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &ReflectionProbe::set_update_mode);
  176. ClassDB::bind_method(D_METHOD("get_update_mode"), &ReflectionProbe::get_update_mode);
  177. ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Once (Fast),Always (Slow)"), "set_update_mode", "get_update_mode");
  178. ADD_PROPERTY(PropertyInfo(Variant::REAL, "intensity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_intensity", "get_intensity");
  179. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_distance", PROPERTY_HINT_EXP_RANGE, "0,16384,0.1,or_greater"), "set_max_distance", "get_max_distance");
  180. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents"), "set_extents", "get_extents");
  181. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "origin_offset"), "set_origin_offset", "get_origin_offset");
  182. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "box_projection"), "set_enable_box_projection", "is_box_projection_enabled");
  183. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_shadows"), "set_enable_shadows", "are_shadows_enabled");
  184. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  185. ADD_GROUP("Interior", "interior_");
  186. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior_enable"), "set_as_interior", "is_set_as_interior");
  187. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "interior_ambient_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_interior_ambient", "get_interior_ambient");
  188. ADD_PROPERTY(PropertyInfo(Variant::REAL, "interior_ambient_energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_interior_ambient_energy", "get_interior_ambient_energy");
  189. ADD_PROPERTY(PropertyInfo(Variant::REAL, "interior_ambient_contrib", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_interior_ambient_probe_contribution", "get_interior_ambient_probe_contribution");
  190. BIND_ENUM_CONSTANT(UPDATE_ONCE);
  191. BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
  192. }
  193. ReflectionProbe::ReflectionProbe() {
  194. intensity = 1.0;
  195. interior_ambient = Color(0, 0, 0);
  196. interior_ambient_probe_contribution = 0;
  197. interior_ambient_energy = 1.0;
  198. max_distance = 0;
  199. extents = Vector3(1, 1, 1);
  200. origin_offset = Vector3(0, 0, 0);
  201. box_projection = false;
  202. interior = false;
  203. enable_shadows = false;
  204. cull_mask = (1 << 20) - 1;
  205. update_mode = UPDATE_ONCE;
  206. probe = RID_PRIME(VisualServer::get_singleton()->reflection_probe_create());
  207. VS::get_singleton()->instance_set_base(get_instance(), probe);
  208. set_disable_scale(true);
  209. }
  210. ReflectionProbe::~ReflectionProbe() {
  211. VS::get_singleton()->free(probe);
  212. }