visual_instance.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**************************************************************************/
  2. /* visual_instance.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.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_server_visibility_and_xform(bool p_force_refresh_server) {
  41. if (!is_inside_tree()) {
  42. return;
  43. }
  44. bool visible = is_visible_in_tree();
  45. // As xforms are not always updated for invisible nodes, there are two circumstances
  46. // where we want to ensure the server has an up to date xform:
  47. // 1) When making a node visible.
  48. // 2) When the node enters the scene.
  49. if (visible || p_force_refresh_server) {
  50. if (!_is_using_identity_transform()) {
  51. Transform gt = get_global_transform();
  52. VisualServer::get_singleton()->instance_set_transform(instance, gt);
  53. }
  54. }
  55. // Aside from entering the scene, there will always have been a visibility change,
  56. // so update this in all cases.
  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, Transform());
  67. } else {
  68. // want to make sure instance is up to date
  69. VisualServer::get_singleton()->instance_set_transform(instance, get_global_transform());
  70. }
  71. }
  72. }
  73. void VisualInstance::fti_update_servers_xform() {
  74. if (!_is_using_identity_transform()) {
  75. VisualServer::get_singleton()->instance_set_transform(get_instance(), _get_cached_global_transform_interpolated());
  76. }
  77. }
  78. void VisualInstance::_notification(int p_what) {
  79. switch (p_what) {
  80. case NOTIFICATION_ENTER_WORLD: {
  81. // CHECK SKELETON => moving skeleton attaching logic to MeshInstance
  82. /*
  83. Skeleton *skeleton=Object::cast_to<Skeleton>(get_parent());
  84. if (skeleton)
  85. VisualServer::get_singleton()->instance_attach_skeleton( instance, skeleton->get_skeleton() );
  86. */
  87. ERR_FAIL_COND(get_world().is_null());
  88. VisualServer::get_singleton()->instance_set_scenario(instance, get_world()->get_scenario());
  89. _update_server_visibility_and_xform(true);
  90. } break;
  91. case NOTIFICATION_TRANSFORM_CHANGED: {
  92. // ToDo : Can we turn off notify transform for physics interpolated cases?
  93. if (is_visible_in_tree() && !SceneTree::is_fti_enabled() && !_is_using_identity_transform()) {
  94. // Physics interpolation global off, always send.
  95. VisualServer::get_singleton()->instance_set_transform(instance, get_global_transform());
  96. }
  97. } break;
  98. case NOTIFICATION_EXIT_WORLD: {
  99. VisualServer::get_singleton()->instance_set_scenario(instance, RID());
  100. VisualServer::get_singleton()->instance_attach_skeleton(instance, RID());
  101. //VS::get_singleton()->instance_geometry_set_baked_light_sampler(instance, RID() );
  102. } break;
  103. case NOTIFICATION_VISIBILITY_CHANGED: {
  104. _update_server_visibility_and_xform(false);
  105. } break;
  106. }
  107. }
  108. RID VisualInstance::get_instance() const {
  109. return instance;
  110. }
  111. RID VisualInstance::_get_visual_instance_rid() const {
  112. return instance;
  113. }
  114. void VisualInstance::set_layer_mask(uint32_t p_mask) {
  115. layers = p_mask;
  116. VisualServer::get_singleton()->instance_set_layer_mask(instance, p_mask);
  117. }
  118. uint32_t VisualInstance::get_layer_mask() const {
  119. return layers;
  120. }
  121. void VisualInstance::set_layer_mask_bit(int p_layer, bool p_enable) {
  122. ERR_FAIL_INDEX(p_layer, 32);
  123. if (p_enable) {
  124. set_layer_mask(layers | (1 << p_layer));
  125. } else {
  126. set_layer_mask(layers & (~(1 << p_layer)));
  127. }
  128. }
  129. bool VisualInstance::get_layer_mask_bit(int p_layer) const {
  130. ERR_FAIL_INDEX_V(p_layer, 32, false);
  131. return (layers & (1 << p_layer));
  132. }
  133. void VisualInstance::set_sorting_offset(float p_offset) {
  134. sorting_offset = p_offset;
  135. VisualServer::get_singleton()->instance_set_pivot_data(instance, sorting_offset, sorting_use_aabb_center);
  136. }
  137. float VisualInstance::get_sorting_offset() {
  138. return sorting_offset;
  139. }
  140. void VisualInstance::set_sorting_use_aabb_center(bool p_enabled) {
  141. sorting_use_aabb_center = p_enabled;
  142. VisualServer::get_singleton()->instance_set_pivot_data(instance, sorting_offset, sorting_use_aabb_center);
  143. }
  144. bool VisualInstance::is_sorting_use_aabb_center() {
  145. return sorting_use_aabb_center;
  146. }
  147. void VisualInstance::_bind_methods() {
  148. ClassDB::bind_method(D_METHOD("_get_visual_instance_rid"), &VisualInstance::_get_visual_instance_rid);
  149. ClassDB::bind_method(D_METHOD("set_base", "base"), &VisualInstance::set_base);
  150. ClassDB::bind_method(D_METHOD("get_base"), &VisualInstance::get_base);
  151. ClassDB::bind_method(D_METHOD("get_instance"), &VisualInstance::get_instance);
  152. ClassDB::bind_method(D_METHOD("set_layer_mask", "mask"), &VisualInstance::set_layer_mask);
  153. ClassDB::bind_method(D_METHOD("get_layer_mask"), &VisualInstance::get_layer_mask);
  154. ClassDB::bind_method(D_METHOD("set_layer_mask_bit", "layer", "enabled"), &VisualInstance::set_layer_mask_bit);
  155. ClassDB::bind_method(D_METHOD("get_layer_mask_bit", "layer"), &VisualInstance::get_layer_mask_bit);
  156. ClassDB::bind_method(D_METHOD("get_transformed_aabb"), &VisualInstance::get_transformed_aabb);
  157. ClassDB::bind_method(D_METHOD("set_sorting_offset", "offset"), &VisualInstance::set_sorting_offset);
  158. ClassDB::bind_method(D_METHOD("get_sorting_offset"), &VisualInstance::get_sorting_offset);
  159. ClassDB::bind_method(D_METHOD("set_sorting_use_aabb_center", "enabled"), &VisualInstance::set_sorting_use_aabb_center);
  160. ClassDB::bind_method(D_METHOD("is_sorting_use_aabb_center"), &VisualInstance::is_sorting_use_aabb_center);
  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::REAL, "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 VisualInstance::set_base(const RID &p_base) {
  167. VisualServer::get_singleton()->instance_set_base(instance, p_base);
  168. base = p_base;
  169. }
  170. RID VisualInstance::get_base() const {
  171. return base;
  172. }
  173. VisualInstance::VisualInstance() {
  174. _define_ancestry(AncestralClass::VISUAL_INSTANCE);
  175. instance = RID_PRIME(VisualServer::get_singleton()->instance_create());
  176. VisualServer::get_singleton()->instance_attach_object_instance_id(instance, get_instance_id());
  177. layers = 1;
  178. sorting_offset = 0.0f;
  179. sorting_use_aabb_center = true;
  180. set_notify_transform(true);
  181. }
  182. VisualInstance::~VisualInstance() {
  183. VisualServer::get_singleton()->free(instance);
  184. }
  185. void GeometryInstance::set_material_override(const Ref<Material> &p_material) {
  186. material_override = p_material;
  187. VS::get_singleton()->instance_geometry_set_material_override(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID());
  188. }
  189. Ref<Material> GeometryInstance::get_material_override() const {
  190. return material_override;
  191. }
  192. void GeometryInstance::set_material_overlay(const Ref<Material> &p_material) {
  193. material_overlay = p_material;
  194. VS::get_singleton()->instance_geometry_set_material_overlay(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID());
  195. }
  196. Ref<Material> GeometryInstance::get_material_overlay() const {
  197. return material_overlay;
  198. }
  199. void GeometryInstance::set_generate_lightmap(bool p_enabled) {
  200. generate_lightmap = p_enabled;
  201. }
  202. bool GeometryInstance::get_generate_lightmap() const {
  203. return generate_lightmap;
  204. }
  205. void GeometryInstance::set_lightmap_scale(LightmapScale p_scale) {
  206. ERR_FAIL_INDEX(p_scale, LIGHTMAP_SCALE_MAX);
  207. lightmap_scale = p_scale;
  208. }
  209. GeometryInstance::LightmapScale GeometryInstance::get_lightmap_scale() const {
  210. return lightmap_scale;
  211. }
  212. void GeometryInstance::_notification(int p_what) {
  213. }
  214. void GeometryInstance::set_flag(Flags p_flag, bool p_value) {
  215. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  216. if (flags[p_flag] == p_value) {
  217. return;
  218. }
  219. flags[p_flag] = p_value;
  220. VS::get_singleton()->instance_geometry_set_flag(get_instance(), (VS::InstanceFlags)p_flag, p_value);
  221. }
  222. bool GeometryInstance::get_flag(Flags p_flag) const {
  223. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  224. return flags[p_flag];
  225. }
  226. void GeometryInstance::set_cast_shadows_setting(ShadowCastingSetting p_shadow_casting_setting) {
  227. if (p_shadow_casting_setting != shadow_casting_setting) {
  228. shadow_casting_setting = p_shadow_casting_setting;
  229. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(get_instance(), (VS::ShadowCastingSetting)p_shadow_casting_setting);
  230. }
  231. }
  232. GeometryInstance::ShadowCastingSetting GeometryInstance::get_cast_shadows_setting() const {
  233. return shadow_casting_setting;
  234. }
  235. void GeometryInstance::set_extra_cull_margin(float p_margin) {
  236. ERR_FAIL_COND(p_margin < 0);
  237. if (p_margin != extra_cull_margin) {
  238. extra_cull_margin = p_margin;
  239. VS::get_singleton()->instance_set_extra_visibility_margin(get_instance(), extra_cull_margin);
  240. }
  241. }
  242. float GeometryInstance::get_extra_cull_margin() const {
  243. return extra_cull_margin;
  244. }
  245. void GeometryInstance::set_custom_aabb(AABB aabb) {
  246. VS::get_singleton()->instance_set_custom_aabb(get_instance(), aabb);
  247. }
  248. void GeometryInstance::_bind_methods() {
  249. ClassDB::bind_method(D_METHOD("set_material_override", "material"), &GeometryInstance::set_material_override);
  250. ClassDB::bind_method(D_METHOD("get_material_override"), &GeometryInstance::get_material_override);
  251. ClassDB::bind_method(D_METHOD("set_material_overlay", "material"), &GeometryInstance::set_material_overlay);
  252. ClassDB::bind_method(D_METHOD("get_material_overlay"), &GeometryInstance::get_material_overlay);
  253. ClassDB::bind_method(D_METHOD("set_flag", "flag", "value"), &GeometryInstance::set_flag);
  254. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &GeometryInstance::get_flag);
  255. ClassDB::bind_method(D_METHOD("set_cast_shadows_setting", "shadow_casting_setting"), &GeometryInstance::set_cast_shadows_setting);
  256. ClassDB::bind_method(D_METHOD("get_cast_shadows_setting"), &GeometryInstance::get_cast_shadows_setting);
  257. ClassDB::bind_method(D_METHOD("set_generate_lightmap", "enabled"), &GeometryInstance::set_generate_lightmap);
  258. ClassDB::bind_method(D_METHOD("get_generate_lightmap"), &GeometryInstance::get_generate_lightmap);
  259. ClassDB::bind_method(D_METHOD("set_lightmap_scale", "scale"), &GeometryInstance::set_lightmap_scale);
  260. ClassDB::bind_method(D_METHOD("get_lightmap_scale"), &GeometryInstance::get_lightmap_scale);
  261. ClassDB::bind_method(D_METHOD("set_extra_cull_margin", "margin"), &GeometryInstance::set_extra_cull_margin);
  262. ClassDB::bind_method(D_METHOD("get_extra_cull_margin"), &GeometryInstance::get_extra_cull_margin);
  263. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &GeometryInstance::set_custom_aabb);
  264. ClassDB::bind_method(D_METHOD("get_aabb"), &GeometryInstance::get_aabb);
  265. ADD_GROUP("Geometry", "");
  266. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_override", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial,ORMSpatialMaterial"), "set_material_override", "get_material_override");
  267. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_overlay", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial,ORMSpatialMaterial"), "set_material_overlay", "get_material_overlay");
  268. ADD_PROPERTY(PropertyInfo(Variant::INT, "cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows_setting", "get_cast_shadows_setting");
  269. ADD_PROPERTY(PropertyInfo(Variant::REAL, "extra_cull_margin", PROPERTY_HINT_RANGE, "0,16384,0.01"), "set_extra_cull_margin", "get_extra_cull_margin");
  270. ADD_GROUP("Baked Light", "");
  271. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "use_in_baked_light"), "set_flag", "get_flag", FLAG_USE_BAKED_LIGHT);
  272. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_lightmap"), "set_generate_lightmap", "get_generate_lightmap");
  273. ADD_PROPERTY(PropertyInfo(Variant::INT, "lightmap_scale", PROPERTY_HINT_ENUM, "1x,2x,4x,8x"), "set_lightmap_scale", "get_lightmap_scale");
  274. //ADD_SIGNAL( MethodInfo("visibility_changed"));
  275. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_1X);
  276. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_2X);
  277. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_4X);
  278. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_8X);
  279. BIND_ENUM_CONSTANT(LIGHTMAP_SCALE_MAX);
  280. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_OFF);
  281. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_ON);
  282. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_DOUBLE_SIDED);
  283. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_SHADOWS_ONLY);
  284. BIND_ENUM_CONSTANT(FLAG_USE_BAKED_LIGHT);
  285. BIND_ENUM_CONSTANT(FLAG_DRAW_NEXT_FRAME_IF_VISIBLE);
  286. BIND_ENUM_CONSTANT(FLAG_MAX);
  287. }
  288. GeometryInstance::GeometryInstance() {
  289. _define_ancestry(AncestralClass::GEOMETRY_INSTANCE);
  290. for (int i = 0; i < FLAG_MAX; i++) {
  291. flags[i] = false;
  292. }
  293. shadow_casting_setting = SHADOW_CASTING_SETTING_ON;
  294. extra_cull_margin = 0;
  295. generate_lightmap = true;
  296. lightmap_scale = LightmapScale::LIGHTMAP_SCALE_1X;
  297. //VS::get_singleton()->instance_geometry_set_baked_light_texture_index(get_instance(),0);
  298. }