visual_instance.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*************************************************************************/
  2. /* visual_instance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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::_update_visibility() {
  38. if (!is_inside_tree())
  39. return;
  40. _change_notify("visible");
  41. VS::get_singleton()->instance_set_visible(get_instance(), is_visible_in_tree());
  42. }
  43. void VisualInstance::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_ENTER_WORLD: {
  46. // CHECK SKELETON => moving skeleton attaching logic to MeshInstance
  47. /*
  48. Skeleton *skeleton=Object::cast_to<Skeleton>(get_parent());
  49. if (skeleton)
  50. VisualServer::get_singleton()->instance_attach_skeleton( instance, skeleton->get_skeleton() );
  51. */
  52. VisualServer::get_singleton()->instance_set_scenario(instance, get_world()->get_scenario());
  53. _update_visibility();
  54. } break;
  55. case NOTIFICATION_TRANSFORM_CHANGED: {
  56. Transform gt = get_global_transform();
  57. VisualServer::get_singleton()->instance_set_transform(instance, gt);
  58. } break;
  59. case NOTIFICATION_EXIT_WORLD: {
  60. VisualServer::get_singleton()->instance_set_scenario(instance, RID());
  61. VisualServer::get_singleton()->instance_attach_skeleton(instance, RID());
  62. //VS::get_singleton()->instance_geometry_set_baked_light_sampler(instance, RID() );
  63. } break;
  64. case NOTIFICATION_VISIBILITY_CHANGED: {
  65. _update_visibility();
  66. } break;
  67. }
  68. }
  69. RID VisualInstance::get_instance() const {
  70. return instance;
  71. }
  72. RID VisualInstance::_get_visual_instance_rid() const {
  73. return instance;
  74. }
  75. void VisualInstance::set_layer_mask(uint32_t p_mask) {
  76. layers = p_mask;
  77. VisualServer::get_singleton()->instance_set_layer_mask(instance, p_mask);
  78. }
  79. uint32_t VisualInstance::get_layer_mask() const {
  80. return layers;
  81. }
  82. void VisualInstance::set_layer_mask_bit(int p_layer, bool p_enable) {
  83. ERR_FAIL_INDEX(p_layer, 32);
  84. if (p_enable) {
  85. set_layer_mask(layers | (1 << p_layer));
  86. } else {
  87. set_layer_mask(layers & (~(1 << p_layer)));
  88. }
  89. }
  90. bool VisualInstance::get_layer_mask_bit(int p_layer) const {
  91. ERR_FAIL_INDEX_V(p_layer, 32, false);
  92. return (layers & (1 << p_layer));
  93. }
  94. void VisualInstance::_bind_methods() {
  95. ClassDB::bind_method(D_METHOD("_get_visual_instance_rid"), &VisualInstance::_get_visual_instance_rid);
  96. ClassDB::bind_method(D_METHOD("set_base", "base"), &VisualInstance::set_base);
  97. ClassDB::bind_method(D_METHOD("set_layer_mask", "mask"), &VisualInstance::set_layer_mask);
  98. ClassDB::bind_method(D_METHOD("get_layer_mask"), &VisualInstance::get_layer_mask);
  99. ClassDB::bind_method(D_METHOD("set_layer_mask_bit", "layer", "enabled"), &VisualInstance::set_layer_mask_bit);
  100. ClassDB::bind_method(D_METHOD("get_layer_mask_bit", "layer"), &VisualInstance::get_layer_mask_bit);
  101. ClassDB::bind_method(D_METHOD("get_transformed_aabb"), &VisualInstance::get_transformed_aabb);
  102. ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_LAYERS_3D_RENDER), "set_layer_mask", "get_layer_mask");
  103. }
  104. void VisualInstance::set_base(const RID &p_base) {
  105. VisualServer::get_singleton()->instance_set_base(instance, p_base);
  106. }
  107. VisualInstance::VisualInstance() {
  108. instance = VisualServer::get_singleton()->instance_create();
  109. VisualServer::get_singleton()->instance_attach_object_instance_id(instance, get_instance_id());
  110. layers = 1;
  111. set_notify_transform(true);
  112. }
  113. VisualInstance::~VisualInstance() {
  114. VisualServer::get_singleton()->free(instance);
  115. }
  116. void GeometryInstance::set_material_override(const Ref<Material> &p_material) {
  117. material_override = p_material;
  118. VS::get_singleton()->instance_geometry_set_material_override(get_instance(), p_material.is_valid() ? p_material->get_rid() : RID());
  119. }
  120. Ref<Material> GeometryInstance::get_material_override() const {
  121. return material_override;
  122. }
  123. void GeometryInstance::set_lod_min_distance(float p_dist) {
  124. lod_min_distance = p_dist;
  125. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(), lod_min_distance, lod_max_distance, lod_min_hysteresis, lod_max_hysteresis);
  126. }
  127. float GeometryInstance::get_lod_min_distance() const {
  128. return lod_min_distance;
  129. }
  130. void GeometryInstance::set_lod_max_distance(float p_dist) {
  131. lod_max_distance = p_dist;
  132. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(), lod_min_distance, lod_max_distance, lod_min_hysteresis, lod_max_hysteresis);
  133. }
  134. float GeometryInstance::get_lod_max_distance() const {
  135. return lod_max_distance;
  136. }
  137. void GeometryInstance::set_lod_min_hysteresis(float p_dist) {
  138. lod_min_hysteresis = p_dist;
  139. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(), lod_min_distance, lod_max_distance, lod_min_hysteresis, lod_max_hysteresis);
  140. }
  141. float GeometryInstance::get_lod_min_hysteresis() const {
  142. return lod_min_hysteresis;
  143. }
  144. void GeometryInstance::set_lod_max_hysteresis(float p_dist) {
  145. lod_max_hysteresis = p_dist;
  146. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(), lod_min_distance, lod_max_distance, lod_min_hysteresis, lod_max_hysteresis);
  147. }
  148. float GeometryInstance::get_lod_max_hysteresis() const {
  149. return lod_max_hysteresis;
  150. }
  151. void GeometryInstance::_notification(int p_what) {
  152. if (p_what == NOTIFICATION_ENTER_WORLD) {
  153. if (flags[FLAG_USE_BAKED_LIGHT]) {
  154. }
  155. } else if (p_what == NOTIFICATION_EXIT_WORLD) {
  156. if (flags[FLAG_USE_BAKED_LIGHT]) {
  157. }
  158. }
  159. }
  160. void GeometryInstance::set_flag(Flags p_flag, bool p_value) {
  161. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  162. if (flags[p_flag] == p_value)
  163. return;
  164. flags[p_flag] = p_value;
  165. VS::get_singleton()->instance_geometry_set_flag(get_instance(), (VS::InstanceFlags)p_flag, p_value);
  166. if (p_flag == FLAG_USE_BAKED_LIGHT) {
  167. }
  168. }
  169. bool GeometryInstance::get_flag(Flags p_flag) const {
  170. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  171. return flags[p_flag];
  172. }
  173. void GeometryInstance::set_cast_shadows_setting(ShadowCastingSetting p_shadow_casting_setting) {
  174. shadow_casting_setting = p_shadow_casting_setting;
  175. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(get_instance(), (VS::ShadowCastingSetting)p_shadow_casting_setting);
  176. }
  177. GeometryInstance::ShadowCastingSetting GeometryInstance::get_cast_shadows_setting() const {
  178. return shadow_casting_setting;
  179. }
  180. void GeometryInstance::set_extra_cull_margin(float p_margin) {
  181. ERR_FAIL_COND(p_margin < 0);
  182. extra_cull_margin = p_margin;
  183. VS::get_singleton()->instance_set_extra_visibility_margin(get_instance(), extra_cull_margin);
  184. }
  185. float GeometryInstance::get_extra_cull_margin() const {
  186. return extra_cull_margin;
  187. }
  188. void GeometryInstance::_bind_methods() {
  189. ClassDB::bind_method(D_METHOD("set_material_override", "material"), &GeometryInstance::set_material_override);
  190. ClassDB::bind_method(D_METHOD("get_material_override"), &GeometryInstance::get_material_override);
  191. ClassDB::bind_method(D_METHOD("set_flag", "flag", "value"), &GeometryInstance::set_flag);
  192. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &GeometryInstance::get_flag);
  193. ClassDB::bind_method(D_METHOD("set_cast_shadows_setting", "shadow_casting_setting"), &GeometryInstance::set_cast_shadows_setting);
  194. ClassDB::bind_method(D_METHOD("get_cast_shadows_setting"), &GeometryInstance::get_cast_shadows_setting);
  195. ClassDB::bind_method(D_METHOD("set_lod_max_hysteresis", "mode"), &GeometryInstance::set_lod_max_hysteresis);
  196. ClassDB::bind_method(D_METHOD("get_lod_max_hysteresis"), &GeometryInstance::get_lod_max_hysteresis);
  197. ClassDB::bind_method(D_METHOD("set_lod_max_distance", "mode"), &GeometryInstance::set_lod_max_distance);
  198. ClassDB::bind_method(D_METHOD("get_lod_max_distance"), &GeometryInstance::get_lod_max_distance);
  199. ClassDB::bind_method(D_METHOD("set_lod_min_hysteresis", "mode"), &GeometryInstance::set_lod_min_hysteresis);
  200. ClassDB::bind_method(D_METHOD("get_lod_min_hysteresis"), &GeometryInstance::get_lod_min_hysteresis);
  201. ClassDB::bind_method(D_METHOD("set_lod_min_distance", "mode"), &GeometryInstance::set_lod_min_distance);
  202. ClassDB::bind_method(D_METHOD("get_lod_min_distance"), &GeometryInstance::get_lod_min_distance);
  203. ClassDB::bind_method(D_METHOD("set_extra_cull_margin", "margin"), &GeometryInstance::set_extra_cull_margin);
  204. ClassDB::bind_method(D_METHOD("get_extra_cull_margin"), &GeometryInstance::get_extra_cull_margin);
  205. ClassDB::bind_method(D_METHOD("get_aabb"), &GeometryInstance::get_aabb);
  206. ADD_GROUP("Geometry", "");
  207. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_override", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial"), "set_material_override", "get_material_override");
  208. ADD_PROPERTY(PropertyInfo(Variant::INT, "cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows_setting", "get_cast_shadows_setting");
  209. ADD_PROPERTY(PropertyInfo(Variant::REAL, "extra_cull_margin", PROPERTY_HINT_RANGE, "0,16384,0.01"), "set_extra_cull_margin", "get_extra_cull_margin");
  210. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "use_in_baked_light"), "set_flag", "get_flag", FLAG_USE_BAKED_LIGHT);
  211. ADD_GROUP("LOD", "lod_");
  212. ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_min_distance", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_lod_min_distance", "get_lod_min_distance");
  213. ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_min_hysteresis", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_lod_min_hysteresis", "get_lod_min_hysteresis");
  214. ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_max_distance", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_lod_max_distance", "get_lod_max_distance");
  215. ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_max_hysteresis", PROPERTY_HINT_RANGE, "0,32768,0.01"), "set_lod_max_hysteresis", "get_lod_max_hysteresis");
  216. //ADD_SIGNAL( MethodInfo("visibility_changed"));
  217. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_OFF);
  218. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_ON);
  219. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_DOUBLE_SIDED);
  220. BIND_ENUM_CONSTANT(SHADOW_CASTING_SETTING_SHADOWS_ONLY);
  221. BIND_ENUM_CONSTANT(FLAG_USE_BAKED_LIGHT);
  222. BIND_ENUM_CONSTANT(FLAG_MAX);
  223. }
  224. GeometryInstance::GeometryInstance() {
  225. lod_min_distance = 0;
  226. lod_max_distance = 0;
  227. lod_min_hysteresis = 0;
  228. lod_max_hysteresis = 0;
  229. for (int i = 0; i < FLAG_MAX; i++) {
  230. flags[i] = false;
  231. }
  232. shadow_casting_setting = SHADOW_CASTING_SETTING_ON;
  233. extra_cull_margin = 0;
  234. //VS::get_singleton()->instance_geometry_set_baked_light_texture_index(get_instance(),0);
  235. }