visual_instance.cpp 12 KB

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