visual_instance.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*************************************************************************/
  2. /* visual_instance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "visual_instance.h"
  30. #include "servers/visual_server.h"
  31. #include "room_instance.h"
  32. #include "scene/scene_string_names.h"
  33. #include "skeleton.h"
  34. Rect3 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 ROOM
  47. Spatial * parent = get_parent_spatial();
  48. Room *room=NULL;
  49. bool is_geom = cast_to<GeometryInstance>();
  50. /* while(parent) {
  51. room = parent->cast_to<Room>();
  52. if (room)
  53. break;
  54. if (is_geom && parent->cast_to<BakedLightSampler>()) {
  55. VS::get_singleton()->instance_geometry_set_baked_light_sampler(get_instance(),parent->cast_to<BakedLightSampler>()->get_instance());
  56. break;
  57. }
  58. parent=parent->get_parent_spatial();
  59. }*/
  60. if (room) {
  61. VisualServer::get_singleton()->instance_set_room(instance,room->get_instance());
  62. }
  63. // CHECK SKELETON => moving skeleton attaching logic to MeshInstance
  64. /*
  65. Skeleton *skeleton=get_parent()?get_parent()->cast_to<Skeleton>():NULL;
  66. if (skeleton)
  67. VisualServer::get_singleton()->instance_attach_skeleton( instance, skeleton->get_skeleton() );
  68. */
  69. VisualServer::get_singleton()->instance_set_scenario( instance, get_world()->get_scenario() );
  70. _update_visibility();
  71. } break;
  72. case NOTIFICATION_TRANSFORM_CHANGED: {
  73. Transform gt = get_global_transform();
  74. VisualServer::get_singleton()->instance_set_transform(instance,gt);
  75. } break;
  76. case NOTIFICATION_EXIT_WORLD: {
  77. VisualServer::get_singleton()->instance_set_scenario( instance, RID() );
  78. VisualServer::get_singleton()->instance_set_room(instance,RID());
  79. VisualServer::get_singleton()->instance_attach_skeleton( instance, RID() );
  80. //VS::get_singleton()->instance_geometry_set_baked_light_sampler(instance, RID() );
  81. } break;
  82. case NOTIFICATION_VISIBILITY_CHANGED: {
  83. _update_visibility();
  84. } break;
  85. }
  86. }
  87. RID VisualInstance::get_instance() const {
  88. return instance;
  89. }
  90. RID VisualInstance::_get_visual_instance_rid() const {
  91. return instance;
  92. }
  93. void VisualInstance::set_layer_mask(uint32_t p_mask) {
  94. layers=p_mask;
  95. VisualServer::get_singleton()->instance_set_layer_mask(instance,p_mask);
  96. }
  97. uint32_t VisualInstance::get_layer_mask() const {
  98. return layers;
  99. }
  100. void VisualInstance::_bind_methods() {
  101. ClassDB::bind_method(_MD("_get_visual_instance_rid"),&VisualInstance::_get_visual_instance_rid);
  102. ClassDB::bind_method(_MD("set_base","base"), &VisualInstance::set_base);
  103. ClassDB::bind_method(_MD("set_layer_mask","mask"), &VisualInstance::set_layer_mask);
  104. ClassDB::bind_method(_MD("get_layer_mask"), &VisualInstance::get_layer_mask);
  105. ClassDB::bind_method(_MD("get_transformed_aabb"), &VisualInstance::get_transformed_aabb);
  106. ADD_PROPERTY( PropertyInfo( Variant::INT, "layers",PROPERTY_HINT_LAYERS_3D_RENDER), "set_layer_mask", "get_layer_mask");
  107. }
  108. void VisualInstance::set_base(const RID& p_base) {
  109. VisualServer::get_singleton()->instance_set_base(instance,p_base);
  110. }
  111. VisualInstance::VisualInstance()
  112. {
  113. instance = VisualServer::get_singleton()->instance_create();
  114. VisualServer::get_singleton()->instance_attach_object_instance_ID( instance, get_instance_ID() );
  115. layers=1;
  116. set_notify_transform(true);
  117. }
  118. VisualInstance::~VisualInstance() {
  119. VisualServer::get_singleton()->free(instance);
  120. }
  121. void GeometryInstance::set_material_override(const Ref<Material>& p_material) {
  122. material_override=p_material;
  123. VS::get_singleton()->instance_geometry_set_material_override(get_instance(),p_material.is_valid() ? p_material->get_rid() : RID());
  124. }
  125. Ref<Material> GeometryInstance::get_material_override() const{
  126. return material_override;
  127. }
  128. void GeometryInstance::set_lod_min_distance(float p_dist){
  129. lod_min_distance=p_dist;
  130. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(),lod_min_distance,lod_max_distance,lod_min_hysteresis,lod_max_hysteresis);
  131. }
  132. float GeometryInstance::get_lod_min_distance() const{
  133. return lod_min_distance;
  134. }
  135. void GeometryInstance::set_lod_max_distance(float p_dist) {
  136. lod_max_distance=p_dist;
  137. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(),lod_min_distance,lod_max_distance,lod_min_hysteresis,lod_max_hysteresis);
  138. }
  139. float GeometryInstance::get_lod_max_distance() const {
  140. return lod_max_distance;
  141. }
  142. void GeometryInstance::set_lod_min_hysteresis(float p_dist){
  143. lod_min_hysteresis=p_dist;
  144. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(),lod_min_distance,lod_max_distance,lod_min_hysteresis,lod_max_hysteresis);
  145. }
  146. float GeometryInstance::get_lod_min_hysteresis() const{
  147. return lod_min_hysteresis;
  148. }
  149. void GeometryInstance::set_lod_max_hysteresis(float p_dist) {
  150. lod_max_hysteresis=p_dist;
  151. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(),lod_min_distance,lod_max_distance,lod_min_hysteresis,lod_max_hysteresis);
  152. }
  153. float GeometryInstance::get_lod_max_hysteresis() const {
  154. return lod_max_hysteresis;
  155. }
  156. void GeometryInstance::_notification(int p_what) {
  157. if (p_what==NOTIFICATION_ENTER_WORLD) {
  158. if (flags[FLAG_USE_BAKED_LIGHT]) {
  159. }
  160. } else if (p_what==NOTIFICATION_EXIT_WORLD) {
  161. if (flags[FLAG_USE_BAKED_LIGHT]) {
  162. }
  163. }
  164. }
  165. void GeometryInstance::set_flag(Flags p_flag,bool p_value) {
  166. ERR_FAIL_INDEX(p_flag,FLAG_MAX);
  167. if (p_flag==FLAG_CAST_SHADOW) {
  168. if (p_value == true) {
  169. set_cast_shadows_setting(SHADOW_CASTING_SETTING_ON);
  170. }
  171. else {
  172. set_cast_shadows_setting(SHADOW_CASTING_SETTING_OFF);
  173. }
  174. }
  175. if (flags[p_flag]==p_value)
  176. return;
  177. flags[p_flag]=p_value;
  178. VS::get_singleton()->instance_geometry_set_flag(get_instance(),(VS::InstanceFlags)p_flag,p_value);
  179. if (p_flag==FLAG_USE_BAKED_LIGHT) {
  180. }
  181. }
  182. bool GeometryInstance::get_flag(Flags p_flag) const{
  183. ERR_FAIL_INDEX_V(p_flag,FLAG_MAX,false);
  184. if (p_flag == FLAG_CAST_SHADOW) {
  185. if (shadow_casting_setting == SHADOW_CASTING_SETTING_OFF) {
  186. return false;
  187. }
  188. else {
  189. return true;
  190. }
  191. }
  192. return flags[p_flag];
  193. }
  194. void GeometryInstance::set_cast_shadows_setting(ShadowCastingSetting p_shadow_casting_setting) {
  195. shadow_casting_setting = p_shadow_casting_setting;
  196. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(get_instance(), (VS::ShadowCastingSetting)p_shadow_casting_setting);
  197. }
  198. GeometryInstance::ShadowCastingSetting GeometryInstance::get_cast_shadows_setting() const {
  199. return shadow_casting_setting;
  200. }
  201. void GeometryInstance::set_extra_cull_margin(float p_margin) {
  202. ERR_FAIL_COND(p_margin<0);
  203. extra_cull_margin=p_margin;
  204. VS::get_singleton()->instance_set_extra_visibility_margin(get_instance(),extra_cull_margin);
  205. }
  206. float GeometryInstance::get_extra_cull_margin() const{
  207. return extra_cull_margin;
  208. }
  209. void GeometryInstance::_bind_methods() {
  210. ClassDB::bind_method(_MD("set_material_override","material"), &GeometryInstance::set_material_override);
  211. ClassDB::bind_method(_MD("get_material_override"), &GeometryInstance::get_material_override);
  212. ClassDB::bind_method(_MD("set_flag","flag","value"), &GeometryInstance::set_flag);
  213. ClassDB::bind_method(_MD("get_flag","flag"), &GeometryInstance::get_flag);
  214. ClassDB::bind_method(_MD("set_cast_shadows_setting", "shadow_casting_setting"), &GeometryInstance::set_cast_shadows_setting);
  215. ClassDB::bind_method(_MD("get_cast_shadows_setting"), &GeometryInstance::get_cast_shadows_setting);
  216. ClassDB::bind_method(_MD("set_lod_max_hysteresis","mode"), &GeometryInstance::set_lod_max_hysteresis);
  217. ClassDB::bind_method(_MD("get_lod_max_hysteresis"), &GeometryInstance::get_lod_max_hysteresis);
  218. ClassDB::bind_method(_MD("set_lod_max_distance","mode"), &GeometryInstance::set_lod_max_distance);
  219. ClassDB::bind_method(_MD("get_lod_max_distance"), &GeometryInstance::get_lod_max_distance);
  220. ClassDB::bind_method(_MD("set_lod_min_hysteresis","mode"), &GeometryInstance::set_lod_min_hysteresis);
  221. ClassDB::bind_method(_MD("get_lod_min_hysteresis"), &GeometryInstance::get_lod_min_hysteresis);
  222. ClassDB::bind_method(_MD("set_lod_min_distance","mode"), &GeometryInstance::set_lod_min_distance);
  223. ClassDB::bind_method(_MD("get_lod_min_distance"), &GeometryInstance::get_lod_min_distance);
  224. ClassDB::bind_method(_MD("set_extra_cull_margin","margin"), &GeometryInstance::set_extra_cull_margin);
  225. ClassDB::bind_method(_MD("get_extra_cull_margin"), &GeometryInstance::get_extra_cull_margin);
  226. ClassDB::bind_method(_MD("get_aabb"),&GeometryInstance::get_aabb);
  227. ADD_GROUP("Geometry","");
  228. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "material_override",PROPERTY_HINT_RESOURCE_TYPE,"Material"), "set_material_override", "get_material_override");
  229. ADD_PROPERTY(PropertyInfo(Variant::INT, "cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows_setting", "get_cast_shadows_setting");
  230. ADD_PROPERTY( PropertyInfo( Variant::REAL, "extra_cull_margin",PROPERTY_HINT_RANGE,"0,16384,0"), "set_extra_cull_margin", "get_extra_cull_margin");
  231. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "use_as_billboard"), "set_flag", "get_flag",FLAG_BILLBOARD);
  232. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "use_as_y_billboard"), "set_flag", "get_flag",FLAG_BILLBOARD_FIX_Y);
  233. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "use_depth_scale"), "set_flag", "get_flag",FLAG_DEPH_SCALE);
  234. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "visible_in_all_rooms"), "set_flag", "get_flag",FLAG_VISIBLE_IN_ALL_ROOMS);
  235. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "use_in_baked_light"), "set_flag", "get_flag",FLAG_USE_BAKED_LIGHT);
  236. ADD_GROUP("LOD","lod_");
  237. ADD_PROPERTY( PropertyInfo( Variant::INT, "lod_min_distance",PROPERTY_HINT_RANGE,"0,32768,0.01"), "set_lod_min_distance", "get_lod_min_distance");
  238. ADD_PROPERTY( PropertyInfo( Variant::INT, "lod_min_hysteresis",PROPERTY_HINT_RANGE,"0,32768,0.01"), "set_lod_min_hysteresis", "get_lod_min_hysteresis");
  239. ADD_PROPERTY( PropertyInfo( Variant::INT, "lod_max_distance",PROPERTY_HINT_RANGE,"0,32768,0.01"), "set_lod_max_distance", "get_lod_max_distance");
  240. ADD_PROPERTY( PropertyInfo( Variant::INT, "lod_max_hysteresis",PROPERTY_HINT_RANGE,"0,32768,0.01"), "set_lod_max_hysteresis", "get_lod_max_hysteresis");
  241. //ADD_SIGNAL( MethodInfo("visibility_changed"));
  242. BIND_CONSTANT(FLAG_CAST_SHADOW );
  243. BIND_CONSTANT(FLAG_BILLBOARD );
  244. BIND_CONSTANT(FLAG_BILLBOARD_FIX_Y );
  245. BIND_CONSTANT(FLAG_DEPH_SCALE );
  246. BIND_CONSTANT(FLAG_VISIBLE_IN_ALL_ROOMS );
  247. BIND_CONSTANT(FLAG_MAX );
  248. BIND_CONSTANT(SHADOW_CASTING_SETTING_OFF);
  249. BIND_CONSTANT(SHADOW_CASTING_SETTING_ON);
  250. BIND_CONSTANT(SHADOW_CASTING_SETTING_DOUBLE_SIDED);
  251. BIND_CONSTANT(SHADOW_CASTING_SETTING_SHADOWS_ONLY);
  252. }
  253. GeometryInstance::GeometryInstance() {
  254. lod_min_distance=0;
  255. lod_max_distance=0;
  256. lod_min_hysteresis=0;
  257. lod_max_hysteresis=0;
  258. for(int i=0;i<FLAG_MAX;i++) {
  259. flags[i]=false;
  260. }
  261. flags[FLAG_CAST_SHADOW]=true;
  262. shadow_casting_setting=SHADOW_CASTING_SETTING_ON;
  263. extra_cull_margin=0;
  264. //VS::get_singleton()->instance_geometry_set_baked_light_texture_index(get_instance(),0);
  265. }