visual_instance_3d.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /**************************************************************************/
  2. /* visual_instance_3d.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 "visual_instance_3d.h"
  31. #include "core/config/project_settings.h"
  32. AABB VisualInstance3D::get_aabb() const {
  33. AABB ret;
  34. GDVIRTUAL_CALL(_get_aabb, ret);
  35. return ret;
  36. }
  37. void VisualInstance3D::_update_visibility() {
  38. if (!is_inside_tree()) {
  39. return;
  40. }
  41. bool already_visible = _is_vi_visible();
  42. bool visible = is_visible_in_tree();
  43. _set_vi_visible(visible);
  44. // If making visible, make sure the rendering server is up to date with the transform.
  45. if (visible && !already_visible) {
  46. if (!_is_using_identity_transform()) {
  47. Transform3D gt = get_global_transform();
  48. RS::get_singleton()->instance_set_transform(instance, gt);
  49. }
  50. }
  51. RS::get_singleton()->instance_set_visible(instance, visible);
  52. }
  53. void VisualInstance3D::set_instance_use_identity_transform(bool p_enable) {
  54. // Prevent sending instance transforms when using global coordinates.
  55. _set_use_identity_transform(p_enable);
  56. if (is_inside_tree()) {
  57. if (p_enable) {
  58. // Want to make sure instance is using identity transform.
  59. RS::get_singleton()->instance_set_transform(instance, Transform3D());
  60. } else {
  61. // Want to make sure instance is up to date.
  62. RS::get_singleton()->instance_set_transform(instance, get_global_transform());
  63. }
  64. }
  65. }
  66. void VisualInstance3D::fti_update_servers_xform() {
  67. if (!_is_using_identity_transform()) {
  68. RS::get_singleton()->instance_set_transform(get_instance(), _get_cached_global_transform_interpolated());
  69. }
  70. }
  71. void VisualInstance3D::_notification(int p_what) {
  72. switch (p_what) {
  73. case NOTIFICATION_ENTER_WORLD: {
  74. ERR_FAIL_COND(get_world_3d().is_null());
  75. RenderingServer::get_singleton()->instance_set_scenario(instance, get_world_3d()->get_scenario());
  76. _update_visibility();
  77. } break;
  78. case NOTIFICATION_TRANSFORM_CHANGED: {
  79. // ToDo : Can we turn off notify transform for physics interpolated cases?
  80. if (_is_vi_visible() && !(is_inside_tree() && get_tree()->is_physics_interpolation_enabled()) && !_is_using_identity_transform()) {
  81. // Physics interpolation global off, always send.
  82. RenderingServer::get_singleton()->instance_set_transform(instance, get_global_transform());
  83. }
  84. } break;
  85. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  86. if (_is_vi_visible() && is_inside_tree()) {
  87. // Allow resetting motion vectors etc
  88. // at the same time as resetting physics interpolation,
  89. // giving users one common interface.
  90. RenderingServer::get_singleton()->instance_teleport(instance);
  91. }
  92. } break;
  93. case NOTIFICATION_EXIT_WORLD: {
  94. RenderingServer::get_singleton()->instance_set_scenario(instance, RID());
  95. RenderingServer::get_singleton()->instance_attach_skeleton(instance, RID());
  96. _set_vi_visible(false);
  97. } break;
  98. case NOTIFICATION_VISIBILITY_CHANGED: {
  99. _update_visibility();
  100. } break;
  101. }
  102. }
  103. RID VisualInstance3D::get_instance() const {
  104. return instance;
  105. }
  106. void VisualInstance3D::set_layer_mask(uint32_t p_mask) {
  107. layers = p_mask;
  108. RenderingServer::get_singleton()->instance_set_layer_mask(instance, p_mask);
  109. }
  110. uint32_t VisualInstance3D::get_layer_mask() const {
  111. return layers;
  112. }
  113. void VisualInstance3D::set_layer_mask_value(int p_layer_number, bool p_value) {
  114. ERR_FAIL_COND_MSG(p_layer_number < 1, "Render layer number must be between 1 and 20 inclusive.");
  115. ERR_FAIL_COND_MSG(p_layer_number > 20, "Render layer number must be between 1 and 20 inclusive.");
  116. uint32_t mask = get_layer_mask();
  117. if (p_value) {
  118. mask |= 1 << (p_layer_number - 1);
  119. } else {
  120. mask &= ~(1 << (p_layer_number - 1));
  121. }
  122. set_layer_mask(mask);
  123. }
  124. bool VisualInstance3D::get_layer_mask_value(int p_layer_number) const {
  125. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Render layer number must be between 1 and 20 inclusive.");
  126. ERR_FAIL_COND_V_MSG(p_layer_number > 20, false, "Render layer number must be between 1 and 20 inclusive.");
  127. return layers & (1 << (p_layer_number - 1));
  128. }
  129. void VisualInstance3D::set_sorting_offset(float p_offset) {
  130. sorting_offset = p_offset;
  131. RenderingServer::get_singleton()->instance_set_pivot_data(instance, sorting_offset, sorting_use_aabb_center);
  132. }
  133. float VisualInstance3D::get_sorting_offset() const {
  134. return sorting_offset;
  135. }
  136. void VisualInstance3D::set_sorting_use_aabb_center(bool p_enabled) {
  137. sorting_use_aabb_center = p_enabled;
  138. RenderingServer::get_singleton()->instance_set_pivot_data(instance, sorting_offset, sorting_use_aabb_center);
  139. }
  140. bool VisualInstance3D::is_sorting_use_aabb_center() const {
  141. return sorting_use_aabb_center;
  142. }
  143. void VisualInstance3D::_validate_property(PropertyInfo &p_property) const {
  144. if (p_property.name == "sorting_offset" || p_property.name == "sorting_use_aabb_center") {
  145. p_property.usage = PROPERTY_USAGE_NONE;
  146. }
  147. }
  148. void VisualInstance3D::_bind_methods() {
  149. ClassDB::bind_method(D_METHOD("set_base", "base"), &VisualInstance3D::set_base);
  150. ClassDB::bind_method(D_METHOD("get_base"), &VisualInstance3D::get_base);
  151. ClassDB::bind_method(D_METHOD("get_instance"), &VisualInstance3D::get_instance);
  152. ClassDB::bind_method(D_METHOD("set_layer_mask", "mask"), &VisualInstance3D::set_layer_mask);
  153. ClassDB::bind_method(D_METHOD("get_layer_mask"), &VisualInstance3D::get_layer_mask);
  154. ClassDB::bind_method(D_METHOD("set_layer_mask_value", "layer_number", "value"), &VisualInstance3D::set_layer_mask_value);
  155. ClassDB::bind_method(D_METHOD("get_layer_mask_value", "layer_number"), &VisualInstance3D::get_layer_mask_value);
  156. ClassDB::bind_method(D_METHOD("set_sorting_offset", "offset"), &VisualInstance3D::set_sorting_offset);
  157. ClassDB::bind_method(D_METHOD("get_sorting_offset"), &VisualInstance3D::get_sorting_offset);
  158. ClassDB::bind_method(D_METHOD("set_sorting_use_aabb_center", "enabled"), &VisualInstance3D::set_sorting_use_aabb_center);
  159. ClassDB::bind_method(D_METHOD("is_sorting_use_aabb_center"), &VisualInstance3D::is_sorting_use_aabb_center);
  160. GDVIRTUAL_BIND(_get_aabb);
  161. ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_LAYERS_3D_RENDER), "set_layer_mask", "get_layer_mask");
  162. ADD_GROUP("Sorting", "sorting_");
  163. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sorting_offset"), "set_sorting_offset", "get_sorting_offset");
  164. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sorting_use_aabb_center"), "set_sorting_use_aabb_center", "is_sorting_use_aabb_center");
  165. }
  166. void VisualInstance3D::set_base(const RID &p_base) {
  167. RenderingServer::get_singleton()->instance_set_base(instance, p_base);
  168. base = p_base;
  169. }
  170. RID VisualInstance3D::get_base() const {
  171. return base;
  172. }
  173. VisualInstance3D::VisualInstance3D() {
  174. instance = RenderingServer::get_singleton()->instance_create();
  175. RenderingServer::get_singleton()->instance_attach_object_instance_id(instance, get_instance_id());
  176. set_notify_transform(true);
  177. }
  178. VisualInstance3D::~VisualInstance3D() {
  179. ERR_FAIL_NULL(RenderingServer::get_singleton());
  180. RenderingServer::get_singleton()->free(instance);
  181. }
  182. void GeometryInstance3D::set_material_override(const Ref<Material> &p_material) {
  183. if (material_override.is_valid()) {
  184. material_override->disconnect(CoreStringName(property_list_changed), callable_mp((Object *)this, &Object::notify_property_list_changed));
  185. }
  186. material_override = p_material;
  187. if (material_override.is_valid()) {
  188. material_override->connect(CoreStringName(property_list_changed), callable_mp((Object *)this, &Object::notify_property_list_changed));
  189. }
  190. RS::get_singleton()->instance_geometry_set_material_override(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID());
  191. }
  192. Ref<Material> GeometryInstance3D::get_material_override() const {
  193. return material_override;
  194. }
  195. void GeometryInstance3D::set_material_overlay(const Ref<Material> &p_material) {
  196. material_overlay = p_material;
  197. RS::get_singleton()->instance_geometry_set_material_overlay(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID());
  198. }
  199. Ref<Material> GeometryInstance3D::get_material_overlay() const {
  200. return material_overlay;
  201. }
  202. void GeometryInstance3D::set_transparency(float p_transparency) {
  203. transparency = CLAMP(p_transparency, 0.0f, 1.0f);
  204. RS::get_singleton()->instance_geometry_set_transparency(get_instance(), transparency);
  205. update_configuration_warnings();
  206. }
  207. float GeometryInstance3D::get_transparency() const {
  208. return transparency;
  209. }
  210. void GeometryInstance3D::set_visibility_range_begin(float p_dist) {
  211. visibility_range_begin = p_dist;
  212. RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode);
  213. update_configuration_warnings();
  214. }
  215. float GeometryInstance3D::get_visibility_range_begin() const {
  216. return visibility_range_begin;
  217. }
  218. void GeometryInstance3D::set_visibility_range_end(float p_dist) {
  219. visibility_range_end = p_dist;
  220. RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode);
  221. update_configuration_warnings();
  222. }
  223. float GeometryInstance3D::get_visibility_range_end() const {
  224. return visibility_range_end;
  225. }
  226. void GeometryInstance3D::set_visibility_range_begin_margin(float p_dist) {
  227. visibility_range_begin_margin = p_dist;
  228. RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode);
  229. update_configuration_warnings();
  230. }
  231. float GeometryInstance3D::get_visibility_range_begin_margin() const {
  232. return visibility_range_begin_margin;
  233. }
  234. void GeometryInstance3D::set_visibility_range_end_margin(float p_dist) {
  235. visibility_range_end_margin = p_dist;
  236. RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode);
  237. update_configuration_warnings();
  238. }
  239. float GeometryInstance3D::get_visibility_range_end_margin() const {
  240. return visibility_range_end_margin;
  241. }
  242. void GeometryInstance3D::set_visibility_range_fade_mode(VisibilityRangeFadeMode p_mode) {
  243. visibility_range_fade_mode = p_mode;
  244. RS::get_singleton()->instance_geometry_set_visibility_range(get_instance(), visibility_range_begin, visibility_range_end, visibility_range_begin_margin, visibility_range_end_margin, (RS::VisibilityRangeFadeMode)visibility_range_fade_mode);
  245. update_configuration_warnings();
  246. }
  247. GeometryInstance3D::VisibilityRangeFadeMode GeometryInstance3D::get_visibility_range_fade_mode() const {
  248. return visibility_range_fade_mode;
  249. }
  250. const StringName *GeometryInstance3D::_instance_uniform_get_remap(const StringName &p_name) const {
  251. StringName *r = instance_shader_parameter_property_remap.getptr(p_name);
  252. if (!r) {
  253. String s = p_name;
  254. #ifndef DISABLE_DEPRECATED
  255. if (s.begins_with("shader_uniforms/")) {
  256. s = s.replace("shader_uniforms/", "instance_shader_parameters/");
  257. }
  258. #endif // DISABLE_DEPRECATED
  259. if (s.begins_with("instance_shader_parameters/")) {
  260. StringName pname = StringName(s);
  261. StringName name = s.replace("instance_shader_parameters/", "");
  262. instance_shader_parameter_property_remap[pname] = name;
  263. return instance_shader_parameter_property_remap.getptr(pname);
  264. }
  265. return nullptr;
  266. }
  267. return r;
  268. }
  269. bool GeometryInstance3D::_set(const StringName &p_name, const Variant &p_value) {
  270. const StringName *r = _instance_uniform_get_remap(p_name);
  271. if (r) {
  272. set_instance_shader_parameter(*r, p_value);
  273. return true;
  274. }
  275. #ifndef DISABLE_DEPRECATED
  276. if (p_name == SNAME("use_in_baked_light") && bool(p_value)) {
  277. set_gi_mode(GI_MODE_STATIC);
  278. return true;
  279. }
  280. if (p_name == SNAME("use_dynamic_gi") && bool(p_value)) {
  281. set_gi_mode(GI_MODE_DYNAMIC);
  282. return true;
  283. }
  284. #endif // DISABLE_DEPRECATED
  285. return false;
  286. }
  287. bool GeometryInstance3D::_get(const StringName &p_name, Variant &r_ret) const {
  288. const StringName *r = _instance_uniform_get_remap(p_name);
  289. if (r) {
  290. r_ret = get_instance_shader_parameter(*r);
  291. return true;
  292. }
  293. return false;
  294. }
  295. void GeometryInstance3D::_get_property_list(List<PropertyInfo> *p_list) const {
  296. List<PropertyInfo> pinfo;
  297. RS::get_singleton()->instance_geometry_get_shader_parameter_list(get_instance(), &pinfo);
  298. for (PropertyInfo &pi : pinfo) {
  299. bool has_def_value = false;
  300. Variant def_value = RS::get_singleton()->instance_geometry_get_shader_parameter_default_value(get_instance(), pi.name);
  301. if (def_value.get_type() != Variant::NIL) {
  302. has_def_value = true;
  303. }
  304. if (instance_shader_parameters.has(pi.name)) {
  305. pi.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | (has_def_value ? (PROPERTY_USAGE_CHECKABLE | PROPERTY_USAGE_CHECKED) : PROPERTY_USAGE_NONE);
  306. } else {
  307. pi.usage = PROPERTY_USAGE_EDITOR | (has_def_value ? PROPERTY_USAGE_CHECKABLE : PROPERTY_USAGE_NONE); //do not save if not changed
  308. }
  309. pi.name = "instance_shader_parameters/" + pi.name;
  310. p_list->push_back(pi);
  311. }
  312. }
  313. void GeometryInstance3D::set_cast_shadows_setting(ShadowCastingSetting p_shadow_casting_setting) {
  314. shadow_casting_setting = p_shadow_casting_setting;
  315. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(get_instance(), (RS::ShadowCastingSetting)p_shadow_casting_setting);
  316. }
  317. GeometryInstance3D::ShadowCastingSetting GeometryInstance3D::get_cast_shadows_setting() const {
  318. return shadow_casting_setting;
  319. }
  320. void GeometryInstance3D::set_extra_cull_margin(float p_margin) {
  321. ERR_FAIL_COND(p_margin < 0);
  322. extra_cull_margin = p_margin;
  323. RS::get_singleton()->instance_set_extra_visibility_margin(get_instance(), extra_cull_margin);
  324. }
  325. float GeometryInstance3D::get_extra_cull_margin() const {
  326. return extra_cull_margin;
  327. }
  328. void GeometryInstance3D::set_lod_bias(float p_bias) {
  329. ERR_FAIL_COND(p_bias < 0.0);
  330. lod_bias = p_bias;
  331. RS::get_singleton()->instance_geometry_set_lod_bias(get_instance(), lod_bias);
  332. }
  333. float GeometryInstance3D::get_lod_bias() const {
  334. return lod_bias;
  335. }
  336. void GeometryInstance3D::set_instance_shader_parameter(const StringName &p_name, const Variant &p_value) {
  337. if (p_value.get_type() == Variant::NIL) {
  338. Variant def_value = RS::get_singleton()->instance_geometry_get_shader_parameter_default_value(get_instance(), p_name);
  339. RS::get_singleton()->instance_geometry_set_shader_parameter(get_instance(), p_name, def_value);
  340. instance_shader_parameters.erase(p_value);
  341. } else {
  342. instance_shader_parameters[p_name] = p_value;
  343. if (p_value.get_type() == Variant::OBJECT) {
  344. RID tex_id = p_value;
  345. RS::get_singleton()->instance_geometry_set_shader_parameter(get_instance(), p_name, tex_id);
  346. } else {
  347. RS::get_singleton()->instance_geometry_set_shader_parameter(get_instance(), p_name, p_value);
  348. }
  349. }
  350. }
  351. Variant GeometryInstance3D::get_instance_shader_parameter(const StringName &p_name) const {
  352. return RS::get_singleton()->instance_geometry_get_shader_parameter(get_instance(), p_name);
  353. }
  354. void GeometryInstance3D::set_custom_aabb(AABB p_aabb) {
  355. if (p_aabb == custom_aabb) {
  356. return;
  357. }
  358. custom_aabb = p_aabb;
  359. RS::get_singleton()->instance_set_custom_aabb(get_instance(), custom_aabb);
  360. update_gizmos();
  361. }
  362. AABB GeometryInstance3D::get_custom_aabb() const {
  363. return custom_aabb;
  364. }
  365. void GeometryInstance3D::set_lightmap_texel_scale(float p_scale) {
  366. lightmap_texel_scale = p_scale;
  367. }
  368. float GeometryInstance3D::get_lightmap_texel_scale() const {
  369. return lightmap_texel_scale;
  370. }
  371. #ifndef DISABLE_DEPRECATED
  372. void GeometryInstance3D::set_lightmap_scale(LightmapScale p_scale) {
  373. ERR_FAIL_INDEX(p_scale, LIGHTMAP_SCALE_MAX);
  374. switch (p_scale) {
  375. case GeometryInstance3D::LIGHTMAP_SCALE_1X:
  376. lightmap_texel_scale = 1.0f;
  377. break;
  378. case GeometryInstance3D::LIGHTMAP_SCALE_2X:
  379. lightmap_texel_scale = 2.0f;
  380. break;
  381. case GeometryInstance3D::LIGHTMAP_SCALE_4X:
  382. lightmap_texel_scale = 4.0f;
  383. break;
  384. case GeometryInstance3D::LIGHTMAP_SCALE_8X:
  385. lightmap_texel_scale = 8.0f;
  386. break;
  387. case GeometryInstance3D::LIGHTMAP_SCALE_MAX:
  388. break; // Can't happen, but silences warning.
  389. }
  390. }
  391. GeometryInstance3D::LightmapScale GeometryInstance3D::get_lightmap_scale() const {
  392. // Return closest approximation.
  393. if (lightmap_texel_scale < 1.5f) {
  394. return GeometryInstance3D::LIGHTMAP_SCALE_1X;
  395. } else if (lightmap_texel_scale < 3.0f) {
  396. return GeometryInstance3D::LIGHTMAP_SCALE_2X;
  397. } else if (lightmap_texel_scale < 6.0f) {
  398. return GeometryInstance3D::LIGHTMAP_SCALE_4X;
  399. }
  400. return GeometryInstance3D::LIGHTMAP_SCALE_8X;
  401. }
  402. #endif // DISABLE_DEPRECATED
  403. void GeometryInstance3D::set_gi_mode(GIMode p_mode) {
  404. switch (p_mode) {
  405. case GI_MODE_DISABLED: {
  406. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  407. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_DYNAMIC_GI, false);
  408. } break;
  409. case GI_MODE_STATIC: {
  410. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_BAKED_LIGHT, true);
  411. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_DYNAMIC_GI, false);
  412. } break;
  413. case GI_MODE_DYNAMIC: {
  414. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  415. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_USE_DYNAMIC_GI, true);
  416. } break;
  417. }
  418. gi_mode = p_mode;
  419. }
  420. GeometryInstance3D::GIMode GeometryInstance3D::get_gi_mode() const {
  421. return gi_mode;
  422. }
  423. void GeometryInstance3D::set_ignore_occlusion_culling(bool p_enabled) {
  424. ignore_occlusion_culling = p_enabled;
  425. RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, ignore_occlusion_culling);
  426. }
  427. bool GeometryInstance3D::is_ignoring_occlusion_culling() {
  428. return ignore_occlusion_culling;
  429. }
  430. Ref<TriangleMesh> GeometryInstance3D::generate_triangle_mesh() const {
  431. return Ref<TriangleMesh>();
  432. }
  433. PackedStringArray GeometryInstance3D::get_configuration_warnings() const {
  434. PackedStringArray warnings = VisualInstance3D::get_configuration_warnings();
  435. if (!Math::is_zero_approx(visibility_range_end) && visibility_range_end <= visibility_range_begin) {
  436. warnings.push_back(RTR("The GeometryInstance3D visibility range's End distance is set to a non-zero value, but is lower than the Begin distance.\nThis means the GeometryInstance3D will never be visible.\nTo resolve this, set the End distance to 0 or to a value greater than the Begin distance."));
  437. }
  438. if ((visibility_range_fade_mode == VISIBILITY_RANGE_FADE_SELF || visibility_range_fade_mode == VISIBILITY_RANGE_FADE_DEPENDENCIES) && !Math::is_zero_approx(visibility_range_begin) && Math::is_zero_approx(visibility_range_begin_margin)) {
  439. warnings.push_back(RTR("The GeometryInstance3D is configured to fade in smoothly over distance, but the fade transition distance is set to 0.\nTo resolve this, increase Visibility Range Begin Margin above 0."));
  440. }
  441. if ((visibility_range_fade_mode == VISIBILITY_RANGE_FADE_SELF || visibility_range_fade_mode == VISIBILITY_RANGE_FADE_DEPENDENCIES) && !Math::is_zero_approx(visibility_range_end) && Math::is_zero_approx(visibility_range_end_margin)) {
  442. warnings.push_back(RTR("The GeometryInstance3D is configured to fade out smoothly over distance, but the fade transition distance is set to 0.\nTo resolve this, increase Visibility Range End Margin above 0."));
  443. }
  444. if (!Math::is_zero_approx(transparency) && OS::get_singleton()->get_current_rendering_method() != "forward_plus") {
  445. warnings.push_back(RTR("GeometryInstance3D transparency is only available when using the Forward+ rendering method."));
  446. }
  447. if ((visibility_range_fade_mode == VISIBILITY_RANGE_FADE_SELF || visibility_range_fade_mode == VISIBILITY_RANGE_FADE_DEPENDENCIES) && OS::get_singleton()->get_current_rendering_method() != "forward_plus") {
  448. warnings.push_back(RTR("GeometryInstance3D visibility range transparency fade is only available when using the Forward+ rendering method."));
  449. }
  450. return warnings;
  451. }
  452. void GeometryInstance3D::_validate_property(PropertyInfo &p_property) const {
  453. if (p_property.name == "sorting_offset" || p_property.name == "sorting_use_aabb_center") {
  454. p_property.usage = PROPERTY_USAGE_DEFAULT;
  455. }
  456. }
  457. void GeometryInstance3D::_bind_methods() {
  458. ClassDB::bind_method(D_METHOD("set_material_override", "material"), &GeometryInstance3D::set_material_override);
  459. ClassDB::bind_method(D_METHOD("get_material_override"), &GeometryInstance3D::get_material_override);
  460. ClassDB::bind_method(D_METHOD("set_material_overlay", "material"), &GeometryInstance3D::set_material_overlay);
  461. ClassDB::bind_method(D_METHOD("get_material_overlay"), &GeometryInstance3D::get_material_overlay);
  462. ClassDB::bind_method(D_METHOD("set_cast_shadows_setting", "shadow_casting_setting"), &GeometryInstance3D::set_cast_shadows_setting);
  463. ClassDB::bind_method(D_METHOD("get_cast_shadows_setting"), &GeometryInstance3D::get_cast_shadows_setting);
  464. ClassDB::bind_method(D_METHOD("set_lod_bias", "bias"), &GeometryInstance3D::set_lod_bias);
  465. ClassDB::bind_method(D_METHOD("get_lod_bias"), &GeometryInstance3D::get_lod_bias);
  466. ClassDB::bind_method(D_METHOD("set_transparency", "transparency"), &GeometryInstance3D::set_transparency);
  467. ClassDB::bind_method(D_METHOD("get_transparency"), &GeometryInstance3D::get_transparency);
  468. ClassDB::bind_method(D_METHOD("set_visibility_range_end_margin", "distance"), &GeometryInstance3D::set_visibility_range_end_margin);
  469. ClassDB::bind_method(D_METHOD("get_visibility_range_end_margin"), &GeometryInstance3D::get_visibility_range_end_margin);
  470. ClassDB::bind_method(D_METHOD("set_visibility_range_end", "distance"), &GeometryInstance3D::set_visibility_range_end);
  471. ClassDB::bind_method(D_METHOD("get_visibility_range_end"), &GeometryInstance3D::get_visibility_range_end);
  472. ClassDB::bind_method(D_METHOD("set_visibility_range_begin_margin", "distance"), &GeometryInstance3D::set_visibility_range_begin_margin);
  473. ClassDB::bind_method(D_METHOD("get_visibility_range_begin_margin"), &GeometryInstance3D::get_visibility_range_begin_margin);
  474. ClassDB::bind_method(D_METHOD("set_visibility_range_begin", "distance"), &GeometryInstance3D::set_visibility_range_begin);
  475. ClassDB::bind_method(D_METHOD("get_visibility_range_begin"), &GeometryInstance3D::get_visibility_range_begin);
  476. ClassDB::bind_method(D_METHOD("set_visibility_range_fade_mode", "mode"), &GeometryInstance3D::set_visibility_range_fade_mode);
  477. ClassDB::bind_method(D_METHOD("get_visibility_range_fade_mode"), &GeometryInstance3D::get_visibility_range_fade_mode);
  478. ClassDB::bind_method(D_METHOD("set_instance_shader_parameter", "name", "value"), &GeometryInstance3D::set_instance_shader_parameter);
  479. ClassDB::bind_method(D_METHOD("get_instance_shader_parameter", "name"), &GeometryInstance3D::get_instance_shader_parameter);
  480. ClassDB::bind_method(D_METHOD("set_extra_cull_margin", "margin"), &GeometryInstance3D::set_extra_cull_margin);
  481. ClassDB::bind_method(D_METHOD("get_extra_cull_margin"), &GeometryInstance3D::get_extra_cull_margin);
  482. ClassDB::bind_method(D_METHOD("set_lightmap_texel_scale", "scale"), &GeometryInstance3D::set_lightmap_texel_scale);
  483. ClassDB::bind_method(D_METHOD("get_lightmap_texel_scale"), &GeometryInstance3D::get_lightmap_texel_scale);
  484. #ifndef DISABLE_DEPRECATED
  485. ClassDB::bind_method(D_METHOD("set_lightmap_scale", "scale"), &GeometryInstance3D::set_lightmap_scale);
  486. ClassDB::bind_method(D_METHOD("get_lightmap_scale"), &GeometryInstance3D::get_lightmap_scale);
  487. #endif // DISABLE_DEPRECATED
  488. ClassDB::bind_method(D_METHOD("set_gi_mode", "mode"), &GeometryInstance3D::set_gi_mode);
  489. ClassDB::bind_method(D_METHOD("get_gi_mode"), &GeometryInstance3D::get_gi_mode);
  490. ClassDB::bind_method(D_METHOD("set_ignore_occlusion_culling", "ignore_culling"), &GeometryInstance3D::set_ignore_occlusion_culling);
  491. ClassDB::bind_method(D_METHOD("is_ignoring_occlusion_culling"), &GeometryInstance3D::is_ignoring_occlusion_culling);
  492. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &GeometryInstance3D::set_custom_aabb);
  493. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &GeometryInstance3D::get_custom_aabb);
  494. ClassDB::bind_method(D_METHOD("get_aabb"), &GeometryInstance3D::get_aabb);
  495. ADD_GROUP("Geometry", "");
  496. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_override", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_DEFAULT), "set_material_override", "get_material_override");
  497. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_overlay", PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial", PROPERTY_USAGE_DEFAULT), "set_material_overlay", "get_material_overlay");
  498. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "transparency", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_transparency", "get_transparency");
  499. ADD_PROPERTY(PropertyInfo(Variant::INT, "cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows_setting", "get_cast_shadows_setting");
  500. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "extra_cull_margin", PROPERTY_HINT_RANGE, "0,16384,0.01,suffix:m"), "set_extra_cull_margin", "get_extra_cull_margin");
  501. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_custom_aabb", "get_custom_aabb");
  502. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lod_bias", PROPERTY_HINT_RANGE, "0.001,128,0.001"), "set_lod_bias", "get_lod_bias");
  503. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_occlusion_culling"), "set_ignore_occlusion_culling", "is_ignoring_occlusion_culling");
  504. ADD_GROUP("Global Illumination", "gi_");
  505. ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_mode", PROPERTY_HINT_ENUM, "Disabled,Static,Dynamic"), "set_gi_mode", "get_gi_mode");
  506. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gi_lightmap_texel_scale", PROPERTY_HINT_RANGE, "0.01,10,0.0001,or_greater"), "set_lightmap_texel_scale", "get_lightmap_texel_scale");
  507. #ifndef DISABLE_DEPRECATED
  508. ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_lightmap_scale", PROPERTY_HINT_ENUM, String::utf8("1×,2×,4×,8×"), PROPERTY_USAGE_NONE), "set_lightmap_scale", "get_lightmap_scale");
  509. #endif // DISABLE_DEPRECATED
  510. ADD_GROUP("Visibility Range", "visibility_range_");
  511. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_begin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_begin", "get_visibility_range_begin");
  512. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_begin_margin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_begin_margin", "get_visibility_range_begin_margin");
  513. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_end", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_end", "get_visibility_range_end");
  514. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visibility_range_end_margin", PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m"), "set_visibility_range_end_margin", "get_visibility_range_end_margin");
  515. ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_range_fade_mode", PROPERTY_HINT_ENUM, "Disabled,Self,Dependencies"), "set_visibility_range_fade_mode", "get_visibility_range_fade_mode");
  516. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_OFF);
  517. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_ON);
  518. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_DOUBLE_SIDED);
  519. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_SHADOWS_ONLY);
  520. BIND_ENUM_CONSTANT(GI_MODE_DISABLED);
  521. BIND_ENUM_CONSTANT(GI_MODE_STATIC);
  522. BIND_ENUM_CONSTANT(GI_MODE_DYNAMIC);
  523. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_1X);
  524. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_2X);
  525. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_4X);
  526. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_8X);
  527. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_MAX);
  528. BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_DISABLED);
  529. BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_SELF);
  530. BIND_ENUM_CONSTANT(VISIBILITY_RANGE_FADE_DEPENDENCIES);
  531. }
  532. GeometryInstance3D::GeometryInstance3D() {
  533. }
  534. GeometryInstance3D::~GeometryInstance3D() {
  535. if (material_overlay.is_valid()) {
  536. set_material_overlay(Ref<Material>());
  537. }
  538. if (material_override.is_valid()) {
  539. set_material_override(Ref<Material>());
  540. }
  541. }