multimesh.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**************************************************************************/
  2. /* multimesh.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 PoolVector<Vector3> &p_array) {
  33. if (transform_format != TRANSFORM_3D) {
  34. return;
  35. }
  36. const PoolVector<Vector3> &xforms = p_array;
  37. int len = xforms.size();
  38. ERR_FAIL_COND((len / 4) != instance_count);
  39. if (len == 0) {
  40. return;
  41. }
  42. PoolVector<Vector3>::Read r = xforms.read();
  43. for (int i = 0; i < len / 4; i++) {
  44. Transform t;
  45. t.basis[0] = r[i * 4 + 0];
  46. t.basis[1] = r[i * 4 + 1];
  47. t.basis[2] = r[i * 4 + 2];
  48. t.origin = r[i * 4 + 3];
  49. set_instance_transform(i, t);
  50. }
  51. }
  52. PoolVector<Vector3> MultiMesh::_get_transform_array() const {
  53. if (transform_format != TRANSFORM_3D) {
  54. return PoolVector<Vector3>();
  55. }
  56. if (instance_count == 0) {
  57. return PoolVector<Vector3>();
  58. }
  59. PoolVector<Vector3> xforms;
  60. xforms.resize(instance_count * 4);
  61. PoolVector<Vector3>::Write w = xforms.write();
  62. for (int i = 0; i < instance_count; i++) {
  63. Transform t = get_instance_transform(i);
  64. w[i * 4 + 0] = t.basis[0];
  65. w[i * 4 + 1] = t.basis[1];
  66. w[i * 4 + 2] = t.basis[2];
  67. w[i * 4 + 3] = t.origin;
  68. }
  69. return xforms;
  70. }
  71. void MultiMesh::_set_transform_2d_array(const PoolVector<Vector2> &p_array) {
  72. if (transform_format != TRANSFORM_2D) {
  73. return;
  74. }
  75. const PoolVector<Vector2> &xforms = p_array;
  76. int len = xforms.size();
  77. ERR_FAIL_COND((len / 3) != instance_count);
  78. if (len == 0) {
  79. return;
  80. }
  81. PoolVector<Vector2>::Read r = xforms.read();
  82. for (int i = 0; i < len / 3; i++) {
  83. Transform2D t;
  84. t.elements[0] = r[i * 3 + 0];
  85. t.elements[1] = r[i * 3 + 1];
  86. t.elements[2] = r[i * 3 + 2];
  87. set_instance_transform_2d(i, t);
  88. }
  89. }
  90. PoolVector<Vector2> MultiMesh::_get_transform_2d_array() const {
  91. if (transform_format != TRANSFORM_2D) {
  92. return PoolVector<Vector2>();
  93. }
  94. if (instance_count == 0) {
  95. return PoolVector<Vector2>();
  96. }
  97. PoolVector<Vector2> xforms;
  98. xforms.resize(instance_count * 3);
  99. PoolVector<Vector2>::Write w = xforms.write();
  100. for (int i = 0; i < instance_count; i++) {
  101. Transform2D t = get_instance_transform_2d(i);
  102. w[i * 3 + 0] = t.elements[0];
  103. w[i * 3 + 1] = t.elements[1];
  104. w[i * 3 + 2] = t.elements[2];
  105. }
  106. return xforms;
  107. }
  108. void MultiMesh::_set_color_array(const PoolVector<Color> &p_array) {
  109. const PoolVector<Color> &colors = p_array;
  110. int len = colors.size();
  111. if (len == 0) {
  112. return;
  113. }
  114. ERR_FAIL_COND(len != instance_count);
  115. PoolVector<Color>::Read r = colors.read();
  116. for (int i = 0; i < len; i++) {
  117. set_instance_color(i, r[i]);
  118. }
  119. }
  120. PoolVector<Color> MultiMesh::_get_color_array() const {
  121. if (instance_count == 0 || color_format == COLOR_NONE) {
  122. return PoolVector<Color>();
  123. }
  124. PoolVector<Color> colors;
  125. colors.resize(instance_count);
  126. for (int i = 0; i < instance_count; i++) {
  127. colors.set(i, get_instance_color(i));
  128. }
  129. return colors;
  130. }
  131. void MultiMesh::_set_custom_data_array(const PoolVector<Color> &p_array) {
  132. const PoolVector<Color> &custom_datas = p_array;
  133. int len = custom_datas.size();
  134. if (len == 0) {
  135. return;
  136. }
  137. ERR_FAIL_COND(len != instance_count);
  138. PoolVector<Color>::Read r = custom_datas.read();
  139. for (int i = 0; i < len; i++) {
  140. set_instance_custom_data(i, r[i]);
  141. }
  142. }
  143. PoolVector<Color> MultiMesh::_get_custom_data_array() const {
  144. if (instance_count == 0 || custom_data_format == CUSTOM_DATA_NONE) {
  145. return PoolVector<Color>();
  146. }
  147. PoolVector<Color> custom_datas;
  148. custom_datas.resize(instance_count);
  149. for (int i = 0; i < instance_count; i++) {
  150. custom_datas.set(i, get_instance_custom_data(i));
  151. }
  152. return custom_datas;
  153. }
  154. void MultiMesh::set_mesh(const Ref<Mesh> &p_mesh) {
  155. mesh = p_mesh;
  156. if (!mesh.is_null()) {
  157. VisualServer::get_singleton()->multimesh_set_mesh(multimesh, mesh->get_rid());
  158. } else {
  159. VisualServer::get_singleton()->multimesh_set_mesh(multimesh, RID());
  160. }
  161. }
  162. Ref<Mesh> MultiMesh::get_mesh() const {
  163. return mesh;
  164. }
  165. void MultiMesh::set_instance_count(int p_count) {
  166. ERR_FAIL_COND(p_count < 0);
  167. VisualServer::get_singleton()->multimesh_allocate(multimesh, p_count, VS::MultimeshTransformFormat(transform_format), VS::MultimeshColorFormat(color_format), VS::MultimeshCustomDataFormat(custom_data_format));
  168. instance_count = p_count;
  169. }
  170. int MultiMesh::get_instance_count() const {
  171. return instance_count;
  172. }
  173. void MultiMesh::set_visible_instance_count(int p_count) {
  174. ERR_FAIL_COND(p_count < -1);
  175. VisualServer::get_singleton()->multimesh_set_visible_instances(multimesh, p_count);
  176. visible_instance_count = p_count;
  177. }
  178. void MultiMesh::set_physics_interpolation_quality(PhysicsInterpolationQuality p_quality) {
  179. _physics_interpolation_quality = p_quality;
  180. VisualServer::get_singleton()->multimesh_set_physics_interpolation_quality(multimesh, (int)p_quality);
  181. }
  182. int MultiMesh::get_visible_instance_count() const {
  183. return visible_instance_count;
  184. }
  185. void MultiMesh::reset_instance_physics_interpolation(int p_instance) {
  186. VisualServer::get_singleton()->multimesh_instance_reset_physics_interpolation(multimesh, p_instance);
  187. }
  188. void MultiMesh::set_instance_transform(int p_instance, const Transform &p_transform) {
  189. VisualServer::get_singleton()->multimesh_instance_set_transform(multimesh, p_instance, p_transform);
  190. }
  191. void MultiMesh::set_instance_transform_2d(int p_instance, const Transform2D &p_transform) {
  192. VisualServer::get_singleton()->multimesh_instance_set_transform_2d(multimesh, p_instance, p_transform);
  193. emit_changed();
  194. }
  195. Transform MultiMesh::get_instance_transform(int p_instance) const {
  196. return VisualServer::get_singleton()->multimesh_instance_get_transform(multimesh, p_instance);
  197. }
  198. Transform2D MultiMesh::get_instance_transform_2d(int p_instance) const {
  199. return VisualServer::get_singleton()->multimesh_instance_get_transform_2d(multimesh, p_instance);
  200. }
  201. void MultiMesh::set_instance_color(int p_instance, const Color &p_color) {
  202. VisualServer::get_singleton()->multimesh_instance_set_color(multimesh, p_instance, p_color);
  203. }
  204. Color MultiMesh::get_instance_color(int p_instance) const {
  205. return VisualServer::get_singleton()->multimesh_instance_get_color(multimesh, p_instance);
  206. }
  207. void MultiMesh::set_instance_custom_data(int p_instance, const Color &p_custom_data) {
  208. VisualServer::get_singleton()->multimesh_instance_set_custom_data(multimesh, p_instance, p_custom_data);
  209. }
  210. Color MultiMesh::get_instance_custom_data(int p_instance) const {
  211. return VisualServer::get_singleton()->multimesh_instance_get_custom_data(multimesh, p_instance);
  212. }
  213. void MultiMesh::set_as_bulk_array(const PoolVector<float> &p_array) {
  214. VisualServer::get_singleton()->multimesh_set_as_bulk_array(multimesh, p_array);
  215. }
  216. void MultiMesh::set_as_bulk_array_interpolated(const PoolVector<float> &p_array_curr, const PoolVector<float> &p_array_prev) {
  217. VisualServer::get_singleton()->multimesh_set_as_bulk_array_interpolated(multimesh, p_array_curr, p_array_prev);
  218. }
  219. void MultiMesh::set_physics_interpolated(bool p_interpolated) {
  220. VisualServer::get_singleton()->multimesh_set_physics_interpolated(multimesh, p_interpolated);
  221. }
  222. AABB MultiMesh::get_aabb() const {
  223. return VisualServer::get_singleton()->multimesh_get_aabb(multimesh);
  224. }
  225. RID MultiMesh::get_rid() const {
  226. return multimesh;
  227. }
  228. void MultiMesh::set_color_format(ColorFormat p_color_format) {
  229. ERR_FAIL_COND(instance_count > 0);
  230. color_format = p_color_format;
  231. }
  232. MultiMesh::ColorFormat MultiMesh::get_color_format() const {
  233. return color_format;
  234. }
  235. void MultiMesh::set_custom_data_format(CustomDataFormat p_custom_data_format) {
  236. ERR_FAIL_COND(instance_count > 0);
  237. custom_data_format = p_custom_data_format;
  238. }
  239. MultiMesh::CustomDataFormat MultiMesh::get_custom_data_format() const {
  240. return custom_data_format;
  241. }
  242. void MultiMesh::set_transform_format(TransformFormat p_transform_format) {
  243. ERR_FAIL_COND(instance_count > 0);
  244. transform_format = p_transform_format;
  245. }
  246. MultiMesh::TransformFormat MultiMesh::get_transform_format() const {
  247. return transform_format;
  248. }
  249. void MultiMesh::_bind_methods() {
  250. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MultiMesh::set_mesh);
  251. ClassDB::bind_method(D_METHOD("get_mesh"), &MultiMesh::get_mesh);
  252. ClassDB::bind_method(D_METHOD("set_color_format", "format"), &MultiMesh::set_color_format);
  253. ClassDB::bind_method(D_METHOD("get_color_format"), &MultiMesh::get_color_format);
  254. ClassDB::bind_method(D_METHOD("set_custom_data_format", "format"), &MultiMesh::set_custom_data_format);
  255. ClassDB::bind_method(D_METHOD("get_custom_data_format"), &MultiMesh::get_custom_data_format);
  256. ClassDB::bind_method(D_METHOD("set_transform_format", "format"), &MultiMesh::set_transform_format);
  257. ClassDB::bind_method(D_METHOD("get_transform_format"), &MultiMesh::get_transform_format);
  258. ClassDB::bind_method(D_METHOD("set_instance_count", "count"), &MultiMesh::set_instance_count);
  259. ClassDB::bind_method(D_METHOD("get_instance_count"), &MultiMesh::get_instance_count);
  260. ClassDB::bind_method(D_METHOD("set_visible_instance_count", "count"), &MultiMesh::set_visible_instance_count);
  261. ClassDB::bind_method(D_METHOD("get_visible_instance_count"), &MultiMesh::get_visible_instance_count);
  262. ClassDB::bind_method(D_METHOD("set_physics_interpolation_quality", "quality"), &MultiMesh::set_physics_interpolation_quality);
  263. ClassDB::bind_method(D_METHOD("get_physics_interpolation_quality"), &MultiMesh::get_physics_interpolation_quality);
  264. ClassDB::bind_method(D_METHOD("set_instance_transform", "instance", "transform"), &MultiMesh::set_instance_transform);
  265. ClassDB::bind_method(D_METHOD("set_instance_transform_2d", "instance", "transform"), &MultiMesh::set_instance_transform_2d);
  266. ClassDB::bind_method(D_METHOD("get_instance_transform", "instance"), &MultiMesh::get_instance_transform);
  267. ClassDB::bind_method(D_METHOD("get_instance_transform_2d", "instance"), &MultiMesh::get_instance_transform_2d);
  268. ClassDB::bind_method(D_METHOD("set_instance_color", "instance", "color"), &MultiMesh::set_instance_color);
  269. ClassDB::bind_method(D_METHOD("get_instance_color", "instance"), &MultiMesh::get_instance_color);
  270. ClassDB::bind_method(D_METHOD("set_instance_custom_data", "instance", "custom_data"), &MultiMesh::set_instance_custom_data);
  271. ClassDB::bind_method(D_METHOD("get_instance_custom_data", "instance"), &MultiMesh::get_instance_custom_data);
  272. ClassDB::bind_method(D_METHOD("reset_instance_physics_interpolation", "instance"), &MultiMesh::reset_instance_physics_interpolation);
  273. ClassDB::bind_method(D_METHOD("set_as_bulk_array", "array"), &MultiMesh::set_as_bulk_array);
  274. ClassDB::bind_method(D_METHOD("set_as_bulk_array_interpolated", "array_current", "array_previous"), &MultiMesh::set_as_bulk_array_interpolated);
  275. ClassDB::bind_method(D_METHOD("get_aabb"), &MultiMesh::get_aabb);
  276. ClassDB::bind_method(D_METHOD("_set_transform_array"), &MultiMesh::_set_transform_array);
  277. ClassDB::bind_method(D_METHOD("_get_transform_array"), &MultiMesh::_get_transform_array);
  278. ClassDB::bind_method(D_METHOD("_set_transform_2d_array"), &MultiMesh::_set_transform_2d_array);
  279. ClassDB::bind_method(D_METHOD("_get_transform_2d_array"), &MultiMesh::_get_transform_2d_array);
  280. ClassDB::bind_method(D_METHOD("_set_color_array"), &MultiMesh::_set_color_array);
  281. ClassDB::bind_method(D_METHOD("_get_color_array"), &MultiMesh::_get_color_array);
  282. ClassDB::bind_method(D_METHOD("_set_custom_data_array"), &MultiMesh::_set_custom_data_array);
  283. ClassDB::bind_method(D_METHOD("_get_custom_data_array"), &MultiMesh::_get_custom_data_array);
  284. ADD_PROPERTY(PropertyInfo(Variant::INT, "color_format", PROPERTY_HINT_ENUM, "None,Byte,Float"), "set_color_format", "get_color_format");
  285. ADD_PROPERTY(PropertyInfo(Variant::INT, "transform_format", PROPERTY_HINT_ENUM, "2D,3D"), "set_transform_format", "get_transform_format");
  286. ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_data_format", PROPERTY_HINT_ENUM, "None,Byte,Float"), "set_custom_data_format", "get_custom_data_format");
  287. ADD_PROPERTY(PropertyInfo(Variant::INT, "instance_count", PROPERTY_HINT_RANGE, "0,16384,1,or_greater"), "set_instance_count", "get_instance_count");
  288. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_instance_count", PROPERTY_HINT_RANGE, "-1,16384,1,or_greater"), "set_visible_instance_count", "get_visible_instance_count");
  289. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  290. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY, "transform_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_transform_array", "_get_transform_array");
  291. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "transform_2d_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_transform_2d_array", "_get_transform_2d_array");
  292. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "color_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_color_array", "_get_color_array");
  293. ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "custom_data_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_custom_data_array", "_get_custom_data_array");
  294. ADD_GROUP("Physics Interpolation", "physics_interpolation");
  295. ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_interpolation_quality", PROPERTY_HINT_ENUM, "Fast,High"), "set_physics_interpolation_quality", "get_physics_interpolation_quality");
  296. BIND_ENUM_CONSTANT(TRANSFORM_2D);
  297. BIND_ENUM_CONSTANT(TRANSFORM_3D);
  298. BIND_ENUM_CONSTANT(COLOR_NONE);
  299. BIND_ENUM_CONSTANT(COLOR_8BIT);
  300. BIND_ENUM_CONSTANT(COLOR_FLOAT);
  301. BIND_ENUM_CONSTANT(CUSTOM_DATA_NONE);
  302. BIND_ENUM_CONSTANT(CUSTOM_DATA_8BIT);
  303. BIND_ENUM_CONSTANT(CUSTOM_DATA_FLOAT);
  304. BIND_ENUM_CONSTANT(INTERP_QUALITY_FAST);
  305. BIND_ENUM_CONSTANT(INTERP_QUALITY_HIGH);
  306. }
  307. MultiMesh::MultiMesh() {
  308. multimesh = RID_PRIME(VisualServer::get_singleton()->multimesh_create());
  309. color_format = COLOR_NONE;
  310. custom_data_format = CUSTOM_DATA_NONE;
  311. transform_format = TRANSFORM_2D;
  312. visible_instance_count = -1;
  313. instance_count = 0;
  314. _physics_interpolation_quality = INTERP_QUALITY_FAST;
  315. }
  316. MultiMesh::~MultiMesh() {
  317. VisualServer::get_singleton()->free(multimesh);
  318. }