multimesh.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/rendering_server.h"
  32. #ifndef DISABLE_DEPRECATED
  33. // Kept for compatibility from 3.x to 4.0.
  34. void MultiMesh::_set_transform_array(const Vector<Vector3> &p_array) {
  35. if (transform_format != TRANSFORM_3D) {
  36. return;
  37. }
  38. const Vector<Vector3> &xforms = p_array;
  39. int len = xforms.size();
  40. ERR_FAIL_COND((len / 4) != instance_count);
  41. if (len == 0) {
  42. return;
  43. }
  44. const Vector3 *r = xforms.ptr();
  45. for (int i = 0; i < len / 4; i++) {
  46. Transform3D t;
  47. t.basis[0] = r[i * 4 + 0];
  48. t.basis[1] = r[i * 4 + 1];
  49. t.basis[2] = r[i * 4 + 2];
  50. t.origin = r[i * 4 + 3];
  51. set_instance_transform(i, t);
  52. }
  53. }
  54. Vector<Vector3> MultiMesh::_get_transform_array() const {
  55. if (transform_format != TRANSFORM_3D) {
  56. return Vector<Vector3>();
  57. }
  58. if (instance_count == 0) {
  59. return Vector<Vector3>();
  60. }
  61. Vector<Vector3> xforms;
  62. xforms.resize(instance_count * 4);
  63. Vector3 *w = xforms.ptrw();
  64. for (int i = 0; i < instance_count; i++) {
  65. Transform3D t = get_instance_transform(i);
  66. w[i * 4 + 0] = t.basis[0];
  67. w[i * 4 + 1] = t.basis[1];
  68. w[i * 4 + 2] = t.basis[2];
  69. w[i * 4 + 3] = t.origin;
  70. }
  71. return xforms;
  72. }
  73. void MultiMesh::_set_transform_2d_array(const Vector<Vector2> &p_array) {
  74. if (transform_format != TRANSFORM_2D) {
  75. return;
  76. }
  77. const Vector<Vector2> &xforms = p_array;
  78. int len = xforms.size();
  79. ERR_FAIL_COND((len / 3) != instance_count);
  80. if (len == 0) {
  81. return;
  82. }
  83. const Vector2 *r = xforms.ptr();
  84. for (int i = 0; i < len / 3; i++) {
  85. Transform2D t;
  86. t.columns[0] = r[i * 3 + 0];
  87. t.columns[1] = r[i * 3 + 1];
  88. t.columns[2] = r[i * 3 + 2];
  89. set_instance_transform_2d(i, t);
  90. }
  91. }
  92. Vector<Vector2> MultiMesh::_get_transform_2d_array() const {
  93. if (transform_format != TRANSFORM_2D) {
  94. return Vector<Vector2>();
  95. }
  96. if (instance_count == 0) {
  97. return Vector<Vector2>();
  98. }
  99. Vector<Vector2> xforms;
  100. xforms.resize(instance_count * 3);
  101. Vector2 *w = xforms.ptrw();
  102. for (int i = 0; i < instance_count; i++) {
  103. Transform2D t = get_instance_transform_2d(i);
  104. w[i * 3 + 0] = t.columns[0];
  105. w[i * 3 + 1] = t.columns[1];
  106. w[i * 3 + 2] = t.columns[2];
  107. }
  108. return xforms;
  109. }
  110. void MultiMesh::_set_color_array(const Vector<Color> &p_array) {
  111. const Vector<Color> &colors = p_array;
  112. int len = colors.size();
  113. if (len == 0) {
  114. return;
  115. }
  116. ERR_FAIL_COND(len != instance_count);
  117. const Color *r = colors.ptr();
  118. for (int i = 0; i < len; i++) {
  119. set_instance_color(i, r[i]);
  120. }
  121. }
  122. Vector<Color> MultiMesh::_get_color_array() const {
  123. if (instance_count == 0 || !use_colors) {
  124. return Vector<Color>();
  125. }
  126. Vector<Color> colors;
  127. colors.resize(instance_count);
  128. for (int i = 0; i < instance_count; i++) {
  129. colors.set(i, get_instance_color(i));
  130. }
  131. return colors;
  132. }
  133. void MultiMesh::_set_custom_data_array(const Vector<Color> &p_array) {
  134. const Vector<Color> &custom_datas = p_array;
  135. int len = custom_datas.size();
  136. if (len == 0) {
  137. return;
  138. }
  139. ERR_FAIL_COND(len != instance_count);
  140. const Color *r = custom_datas.ptr();
  141. for (int i = 0; i < len; i++) {
  142. set_instance_custom_data(i, r[i]);
  143. }
  144. }
  145. Vector<Color> MultiMesh::_get_custom_data_array() const {
  146. if (instance_count == 0 || !use_custom_data) {
  147. return Vector<Color>();
  148. }
  149. Vector<Color> custom_datas;
  150. custom_datas.resize(instance_count);
  151. for (int i = 0; i < instance_count; i++) {
  152. custom_datas.set(i, get_instance_custom_data(i));
  153. }
  154. return custom_datas;
  155. }
  156. #endif // DISABLE_DEPRECATED
  157. void MultiMesh::set_buffer(const Vector<float> &p_buffer) {
  158. RS::get_singleton()->multimesh_set_buffer(multimesh, p_buffer);
  159. }
  160. Vector<float> MultiMesh::get_buffer() const {
  161. return RS::get_singleton()->multimesh_get_buffer(multimesh);
  162. }
  163. void MultiMesh::set_buffer_interpolated(const Vector<float> &p_buffer_curr, const Vector<float> &p_buffer_prev) {
  164. RS::get_singleton()->multimesh_set_buffer_interpolated(multimesh, p_buffer_curr, p_buffer_prev);
  165. }
  166. void MultiMesh::set_mesh(const Ref<Mesh> &p_mesh) {
  167. mesh = p_mesh;
  168. if (!mesh.is_null()) {
  169. RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, mesh->get_rid());
  170. } else {
  171. RenderingServer::get_singleton()->multimesh_set_mesh(multimesh, RID());
  172. }
  173. }
  174. Ref<Mesh> MultiMesh::get_mesh() const {
  175. return mesh;
  176. }
  177. void MultiMesh::set_instance_count(int p_count) {
  178. ERR_FAIL_COND(p_count < 0);
  179. RenderingServer::get_singleton()->multimesh_allocate_data(multimesh, p_count, RS::MultimeshTransformFormat(transform_format), use_colors, use_custom_data);
  180. instance_count = p_count;
  181. }
  182. int MultiMesh::get_instance_count() const {
  183. return instance_count;
  184. }
  185. void MultiMesh::set_visible_instance_count(int p_count) {
  186. ERR_FAIL_COND(p_count < -1);
  187. ERR_FAIL_COND(p_count > instance_count);
  188. RenderingServer::get_singleton()->multimesh_set_visible_instances(multimesh, p_count);
  189. visible_instance_count = p_count;
  190. }
  191. int MultiMesh::get_visible_instance_count() const {
  192. return visible_instance_count;
  193. }
  194. void MultiMesh::set_physics_interpolation_quality(PhysicsInterpolationQuality p_quality) {
  195. _physics_interpolation_quality = p_quality;
  196. RenderingServer::get_singleton()->multimesh_set_physics_interpolation_quality(multimesh, (RS::MultimeshPhysicsInterpolationQuality)p_quality);
  197. }
  198. void MultiMesh::set_instance_transform(int p_instance, const Transform3D &p_transform) {
  199. RenderingServer::get_singleton()->multimesh_instance_set_transform(multimesh, p_instance, p_transform);
  200. }
  201. void MultiMesh::set_instance_transform_2d(int p_instance, const Transform2D &p_transform) {
  202. RenderingServer::get_singleton()->multimesh_instance_set_transform_2d(multimesh, p_instance, p_transform);
  203. emit_changed();
  204. }
  205. Transform3D MultiMesh::get_instance_transform(int p_instance) const {
  206. return RenderingServer::get_singleton()->multimesh_instance_get_transform(multimesh, p_instance);
  207. }
  208. Transform2D MultiMesh::get_instance_transform_2d(int p_instance) const {
  209. return RenderingServer::get_singleton()->multimesh_instance_get_transform_2d(multimesh, p_instance);
  210. }
  211. void MultiMesh::set_instance_color(int p_instance, const Color &p_color) {
  212. RenderingServer::get_singleton()->multimesh_instance_set_color(multimesh, p_instance, p_color);
  213. }
  214. Color MultiMesh::get_instance_color(int p_instance) const {
  215. return RenderingServer::get_singleton()->multimesh_instance_get_color(multimesh, p_instance);
  216. }
  217. void MultiMesh::set_instance_custom_data(int p_instance, const Color &p_custom_data) {
  218. RenderingServer::get_singleton()->multimesh_instance_set_custom_data(multimesh, p_instance, p_custom_data);
  219. }
  220. Color MultiMesh::get_instance_custom_data(int p_instance) const {
  221. return RenderingServer::get_singleton()->multimesh_instance_get_custom_data(multimesh, p_instance);
  222. }
  223. void MultiMesh::reset_instance_physics_interpolation(int p_instance) {
  224. RenderingServer::get_singleton()->multimesh_instance_reset_physics_interpolation(multimesh, p_instance);
  225. }
  226. void MultiMesh::set_physics_interpolated(bool p_interpolated) {
  227. RenderingServer::get_singleton()->multimesh_set_physics_interpolated(multimesh, p_interpolated);
  228. }
  229. void MultiMesh::set_custom_aabb(const AABB &p_custom) {
  230. custom_aabb = p_custom;
  231. RS::get_singleton()->multimesh_set_custom_aabb(multimesh, custom_aabb);
  232. emit_changed();
  233. }
  234. AABB MultiMesh::get_custom_aabb() const {
  235. return custom_aabb;
  236. }
  237. AABB MultiMesh::get_aabb() const {
  238. return RenderingServer::get_singleton()->multimesh_get_aabb(multimesh);
  239. }
  240. RID MultiMesh::get_rid() const {
  241. return multimesh;
  242. }
  243. void MultiMesh::set_use_colors(bool p_enable) {
  244. ERR_FAIL_COND(instance_count > 0);
  245. use_colors = p_enable;
  246. }
  247. bool MultiMesh::is_using_colors() const {
  248. return use_colors;
  249. }
  250. void MultiMesh::set_use_custom_data(bool p_enable) {
  251. ERR_FAIL_COND(instance_count > 0);
  252. use_custom_data = p_enable;
  253. }
  254. bool MultiMesh::is_using_custom_data() const {
  255. return use_custom_data;
  256. }
  257. void MultiMesh::set_transform_format(TransformFormat p_transform_format) {
  258. ERR_FAIL_COND(instance_count > 0);
  259. transform_format = p_transform_format;
  260. }
  261. MultiMesh::TransformFormat MultiMesh::get_transform_format() const {
  262. return transform_format;
  263. }
  264. void MultiMesh::_bind_methods() {
  265. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MultiMesh::set_mesh);
  266. ClassDB::bind_method(D_METHOD("get_mesh"), &MultiMesh::get_mesh);
  267. ClassDB::bind_method(D_METHOD("set_use_colors", "enable"), &MultiMesh::set_use_colors);
  268. ClassDB::bind_method(D_METHOD("is_using_colors"), &MultiMesh::is_using_colors);
  269. ClassDB::bind_method(D_METHOD("set_use_custom_data", "enable"), &MultiMesh::set_use_custom_data);
  270. ClassDB::bind_method(D_METHOD("is_using_custom_data"), &MultiMesh::is_using_custom_data);
  271. ClassDB::bind_method(D_METHOD("set_transform_format", "format"), &MultiMesh::set_transform_format);
  272. ClassDB::bind_method(D_METHOD("get_transform_format"), &MultiMesh::get_transform_format);
  273. ClassDB::bind_method(D_METHOD("set_instance_count", "count"), &MultiMesh::set_instance_count);
  274. ClassDB::bind_method(D_METHOD("get_instance_count"), &MultiMesh::get_instance_count);
  275. ClassDB::bind_method(D_METHOD("set_visible_instance_count", "count"), &MultiMesh::set_visible_instance_count);
  276. ClassDB::bind_method(D_METHOD("get_visible_instance_count"), &MultiMesh::get_visible_instance_count);
  277. ClassDB::bind_method(D_METHOD("set_physics_interpolation_quality", "quality"), &MultiMesh::set_physics_interpolation_quality);
  278. ClassDB::bind_method(D_METHOD("get_physics_interpolation_quality"), &MultiMesh::get_physics_interpolation_quality);
  279. ClassDB::bind_method(D_METHOD("set_instance_transform", "instance", "transform"), &MultiMesh::set_instance_transform);
  280. ClassDB::bind_method(D_METHOD("set_instance_transform_2d", "instance", "transform"), &MultiMesh::set_instance_transform_2d);
  281. ClassDB::bind_method(D_METHOD("get_instance_transform", "instance"), &MultiMesh::get_instance_transform);
  282. ClassDB::bind_method(D_METHOD("get_instance_transform_2d", "instance"), &MultiMesh::get_instance_transform_2d);
  283. ClassDB::bind_method(D_METHOD("set_instance_color", "instance", "color"), &MultiMesh::set_instance_color);
  284. ClassDB::bind_method(D_METHOD("get_instance_color", "instance"), &MultiMesh::get_instance_color);
  285. ClassDB::bind_method(D_METHOD("set_instance_custom_data", "instance", "custom_data"), &MultiMesh::set_instance_custom_data);
  286. ClassDB::bind_method(D_METHOD("get_instance_custom_data", "instance"), &MultiMesh::get_instance_custom_data);
  287. ClassDB::bind_method(D_METHOD("reset_instance_physics_interpolation", "instance"), &MultiMesh::reset_instance_physics_interpolation);
  288. ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &MultiMesh::set_custom_aabb);
  289. ClassDB::bind_method(D_METHOD("get_custom_aabb"), &MultiMesh::get_custom_aabb);
  290. ClassDB::bind_method(D_METHOD("get_aabb"), &MultiMesh::get_aabb);
  291. ClassDB::bind_method(D_METHOD("get_buffer"), &MultiMesh::get_buffer);
  292. ClassDB::bind_method(D_METHOD("set_buffer", "buffer"), &MultiMesh::set_buffer);
  293. ClassDB::bind_method(D_METHOD("set_buffer_interpolated", "buffer_curr", "buffer_prev"), &MultiMesh::set_buffer_interpolated);
  294. ADD_PROPERTY(PropertyInfo(Variant::INT, "transform_format", PROPERTY_HINT_ENUM, "2D,3D"), "set_transform_format", "get_transform_format");
  295. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_colors"), "set_use_colors", "is_using_colors");
  296. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_data"), "set_use_custom_data", "is_using_custom_data");
  297. ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_custom_aabb", "get_custom_aabb");
  298. ADD_PROPERTY(PropertyInfo(Variant::INT, "instance_count", PROPERTY_HINT_RANGE, "0,16384,1,or_greater"), "set_instance_count", "get_instance_count");
  299. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_instance_count", PROPERTY_HINT_RANGE, "-1,16384,1,or_greater"), "set_visible_instance_count", "get_visible_instance_count");
  300. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  301. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "buffer", PROPERTY_HINT_NONE), "set_buffer", "get_buffer");
  302. #ifndef DISABLE_DEPRECATED
  303. // Kept for compatibility from 3.x to 4.0.
  304. ClassDB::bind_method(D_METHOD("_set_transform_array", "array"), &MultiMesh::_set_transform_array);
  305. ClassDB::bind_method(D_METHOD("_get_transform_array"), &MultiMesh::_get_transform_array);
  306. ClassDB::bind_method(D_METHOD("_set_transform_2d_array", "array"), &MultiMesh::_set_transform_2d_array);
  307. ClassDB::bind_method(D_METHOD("_get_transform_2d_array"), &MultiMesh::_get_transform_2d_array);
  308. ClassDB::bind_method(D_METHOD("_set_color_array", "array"), &MultiMesh::_set_color_array);
  309. ClassDB::bind_method(D_METHOD("_get_color_array"), &MultiMesh::_get_color_array);
  310. ClassDB::bind_method(D_METHOD("_set_custom_data_array", "array"), &MultiMesh::_set_custom_data_array);
  311. ClassDB::bind_method(D_METHOD("_get_custom_data_array"), &MultiMesh::_get_custom_data_array);
  312. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "transform_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "_set_transform_array", "_get_transform_array");
  313. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "transform_2d_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "_set_transform_2d_array", "_get_transform_2d_array");
  314. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "color_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "_set_color_array", "_get_color_array");
  315. ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "custom_data_array", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "_set_custom_data_array", "_get_custom_data_array");
  316. #endif
  317. ADD_GROUP("Physics Interpolation", "physics_interpolation");
  318. ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_interpolation_quality", PROPERTY_HINT_ENUM, "Fast,High"), "set_physics_interpolation_quality", "get_physics_interpolation_quality");
  319. BIND_ENUM_CONSTANT(TRANSFORM_2D);
  320. BIND_ENUM_CONSTANT(TRANSFORM_3D);
  321. BIND_ENUM_CONSTANT(INTERP_QUALITY_FAST);
  322. BIND_ENUM_CONSTANT(INTERP_QUALITY_HIGH);
  323. }
  324. MultiMesh::MultiMesh() {
  325. multimesh = RenderingServer::get_singleton()->multimesh_create();
  326. }
  327. MultiMesh::~MultiMesh() {
  328. ERR_FAIL_NULL(RenderingServer::get_singleton());
  329. RenderingServer::get_singleton()->free(multimesh);
  330. }