visual_instance.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*************************************************************************/
  2. /* visual_instance.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 "visual_instance.h"
  31. #include "scene/scene_string_names.h"
  32. #include "servers/visual_server.h"
  33. #include "skeleton.h"
  34. AABB VisualInstance::get_transformed_aabb() const {
  35. return get_global_transform().xform(get_aabb());
  36. }
  37. void VisualInstance::_refresh_portal_mode() {
  38. VisualServer::get_singleton()->instance_set_portal_mode(instance, (VisualServer::InstancePortalMode)get_portal_mode());
  39. }
  40. void VisualInstance::_update_visibility() {
  41. if (!is_inside_tree()) {
  42. return;
  43. }
  44. bool visible = is_visible_in_tree();
  45. // keep a quick flag available in each node.
  46. // no need to call is_visible_in_tree all over the place,
  47. // providing it is propagated with a notification.
  48. bool already_visible = _is_vi_visible();
  49. _set_vi_visible(visible);
  50. // if making visible, make sure the visual server is up to date with the transform
  51. if (visible && (!already_visible)) {
  52. if (!_is_using_identity_transform()) {
  53. Transform gt = get_global_transform();
  54. VisualServer::get_singleton()->instance_set_transform(instance, gt);
  55. }
  56. }
  57. _change_notify("visible");
  58. VS::get_singleton()->instance_set_visible(get_instance(), visible);
  59. }
  60. void VisualInstance::set_instance_use_identity_transform(bool p_enable) {
  61. // prevent sending instance transforms when using global coords
  62. _set_use_identity_transform(p_enable);
  63. if (is_inside_tree()) {
  64. if (p_enable) {
  65. // want to make sure instance is using identity transform
  66. VisualServer::get_singleton()->instance_set_transform(instance, get_global_transform());
  67. } else {
  68. // want to make sure instance is up to date
  69. VisualServer::get_singleton()->instance_set_transform(instance, Transform());
  70. }
  71. }
  72. }
  73. void VisualInstance::_notification(int p_what) {
  74. switch (p_what) {
  75. case NOTIFICATION_ENTER_WORLD: {
  76. // CHECK SKELETON => moving skeleton attaching logic to MeshInstance
  77. /*
  78. Skeleton *skeleton=Object::cast_to<Skeleton>(get_parent());
  79. if (skeleton)
  80. VisualServer::get_singleton()->instance_attach_skeleton( instance, skeleton->get_skeleton() );
  81. */
  82. ERR_FAIL_COND(get_world().is_null());
  83. VisualServer::get_singleton()->instance_set_scenario(instance, get_world()->get_scenario());
  84. _update_visibility();
  85. } break;
  86. case NOTIFICATION_TRANSFORM_CHANGED: {
  87. if (_is_vi_visible() || is_physics_interpolated_and_enabled()) {
  88. if (!_is_using_identity_transform()) {
  89. Transform gt = get_global_transform();
  90. VisualServer::get_singleton()->instance_set_transform(instance, gt);
  91. // For instance when first adding to the tree, when the previous transform is
  92. // unset, to prevent streaking from the origin.
  93. if (_is_physics_interpolation_reset_requested()) {
  94. if (_is_vi_visible()) {
  95. _notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  96. }
  97. _set_physics_interpolation_reset_requested(false);
  98. }
  99. }
  100. }
  101. } break;
  102. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  103. if (_is_vi_visible() && is_physics_interpolated()) {
  104. VisualServer::get_singleton()->instance_reset_physics_interpolation(instance);
  105. }
  106. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  107. else if (GLOBAL_GET("debug/settings/physics_interpolation/enable_warnings")) {
  108. String node_name = is_inside_tree() ? String(get_path()) : String(get_name());
  109. if (!_is_vi_visible()) {
  110. WARN_PRINT("[Physics interpolation] NOTIFICATION_RESET_PHYSICS_INTERPOLATION only works with unhidden nodes: \"" + node_name + "\".");
  111. }
  112. if (!is_physics_interpolated()) {
  113. WARN_PRINT("[Physics interpolation] NOTIFICATION_RESET_PHYSICS_INTERPOLATION only works with interpolated nodes: \"" + node_name + "\".");
  114. }
  115. }
  116. #endif
  117. } break;
  118. case NOTIFICATION_EXIT_WORLD: {
  119. VisualServer::get_singleton()->instance_set_scenario(instance, RID());
  120. VisualServer::get_singleton()->instance_attach_skeleton(instance, RID());
  121. //VS::get_singleton()->instance_geometry_set_baked_light_sampler(instance, RID() );
  122. // the vi visible flag is always set to invisible when outside the tree,
  123. // so it can detect re-entering the tree and becoming visible, and send
  124. // the transform to the visual server
  125. _set_vi_visible(false);
  126. } break;
  127. case NOTIFICATION_VISIBILITY_CHANGED: {
  128. _update_visibility();
  129. } break;
  130. }
  131. }
  132. void VisualInstance::_physics_interpolated_changed() {
  133. VisualServer::get_singleton()->instance_set_interpolated(instance, is_physics_interpolated());
  134. }
  135. RID VisualInstance::get_instance() const {
  136. return instance;
  137. }
  138. RID VisualInstance::_get_visual_instance_rid() const {
  139. return instance;
  140. }
  141. void VisualInstance::set_layer_mask(uint32_t p_mask) {
  142. layers = p_mask;
  143. VisualServer::get_singleton()->instance_set_layer_mask(instance, p_mask);
  144. }
  145. uint32_t VisualInstance::get_layer_mask() const {
  146. return layers;
  147. }
  148. void VisualInstance::set_layer_mask_bit(int p_layer, bool p_enable) {
  149. ERR_FAIL_INDEX(p_layer, 32);
  150. if (p_enable) {
  151. set_layer_mask(layers | (1 << p_layer));
  152. } else {
  153. set_layer_mask(layers & (~(1 << p_layer)));
  154. }
  155. }
  156. bool VisualInstance::get_layer_mask_bit(int p_layer) const {
  157. ERR_FAIL_INDEX_V(p_layer, 32, false);
  158. return (layers & (1 << p_layer));
  159. }
  160. void VisualInstance::_bind_methods() {
  161. ClassDB::bind_method(D_METHOD("_get_visual_instance_rid"), &VisualInstance::_get_visual_instance_rid);
  162. ClassDB::bind_method(D_METHOD("set_base", "base"), &VisualInstance::set_base);
  163. ClassDB::bind_method(D_METHOD("get_base"), &VisualInstance::get_base);
  164. ClassDB::bind_method(D_METHOD("get_instance"), &VisualInstance::get_instance);
  165. ClassDB::bind_method(D_METHOD("set_layer_mask", "mask"), &VisualInstance::set_layer_mask);
  166. ClassDB::bind_method(D_METHOD("get_layer_mask"), &VisualInstance::get_layer_mask);
  167. ClassDB::bind_method(D_METHOD("set_layer_mask_bit", "layer", "enabled"), &VisualInstance::set_layer_mask_bit);
  168. ClassDB::bind_method(D_METHOD("get_layer_mask_bit", "layer"), &VisualInstance::get_layer_mask_bit);
  169. ClassDB::bind_method(D_METHOD("get_transformed_aabb"), &VisualInstance::get_transformed_aabb);
  170. ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_LAYERS_3D_RENDER), "set_layer_mask", "get_layer_mask");
  171. }
  172. void VisualInstance::set_base(const RID &p_base) {
  173. VisualServer::get_singleton()->instance_set_base(instance, p_base);
  174. base = p_base;
  175. }
  176. RID VisualInstance::get_base() const {
  177. return base;
  178. }
  179. VisualInstance::VisualInstance() {
  180. instance = RID_PRIME(VisualServer::get_singleton()->instance_create());
  181. VisualServer::get_singleton()->instance_attach_object_instance_id(instance, get_instance_id());
  182. layers = 1;
  183. set_notify_transform(true);
  184. }
  185. VisualInstance::~VisualInstance() {
  186. VisualServer::get_singleton()->free(instance);
  187. }
  188. void GeometryInstance::set_material_override(const Ref<Material> &p_material) {
  189. material_override = p_material;
  190. VS::get_singleton()->instance_geometry_set_material_override(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID());
  191. }
  192. Ref<Material> GeometryInstance::get_material_override() const {
  193. return material_override;
  194. }
  195. void GeometryInstance::set_material_overlay(const Ref<Material> &p_material) {
  196. material_overlay = p_material;
  197. VS::get_singleton()->instance_geometry_set_material_overlay(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID());
  198. }
  199. Ref<Material> GeometryInstance::get_material_overlay() const {
  200. return material_overlay;
  201. }
  202. void GeometryInstance::set_generate_lightmap(bool p_enabled) {
  203. generate_lightmap = p_enabled;
  204. }
  205. bool GeometryInstance::get_generate_lightmap() {
  206. return generate_lightmap;
  207. }
  208. void GeometryInstance::set_lightmap_scale(LightmapScale p_scale) {
  209. ERR_FAIL_INDEX(p_scale, LIGHTMAP_SCALE_MAX);
  210. lightmap_scale = p_scale;
  211. }
  212. GeometryInstance::LightmapScale GeometryInstance::get_lightmap_scale() const {
  213. return lightmap_scale;
  214. }
  215. void GeometryInstance::set_lod_min_distance(float p_dist) {
  216. lod_min_distance = p_dist;
  217. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(), lod_min_distance, lod_max_distance, lod_min_hysteresis, lod_max_hysteresis);
  218. }
  219. float GeometryInstance::get_lod_min_distance() const {
  220. return lod_min_distance;
  221. }
  222. void GeometryInstance::set_lod_max_distance(float p_dist) {
  223. lod_max_distance = p_dist;
  224. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(), lod_min_distance, lod_max_distance, lod_min_hysteresis, lod_max_hysteresis);
  225. }
  226. float GeometryInstance::get_lod_max_distance() const {
  227. return lod_max_distance;
  228. }
  229. void GeometryInstance::set_lod_min_hysteresis(float p_dist) {
  230. lod_min_hysteresis = p_dist;
  231. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(), lod_min_distance, lod_max_distance, lod_min_hysteresis, lod_max_hysteresis);
  232. }
  233. float GeometryInstance::get_lod_min_hysteresis() const {
  234. return lod_min_hysteresis;
  235. }
  236. void GeometryInstance::set_lod_max_hysteresis(float p_dist) {
  237. lod_max_hysteresis = p_dist;
  238. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(), lod_min_distance, lod_max_distance, lod_min_hysteresis, lod_max_hysteresis);
  239. }
  240. float GeometryInstance::get_lod_max_hysteresis() const {
  241. return lod_max_hysteresis;
  242. }
  243. void GeometryInstance::_notification(int p_what) {
  244. }
  245. void GeometryInstance::set_flag(Flags p_flag, bool p_value) {
  246. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  247. if (flags[p_flag] == p_value) {
  248. return;
  249. }
  250. flags[p_flag] = p_value;
  251. VS::get_singleton()->instance_geometry_set_flag(get_instance(), (VS::InstanceFlags)p_flag, p_value);
  252. }
  253. bool GeometryInstance::get_flag(Flags p_flag) const {
  254. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  255. return flags[p_flag];
  256. }
  257. void GeometryInstance::set_cast_shadows_setting(ShadowCastingSetting p_shadow_casting_setting) {
  258. shadow_casting_setting = p_shadow_casting_setting;
  259. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(get_instance(), (VS::ShadowCastingSetting)p_shadow_casting_setting);
  260. }
  261. GeometryInstance::ShadowCastingSetting GeometryInstance::get_cast_shadows_setting() const {
  262. return shadow_casting_setting;
  263. }
  264. void GeometryInstance::set_extra_cull_margin(float p_margin) {
  265. ERR_FAIL_COND(p_margin < 0);
  266. extra_cull_margin = p_margin;
  267. VS::get_singleton()->instance_set_extra_visibility_margin(get_instance(), extra_cull_margin);
  268. }
  269. float GeometryInstance::get_extra_cull_margin() const {
  270. return extra_cull_margin;
  271. }
  272. void GeometryInstance::set_custom_aabb(AABB aabb) {
  273. VS::get_singleton()->instance_set_custom_aabb(get_instance(), aabb);
  274. }
  275. void GeometryInstance::_bind_methods() {
  276. ClassDB::bind_method(D_METHOD("set_material_override", "material"), &GeometryInstance::set_material_override);
  277. ClassDB::bind_method(D_METHOD("get_material_override"), &GeometryInstance::get_material_override);
  278. ClassDB::bind_method(D_METHOD("set_material_overlay", "material"), &GeometryInstance::set_material_overlay);
  279. ClassDB::bind_method(D_METHOD("get_material_overlay"), &GeometryInstance::get_material_overlay);
  280. ClassDB::bind_method(D_METHOD("set_flag", "flag", "value"), &GeometryInstance::set_flag);
  281. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &GeometryInstance::get_flag);
  282. ClassDB::bind_method(D_METHOD("set_cast_shadows_setting", "shadow_casting_setting"), &GeometryInstance::set_cast_shadows_setting);
  283. ClassDB::bind_method(D_METHOD("get_cast_shadows_setting"), &GeometryInstance::get_cast_shadows_setting);
  284. ClassDB::bind_method(D_METHOD("set_generate_lightmap", "enabled"), &GeometryInstance::set_generate_lightmap);
  285. ClassDB::bind_method(D_METHOD("get_generate_lightmap"), &GeometryInstance::get_generate_lightmap);
  286. ClassDB::bind_method(D_METHOD("set_lightmap_scale", "scale"), &GeometryInstance::set_lightmap_scale);
  287. ClassDB::bind_method(D_METHOD("get_lightmap_scale"), &GeometryInstance::get_lightmap_scale);
  288. ClassDB::bind_method(D_METHOD("set_lod_max_hysteresis", "mode"), &GeometryInstance::set_lod_max_hysteresis);
  289. ClassDB::bind_method(D_METHOD("get_lod_max_hysteresis"), &GeometryInstance::get_lod_max_hysteresis);
  290. ClassDB::bind_method(D_METHOD("set_lod_max_distance", "mode"), &GeometryInstance::set_lod_max_distance);
  291. ClassDB::bind_method(D_METHOD("get_lod_max_distance"), &GeometryInstance::get_lod_max_distance);
  292. ClassDB::bind_method(D_METHOD("set_lod_min_hysteresis", "mode"), &GeometryInstance::set_lod_min_hysteresis);
  293. ClassDB::bind_method(D_METHOD("get_lod_min_hysteresis"), &GeometryInstance::get_lod_min_hysteresis);
  294. ClassDB::bind_method(D_METHOD("set_lod_min_distance", "mode"), &GeometryInstance::set_lod_min_distance);
  295. ClassDB::bind_method(D_METHOD("get_lod_min_distance"), &GeometryInstance::get_lod_min_distance);
  296. ClassDB::bind_method(D_METHOD("set_extra_cull_margin", "margin"), &GeometryInstance::set_extra_cull_margin);
  297. ClassDB::bind_method(D_METHOD("get_extra_cull_margin"), &GeometryInstance::get_extra_cull_margin);
  298. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &GeometryInstance::set_custom_aabb);
  299. ClassDB::bind_method(D_METHOD("get_aabb"), &GeometryInstance::get_aabb);
  300. ADD_GROUP("Geometry", "");
  301. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_override", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial"), "set_material_override", "get_material_override");
  302. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_overlay", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial"), "set_material_overlay", "get_material_overlay");
  303. ADD_PROPERTY(PropertyInfo(Variant::INT, "cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows_setting", "get_cast_shadows_setting");
  304. ADD_PROPERTY(PropertyInfo(Variant::REAL, "extra_cull_margin", PROPERTY_HINT_RANGE, "0,16384,0.01"), "set_extra_cull_margin", "get_extra_cull_margin");
  305. ADD_GROUP("Baked Light", "");
  306. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "use_in_baked_light"), "set_flag", "get_flag", FLAG_USE_BAKED_LIGHT);
  307. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_lightmap"), "set_generate_lightmap", "get_generate_lightmap");
  308. ADD_PROPERTY(PropertyInfo(Variant::INT, "lightmap_scale", PROPERTY_HINT_ENUM, "1x,2x,4x,8x"), "set_lightmap_scale", "get_lightmap_scale");
  309. ADD_GROUP("LOD", "lod_");
  310. ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_min_distance", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_lod_min_distance", "get_lod_min_distance");
  311. ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_min_hysteresis", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_lod_min_hysteresis", "get_lod_min_hysteresis");
  312. ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_max_distance", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_lod_max_distance", "get_lod_max_distance");
  313. ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_max_hysteresis", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_lod_max_hysteresis", "get_lod_max_hysteresis");
  314. //ADD_SIGNAL( MethodInfo("visibility_changed"));
  315. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_1X);
  316. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_2X);
  317. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_4X);
  318. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_8X);
  319. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_MAX);
  320. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_OFF);
  321. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_ON);
  322. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_DOUBLE_SIDED);
  323. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_SHADOWS_ONLY);
  324. BIND_ENUM_CONSTANT(FLAG_USE_BAKED_LIGHT);
  325. BIND_ENUM_CONSTANT(FLAG_DRAW_NEXT_FRAME_IF_VISIBLE);
  326. BIND_ENUM_CONSTANT(FLAG_MAX);
  327. }
  328. GeometryInstance::GeometryInstance() {
  329. lod_min_distance = 0;
  330. lod_max_distance = 0;
  331. lod_min_hysteresis = 0;
  332. lod_max_hysteresis = 0;
  333. for (int i = 0; i < FLAG_MAX; i++) {
  334. flags[i] = false;
  335. }
  336. shadow_casting_setting = SHADOW_CASTING_SETTING_ON;
  337. extra_cull_margin = 0;
  338. generate_lightmap = true;
  339. lightmap_scale = LightmapScale::LIGHTMAP_SCALE_1X;
  340. //VS::get_singleton()->instance_geometry_set_baked_light_texture_index(get_instance(),0);
  341. }