immediate_mesh.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**************************************************************************/
  2. /* immediate_mesh.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 "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 (Color &color : colors) {
  42. color = 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 (Vector3 &normal : normals) {
  53. normal = 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 (Plane &tangent : tangents) {
  64. tangent = 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 (Vector2 &uv : uvs) {
  75. uv = 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 (Vector2 &uv : uv2s) {
  86. uv = 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.is_empty(), "No vertices were added, surface can't be created.");
  137. uint64_t format = ARRAY_FORMAT_VERTEX | ARRAY_FLAG_FORMAT_CURRENT_VERSION;
  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_tangent_stride = 0;
  146. uint32_t normal_offset = 0;
  147. if (uses_normals) {
  148. format |= ARRAY_FORMAT_NORMAL;
  149. normal_offset = vertex_stride * vertices.size();
  150. normal_tangent_stride += sizeof(uint32_t);
  151. }
  152. uint32_t tangent_offset = 0;
  153. if (uses_tangents || uses_normals) {
  154. format |= ARRAY_FORMAT_TANGENT;
  155. tangent_offset = vertex_stride * vertices.size() + normal_tangent_stride;
  156. normal_tangent_stride += sizeof(uint32_t);
  157. }
  158. AABB aabb;
  159. {
  160. surface_vertex_create_cache.resize((vertex_stride + normal_tangent_stride) * vertices.size());
  161. uint8_t *surface_vertex_ptr = surface_vertex_create_cache.ptrw();
  162. for (uint32_t i = 0; i < vertices.size(); i++) {
  163. {
  164. float *vtx = (float *)&surface_vertex_ptr[i * vertex_stride];
  165. vtx[0] = vertices[i].x;
  166. vtx[1] = vertices[i].y;
  167. if (!active_surface_data.vertex_2d) {
  168. vtx[2] = vertices[i].z;
  169. }
  170. if (i == 0) {
  171. aabb = AABB(vertices[i], SMALL_VEC3); // Must have a bit of size.
  172. } else {
  173. aabb.expand_to(vertices[i]);
  174. }
  175. }
  176. if (uses_normals) {
  177. uint32_t *normal = (uint32_t *)&surface_vertex_ptr[i * normal_tangent_stride + normal_offset];
  178. Vector2 n = normals[i].octahedron_encode();
  179. uint32_t value = 0;
  180. value |= (uint16_t)CLAMP(n.x * 65535, 0, 65535);
  181. value |= (uint16_t)CLAMP(n.y * 65535, 0, 65535) << 16;
  182. *normal = value;
  183. }
  184. if (uses_tangents || uses_normals) {
  185. uint32_t *tangent = (uint32_t *)&surface_vertex_ptr[i * normal_tangent_stride + tangent_offset];
  186. Vector2 t;
  187. if (uses_tangents) {
  188. t = tangents[i].normal.octahedron_tangent_encode(tangents[i].d);
  189. } else {
  190. Vector3 tan = Vector3(normals[i].z, -normals[i].x, normals[i].y).cross(normals[i].normalized()).normalized();
  191. t = tan.octahedron_tangent_encode(1.0);
  192. }
  193. uint32_t value = 0;
  194. value |= (uint16_t)CLAMP(t.x * 65535, 0, 65535);
  195. value |= (uint16_t)CLAMP(t.y * 65535, 0, 65535) << 16;
  196. if (value == 4294901760) {
  197. // (1, 1) and (0, 1) decode to the same value, but (0, 1) messes with our compression detection.
  198. // So we sanitize here.
  199. value = 4294967295;
  200. }
  201. *tangent = value;
  202. }
  203. }
  204. }
  205. if (uses_colors || uses_uvs || uses_uv2s) {
  206. uint32_t attribute_stride = 0;
  207. if (uses_colors) {
  208. format |= ARRAY_FORMAT_COLOR;
  209. attribute_stride += sizeof(uint8_t) * 4;
  210. }
  211. uint32_t uv_offset = 0;
  212. if (uses_uvs) {
  213. format |= ARRAY_FORMAT_TEX_UV;
  214. uv_offset = attribute_stride;
  215. attribute_stride += sizeof(float) * 2;
  216. }
  217. uint32_t uv2_offset = 0;
  218. if (uses_uv2s) {
  219. format |= ARRAY_FORMAT_TEX_UV2;
  220. uv2_offset = attribute_stride;
  221. attribute_stride += sizeof(float) * 2;
  222. }
  223. surface_attribute_create_cache.resize(vertices.size() * attribute_stride);
  224. uint8_t *surface_attribute_ptr = surface_attribute_create_cache.ptrw();
  225. for (uint32_t i = 0; i < vertices.size(); i++) {
  226. if (uses_colors) {
  227. uint8_t *color8 = (uint8_t *)&surface_attribute_ptr[i * attribute_stride];
  228. color8[0] = uint8_t(CLAMP(colors[i].r * 255.0, 0.0, 255.0));
  229. color8[1] = uint8_t(CLAMP(colors[i].g * 255.0, 0.0, 255.0));
  230. color8[2] = uint8_t(CLAMP(colors[i].b * 255.0, 0.0, 255.0));
  231. color8[3] = uint8_t(CLAMP(colors[i].a * 255.0, 0.0, 255.0));
  232. }
  233. if (uses_uvs) {
  234. float *uv = (float *)&surface_attribute_ptr[i * attribute_stride + uv_offset];
  235. uv[0] = uvs[i].x;
  236. uv[1] = uvs[i].y;
  237. }
  238. if (uses_uv2s) {
  239. float *uv2 = (float *)&surface_attribute_ptr[i * attribute_stride + uv2_offset];
  240. uv2[0] = uv2s[i].x;
  241. uv2[1] = uv2s[i].y;
  242. }
  243. }
  244. }
  245. RS::SurfaceData sd;
  246. sd.primitive = RS::PrimitiveType(active_surface_data.primitive);
  247. sd.format = format;
  248. sd.vertex_data = surface_vertex_create_cache;
  249. if (uses_colors || uses_uvs || uses_uv2s) {
  250. sd.attribute_data = surface_attribute_create_cache;
  251. }
  252. sd.vertex_count = vertices.size();
  253. sd.aabb = aabb;
  254. if (active_surface_data.material.is_valid()) {
  255. sd.material = active_surface_data.material->get_rid();
  256. }
  257. RS::get_singleton()->mesh_add_surface(mesh, sd);
  258. active_surface_data.aabb = aabb;
  259. active_surface_data.format = format;
  260. active_surface_data.array_len = vertices.size();
  261. surfaces.push_back(active_surface_data);
  262. colors.clear();
  263. normals.clear();
  264. tangents.clear();
  265. uvs.clear();
  266. uv2s.clear();
  267. vertices.clear();
  268. uses_colors = false;
  269. uses_normals = false;
  270. uses_tangents = false;
  271. uses_uvs = false;
  272. uses_uv2s = false;
  273. surface_active = false;
  274. emit_changed();
  275. }
  276. void ImmediateMesh::clear_surfaces() {
  277. RS::get_singleton()->mesh_clear(mesh);
  278. surfaces.clear();
  279. surface_active = false;
  280. colors.clear();
  281. normals.clear();
  282. tangents.clear();
  283. uvs.clear();
  284. uv2s.clear();
  285. vertices.clear();
  286. uses_colors = false;
  287. uses_normals = false;
  288. uses_tangents = false;
  289. uses_uvs = false;
  290. uses_uv2s = false;
  291. }
  292. int ImmediateMesh::get_surface_count() const {
  293. return surfaces.size();
  294. }
  295. int ImmediateMesh::surface_get_array_len(int p_idx) const {
  296. ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), -1);
  297. return surfaces[p_idx].array_len;
  298. }
  299. int ImmediateMesh::surface_get_array_index_len(int p_idx) const {
  300. return 0;
  301. }
  302. Array ImmediateMesh::surface_get_arrays(int p_surface) const {
  303. ERR_FAIL_INDEX_V(p_surface, int(surfaces.size()), Array());
  304. return RS::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
  305. }
  306. TypedArray<Array> ImmediateMesh::surface_get_blend_shape_arrays(int p_surface) const {
  307. return TypedArray<Array>();
  308. }
  309. Dictionary ImmediateMesh::surface_get_lods(int p_surface) const {
  310. return Dictionary();
  311. }
  312. BitField<Mesh::ArrayFormat> ImmediateMesh::surface_get_format(int p_idx) const {
  313. ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), 0);
  314. return surfaces[p_idx].format;
  315. }
  316. Mesh::PrimitiveType ImmediateMesh::surface_get_primitive_type(int p_idx) const {
  317. ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), PRIMITIVE_MAX);
  318. return surfaces[p_idx].primitive;
  319. }
  320. void ImmediateMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
  321. ERR_FAIL_INDEX(p_idx, int(surfaces.size()));
  322. surfaces[p_idx].material = p_material;
  323. RID mat;
  324. if (p_material.is_valid()) {
  325. mat = p_material->get_rid();
  326. }
  327. RS::get_singleton()->mesh_surface_set_material(mesh, p_idx, mat);
  328. }
  329. Ref<Material> ImmediateMesh::surface_get_material(int p_idx) const {
  330. ERR_FAIL_INDEX_V(p_idx, int(surfaces.size()), Ref<Material>());
  331. return surfaces[p_idx].material;
  332. }
  333. int ImmediateMesh::get_blend_shape_count() const {
  334. return 0;
  335. }
  336. StringName ImmediateMesh::get_blend_shape_name(int p_index) const {
  337. return StringName();
  338. }
  339. void ImmediateMesh::set_blend_shape_name(int p_index, const StringName &p_name) {
  340. }
  341. AABB ImmediateMesh::get_aabb() const {
  342. AABB aabb;
  343. for (uint32_t i = 0; i < surfaces.size(); i++) {
  344. if (i == 0) {
  345. aabb = surfaces[i].aabb;
  346. } else {
  347. aabb = aabb.merge(surfaces[i].aabb);
  348. }
  349. }
  350. return aabb;
  351. }
  352. void ImmediateMesh::_bind_methods() {
  353. ClassDB::bind_method(D_METHOD("surface_begin", "primitive", "material"), &ImmediateMesh::surface_begin, DEFVAL(Ref<Material>()));
  354. ClassDB::bind_method(D_METHOD("surface_set_color", "color"), &ImmediateMesh::surface_set_color);
  355. ClassDB::bind_method(D_METHOD("surface_set_normal", "normal"), &ImmediateMesh::surface_set_normal);
  356. ClassDB::bind_method(D_METHOD("surface_set_tangent", "tangent"), &ImmediateMesh::surface_set_tangent);
  357. ClassDB::bind_method(D_METHOD("surface_set_uv", "uv"), &ImmediateMesh::surface_set_uv);
  358. ClassDB::bind_method(D_METHOD("surface_set_uv2", "uv2"), &ImmediateMesh::surface_set_uv2);
  359. ClassDB::bind_method(D_METHOD("surface_add_vertex", "vertex"), &ImmediateMesh::surface_add_vertex);
  360. ClassDB::bind_method(D_METHOD("surface_add_vertex_2d", "vertex"), &ImmediateMesh::surface_add_vertex_2d);
  361. ClassDB::bind_method(D_METHOD("surface_end"), &ImmediateMesh::surface_end);
  362. ClassDB::bind_method(D_METHOD("clear_surfaces"), &ImmediateMesh::clear_surfaces);
  363. }
  364. RID ImmediateMesh::get_rid() const {
  365. return mesh;
  366. }
  367. ImmediateMesh::ImmediateMesh() {
  368. mesh = RS::get_singleton()->mesh_create();
  369. }
  370. ImmediateMesh::~ImmediateMesh() {
  371. ERR_FAIL_NULL(RenderingServer::get_singleton());
  372. RS::get_singleton()->free(mesh);
  373. }