multimesh.cpp 8.0 KB

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