visual_instance.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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-2016 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 "baked_light_instance.h"
  34. #include "skeleton.h"
  35. AABB VisualInstance::get_transformed_aabb() const {
  36. return get_global_transform().xform( get_aabb() );
  37. }
  38. void VisualInstance::_notification(int p_what) {
  39. switch(p_what) {
  40. case NOTIFICATION_ENTER_WORLD: {
  41. // CHECK ROOM
  42. Spatial * parent = get_parent_spatial();
  43. Room *room=NULL;
  44. bool is_geom = cast_to<GeometryInstance>();
  45. while(parent) {
  46. room = parent->cast_to<Room>();
  47. if (room)
  48. break;
  49. if (is_geom && parent->cast_to<BakedLightSampler>()) {
  50. VS::get_singleton()->instance_geometry_set_baked_light_sampler(get_instance(),parent->cast_to<BakedLightSampler>()->get_instance());
  51. break;
  52. }
  53. parent=parent->get_parent_spatial();
  54. }
  55. if (room) {
  56. VisualServer::get_singleton()->instance_set_room(instance,room->get_instance());
  57. }
  58. // CHECK SKELETON => moving skeleton attaching logic to MeshInstance
  59. /*
  60. Skeleton *skeleton=get_parent()?get_parent()->cast_to<Skeleton>():NULL;
  61. if (skeleton)
  62. VisualServer::get_singleton()->instance_attach_skeleton( instance, skeleton->get_skeleton() );
  63. */
  64. VisualServer::get_singleton()->instance_set_scenario( instance, get_world()->get_scenario() );
  65. } break;
  66. case NOTIFICATION_TRANSFORM_CHANGED: {
  67. Transform gt = get_global_transform();
  68. VisualServer::get_singleton()->instance_set_transform(instance,gt);
  69. } break;
  70. case NOTIFICATION_EXIT_WORLD: {
  71. VisualServer::get_singleton()->instance_set_scenario( instance, RID() );
  72. VisualServer::get_singleton()->instance_set_room(instance,RID());
  73. VisualServer::get_singleton()->instance_attach_skeleton( instance, RID() );
  74. VS::get_singleton()->instance_geometry_set_baked_light_sampler(instance, RID() );
  75. } break;
  76. }
  77. }
  78. RID VisualInstance::get_instance() const {
  79. return instance;
  80. }
  81. RID VisualInstance::_get_visual_instance_rid() const {
  82. return instance;
  83. }
  84. void VisualInstance::set_layer_mask(uint32_t p_mask) {
  85. layers=p_mask;
  86. VisualServer::get_singleton()->instance_set_layer_mask(instance,p_mask);
  87. }
  88. uint32_t VisualInstance::get_layer_mask() const {
  89. return layers;
  90. }
  91. void VisualInstance::_bind_methods() {
  92. ObjectTypeDB::bind_method(_MD("_get_visual_instance_rid"),&VisualInstance::_get_visual_instance_rid);
  93. ObjectTypeDB::bind_method(_MD("set_base","base"), &VisualInstance::set_base);
  94. ObjectTypeDB::bind_method(_MD("set_layer_mask","mask"), &VisualInstance::set_layer_mask);
  95. ObjectTypeDB::bind_method(_MD("get_layer_mask"), &VisualInstance::get_layer_mask);
  96. ADD_PROPERTY( PropertyInfo( Variant::INT, "layers",PROPERTY_HINT_ALL_FLAGS), _SCS("set_layer_mask"), _SCS("get_layer_mask"));
  97. }
  98. void VisualInstance::set_base(const RID& p_base) {
  99. VisualServer::get_singleton()->instance_set_base(instance,p_base);
  100. }
  101. VisualInstance::VisualInstance()
  102. {
  103. instance = VisualServer::get_singleton()->instance_create();
  104. VisualServer::get_singleton()->instance_attach_object_instance_ID( instance, get_instance_ID() );
  105. layers=1;
  106. }
  107. VisualInstance::~VisualInstance() {
  108. VisualServer::get_singleton()->free(instance);
  109. }
  110. void GeometryInstance::set_material_override(const Ref<Material>& p_material) {
  111. material_override=p_material;
  112. VS::get_singleton()->instance_geometry_set_material_override(get_instance(),p_material.is_valid() ? p_material->get_rid() : RID());
  113. }
  114. Ref<Material> GeometryInstance::get_material_override() const{
  115. return material_override;
  116. }
  117. void GeometryInstance::set_draw_range_begin(float p_dist){
  118. draw_begin=p_dist;
  119. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(),draw_begin,draw_end);
  120. }
  121. float GeometryInstance::get_draw_range_begin() const{
  122. return draw_begin;
  123. }
  124. void GeometryInstance::set_draw_range_end(float p_dist) {
  125. draw_end=p_dist;
  126. VS::get_singleton()->instance_geometry_set_draw_range(get_instance(),draw_begin,draw_end);
  127. }
  128. float GeometryInstance::get_draw_range_end() const {
  129. return draw_end;
  130. }
  131. void GeometryInstance::_notification(int p_what) {
  132. if (p_what==NOTIFICATION_ENTER_WORLD) {
  133. if (flags[FLAG_USE_BAKED_LIGHT]) {
  134. _find_baked_light();
  135. }
  136. _update_visibility();
  137. } else if (p_what==NOTIFICATION_EXIT_WORLD) {
  138. if (flags[FLAG_USE_BAKED_LIGHT]) {
  139. if (baked_light_instance) {
  140. baked_light_instance->disconnect(SceneStringNames::get_singleton()->baked_light_changed,this,SceneStringNames::get_singleton()->_baked_light_changed);
  141. baked_light_instance=NULL;
  142. }
  143. _baked_light_changed();
  144. }
  145. } if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  146. _update_visibility();
  147. }
  148. }
  149. void GeometryInstance::_baked_light_changed() {
  150. if (!baked_light_instance)
  151. VS::get_singleton()->instance_geometry_set_baked_light(get_instance(),RID());
  152. else
  153. VS::get_singleton()->instance_geometry_set_baked_light(get_instance(),baked_light_instance->get_baked_light_instance());
  154. }
  155. void GeometryInstance::_find_baked_light() {
  156. Node *n=get_parent();
  157. while(n) {
  158. BakedLightInstance *bl=n->cast_to<BakedLightInstance>();
  159. if (bl) {
  160. baked_light_instance=bl;
  161. baked_light_instance->connect(SceneStringNames::get_singleton()->baked_light_changed,this,SceneStringNames::get_singleton()->_baked_light_changed);
  162. _baked_light_changed();
  163. return;
  164. }
  165. n=n->get_parent();
  166. }
  167. _baked_light_changed();
  168. }
  169. void GeometryInstance::_update_visibility() {
  170. if (!is_inside_tree())
  171. return;
  172. _change_notify("geometry/visible");
  173. VS::get_singleton()->instance_geometry_set_flag(get_instance(),VS::INSTANCE_FLAG_VISIBLE,is_visible() && flags[FLAG_VISIBLE]);
  174. }
  175. void GeometryInstance::set_flag(Flags p_flag,bool p_value) {
  176. ERR_FAIL_INDEX(p_flag,FLAG_MAX);
  177. if (flags[p_flag]==p_value)
  178. return;
  179. flags[p_flag]=p_value;
  180. VS::get_singleton()->instance_geometry_set_flag(get_instance(),(VS::InstanceFlags)p_flag,p_value);
  181. if (p_flag==FLAG_VISIBLE) {
  182. _update_visibility();
  183. }
  184. if (p_flag==FLAG_USE_BAKED_LIGHT) {
  185. if (is_inside_world()) {
  186. if (!p_value) {
  187. if (baked_light_instance) {
  188. baked_light_instance->disconnect(SceneStringNames::get_singleton()->baked_light_changed,this,SceneStringNames::get_singleton()->_baked_light_changed);
  189. baked_light_instance=NULL;
  190. }
  191. _baked_light_changed();
  192. } else {
  193. _find_baked_light();
  194. }
  195. }
  196. }
  197. }
  198. bool GeometryInstance::get_flag(Flags p_flag) const{
  199. ERR_FAIL_INDEX_V(p_flag,FLAG_MAX,false);
  200. return flags[p_flag];
  201. }
  202. void GeometryInstance::set_baked_light_texture_id(int p_id) {
  203. baked_light_texture_id=p_id;
  204. VS::get_singleton()->instance_geometry_set_baked_light_texture_index(get_instance(),baked_light_texture_id);
  205. }
  206. int GeometryInstance::get_baked_light_texture_id() const{
  207. return baked_light_texture_id;
  208. }
  209. void GeometryInstance::set_extra_cull_margin(float p_margin) {
  210. ERR_FAIL_COND(p_margin<0);
  211. extra_cull_margin=p_margin;
  212. VS::get_singleton()->instance_set_extra_visibility_margin(get_instance(),extra_cull_margin);
  213. }
  214. float GeometryInstance::get_extra_cull_margin() const{
  215. return extra_cull_margin;
  216. }
  217. void GeometryInstance::_bind_methods() {
  218. ObjectTypeDB::bind_method(_MD("set_material_override","material"), &GeometryInstance::set_material_override);
  219. ObjectTypeDB::bind_method(_MD("get_material_override"), &GeometryInstance::get_material_override);
  220. ObjectTypeDB::bind_method(_MD("set_flag","flag","value"), &GeometryInstance::set_flag);
  221. ObjectTypeDB::bind_method(_MD("get_flag","flag"), &GeometryInstance::get_flag);
  222. ObjectTypeDB::bind_method(_MD("set_draw_range_begin","mode"), &GeometryInstance::set_draw_range_begin);
  223. ObjectTypeDB::bind_method(_MD("get_draw_range_begin"), &GeometryInstance::get_draw_range_begin);
  224. ObjectTypeDB::bind_method(_MD("set_draw_range_end","mode"), &GeometryInstance::set_draw_range_end);
  225. ObjectTypeDB::bind_method(_MD("get_draw_range_end"), &GeometryInstance::get_draw_range_end);
  226. ObjectTypeDB::bind_method(_MD("set_baked_light_texture_id","id"), &GeometryInstance::set_baked_light_texture_id);
  227. ObjectTypeDB::bind_method(_MD("get_baked_light_texture_id"), &GeometryInstance::get_baked_light_texture_id);
  228. ObjectTypeDB::bind_method(_MD("set_extra_cull_margin","margin"), &GeometryInstance::set_extra_cull_margin);
  229. ObjectTypeDB::bind_method(_MD("get_extra_cull_margin"), &GeometryInstance::get_extra_cull_margin);
  230. ObjectTypeDB::bind_method(_MD("_baked_light_changed"), &GeometryInstance::_baked_light_changed);
  231. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "geometry/visible"), _SCS("set_flag"), _SCS("get_flag"),FLAG_VISIBLE);
  232. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "geometry/material_override",PROPERTY_HINT_RESOURCE_TYPE,"Material"), _SCS("set_material_override"), _SCS("get_material_override"));
  233. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "geometry/cast_shadow"), _SCS("set_flag"), _SCS("get_flag"),FLAG_CAST_SHADOW);
  234. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "geometry/receive_shadows"), _SCS("set_flag"), _SCS("get_flag"),FLAG_RECEIVE_SHADOWS);
  235. ADD_PROPERTY( PropertyInfo( Variant::INT, "geometry/range_begin",PROPERTY_HINT_RANGE,"0,32768,0.01"), _SCS("set_draw_range_begin"), _SCS("get_draw_range_begin"));
  236. ADD_PROPERTY( PropertyInfo( Variant::INT, "geometry/range_end",PROPERTY_HINT_RANGE,"0,32768,0.01"), _SCS("set_draw_range_end"), _SCS("get_draw_range_end"));
  237. ADD_PROPERTY( PropertyInfo( Variant::REAL, "geometry/extra_cull_margin",PROPERTY_HINT_RANGE,"0,16384,0"), _SCS("set_extra_cull_margin"), _SCS("get_extra_cull_margin"));
  238. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "geometry/billboard"), _SCS("set_flag"), _SCS("get_flag"),FLAG_BILLBOARD);
  239. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "geometry/billboard_y"), _SCS("set_flag"), _SCS("get_flag"),FLAG_BILLBOARD_FIX_Y);
  240. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "geometry/depth_scale"), _SCS("set_flag"), _SCS("get_flag"),FLAG_DEPH_SCALE);
  241. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "geometry/visible_in_all_rooms"), _SCS("set_flag"), _SCS("get_flag"),FLAG_VISIBLE_IN_ALL_ROOMS);
  242. ADD_PROPERTYI( PropertyInfo( Variant::BOOL, "geometry/use_baked_light"), _SCS("set_flag"), _SCS("get_flag"),FLAG_USE_BAKED_LIGHT);
  243. ADD_PROPERTY( PropertyInfo( Variant::INT, "geometry/baked_light_tex_id"), _SCS("set_baked_light_texture_id"), _SCS("get_baked_light_texture_id"));
  244. // ADD_SIGNAL( MethodInfo("visibility_changed"));
  245. BIND_CONSTANT(FLAG_VISIBLE );
  246. BIND_CONSTANT(FLAG_CAST_SHADOW );
  247. BIND_CONSTANT(FLAG_RECEIVE_SHADOWS );
  248. BIND_CONSTANT(FLAG_BILLBOARD );
  249. BIND_CONSTANT(FLAG_BILLBOARD_FIX_Y );
  250. BIND_CONSTANT(FLAG_DEPH_SCALE );
  251. BIND_CONSTANT(FLAG_VISIBLE_IN_ALL_ROOMS );
  252. BIND_CONSTANT(FLAG_MAX );
  253. }
  254. GeometryInstance::GeometryInstance() {
  255. draw_begin=0;
  256. draw_end=0;
  257. for(int i=0;i<FLAG_MAX;i++) {
  258. flags[i]=false;
  259. }
  260. flags[FLAG_VISIBLE]=true;
  261. flags[FLAG_CAST_SHADOW]=true;
  262. flags[FLAG_RECEIVE_SHADOWS]=true;
  263. baked_light_instance=NULL;
  264. baked_light_texture_id=0;
  265. extra_cull_margin=0;
  266. VS::get_singleton()->instance_geometry_set_baked_light_texture_index(get_instance(),0);
  267. }