immediate_mesh.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*************************************************************************/
  2. /* immediate_mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "immediate_mesh.h"
  31. void ImmediateMesh::surface_begin(PrimitiveType p_primitive, const Ref<Material> &p_material) {
  32. ERR_FAIL_COND_MSG(surface_active, "Already creating a new surface.");
  33. active_surface_data.primitive = p_primitive;
  34. active_surface_data.material = p_material;
  35. surface_active = true;
  36. }
  37. void ImmediateMesh::surface_set_color(const Color &p_color) {
  38. ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
  39. if (!uses_colors) {
  40. colors.resize(vertices.size());
  41. for (uint32_t i = 0; i < colors.size(); i++) {
  42. colors[i] = p_color;
  43. }
  44. uses_colors = true;
  45. }
  46. current_color = p_color;
  47. }
  48. void ImmediateMesh::surface_set_normal(const Vector3 &p_normal) {
  49. ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
  50. if (!uses_normals) {
  51. normals.resize(vertices.size());
  52. for (uint32_t i = 0; i < normals.size(); i++) {
  53. normals[i] = p_normal;
  54. }
  55. uses_normals = true;
  56. }
  57. current_normal = p_normal;
  58. }
  59. void ImmediateMesh::surface_set_tangent(const Plane &p_tangent) {
  60. ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
  61. if (!uses_tangents) {
  62. tangents.resize(vertices.size());
  63. for (uint32_t i = 0; i < tangents.size(); i++) {
  64. tangents[i] = p_tangent;
  65. }
  66. uses_tangents = true;
  67. }
  68. current_tangent = p_tangent;
  69. }
  70. void ImmediateMesh::surface_set_uv(const Vector2 &p_uv) {
  71. ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
  72. if (!uses_uvs) {
  73. uvs.resize(vertices.size());
  74. for (uint32_t i = 0; i < uvs.size(); i++) {
  75. uvs[i] = p_uv;
  76. }
  77. uses_uvs = true;
  78. }
  79. current_uv = p_uv;
  80. }
  81. void ImmediateMesh::surface_set_uv2(const Vector2 &p_uv2) {
  82. ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
  83. if (!uses_uv2s) {
  84. uv2s.resize(vertices.size());
  85. for (uint32_t i = 0; i < uv2s.size(); i++) {
  86. uv2s[i] = p_uv2;
  87. }
  88. uses_uv2s = true;
  89. }
  90. current_uv2 = p_uv2;
  91. }
  92. void ImmediateMesh::surface_add_vertex(const Vector3 &p_vertex) {
  93. ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
  94. ERR_FAIL_COND_MSG(vertices.size() && active_surface_data.vertex_2d, "Can't mix 2D and 3D vertices in a surface.");
  95. if (uses_colors) {
  96. colors.push_back(current_color);
  97. }
  98. if (uses_normals) {
  99. normals.push_back(current_normal);
  100. }
  101. if (uses_tangents) {
  102. tangents.push_back(current_tangent);
  103. }
  104. if (uses_uvs) {
  105. uvs.push_back(current_uv);
  106. }
  107. if (uses_uv2s) {
  108. uv2s.push_back(current_uv2);
  109. }
  110. vertices.push_back(p_vertex);
  111. }
  112. void ImmediateMesh::surface_add_vertex_2d(const Vector2 &p_vertex) {
  113. ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
  114. ERR_FAIL_COND_MSG(vertices.size() && !active_surface_data.vertex_2d, "Can't mix 2D and 3D vertices in a surface.");
  115. if (uses_colors) {
  116. colors.push_back(current_color);
  117. }
  118. if (uses_normals) {
  119. normals.push_back(current_normal);
  120. }
  121. if (uses_tangents) {
  122. tangents.push_back(current_tangent);
  123. }
  124. if (uses_uvs) {
  125. uvs.push_back(current_uv);
  126. }
  127. if (uses_uv2s) {
  128. uv2s.push_back(current_uv2);
  129. }
  130. Vector3 v(p_vertex.x, p_vertex.y, 0);
  131. vertices.push_back(v);
  132. active_surface_data.vertex_2d = true;
  133. }
  134. void ImmediateMesh::surface_end() {
  135. ERR_FAIL_COND_MSG(!surface_active, "Not creating any surface. Use surface_begin() to do it.");
  136. ERR_FAIL_COND_MSG(!vertices.size(), "No vertices were added, surface can't be created.");
  137. uint32_t format = ARRAY_FORMAT_VERTEX;
  138. uint32_t vertex_stride = 0;
  139. if (active_surface_data.vertex_2d) {
  140. format |= ARRAY_FLAG_USE_2D_VERTICES;
  141. vertex_stride = sizeof(float) * 2;
  142. } else {
  143. vertex_stride = sizeof(float) * 3;
  144. }
  145. uint32_t normal_offset = 0;
  146. if (uses_normals) {
  147. format |= ARRAY_FORMAT_NORMAL;
  148. normal_offset = vertex_stride;
  149. vertex_stride += sizeof(uint32_t);
  150. }
  151. uint32_t tangent_offset = 0;
  152. if (uses_tangents) {
  153. format |= ARRAY_FORMAT_TANGENT;
  154. tangent_offset += vertex_stride;
  155. vertex_stride += sizeof(uint32_t);
  156. }
  157. AABB aabb;
  158. {
  159. surface_vertex_create_cache.resize(vertex_stride * vertices.size());
  160. uint8_t *surface_vertex_ptr = surface_vertex_create_cache.ptrw();
  161. for (uint32_t i = 0; i < vertices.size(); i++) {
  162. {
  163. float *vtx = (float *)&surface_vertex_ptr[i * vertex_stride];
  164. vtx[0] = vertices[i].x;
  165. vtx[1] = vertices[i].y;
  166. if (!active_surface_data.vertex_2d) {
  167. vtx[2] = vertices[i].z;
  168. }
  169. if (i == 0) {
  170. aabb = AABB(vertices[i], SMALL_VEC3); // Must have a bit of size.
  171. } else {
  172. aabb.expand_to(vertices[i]);
  173. }
  174. }
  175. if (uses_normals) {
  176. uint32_t *normal = (uint32_t *)&surface_vertex_ptr[i * vertex_stride + normal_offset];
  177. Vector3 n = normals[i] * Vector3(0.5, 0.5, 0.5) + Vector3(0.5, 0.5, 0.5);
  178. uint32_t value = 0;
  179. value |= CLAMP(int(n.x * 1023.0), 0, 1023);
  180. value |= CLAMP(int(n.y * 1023.0), 0, 1023) << 10;
  181. value |= CLAMP(int(n.z * 1023.0), 0, 1023) << 20;
  182. *normal = value;
  183. }
  184. if (uses_tangents) {
  185. uint32_t *tangent = (uint32_t *)&surface_vertex_ptr[i * vertex_stride + tangent_offset];
  186. Plane t = tangents[i];
  187. uint32_t value = 0;
  188. value |= CLAMP(int((t.normal.x * 0.5 + 0.5) * 1023.0), 0, 1023);
  189. value |= CLAMP(int((t.normal.y * 0.5 + 0.5) * 1023.0), 0, 1023) << 10;
  190. value |= CLAMP(int((t.normal.z * 0.5 + 0.5) * 1023.0), 0, 1023) << 20;
  191. if (t.d > 0) {
  192. value |= 3UL << 30;
  193. }
  194. *tangent = value;
  195. }
  196. }
  197. }
  198. if (uses_colors || uses_uvs || uses_uv2s) {
  199. uint32_t attribute_stride = 0;
  200. if (uses_colors) {
  201. format |= ARRAY_FORMAT_COLOR;
  202. attribute_stride += sizeof(uint8_t) * 4;
  203. }
  204. uint32_t uv_offset = 0;
  205. if (uses_uvs) {
  206. format |= ARRAY_FORMAT_TEX_UV;
  207. uv_offset = attribute_stride;
  208. attribute_stride += sizeof(float) * 2;
  209. }
  210. uint32_t uv2_offset = 0;
  211. if (uses_uv2s) {
  212. format |= ARRAY_FORMAT_TEX_UV2;
  213. uv2_offset = attribute_stride;
  214. attribute_stride += sizeof(float) * 2;
  215. }
  216. surface_attribute_create_cache.resize(vertices.size() * attribute_stride);
  217. uint8_t *surface_attribute_ptr = surface_attribute_create_cache.ptrw();
  218. for (uint32_t i = 0; i < vertices.size(); i++) {
  219. if (uses_colors) {
  220. uint8_t *color8 = (uint8_t *)&surface_attribute_ptr[i * attribute_stride];
  221. color8[0] = uint8_t(CLAMP(colors[i].r * 255.0, 0.0, 255.0));
  222. color8[1] = uint8_t(CLAMP(colors[i].g * 255.0, 0.0, 255.0));
  223. color8[2] = uint8_t(CLAMP(colors[i].b * 255.0, 0.0, 255.0));
  224. color8[3] = uint8_t(CLAMP(colors[i].a * 255.0, 0.0, 255.0));
  225. }
  226. if (uses_uvs) {
  227. float *uv = (float *)&surface_attribute_ptr[i * attribute_stride + uv_offset];
  228. uv[0] = uvs[i].x;
  229. uv[1] = uvs[i].y;
  230. }
  231. if (uses_uv2s) {
  232. float *uv2 = (float *)&surface_attribute_ptr[i * attribute_stride + uv2_offset];
  233. uv2[0] = uv2s[i].x;
  234. uv2[1] = uv2s[i].y;
  235. }
  236. }
  237. }
  238. RS::SurfaceData sd;
  239. sd.primitive = RS::PrimitiveType(active_surface_data.primitive);
  240. sd.format = format;
  241. sd.vertex_data = surface_vertex_create_cache;
  242. if (uses_colors || uses_uvs || uses_uv2s) {
  243. sd.attribute_data = surface_attribute_create_cache;
  244. }
  245. sd.vertex_count = vertices.size();
  246. sd.aabb = aabb;
  247. if (active_surface_data.material.is_valid()) {
  248. sd.material = active_surface_data.material->get_rid();
  249. }
  250. RS::get_singleton()->mesh_add_surface(mesh, sd);
  251. active_surface_data.aabb = aabb;
  252. active_surface_data.format = format;
  253. active_surface_data.array_len = vertices.size();
  254. surfaces.push_back(active_surface_data);
  255. colors.clear();
  256. normals.clear();
  257. tangents.clear();
  258. uvs.clear();
  259. uv2s.clear();
  260. vertices.clear();
  261. uses_colors = false;
  262. uses_normals = false;
  263. uses_tangents = false;
  264. uses_uvs = false;
  265. uses_uv2s = false;
  266. surface_active = false;
  267. }
  268. void ImmediateMesh::clear_surfaces() {
  269. RS::get_singleton()->mesh_clear(mesh);
  270. surfaces.clear();
  271. surface_active = false;
  272. colors.clear();
  273. normals.clear();
  274. tangents.clear();
  275. uvs.clear();
  276. uv2s.clear();
  277. vertices.clear();
  278. uses_colors = false;
  279. uses_normals = false;
  280. uses_tangents = false;
  281. uses_uvs = false;
  282. uses_uv2s = false;
  283. }
  284. int ImmediateMesh::get_surface_count() const {
  285. return surfaces.size();
  286. }
  287. int ImmediateMesh::surface_get_array_len(int p_idx) const {
  288. ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), -1);
  289. return surfaces[p_idx].array_len;
  290. }
  291. int ImmediateMesh::surface_get_array_index_len(int p_idx) const {
  292. return 0;
  293. }
  294. Array ImmediateMesh::surface_get_arrays(int p_surface) const {
  295. ERR_FAIL_INDEX_V(p_surface, int(surfaces.size()), Array());
  296. return RS::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
  297. }
  298. TypedArray<Array> ImmediateMesh::surface_get_blend_shape_arrays(int p_surface) const {
  299. return TypedArray<Array>();
  300. }
  301. Dictionary ImmediateMesh::surface_get_lods(int p_surface) const {
  302. return Dictionary();
  303. }
  304. uint32_t ImmediateMesh::surface_get_format(int p_idx) const {
  305. ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), 0);
  306. return surfaces[p_idx].format;
  307. }
  308. Mesh::PrimitiveType ImmediateMesh::surface_get_primitive_type(int p_idx) const {
  309. ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), PRIMITIVE_MAX);
  310. return surfaces[p_idx].primitive;
  311. }
  312. void ImmediateMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  313. ERR_FAIL_INDEX(p_idx, int(surfaces.size()));
  314. surfaces[p_idx].material = p_material;
  315. RID mat;
  316. if (p_material.is_valid()) {
  317. mat = p_material->get_rid();
  318. }
  319. RS::get_singleton()->mesh_surface_set_material(mesh, p_idx, mat);
  320. }
  321. Ref<Material> ImmediateMesh::surface_get_material(int p_idx) const {
  322. ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), Ref<Material>());
  323. return surfaces[p_idx].material;
  324. }
  325. int ImmediateMesh::get_blend_shape_count() const {
  326. return 0;
  327. }
  328. StringName ImmediateMesh::get_blend_shape_name(int p_index) const {
  329. return StringName();
  330. }
  331. void ImmediateMesh::set_blend_shape_name(int p_index, const StringName &p_name) {
  332. }
  333. AABB ImmediateMesh::get_aabb() const {
  334. AABB aabb;
  335. for (uint32_t i = 0; i < surfaces.size(); i++) {
  336. if (i == 0) {
  337. aabb = surfaces[i].aabb;
  338. } else {
  339. aabb = aabb.merge(surfaces[i].aabb);
  340. }
  341. }
  342. return aabb;
  343. }
  344. void ImmediateMesh::_bind_methods() {
  345. ClassDB::bind_method(D_METHOD("surface_begin", "primitive", "material"), &ImmediateMesh::surface_begin, DEFVAL(Ref<Material>()));
  346. ClassDB::bind_method(D_METHOD("surface_set_color", "color"), &ImmediateMesh::surface_set_color);
  347. ClassDB::bind_method(D_METHOD("surface_set_normal", "normal"), &ImmediateMesh::surface_set_normal);
  348. ClassDB::bind_method(D_METHOD("surface_set_tangent", "tangent"), &ImmediateMesh::surface_set_tangent);
  349. ClassDB::bind_method(D_METHOD("surface_set_uv", "uv"), &ImmediateMesh::surface_set_uv);
  350. ClassDB::bind_method(D_METHOD("surface_set_uv2", "uv2"), &ImmediateMesh::surface_set_uv2);
  351. ClassDB::bind_method(D_METHOD("surface_add_vertex", "vertex"), &ImmediateMesh::surface_add_vertex);
  352. ClassDB::bind_method(D_METHOD("surface_add_vertex_2d", "vertex"), &ImmediateMesh::surface_add_vertex_2d);
  353. ClassDB::bind_method(D_METHOD("surface_end"), &ImmediateMesh::surface_end);
  354. ClassDB::bind_method(D_METHOD("clear_surfaces"), &ImmediateMesh::clear_surfaces);
  355. }
  356. RID ImmediateMesh::get_rid() const {
  357. return mesh;
  358. }
  359. ImmediateMesh::ImmediateMesh() {
  360. mesh = RS::get_singleton()->mesh_create();
  361. }
  362. ImmediateMesh::~ImmediateMesh() {
  363. RS::get_singleton()->free(mesh);
  364. }