multimesh.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*************************************************************************/
  2. /* multimesh.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 "multimesh.h"
  30. #include "servers/visual_server.h"
  31. void MultiMesh::_set_transform_array(const DVector<Vector3>& p_array) {
  32. int instance_count = get_instance_count();
  33. DVector<Vector3> xforms = p_array;
  34. int len=xforms.size();
  35. ERR_FAIL_COND((len/4) != instance_count);
  36. if (len==0)
  37. return;
  38. DVector<Vector3>::Read r = xforms.read();
  39. for(int i=0;i<len/4;i++) {
  40. Transform t;
  41. t.basis[0]=r[i*4+0];
  42. t.basis[1]=r[i*4+1];
  43. t.basis[2]=r[i*4+2];
  44. t.origin=r[i*4+3];
  45. set_instance_transform(i,t);
  46. }
  47. }
  48. DVector<Vector3> MultiMesh::_get_transform_array() const {
  49. int instance_count = get_instance_count();
  50. if (instance_count==0)
  51. return DVector<Vector3>();
  52. DVector<Vector3> xforms;
  53. xforms.resize(instance_count*4);
  54. DVector<Vector3>::Write w = xforms.write();
  55. for(int i=0;i<instance_count;i++) {
  56. Transform t=get_instance_transform(i);
  57. w[i*4+0]=t.basis[0];
  58. w[i*4+1]=t.basis[1];
  59. w[i*4+2]=t.basis[2];
  60. w[i*4+3]=t.origin;
  61. }
  62. return xforms;
  63. }
  64. void MultiMesh::_set_color_array(const DVector<Color>& p_array) {
  65. int instance_count = get_instance_count();
  66. DVector<Color> colors = p_array;
  67. int len=colors.size();
  68. ERR_FAIL_COND(len != instance_count);
  69. if (len==0)
  70. return;
  71. DVector<Color>::Read r = colors.read();
  72. for(int i=0;i<len;i++) {
  73. set_instance_color(i,r[i]);
  74. }
  75. }
  76. DVector<Color> MultiMesh::_get_color_array() const {
  77. int instance_count = get_instance_count();
  78. if (instance_count==0)
  79. return DVector<Color>();
  80. DVector<Color> colors;
  81. colors.resize(instance_count);
  82. for(int i=0;i<instance_count;i++) {
  83. colors.set(i,get_instance_color(i));
  84. }
  85. return colors;
  86. }
  87. void MultiMesh::set_mesh(const Ref<Mesh>& p_mesh) {
  88. mesh=p_mesh;
  89. if (!mesh.is_null())
  90. VisualServer::get_singleton()->multimesh_set_mesh(multimesh,mesh->get_rid());
  91. else
  92. VisualServer::get_singleton()->multimesh_set_mesh(multimesh,RID());
  93. }
  94. Ref<Mesh> MultiMesh::get_mesh() const {
  95. return mesh;
  96. }
  97. void MultiMesh::set_instance_count(int p_count) {
  98. VisualServer::get_singleton()->multimesh_set_instance_count(multimesh,p_count);
  99. }
  100. int MultiMesh::get_instance_count() const {
  101. return VisualServer::get_singleton()->multimesh_get_instance_count(multimesh);
  102. }
  103. void MultiMesh::set_instance_transform(int p_instance, const Transform& p_transform) {
  104. VisualServer::get_singleton()->multimesh_instance_set_transform(multimesh,p_instance,p_transform);
  105. }
  106. Transform MultiMesh::get_instance_transform(int p_instance) const {
  107. return VisualServer::get_singleton()->multimesh_instance_get_transform(multimesh,p_instance);
  108. }
  109. void MultiMesh::set_instance_color(int p_instance, const Color& p_color) {
  110. VisualServer::get_singleton()->multimesh_instance_set_color(multimesh,p_instance,p_color);
  111. }
  112. Color MultiMesh::get_instance_color(int p_instance) const {
  113. return VisualServer::get_singleton()->multimesh_instance_get_color(multimesh,p_instance);
  114. }
  115. void MultiMesh::set_aabb(const AABB& p_aabb) {
  116. aabb=p_aabb;
  117. VisualServer::get_singleton()->multimesh_set_aabb(multimesh,p_aabb);
  118. }
  119. AABB MultiMesh::get_aabb() const {
  120. return aabb;
  121. }
  122. void MultiMesh::generate_aabb() {
  123. ERR_EXPLAIN("Cannot generate AABB if mesh is null.");
  124. ERR_FAIL_COND(mesh.is_null());
  125. AABB base_aabb=mesh->get_aabb();
  126. aabb=AABB();
  127. int instance_count = get_instance_count();
  128. for(int i=0;i<instance_count;i++) {
  129. Transform xform = get_instance_transform(i);
  130. if(i==0)
  131. aabb=xform.xform(base_aabb);
  132. else
  133. aabb.merge_with(xform.xform(base_aabb));
  134. }
  135. set_aabb(aabb);
  136. }
  137. RID MultiMesh::get_rid() const {
  138. return multimesh;
  139. }
  140. void MultiMesh::_bind_methods() {
  141. ObjectTypeDB::bind_method(_MD("set_mesh","mesh:Mesh"),&MultiMesh::set_mesh);
  142. ObjectTypeDB::bind_method(_MD("get_mesh:Mesh"),&MultiMesh::get_mesh);
  143. ObjectTypeDB::bind_method(_MD("set_instance_count"),&MultiMesh::set_instance_count);
  144. ObjectTypeDB::bind_method(_MD("get_instance_count"),&MultiMesh::get_instance_count);
  145. ObjectTypeDB::bind_method(_MD("set_instance_transform"),&MultiMesh::set_instance_transform);
  146. ObjectTypeDB::bind_method(_MD("get_instance_transform"),&MultiMesh::get_instance_transform);
  147. ObjectTypeDB::bind_method(_MD("set_instance_color"),&MultiMesh::set_instance_color);
  148. ObjectTypeDB::bind_method(_MD("get_instance_color"),&MultiMesh::get_instance_color);
  149. ObjectTypeDB::bind_method(_MD("set_aabb"),&MultiMesh::set_aabb);
  150. ObjectTypeDB::bind_method(_MD("get_aabb"),&MultiMesh::get_aabb);
  151. ObjectTypeDB::bind_method(_MD("generate_aabb"),&MultiMesh::generate_aabb);
  152. ObjectTypeDB::bind_method(_MD("_set_transform_array"),&MultiMesh::_set_transform_array);
  153. ObjectTypeDB::bind_method(_MD("_get_transform_array"),&MultiMesh::_get_transform_array);
  154. ObjectTypeDB::bind_method(_MD("_set_color_array"),&MultiMesh::_set_color_array);
  155. ObjectTypeDB::bind_method(_MD("_get_color_array"),&MultiMesh::_get_color_array);
  156. ADD_PROPERTY(PropertyInfo(Variant::INT,"instance_count",PROPERTY_HINT_RANGE,"0,16384,1"), _SCS("set_instance_count"), _SCS("get_instance_count"));
  157. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"mesh",PROPERTY_HINT_RESOURCE_TYPE,"Mesh"), _SCS("set_mesh"), _SCS("get_mesh"));
  158. ADD_PROPERTY(PropertyInfo(Variant::_AABB,"aabb"), _SCS("set_aabb"), _SCS("get_aabb") );
  159. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3_ARRAY,"transform_array",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_transform_array"), _SCS("_get_transform_array"));
  160. ADD_PROPERTY(PropertyInfo(Variant::COLOR_ARRAY,"color_array",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_color_array"), _SCS("_get_color_array"));
  161. }
  162. MultiMesh::MultiMesh() {
  163. multimesh = VisualServer::get_singleton()->multimesh_create();
  164. }
  165. MultiMesh::~MultiMesh() {
  166. VisualServer::get_singleton()->free(multimesh);
  167. }